Skip to content

Using MorhpSVG plugin without animation #118

@lordanubi

Description

@lordanubi

I'm working on a React Component animated Transform that uses SVGPathCommander under the hood:

const AnimatedTransform = ({children,...transfProps }) => {
  if (window.isBrowser) {
    return children.map(path => {
      const { transformeD, originalD, d, ...pathProps } = path.props;
      const after = transformPath(d, {...transfProps})
      return <animated.path {...pathProps} {...getMorph(d, after)} />
    })
  }
  return children
}

transformPath is a wrapper for SVGPathCommander basic methods:

export default function transformPath(pathString, transformProps) {
    const pathCommander = new SVGPathCommander(pathString)
    const { flipX, flipY, rotate, scale, scaleX, scaleY, x, y, origin, optimize ,reverse} = transformProps;
    if (flipX) pathCommander.transform({rotate: [0, 180, 0], origin: origin });
    if (flipY) pathCommander.transform({ rotate: [180, 0, 0], origin: origin });
    if (rotate) pathCommander.transform({ rotate, origin: origin });
    if (scale || scaleX || scaleY) pathCommander.transform({ origin: origin, scale: [scaleX || scale || 1, scaleY || scale || 1] });
    if (x || y) pathCommander.transform({ translate: [x, y] });
 
    if (optimize) pathCommander.optimize().optimize()
    return pathCommander.toString()
 }
 export default function getMorph(from, to) {
    const springProps = useSpring({ from: { d: from }, to: { d: to } ,config: { mass: 1, tension: 280, friction: 120 }})
    return {...springProps, originalD: from, transformeD: to}
  }

while getMorph is a function that uses React Spring to get the interpolated d attribute that gets the path to morph from what it was before the transform to what it was after.
It's working for simple transforms, but it won't work for complex paths that gets transformed too much (ofc it won't work with path optimisation as well). I was wondering if there's any way to still use React Spring for physics animations and SVGPathCommnader to make the transforms but have some sort of utility function from KUTE.js that prepare the before and after path string so that it animates correctly from one to the other.

And by the way thanks again to all the amazing work you did in the SVG field. It's really appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions