javascript - 如何将图像数据从node.js传输到html5 canvas?

标签 javascript node.js v8 electron

我正在尝试构建一个演示,它是一个 Node.js C++ 插件。在node.js中,我得到一个ARGB格式的位图,我需要将其传递给html5 Canvas 。我正在尝试找到最有效的方法来做到这一点,因为它非常慢。

目前我执行以下操作:

--- in node.js ---
- Convert ARGB to RGBA (because ImageData accepts that format)
- Create v8::ArrayBuffer (wrapper over the underlying buffer)
- Create v8::Uint8ClampedArray (wrapper over the array buffer)
- Return an object which has the Uint8ClampedArray, width and height

--- in the browser ---
- Get the result from my function
- Create ImageData instance with the specified width and height
- Loop over all bytes in the Uint8ClampedArray and copy them to the image data
- context.putImageData(image_data, 0, 0);

我很确定一定有更优化的方法来做到这一点。以某种方式使插件中的缓冲区保持事件状态不是问题,但我想至少避免将缓冲区逐字节复制到图像数据。

我也不确定为什么如果我尝试使用以 Uint8ClampedArray 作为第一个参数的 ImageData 构造函数会崩溃。在这种情况下,我只是得到: v8::Object::GetAlignedPointerFromInternalField()。错误:不是 Smi

谢谢

最佳答案

您可以使用resterize js在 Canvas 中绑定(bind)数据。我用过这个,即使对于大尺寸的图像,它也能快速工作。

关于javascript - 如何将图像数据从node.js传输到html5 canvas?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38526261/

相关文章:

javascript - 使用 promise 顺序执行功能

javascript - Chrome 没有运行 javascript 垃圾收集器

javascript - 在 V8 中执行函数的确切数量变热之后?

javascript - 在 Ionic 中将 $ionicSlideBoxDelegate 用于两个或多个幻灯片框

构造函数的 JavaScript prototype.constructor 属性不在规范中?

javascript - yui和yui3的比较

node.js - 当我尝试在 cPanel 上安装 Laravel 时出现 pngquant 错误

javascript - NodeJS |等待使用请求模块重定向页面

javascript - 为什么 typeof null 的值会在循环内发生变化?

javascript - 如何在循环内进行 HTTP 调用并等待一个完成后再进行另一个调用?