Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

แอนิเมชั่นแคนวาสเพื่อสร้างเสียงเคลื่อนไหวอย่างไม่สิ้นสุดเพื่อให้ดูเหมือนการเคลื่อนไหวใน 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);
}