动画画布,无限次地为噪点添加动画效果,使其在 HTML 中呈现动感效果

htmlweb developmentfront end technology更新于 2025/8/19 0:44:17

putImageData() 方法将图像数据放置到画布上。为了使画布动起来,我们在主循环之外创建一个可重用的 ImageData 对象。

var ct = c.getContext("2d", {alpha: false});       // 不包含 Alpha 通道的上下文。
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);
}

相关文章


有用资源