我正在尝试更新 Canvas 的图像上下文的 ImageData,当我尝试在数据数组中设置元素时,我收到一条错误消息,指出该数组具有Js.Typed_array.Uint8ClampedArray.t
预期的类型array('a)
。
为什么我不能更新 JS TypedArray 实现?
这是我的组件代码(为了清楚起见,有些简化):
let make = _children => {
let map = FeatureMap.make(100, 100);
let (width, height) = map.dimensions;
{...component,
initialState: () => {
map: map,
canvasRef: ref(None)
},
didMount: self => switch (self.state.canvasRef^) {
| None => ()
| Some(canvas) => {
let ctx = getContext2d(canvas);
let imageData = createImageDataCoords(ctx, ~width=float_of_int(width), ~height=float_of_int(height));
let data = Webapi.Dom.Image.data(imageData);
Array.iteri((x, row) => {
Array.iteri((y, weight) => {
let index = (x * width + y) * 4;
let (r, g, b) = weight;
data[index + 0] = r;
data[index + 1] = g;
data[index + 2] = b;
data[index + 3] = 0;
}, row);
}, map.weights);
ctx |> putImageData(imageData, 0., 0., 0., 0., float_of_int(width), float_of_int(height));
}
},
render: _self => <canvas id="weight-map"
width={string_of_int(width)}
height={string_of_int(width)}
ref={_self.handle(setCanvasRef)}></canvas>
};
};