const button = document.getElementById('healer')
const preloaded = new Image()
preloaded.src = "https://svgshare.com/i/KzN.svg"
const img = document.getElementById("heal")
//img.style.display = "none"
button.onclick = () => {
button.disabled = true
img.src = "https://svgshare.com/i/KzN.svg"
img.style.display = ""
setTimeout(() => {
button.disabled = false
img.src = ""
//img.style.display = "none"
}, 1000)
}
#monster {
height: 160px;
width: 160px;
}
#heal {
height: 160px;
width: 160px;
position: absolute;
top: 0;
left: 0;
}
<img id="monster" src="https://i.imgur.com/NMocKWy.png">
<img id="heal">
<button id="healer">
Show heal anim
</button>
As you can see, only the first click produces the animation. Subsequent clicks have no effect. I think this is because the SVG is set to only run animation once (SVG source code), and the browser does not reset the animation even when the img
tag's src
attribute is set to nothing and then to the SVG again (even though according to this Stackoverflow thread resetting src
should reset the animation: Proper way to reset a GIF animation with display:none on Chrome).
Setting the SVG to run the animation infinitely (modified SVG source code) is only slightly better:
const button = document.getElementById('healer')
const preloaded = new Image()
preloaded.src = "https://svgshare.com/i/L0q.svg"
const img = document.getElementById("heal")
//img.style.display = "none"
button.onclick = () => {
button.disabled = true
img.src = "https://svgshare.com/i/L0q.svg"
img.style.display = ""
setTimeout(() => {
button.disabled = false
img.src = ""
//img.style.display = "none"
}, 1000)
}
#monster {
height: 160px;
width: 160px;
}
#heal {
height: 160px;
width: 160px;
position: absolute;
top: 0;
left: 0;
}
<img id="monster" src="https://i.imgur.com/NMocKWy.png">
<img id="heal">
<button id="healer">
Show heal anim
</button>
It seems the browser keeps running the anim internally so again only the first button click is correct, subsequent clicks start the anim from the middle and end it in the middle as well, which I believe looks suboptimal.
Is there any way to make this work as intended? (I'm trying to implement healing animation for a Pokemon-like browser based game)
I suppose I could make it work if I inlined the SVG in HTML code since this would give me direct access to styling, and resetting the style AFAIK DOES restart the animation, but I find inlining graphics in HTML ugly. Is doing so my only option?
BTW display: none'
is commented out since as per the linked SO question it might mess with animation resetting (doesn't change anything for me though). Preloading doesn't seem to change much for me as well, but is needed due to svgshare's slow responsiveness. I'm on Firefox, Windows.