【问题标题】:Control GIF animation with JS. Play, stop, play backwards用 JS 控制 GIF 动画。播放,停止,向后播放
【发布时间】:2016-09-07 16:18:37
【问题描述】:

我想要实现的是交互式动画徽标。默认情况下它在第一帧。当悬停时,它向前播放并在最后一帧停止。在鼠标离开时,它从最后一帧播放到第一帧(向后)并在第一帧停止。如果在正向动画期间 mouseleave -> 获取当前帧并向后播放。

我尝试使用画布和精灵来做到这一点,但这非常具有挑战性。其实我也不是很明白。我试过this plugin,但可能性有限。

所以我想知道我是否可以使用 GIF 来做到这一点?也许我可以获取当前动画帧并从该帧向后播放 gif?

【问题讨论】:

标签: javascript jquery animation canvas animated-gif


【解决方案1】:

是的,你可以用一些 js lib 来控制 gif,像这样:https://github.com/buzzfeed/libgif-js

html:

```

    <div id="controller-bar">
      <button id="backward" href="#">|< Step back </button>&nbsp;&nbsp;
      <button id="play" href="#"> Play | Pause </button>&nbsp;&nbsp;
      <button id="forward" href="#"> Step forward >|</button>
    </div>

```

javascript(使用 jQuery):

```

var gif = new SuperGif({
  gif: document.getElementById('example'),
  loop_mode: 'auto',
  auto_play: true,
  draw_while_loading: false,
  show_progress_bar: true,
  progressbar_height: 10,
  progressbar_foreground_color: 'rgba(0, 255, 4, 0.1)',
  progressbar_background_color: 'rgba(255,255,255,0.8)'

});


gif.load(function(){
  document.getElementById("controller-bar").style.visibility = 'visible';
  loaded = true;
  console.log('loaded');
});


$('button#backward').click(function(){
  console.log('current: ', gif.get_current_frame());
  var total_frames = gif.get_length();
  gif.pause();
  if(gif.get_current_frame() == 0) {
    gif.move_to(total_frames-1);
  } else {
    gif.move_relative(-1);
  }
  console.log('next: ', gif.get_current_frame());
})


$('button#play').click(function(){
  console.log('iam play');
  if(gif.get_playing()){
    gif.pause();
  } else {
    gif.play();
  }
})

$('button#forward').click(function(){
  console.log('current: ', gif.get_current_frame());
  gif.pause();
  gif.move_relative(1);
  console.log('next: ', gif.get_current_frame());
})

```

【讨论】:

  • “虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。”
猜你喜欢
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多