Beispiel #1
0
func newReaderFromJSObject(in *js.Object) (readerAt, error) {
	if jsbuiltin.InstanceOf(in, js.Global.Get("ArrayBuffer")) {
		return &byteSliceReader{Buf: arrayBufferToByteSlice(in)}, nil
	}
	if jsbuiltin.InstanceOf(in, js.Global.Get("Blob")) {
		return newBlobReader(in), nil
	}
	return nil, errors.New("unsupported input type: " + jsbuiltin.TypeOf(in))
}
Beispiel #2
0
func parsePSDInWorker(in *js.Object, progress *js.Object, complete *js.Object, failed *js.Object) {
	script := js.Global.Get("document").Call("getElementById", "psdgo")
	if !script.Bool() {
		panic("id=psdgo not found")
	}
	worker := js.Global.Get("Worker").New(script.Get("src"))
	script.Set("psdgo", worker)
	canvasMap := map[int][2]*js.Object{}
	worker.Set("onmessage", func(e *js.Object) {
		data := e.Get("data")
		switch data.Get("type").String() {
		case "makeCanvas":
			var canvas, mask *js.Object
			if w, h := data.Get("w").Int(), data.Get("h").Int(); w*h > 0 {
				r := js.Global.Get("Uint8Array").New(data.Get("r")).Interface().([]byte)
				g := js.Global.Get("Uint8Array").New(data.Get("g")).Interface().([]byte)
				b := js.Global.Get("Uint8Array").New(data.Get("b")).Interface().([]byte)
				var a []byte
				if aab := data.Get("a"); aab.Bool() {
					a = js.Global.Get("Uint8Array").New(aab).Interface().([]byte)
				}
				canvas = createImageCanvas(w, h, r, g, b, a)
			}
			if m := data.Get("m"); m.Bool() {
				mask = createMaskCanvas(
					data.Get("mw").Int(), data.Get("mh").Int(),
					js.Global.Get("Uint8Array").New(m).Interface().([]byte),
					data.Get("mc").Int(),
				)
			}
			canvasMap[data.Get("id").Int()] = [2]*js.Object{canvas, mask}
		case "progress":
			progress.Invoke(data.Get("progress"))
		case "error":
			failed.Invoke(data.Get("error"))
		case "complete":
			root := data.Get("root")
			mapCanvasJS(canvasMap, root)
			complete.Invoke(root)
		}
	})
	if jsbuiltin.InstanceOf(in, js.Global.Get("ArrayBuffer")) {
		worker.Call("postMessage", js.M{
			"input": in,
		}, js.S{in})
	} else {
		worker.Call("postMessage", js.M{"input": in})
	}
}