Repository navigation
SVG morphTo results in "TypeError: $path1.getTotalLength is not a function" #1060
Answered
by
juliangarnier
PapiPeppers
asked this question in
Q&A
|
I have a theme toggle button on my astro site that uses either a sun or a moon icon. I'd like to morph between the sun and moon on clicks, but I'm not sure if I'm doing so properly. Console error reads: <button
id="theme-btn"
title="Toggles light and dark theme"
type="button"
aria-label="Color mode toggle"
>
<div class="icon"><Moon id="moon" /></div>
<div class="icon"><Sun id="sun" /></div>
</button>import { animate, utils, svg } from "animejs";
const [ $button ] = utils.$("#theme-btn");
const [ $sun ] = utils.$("#sun");
const [ $moon ] = utils.$("#moon");
const visibility = $sun.checkVisibility();
function morphToMoon() {
animate($sun, {
d: svg.morphTo($moon),
ease: "inOutCirc",
duration: 500
});
};
function morphToSun() {
animate($moon, {
d: svg.morphTo($sun),
ease: "inOutCirc",
duration: 500
});
};
const morph = (visibility === true) ? morphToMoon : morphToSun;
$button.addEventListener("click", morph);Environment details
|
Answered by
juliangarnier
Jul 31, 2025
Replies: 1 comment 1 reply
|
https://animejs.com/documentation/svg/morphto only works when targeting a animate('#sun path', {
d: svg.morphTo('#moon path'),
ease: "inOutCirc",
duration: 500
}); |
1 reply
Answer selected by
PapiPeppers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://animejs.com/documentation/svg/morphto only works when targeting a
SVGPolygonElement,SVGPolylineElementorSVGPathElement.And I believe that in your example,
#moonand#sunareSVGElement.You need to be more precise in the the parts of your SVG that you're trying to animate.
Something that should look more like this, but of course you'll need to decide which path element of your icon should be morphed: