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
}His final verdict was that the pure Javascript version performed better than the Go-WASM+JS version.
I've no relevant experience, but I can recommend a couple of projects to look at in the absence of anyone chiming in with actual experience:This is a play-project that involves loading images. It appears to be using a side-load method of converting the image to Base64 and assigning it to the image's src attribute - so it is not using the canvas directly, but if a call to the canvas can draw an image from a URL, this might work - have a look at// updateImage writes the image to a byte buffer and then converts it to base64.// Then it sets the value to the src attribute of the target image.func (s *Shimmer) updateImage(img *image.RGBA, start time.Time)
There is no base64 conversion now anywhere. If you read the code below, you'll see I usedst := js.Global().Get("Uint8Array").New(len(s.outBuf.Bytes()))n := js.CopyBytesToJS(dst, s.outBuf.Bytes())s.console.Call("log", "bytes copied:", strconv.Itoa(n))js.Global().Call("displayImage", dst)Essentially, I copy over the bytes to the array and pass the array over to JS land.And then displayImage does this:
function displayImage(buf) {
let blob = new Blob([buf], {'type': imageType});
document.getElementById('targetImg').src = URL.createObjectURL(blob);
}
let blob = new Blob([buf], {'type': imageType});
document.getElementById('targetImg').src = URL.createObjectURL(blob);
}I see. In your case, targetImg is an <img>, right? Do you know how I could use this same technique with a <canvas>?
If the <img> was off-screen or hidden, couldn't you still reference it to get the data into the canvas like:var img = document.getElementById('targetImg');ctx.drawImage(img, 10, 10);Might not be the most efficient way, but it seems like it should work.
function copyBytesToCanvas(data) {
let blob = new Blob([data], {"type": "image/png"});
let img = document.getElementById("myImage");
img.onload = function() {
let canvas = document.getElementById("myCanvas");
let ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0);
};
img.src = URL.createObjectURL(blob);
}--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/267bc2d7-1e7a-4704-98cd-3c471fa0d19dn%40googlegroups.com.