该putImageData()
方法将图像数据放置在画布上。为了给画布制作动画,我们在主循环之外创建了一个可重用的ImageData对象,
var ct = c.getContext("2d", {alpha: false}); // context without alpha channel. var a = ct.createImageData(c.width, c.height); var buffer = new Uint32Array(a.data.buffer); function loop() { noise(ct); requestAnimationFrame(loop) })() function noise(ct) { var l =buffer.length - 1; while(l--) buffer[l] = Math.random() <0.5 ?0 : -1>>0; ct.putImageData(a, 0, 0); }