1

I am trying to improve performance of a fairly intensive graphical method by porting it from Typescript to WebAssembly using AssemblyScript. The method manipulates ImageData of a canvas.

I am having trouble passing this ImageData (or at least the Uint8ClampedArray) from Typescript to AssemblyScript. I found a really nice tutorial detailing how to create and send this data from AS to TS, but haven't found much information about going the other way around.

I'd love to find out how to do this, preferably without having to make a new copy of the Uint8ClampedArray in AS, but I am not sure if that is even possible. Details as to why it is(n't) would be much appreciated!

ColinE
  • 68,894
  • 15
  • 164
  • 232
Bram Vanbilsen
  • 5,763
  • 12
  • 51
  • 84

1 Answers1

2

I'd love to find out how to do this, preferably without having to make a new copy of the Uint8ClampedArray in AS

You cannot read / write directly to canvas via WebAssembly, as detailed in this answer to a similar question. You can share a WebAssembly.Memory instance between WebAssembly and the host JavaScript, however, you still have to copy the image from this memory into your canvas for each frame that you render.

ColinE
  • 68,894
  • 15
  • 164
  • 232
  • Unfortunate that it is not possible to directly write to the canvas, but I guess it makes sense that it should be sandboxed. Either way, how can one write an array to a `WebAssembly.Memory` instance? Also, is the other way around possible? Accessing a "WebAssembly owned array" from Javascript directly? Thus writing to that memory. – Bram Vanbilsen Apr 11 '20 at 15:10