Skip to content

Commit

Permalink
Refactor treasure map block
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermesimoes committed Sep 11, 2023
1 parent ffe1235 commit a03bfe1
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions assets/blocks/animating-a-pirate-treasure-map-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,34 @@
var lineLength = line.getTotalLength();
var numDashes = 14;

function animate(event) {
function animate(el, transitionFn = "ease") {
el.style.strokeDashoffset = lineLength;
el.style.strokeDasharray = lineLength;

setTimeout(() => {
el.style["transition-duration"] = "3s";
el.style["transition-property"] = "stroke-dashoffset";
el.style["transition-timing-function"] = transitionFn;
el.style.strokeDashoffset = 0;
}, 10);
}

function handleClick(event) {
var button = event.target;

reset();

if (button.value === "continuous-line") {
animatedLine.style.strokeDashoffset = lineLength;
animatedLine.style.strokeDasharray = lineLength;
setTimeout(() => {
animatedLine.style["transition-duration"] = "3s";
animatedLine.style["transition-property"] = "stroke-dashoffset";
animatedLine.style.strokeDashoffset = 0;
}, 10);
} else { // "continuous-dashes" and "dash-by-dash"
animate(animatedLine);
} else {
// Make line dashed
animatedLine.style.strokeDasharray = lineLength / (numDashes * 2 - 1);

animatedLineMask.style.strokeDashoffset = lineLength;
animatedLineMask.style.strokeDasharray = lineLength;
setTimeout(() => {
animatedLineMask.style["transition-duration"] = "3s";
animatedLineMask.style["transition-property"] = "stroke-dashoffset";

if (button.value === "dash-by-dash") {
animatedLineMask.style["transition-timing-function"] = `steps(${numDashes}, jump-start)`;
}

animatedLineMask.style.strokeDashoffset = 0;
}, 10);
var transitionFn;
if (button.value === "dash-by-dash") {
transitionFn = `steps(${numDashes}, jump-start)`;
}
animate(animatedLineMask, transitionFn);
}
}

Expand All @@ -95,6 +95,6 @@
animatedLineMask.style["transition-property"] = "none";
}

document.querySelector(".controls").addEventListener("click", animate);
document.querySelector(".controls").addEventListener("click", handleClick);
</script>
</body>

0 comments on commit a03bfe1

Please sign in to comment.