Modify canvas from wasm

前端 未结 4 912
死守一世寂寞
死守一世寂寞 2021-02-10 05:53

Is it possible to efficiently modify the html5 canvas from web assembly?

Update:

var imageData = context.getImageData(x, y, w, h)
var buffer = imageData         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-10 06:21

    1. Anyway, you will have to copy pixels to WebAssembly.Memory instance. Then modify and copy back.
    2. I don't know why, but Uint8Array.set() is not fast in latest Chrome. It's better to recast data to 32 bit (new Uint32Array(your_uint8array)), and then use Uint32Array.set() to copy.
    3. Keep in mind, that canva's .getImageData()/.setImageData() are not fast. Probably, because they do alpha premultiply and other things.

    To summarize things: your most speed loss will be in .getImageData/.setImageData, and that can't be avoided. Other things have workarounds.

    If compare with optimized JS, wasm will give you 10-20% of benefits, not too much.

提交回复
热议问题