I'm new to WebAssembly and am trying to get Go to generate a bitmapped 
image to display in the Web browser.  Here's what I've come up with so far:

func DrawPicture(this js.Value, args []js.Value) interface{} {
        // Create a canvas on the Web page.
        const width = 256
        const height = 256
        canvas := js.Global().Get("document").Call("getElementById", 
"my-canvas")
        canvas.Set("width", width)
        canvas.Set("height", height)
        canvas.Set("style", "border: thin solid black")
        ctx := canvas.Call("getContext", "2d")
        ctx.Call("clearRect", 0, 0, width, height)


        // Render a picture.
        imgData := ctx.Call("createImageData", width, height)
        data := imgData.Get("data")
        for y := 0; y < height; y++ {
                for x := 0; x < width; x++ {
                        ofs := 4 * (y*width + x)
                        data.SetIndex(ofs+2, x+y)
                        data.SetIndex(ofs+3, 255)
                }
        }
        ctx.Call("putImageData", imgData, 0, 0)
        return nil
}

My question is: Is SetIndex 
<https://golang.org/pkg/syscall/js/#Value.SetIndex> the most efficient way 
to write the image's pixels?  I see that syscall/js 
<https://golang.org/pkg/syscall/js/> provides a CopyBytesToJS 
<https://golang.org/pkg/syscall/js/#CopyBytesToJS> function, but I was 
unable to get that to work because the image data is a Uint8ClampedArray
 and CopyBytesToJS expects a Uint8Array.  I kept trying to populate a Go 
[]byte and transfer that to the image but never succeeded in making that 
work.

Thanks,
— Scott

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/78570dbf-9e6e-429b-a776-76ce50f5c7e2%40googlegroups.com.

Reply via email to