0

我能够生成一个新ImageData对象,但无法将其渲染到画布上下文。出于某种原因,画布 2d 上下文不喜欢它创建的 ImageData 对象。

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var width = 50;
var height = 50;


var ci = ctx.createImageData(width,height); // ImageData { width: ..., height: ..., data: Uint8ClampedArray(10000) }

// Draw on ci ImageData object...

ctx.drawImage(ci, 0, 0, width, height);

但是,这会引发错误:

TypeError: Argument 1 of CanvasRenderingContext2D.drawImage could not be converted to any of: HTMLImageElement, SVGImageElement, HTMLCanvasElement, HTMLVideoElement, ImageBitmap.

如何将ImageData对象写入画布?

4

1 回答 1

2

要将ImageData对象写回画布,您必须使用context.putImageData() 方法

var ci = ctx.createImageData(width,height);
ctx.putImageData(ci, 0, 0);
于 2018-09-27T16:03:03.617 回答