Skip to content

Instantly share code, notes, and snippets.

@donatj
Created January 27, 2022 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save donatj/8ec12c55f256c09fdfd2ecd153404c06 to your computer and use it in GitHub Desktop.
Save donatj/8ec12c55f256c09fdfd2ecd153404c06 to your computer and use it in GitHub Desktop.
Simple Gif Play/Pause
<html>
<body>
<img src="tumblr_ngqdgj3vqt1sulnzno1_500.gif">
<img src="tumblr_ngqdgj3vqt1sulnzno1_500.gif" width="100">
<img src="tumblr_ngqdgj3vqt1sulnzno1_500.gif" style="width: 30px; height: 200px;">
<br>
<button onclick="play()">Play</button>
<button onclick="pause()">Pause</button>
<script>
const gifs = document.querySelectorAll('img[src*=".gif"]');
for (const g of gifs) {
g.setAttribute('data-src', g.src);
}
function play() {
for (const g of gifs) {
g.src = g.getAttribute('data-src');
}
}
function pause() {
for (const g of gifs) {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
var img = document.createElement("img");
img.onload = function () {
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
g.src = canvas.toDataURL();
}
img.src = g.src;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment