Displaying images in html5 canvas from binary data

故事扮演 提交于 2019-12-12 02:29:57

问题


I am trying to send an image from a canvas through the google app engine channel api to another client who will then display the same image. The message is being received but it is not displaying the image.

On the sending side:

var image = context.getImageData(0, 0, imageCanvas.width, imageCanvas.height);
var buffer = new ArrayBuffer(image.data.length);
var bytes = new Uint8Array(buffer);
for (var i=0; i<bytes.length; i++) {
    bytes[i] = image.data[i];
}

sendMessage({image: buffer});

Rendering the data at the other end:

var bytes = new Uint8Array(buffer.size);
var image = context.createImageData(imageCanvas.width, imageCanvas.height);
for (var i=0; i<image.length; i++) {
    image.data[i] = bytes[i];
}
context.drawImage(image, 0, 0);

The console keeps saying there is a Type error on the final line.


回答1:


swap drawImage with putImageData

createImageData() returns an ImageData object.

http://tinker.io/e3ec8

you also have a mistake here: for (var i=0; i<image.length; i++) {
you want the image.data.length not the image length



来源:https://stackoverflow.com/questions/17879641/displaying-images-in-html5-canvas-from-binary-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!