1

我有两个png,一个白色,另一个红色。

<img class="rangeHorizontal" id="seek"     src="http://i.imgur.com/hRHH9VO.png">
            <img id="seekFill" src="http://i.imgur.com/WoJggN0.png">

当歌曲没有播放时,它应该是白色的,当歌曲正在播放时,它应该随着歌曲的进行而填充红色,并且分别向后和向前擦洗时也是如此。

除了 Canvas 部分之外,我已经能够应付大部分的播放功能。

目前,这两个 png 是相互叠加的,当歌曲播放时,整个红色 png 叠加在顶部,.. 而不是只显示一部分,.. 这很难解释,但我有一个小提琴,所以事情变得更清楚了。

https://jsfiddle.net/tallgirltaadaa/q9qgyob0/

想要的结果就像这个播放器一样,它还使用了两个 png 方法:

http://codecanyon.net/item/zoomsounds-neat-html5-audio-player/full_screen_preview/4525354?ref=hkeyjun

如果有人可以帮助我一点,我会喜欢的。我整个早上都在蒙面并尝试使用画布,但没有运气。

4

1 回答 1

1

代码有点多,但这里有一种技术可以用来绘制图像的剪辑版本。根据需要实施 -

现场示例

在每个timeupdate

  • 画布被清除
  • 绘制了白色底部图像(您可以根据需要缩放这些图像)
  • 计算进度(currentTime / duration)
  • 红顶图像是使用裁剪参数绘制的:

IE。

ctx.drawImage(image, sx, sy, sw, sh,  dx, dy, dw, dh);

完整的示例代码(由于 API 的使用不得不替换音乐) -

var imgBg = new Image(),
    imgFg = new Image(),
    count = 2;
imgBg.onload = imgFg.onload = init;
imgBg.src = "http://i.imgur.com/hRHH9VO.png";
imgFg.src = "http://i.imgur.com/WoJggN0.png";

function init() {
  if (--count) return;   // makes sure both images are loaded
  
   var canvas = document.querySelector("canvas"),
       ctx = canvas.getContext("2d"),
       audio =  document.querySelector("audio");
  
  canvas.width = imgBg.naturalWidth;
  canvas.height = imgBg.naturalHeight;
  
  render();
  
  audio.volume = 0.5;
  audio.addEventListener("timeupdate", render);
  
  function render() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.drawImage(imgBg, 0, 0);
    
    // calc progress
    var pst = audio.currentTime / audio.duration;
    
    // draw clipped version of top image
    if (pst > 0) {
      ctx.drawImage(imgFg, 0, 0, (canvas.width * pst)|0, canvas.height,  // source
                           0, 0, (canvas.width * pst)|0, canvas.height); // dst
    }
  }
}
body {background:#ccc}
canvas {width:600px;height:auto}
<audio src="https://raw.githubusercontent.com/epistemex/free-music-for-test-and-demo/master/music/kf_colibris.mp3" controls></audio>
<canvas></canvas>

于 2015-04-20T23:06:40.870 回答