javascript - 从二进制数据在 html5 canvas 中显示图像

标签 javascript google-app-engine python-2.7 html5-canvas channel-api

我正在尝试通过 Google 应用程序引擎 channel API 将 Canvas 中的图像发送到另一个客户端,然后该客户端将显示相同的图像。正在接收消息,但未显示图像。

在发送端:

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});

在另一端渲染数据:

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);

控制台一直提示最后一行有类型错误

最佳答案

将drawImage与putImageData交换

createImageData() 返回一个 ImageData 对象。

http://tinker.io/e3ec8

你这里也有一个错误: for (var i=0; i<image.length; i++) {
你想要image.data.length不是图像长度

关于javascript - 从二进制数据在 html5 canvas 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17879641/

相关文章:

javascript - "JavaScript heap out of memory"传输大文件时

javascript - Firefox 的 Math.round 或 Date 不同吗?

python - 尝试在 Google App Engine 项目中创建备份时出现 404

python - `tabulate` 按键排序列

python - 在 Trinket.io 上更改 python 2 中海龟的大小

javascript - 对图像文件 react native 使用变量

javascript - 修复 JS 粘性标题故障

java - 无法构建 Spring-boot-sample-gae(在 GAE 上部署 Spring Boot)

php - 调用 URLFetch 失败,出现 url x 的应用程序错误 5

python-2.7 - 在 Azure WebJobs 中使用 Python 包显示错误 - ImportError : DLL load failed