feat(2d): add Path component - #700
Conversation
This is based on kute.js svgMorph plugin
|
Lets gooo!! one step closer to latex animation |
aarthificial
left a comment
There was a problem hiding this comment.
There are some problems with the tweening algorithm:
Subpaths disappear when tweening:
project.mp4
Code
import {makeScene2D} from '@motion-canvas/2d/lib/scenes';
import {Path} from '@motion-canvas/2d/lib/components';
import {createRef} from '@motion-canvas/core/lib/utils';
import {waitFor} from '@motion-canvas/core/lib';
export default makeScene2D(function* (view) {
const path = createRef<Path>();
view.add(
<Path
ref={path}
lineWidth={8}
stroke={'white'}
data="M -200,0 h 100 M 100,0 h 100"
/>,
);
yield* waitFor(0.5);
yield* path().data('M -200,100 h 100 M 100,0 h 100', 1);
yield* waitFor(0.5);
});The path gets closed during the transition
close.mp4
Code
import {makeScene2D} from '@motion-canvas/2d/lib/scenes';
import {Path} from '@motion-canvas/2d/lib/components';
import {createRef} from '@motion-canvas/core/lib/utils';
import {waitFor} from '@motion-canvas/core/lib';
export default makeScene2D(function* (view) {
const path = createRef<Path>();
view.add(
<Path
ref={path}
lineWidth={8}
stroke={'white'}
data="M -200,-200 h 400 v 400"
/>,
);
yield* waitFor(0.5);
yield* path().data('M -200,-200 v 400 h 400', 1);
yield* waitFor(0.5);
});Tweening to an empty path throws an error.
Code
import {makeScene2D} from '@motion-canvas/2d/lib/scenes';
import {Path} from '@motion-canvas/2d/lib/components';
import {createRef} from '@motion-canvas/core/lib/utils';
import {waitFor} from '@motion-canvas/core/lib';
export default makeScene2D(function* (view) {
const path = createRef<Path>();
view.add(
<Path
ref={path}
lineWidth={8}
stroke={'white'}
data="M -200,-200 h 400 v 400"
/>,
);
yield* waitFor(0.5);
yield* path().data('M 0,0', 1);
yield* waitFor(0.5);
});Co-authored-by: Jacob <64662184+aarthificial@users.noreply.github.com>
Added function: - static function `angleBetween` to calculate angle between two vector. - `cross` to calc length of cross product of current vector and other.
217dc14 to
931a4d9
Compare
Use getBBox to calculate arc extreme point Co-authored-by: Jacob <64662184+aarthificial@users.noreply.github.com>
Add points property to Segment and implement in all Segment subclass
Co-authored-by: Jacob <64662184+aarthificial@users.noreply.github.com>
Path balancing mechanism If a path lacks subpaths, a new subpath is added to it. A new subpath consists of 1 LineSegment that first point and end point is first point of subpath in another path that have same index.
This also prevent filled area gone when morphing between closed and unclosed path.
This is more simple and performant approach than before.
This happened especially when arc segment is first segment.
When close path contain arc then it will parsed as 2 open path.
|
I have fixed all issue. Kute.js svgMorph actually cannot tween unclosed path well. Its also caanot tween between closed and unclosed path. It only can tween unclosed single line into closed shape. I create my own implementation to do that. I implement 2 method in 2 commit.
In my opionion, we must use second method. Although the interpolation is different from KUTE.js, the code is much simpler and performant. |
aarthificial
left a comment
There was a problem hiding this comment.
The PR is generally good for merging but the documentation requires some updates.
Since #712, fiddles (```tsx editor) are required to correctly import all modules they use.
|
I have fixed documentation error. |
|
Awesome! Thanks for the PR and apologies for the delays. |
|
Thanks a ton @levirs565! Whenever you're ready, please go ahead and open the next PR. We really appreciate your work on this! |
This will add
Pathcomponent to render and animate SVG path.This is part of add Latex component pull request.
Pathcomponent will inheritCurvecomponent. This component can morph between two SVG path.Why Morphing Path is Necessary for Latex Tweening
When we render Latex, some character can render into different SVG path depending on its situtation.
For example:
(and)character.You can see these two math.
We can see that
(and)in these two math look have two size. Path of(character in two math are actually different. When tweening latex between these math, we must morph path of(character.