On Wednesday, 11 March 2020 18:52:15 UTC+5:30, howar...@gmail.com wrote:
>
> 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:
>
> https://agniva.me/wasm/2018/06/18/shimmer-wasm.html
> https://github.com/agnivade/shimmer
> 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) 
>

Woops, that comment is outdated and needs to be fixed. The blog post is 
also outdated now :D.
There is no base64 conversion now anywhere. If you read the code below, 
you'll see I use 

dst := 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);
} 

Before the CopyBytesToJS <https://golang.org/pkg/syscall/js/#CopyBytesToJS> 
API, 
I used unsafe to get the slice header and then pass it to JS, and populate 
the slice with the image contents. Definitely hacky, but that did not 
require passing the entire image from JS to wasm. With the new API, we are 
back to passing data, but it's a lot safer.

Coming to the original problem, yes there is no Uint8ClampedArray support. 
People have already raised it here: 
https://github.com/golang/go/issues/32402. I think you can use a similar 
hack for your purposes.

Feel free to hop in to #webassembly slack channel if you have more 
questions.

-Agniva


> in https://github.com/agnivade/shimmer/blob/master/shimmer.go
>
> Here is another project that also went with the Base64 method of passing 
> the array, look for the section labeled "Pixels are Pixels": 
>
> https://blog.jeremylikness.com/blog/2019-03-03_gopher-meet-plasma-a-webassembly-experiment/
>
> His final verdict was that the pure Javascript version performed better 
> than the Go-WASM+JS version.
>

-- 
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/2eab5b50-9168-4584-ab42-11bac47ea8d4%40googlegroups.com.

Reply via email to