可以使用的複合模式和削波的組合來實現這一點,而無需通過像素迭代(這意味着CORS不會在這個例子中一個問題):
Fiddle
基本代碼:
假設圖像已經加載,畫布設置,我們是好去:
function loop() {
x++; // sync this with actual progress
// since we will change composite mode and clip:
ctx.save();
// clear background with black
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, w, h);
// remove waveform, change composite mode
ctx.globalCompositeOperation = 'destination-atop';
ctx.drawImage(img, 0, 0, w, h);
// fill new alpha, same mode, different region.
// as this will remove anything else we need to clip it
ctx.fillStyle = '#00a'; /// gradient
ctx.rect(0, 0, x, h);
ctx.clip(); /// set clipping rect
ctx.fill(); /// use the same rect to fill
// remove clip and use default composite mode
ctx.restore();
// loop until end
if (x < w) requestAnimationFrame(start);
}
如果你想在「未播放」波形有不同的顏色簡單的風格,配有background
canvas元素。
來源
2013-10-23 23:23:49
K3N
嗨,我想這會做的伎倆,但只有一個問題。進度條將是一個像'
'的div,寬度會隨着歌曲的進度逐漸增加。有什麼辦法可以像這樣做進度條嗎?如果您需要查看我的網站,那麼我可以向您發送網址。 –