為畫布新增動畫以無限地流動噪聲,從而在 HTML 中形成動畫效果


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);
}

更新日期:04-Mar-2020

77 次瀏覽

啟動你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.