-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotionPath.js
49 lines (45 loc) · 1.28 KB
/
MotionPath.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Effect } from "@donkeyclip/motorcortex";
import { path, getPathProgress } from "mc-animejs-core/lib/anime.es.js";
/**
* Takes as attributes:
* {
* animatedAttrs: {
* positionOn: {
* pathElement: "selector of the path element"
* }
* }
* }
}
**/
export default class MotionPath extends Effect {
onGetContext() {
this.pixelsAccuracy = this.attrs.pixelsAccuracy || 4;
this.calculatedPoints = [];
const svgEl = this.context.getElements(this.targetValue.pathElement)[0];
this.path = path(svgEl);
this.isPathTargetInsideSVG = this.element instanceof SVGElement;
}
onProgress(m) {
let toSet;
const distance =
Math.round(
(this.path.totalLength / this.pixelsAccuracy) * this.getFraction(m)
) * this.pixelsAccuracy;
if (this.calculatedPoints[distance] != null) {
toSet = this.calculatedPoints[distance];
} else {
const position = getPathProgress(
this.path,
distance / this.path.totalLength,
this.isPathTargetInsideSVG
);
toSet = `
translateX(${position.x}px)
translateY(${position.y}px)
rotate(${position.angle}deg)
`;
this.calculatedPoints[distance] = toSet;
}
this.element.style.transform = toSet;
}
}