From 3fd70dd69ef1968eb5bb787e69a4e84a53933751 Mon Sep 17 00:00:00 2001 From: Guilherme Simoes Date: Sun, 10 Sep 2023 16:27:39 +0100 Subject: [PATCH] Refactor treasure map block --- .../animating-a-pirate-treasure-map-path.html | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/assets/blocks/animating-a-pirate-treasure-map-path.html b/assets/blocks/animating-a-pirate-treasure-map-path.html index 90ada7bb..d2bf8e1b 100644 --- a/assets/blocks/animating-a-pirate-treasure-map-path.html +++ b/assets/blocks/animating-a-pirate-treasure-map-path.html @@ -65,34 +65,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); } } @@ -103,6 +103,6 @@ animatedLineMask.style["transition-property"] = "none"; } -document.querySelector(".js-controls").addEventListener("click", animate); +document.querySelector(".js-controls").addEventListener("click", handleClick);