可以使用的复合模式和削波的组合来实现这一点,而无需通过像素迭代(这意味着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,宽度会随着歌曲的进度逐渐增加。有什么办法可以像这样做进度条吗?如果您需要查看我的网站,那么我可以向您发送网址。 –