<canvas id='myCanvas'></canvas>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var amplitude = 50; // 振幅 var period = 200; // 周期 var phase = 0; // 相位 var speed = 0.05; // 速度
function drawWave() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); for (var x = 0; x < canvas.width; x++) { var y = amplitude * Math.sin(2 * Math.PI * x / period + phase); ctx.lineTo(x, canvas.height / 2 + y); } ctx.strokeStyle = 'blue'; ctx.lineWidth = 2; ctx.stroke(); phase += speed; requestAnimationFrame(drawWave); }
drawWave();
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com