WebAssemblyでScratchプラグインを作ろう!
イベントリスナーも編集する
document.getElementById("snap").addEventListener("click", e => {
if (window.wasm === null) { return; }
const canvas = document.createElement('canvas');
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
const context = canvas.getContext('2d');
context.drawImage(video, 0, 0, width, height);
const dataUrl = canvas.toDataURL("image/png");
let offset = wasm.exports.__heap_base;
let memory = wasm.exports.memory;
let buffer = new Uint8Array(memory.buffer, offset, dataUrl.length);
for( var i = 0; i < dataUrl.length; i++ ) {
buffer[i] = dataUrl.charCodeAt(i);
}
const resOffset = grayscaleBlob(width, height, offset, dataUrl.length);
const resultBuf = new Uint8Array(memory.buffer, resOffset, 1 << 22);
let resultBase64 = "";
for ( var i = 0; resultBuf[i] != 0; i++) {
resultBase64 += String.fromCharCode(resultBuf[i]);
}
const image = document.getElementById("destination");
image.style.display = "inline";
image.src = "data:image/png;base64," + resultBase64;
});