简体   繁体   English

HTMLCanvasElement:获取 Uint8ClampedArray

[英]HTMLCanvasElement : Getting Uint8ClampedArray

I'm trying to implement this library on an angular 7 application.我正在尝试在 angular 7 应用程序上实现这个库。 For getting faces I need to convert a canvas html dom element to a Uint8ClampedArray .为了获得面孔,我需要将canvas html dom element转换为Uint8ClampedArray

Anyone knows how can I get a Uint8ClampedArray object from a CanvasElement?任何人都知道如何从 CanvasElement 获取Uint8ClampedArray对象?

ctx.getImageData will return an ImageData object, whichdata property will be an Uint8ClampedArray just like your lib is requiring. ctx.getImageData将返回一个 ImageData 对象,该对象的data属性将是一个 Uint8ClampedArray,就像你的库所要求的那样。

 const ctx = document.createElement('canvas').getContext('2d'); ctx.canvas.width = ctx.canvas.height = 20; ctx.fillStyle='red'; ctx.fillRect(0,0,20,20); // get an ImageData of your whole canvas const img = ctx.getImageData(0,0,ctx.canvas.width,ctx.canvas.height); console.log(img.data instanceof Uint8ClampedArray, img.data); // [r, g, b, a, r, g, b, a... // [255, 0, 0, 255, 255, 0, 0, 255...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM