@leafer-in/motion-path 1.7.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,13 @@
1
- import { BezierHelper, PathCommandMap, OneRadian, UnitConvert, PointHelper, MatrixHelper, decorateLeafAttr, attr, isNull, Plugin, Transition, UI, LeafHelper, BranchHelper } from '@leafer-ui/draw';
1
+ import { BezierHelper, PathCommandMap, OneRadian, UnitConvert, PointHelper, MatrixHelper, decorateLeafAttr, attr, isNull, Plugin, Transition, isObject, UI, LeafHelper, BranchHelper, isNumber } from "@leafer-ui/draw";
2
+
3
+ const gaussNodes = [ .1488743389, .4333953941, .6794095682, .8650633666, .9739065285 ];
4
+
5
+ const gaussWeights = [ .2955242247, .2692667193, .2190863625, .1494513491, .0666713443 ];
6
+
7
+ const {sqrt: sqrt} = Math;
8
+
9
+ const {getDerivative: getDerivative} = BezierHelper;
2
10
 
3
- const gaussNodes = [0.1488743389, 0.4333953941, 0.6794095682, 0.8650633666, 0.9739065285];
4
- const gaussWeights = [0.2955242247, 0.2692667193, 0.2190863625, 0.1494513491, 0.0666713443];
5
- const { sqrt } = Math;
6
- const { getDerivative } = BezierHelper;
7
11
  const HighBezierHelper = {
8
12
  getDistance(fromX, fromY, x1, y1, x2, y2, toX, toY, t = 1) {
9
13
  let distance = 0, t1, t2, d1X, d1Y, d2X, d2Y, half = t / 2;
@@ -25,10 +29,8 @@ const HighBezierHelper = {
25
29
  },
26
30
  getT(distance, totalDistance, fromX, fromY, x1, y1, x2, y2, toX, toY, precision = 1) {
27
31
  let low = 0, high = 1, middle = distance / totalDistance, realPrecision = precision / totalDistance / 3;
28
- if (middle >= 1)
29
- return 1;
30
- if (middle <= 0)
31
- return 0;
32
+ if (middle >= 1) return 1;
33
+ if (middle <= 0) return 0;
32
34
  while (high - low > realPrecision) {
33
35
  getDistance(fromX, fromY, x1, y1, x2, y2, toX, toY, middle) < distance ? low = middle : high = middle;
34
36
  middle = (low + high) / 2;
@@ -46,10 +48,13 @@ const HighBezierHelper = {
46
48
  data.push(PathCommandMap.C, ax, ay, bx, by, cx, cy);
47
49
  }
48
50
  };
49
- const { getDistance } = HighBezierHelper;
50
51
 
51
- const { M, L, C, Z } = PathCommandMap;
52
+ const {getDistance: getDistance} = HighBezierHelper;
53
+
54
+ const {M: M, L: L, C: C, Z: Z} = PathCommandMap;
55
+
52
56
  const tempPoint = {}, tempFrom = {};
57
+
53
58
  const HighCurveHelper = {
54
59
  transform(data, matrix) {
55
60
  let i = 0, command;
@@ -57,17 +62,19 @@ const HighCurveHelper = {
57
62
  while (i < len) {
58
63
  command = data[i];
59
64
  switch (command) {
60
- case M:
61
- case L:
62
- HighCurveHelper.transformPoints(data, matrix, i, 1);
63
- i += 3;
64
- break;
65
- case C:
66
- HighCurveHelper.transformPoints(data, matrix, i, 3);
67
- i += 7;
68
- break;
69
- case Z:
70
- i += 1;
65
+ case M:
66
+ case L:
67
+ HighCurveHelper.transformPoints(data, matrix, i, 1);
68
+ i += 3;
69
+ break;
70
+
71
+ case C:
72
+ HighCurveHelper.transformPoints(data, matrix, i, 3);
73
+ i += 7;
74
+ break;
75
+
76
+ case Z:
77
+ i += 1;
71
78
  }
72
79
  }
73
80
  },
@@ -87,35 +94,42 @@ const HighCurveHelper = {
87
94
  while (i < len) {
88
95
  command = data[i];
89
96
  switch (command) {
90
- case M:
91
- case L:
92
- toX = data[i + 1];
93
- toY = data[i + 2];
94
- distance = (command === L && i > 0) ? PointHelper.getDistanceFrom(x, y, toX, toY) : 0;
95
- x = toX;
96
- y = toY;
97
- i += 3;
98
- break;
99
- case C:
100
- toX = data[i + 5];
101
- toY = data[i + 6];
102
- distance = HighBezierHelper.getDistance(x, y, data[i + 1], data[i + 2], data[i + 3], data[i + 4], toX, toY);
103
- x = toX;
104
- y = toY;
105
- i += 7;
106
- break;
107
- case Z:
108
- i += 1;
109
- default:
110
- distance = 0;
97
+ case M:
98
+ case L:
99
+ toX = data[i + 1];
100
+ toY = data[i + 2];
101
+ distance = command === L && i > 0 ? PointHelper.getDistanceFrom(x, y, toX, toY) : 0;
102
+ x = toX;
103
+ y = toY;
104
+ i += 3;
105
+ break;
106
+
107
+ case C:
108
+ toX = data[i + 5];
109
+ toY = data[i + 6];
110
+ distance = HighBezierHelper.getDistance(x, y, data[i + 1], data[i + 2], data[i + 3], data[i + 4], toX, toY);
111
+ x = toX;
112
+ y = toY;
113
+ i += 7;
114
+ break;
115
+
116
+ case Z:
117
+ i += 1;
118
+
119
+ default:
120
+ distance = 0;
111
121
  }
112
122
  segments.push(distance);
113
123
  total += distance;
114
124
  }
115
- return { total, segments, data };
125
+ return {
126
+ total: total,
127
+ segments: segments,
128
+ data: data
129
+ };
116
130
  },
117
131
  getDistancePoint(distanceData, motionDistance, motionPrecision) {
118
- const { segments, data } = distanceData;
132
+ const {segments: segments, data: data} = distanceData;
119
133
  motionDistance = UnitConvert.number(motionDistance, distanceData.total);
120
134
  let total = 0, distance, to = {};
121
135
  let i = 0, index = 0, x = 0, y = 0, toX, toY, command;
@@ -124,45 +138,47 @@ const HighCurveHelper = {
124
138
  while (i < len) {
125
139
  command = data[i];
126
140
  switch (command) {
127
- case M:
128
- case L:
129
- toX = data[i + 1];
130
- toY = data[i + 2];
131
- distance = segments[index];
132
- if (total + distance >= motionDistance || !distanceData.total) {
133
- if (!i)
134
- x = toX, y = toY;
135
- tempFrom.x = x;
136
- tempFrom.y = y;
137
- to.x = toX;
138
- to.y = toY;
139
- PointHelper.getDistancePoint(tempFrom, to, motionDistance - total, true);
140
- to.rotation = PointHelper.getAngle(tempFrom, to);
141
- return to;
142
- }
143
- x = toX;
144
- y = toY;
145
- i += 3;
146
- break;
147
- case C:
148
- toX = data[i + 5];
149
- toY = data[i + 6];
150
- distance = segments[index];
151
- if (total + distance >= motionDistance) {
152
- x1 = data[i + 1], y1 = data[i + 2], x2 = data[i + 3], y2 = data[i + 4];
153
- t = HighBezierHelper.getT(motionDistance - total, distance, x, y, x1, y1, x2, y2, toX, toY, motionPrecision);
154
- BezierHelper.getPointAndSet(t, x, y, x1, y1, x2, y2, toX, toY, to);
155
- to.rotation = HighBezierHelper.getRotation(t, x, y, x1, y1, x2, y2, toX, toY);
156
- return to;
157
- }
158
- x = toX;
159
- y = toY;
160
- i += 7;
161
- break;
162
- case Z:
163
- i += 1;
164
- default:
165
- distance = 0;
141
+ case M:
142
+ case L:
143
+ toX = data[i + 1];
144
+ toY = data[i + 2];
145
+ distance = segments[index];
146
+ if (total + distance >= motionDistance || !distanceData.total) {
147
+ if (!i) x = toX, y = toY;
148
+ tempFrom.x = x;
149
+ tempFrom.y = y;
150
+ to.x = toX;
151
+ to.y = toY;
152
+ PointHelper.getDistancePoint(tempFrom, to, motionDistance - total, true);
153
+ to.rotation = PointHelper.getAngle(tempFrom, to);
154
+ return to;
155
+ }
156
+ x = toX;
157
+ y = toY;
158
+ i += 3;
159
+ break;
160
+
161
+ case C:
162
+ toX = data[i + 5];
163
+ toY = data[i + 6];
164
+ distance = segments[index];
165
+ if (total + distance >= motionDistance) {
166
+ x1 = data[i + 1], y1 = data[i + 2], x2 = data[i + 3], y2 = data[i + 4];
167
+ t = HighBezierHelper.getT(motionDistance - total, distance, x, y, x1, y1, x2, y2, toX, toY, motionPrecision);
168
+ BezierHelper.getPointAndSet(t, x, y, x1, y1, x2, y2, toX, toY, to);
169
+ to.rotation = HighBezierHelper.getRotation(t, x, y, x1, y1, x2, y2, toX, toY);
170
+ return to;
171
+ }
172
+ x = toX;
173
+ y = toY;
174
+ i += 7;
175
+ break;
176
+
177
+ case Z:
178
+ i += 1;
179
+
180
+ default:
181
+ distance = 0;
166
182
  }
167
183
  index++;
168
184
  total += distance;
@@ -170,7 +186,7 @@ const HighCurveHelper = {
170
186
  return to;
171
187
  },
172
188
  getDistancePath(distanceData, motionDistance, motionPrecision) {
173
- const { segments, data } = distanceData, path = [];
189
+ const {segments: segments, data: data} = distanceData, path = [];
174
190
  motionDistance = UnitConvert.number(motionDistance, distanceData.total);
175
191
  let total = 0, distance, to = {};
176
192
  let i = 0, index = 0, x = 0, y = 0, toX, toY, command;
@@ -179,47 +195,49 @@ const HighCurveHelper = {
179
195
  while (i < len) {
180
196
  command = data[i];
181
197
  switch (command) {
182
- case M:
183
- case L:
184
- toX = data[i + 1];
185
- toY = data[i + 2];
186
- distance = segments[index];
187
- if (total + distance >= motionDistance || !distanceData.total) {
188
- if (!i)
189
- x = toX, y = toY;
190
- tempFrom.x = x;
191
- tempFrom.y = y;
192
- to.x = toX;
193
- to.y = toY;
194
- PointHelper.getDistancePoint(tempFrom, to, motionDistance - total, true);
195
- path.push(command, to.x, to.y);
196
- return path;
197
- }
198
- x = toX;
199
- y = toY;
200
- i += 3;
201
- path.push(command, x, y);
202
- break;
203
- case C:
204
- x1 = data[i + 1], y1 = data[i + 2], x2 = data[i + 3], y2 = data[i + 4];
205
- toX = data[i + 5];
206
- toY = data[i + 6];
207
- distance = segments[index];
208
- if (total + distance >= motionDistance) {
209
- t = HighBezierHelper.getT(motionDistance - total, distance, x, y, x1, y1, x2, y2, toX, toY, motionPrecision);
210
- HighBezierHelper.cut(path, t, x, y, x1, y1, x2, y2, toX, toY);
211
- return path;
212
- }
213
- x = toX;
214
- y = toY;
215
- i += 7;
216
- path.push(command, x1, y1, x2, y2, toX, toY);
217
- break;
218
- case Z:
219
- i += 1;
220
- path.push(command);
221
- default:
222
- distance = 0;
198
+ case M:
199
+ case L:
200
+ toX = data[i + 1];
201
+ toY = data[i + 2];
202
+ distance = segments[index];
203
+ if (total + distance >= motionDistance || !distanceData.total) {
204
+ if (!i) x = toX, y = toY;
205
+ tempFrom.x = x;
206
+ tempFrom.y = y;
207
+ to.x = toX;
208
+ to.y = toY;
209
+ PointHelper.getDistancePoint(tempFrom, to, motionDistance - total, true);
210
+ path.push(command, to.x, to.y);
211
+ return path;
212
+ }
213
+ x = toX;
214
+ y = toY;
215
+ i += 3;
216
+ path.push(command, x, y);
217
+ break;
218
+
219
+ case C:
220
+ x1 = data[i + 1], y1 = data[i + 2], x2 = data[i + 3], y2 = data[i + 4];
221
+ toX = data[i + 5];
222
+ toY = data[i + 6];
223
+ distance = segments[index];
224
+ if (total + distance >= motionDistance) {
225
+ t = HighBezierHelper.getT(motionDistance - total, distance, x, y, x1, y1, x2, y2, toX, toY, motionPrecision);
226
+ HighBezierHelper.cut(path, t, x, y, x1, y1, x2, y2, toX, toY);
227
+ return path;
228
+ }
229
+ x = toX;
230
+ y = toY;
231
+ i += 7;
232
+ path.push(command, x1, y1, x2, y2, toX, toY);
233
+ break;
234
+
235
+ case Z:
236
+ i += 1;
237
+ path.push(command);
238
+
239
+ default:
240
+ distance = 0;
223
241
  }
224
242
  index++;
225
243
  total += distance;
@@ -229,7 +247,7 @@ const HighCurveHelper = {
229
247
  };
230
248
 
231
249
  function motionPathType(defaultValue) {
232
- return decorateLeafAttr(defaultValue, (key) => attr({
250
+ return decorateLeafAttr(defaultValue, key => attr({
233
251
  set(value) {
234
252
  this.__setAttr(key, value);
235
253
  this.__hasMotionPath = this.motionPath || !isNull(this.motion);
@@ -238,105 +256,97 @@ function motionPathType(defaultValue) {
238
256
  }));
239
257
  }
240
258
 
241
- Plugin.add('motion-path');
242
- Transition.register('motion', function (from, to, t, target) {
243
- if (!from)
244
- from = 0;
245
- else if (typeof from === 'object')
246
- from = UnitConvert.number(from, target.getMotionTotal());
247
- if (!to)
248
- to = 0;
249
- else if (typeof to === 'object')
250
- to = UnitConvert.number(to, target.getMotionTotal());
251
- return Transition.number(from, to, t);
259
+ Plugin.add("motion-path");
260
+
261
+ Transition.register("motion", function(from, to, t, target) {
262
+ if (isObject(from)) from = UnitConvert.number(from, target.getMotionTotal());
263
+ if (isObject(to)) to = UnitConvert.number(to, target.getMotionTotal());
264
+ return Transition.number(from || 0, to || 0, t);
252
265
  });
253
- Transition.register('motionRotation', function (from, to, t) {
266
+
267
+ Transition.register("motionRotation", function(from, to, t) {
254
268
  return Transition.number(from, to, t);
255
269
  });
270
+
256
271
  const ui = UI.prototype;
257
- const { updateMatrix, updateAllMatrix } = LeafHelper;
258
- const { updateBounds } = BranchHelper;
259
- UI.addAttr('motionPath', undefined, motionPathType);
260
- UI.addAttr('motionPrecision', 1, motionPathType);
261
- UI.addAttr('motion', undefined, motionPathType);
262
- UI.addAttr('motionRotation', true, motionPathType);
263
- ui.getMotionPathData = function () {
272
+
273
+ const {updateMatrix: updateMatrix, updateAllMatrix: updateAllMatrix} = LeafHelper;
274
+
275
+ const {updateBounds: updateBounds} = BranchHelper;
276
+
277
+ UI.addAttr("motionPath", undefined, motionPathType);
278
+
279
+ UI.addAttr("motionPrecision", 1, motionPathType);
280
+
281
+ UI.addAttr("motion", undefined, motionPathType);
282
+
283
+ UI.addAttr("motionRotation", true, motionPathType);
284
+
285
+ ui.getMotionPathData = function() {
264
286
  return getMotionPathData(getMotionPath(this));
265
287
  };
266
- ui.getMotionPoint = function (motionDistance) {
288
+
289
+ ui.getMotionPoint = function(motionDistance) {
267
290
  const path = getMotionPath(this);
268
291
  const data = getMotionPathData(path);
269
- if (!data.total)
270
- return {};
292
+ if (!data.total) return {};
271
293
  const point = HighCurveHelper.getDistancePoint(data, motionDistance, path.motionPrecision);
272
294
  MatrixHelper.toOuterPoint(path.localTransform, point);
273
- const { motionRotation } = this;
274
- if (motionRotation === false)
275
- delete point.rotation;
276
- else if (typeof motionRotation === 'number')
277
- point.rotation += motionRotation;
295
+ const {motionRotation: motionRotation} = this;
296
+ if (motionRotation === false) delete point.rotation; else if (isNumber(motionRotation)) point.rotation += motionRotation;
278
297
  return point;
279
298
  };
280
- ui.getMotionTotal = function () {
299
+
300
+ ui.getMotionTotal = function() {
281
301
  return this.getMotionPathData().total;
282
302
  };
283
- ui.__updateMotionPath = function () {
303
+
304
+ ui.__updateMotionPath = function() {
284
305
  const data = this.__;
285
- if (this.__layout.resized && data.__pathForMotion)
286
- data.__pathForMotion = undefined;
306
+ if (this.__layout.resized && data.__pathForMotion) data.__pathForMotion = undefined;
287
307
  if (this.motionPath) {
288
308
  let child;
289
- const { children } = this.parent;
309
+ const {children: children} = this.parent;
290
310
  for (let i = 0; i < children.length; i++) {
291
311
  child = children[i];
292
312
  if (!isNull(child.motion) && !child.__layout.matrixChanged) {
293
- if (child !== this)
294
- child.__extraUpdate();
313
+ if (child !== this) child.__extraUpdate();
295
314
  updateMotion(child);
296
315
  }
297
316
  }
298
- }
299
- else
300
- updateMotion(this);
317
+ } else updateMotion(this);
301
318
  };
319
+
302
320
  function updateMotion(leaf) {
303
- const { motion, leaferIsCreated } = leaf;
304
- if (isNull(motion))
305
- return;
306
- if (leaferIsCreated)
307
- leaf.leafer.created = false;
321
+ const {motion: motion, leaferIsCreated: leaferIsCreated} = leaf;
322
+ if (isNull(motion)) return;
323
+ if (leaferIsCreated) leaf.leafer.created = false;
308
324
  if (leaf.motionPath) {
309
325
  const data = getMotionPathData(leaf);
310
- if (data.total)
311
- leaf.__.__pathForRender = HighCurveHelper.getDistancePath(data, motion, leaf.motionPrecision);
312
- }
313
- else {
326
+ if (data.total) leaf.__.__pathForRender = HighCurveHelper.getDistancePath(data, motion, leaf.motionPrecision);
327
+ } else {
314
328
  leaf.set(leaf.getMotionPoint(motion));
315
329
  if (!leaf.__hasAutoLayout) {
316
- if (leaf.isBranch)
317
- updateAllMatrix(leaf), updateBounds(leaf, leaf);
318
- else
319
- updateMatrix(leaf);
330
+ if (leaf.isBranch) updateAllMatrix(leaf), updateBounds(leaf, leaf); else updateMatrix(leaf);
320
331
  }
321
332
  }
322
- if (leaferIsCreated)
323
- leaf.leafer.created = true;
333
+ if (leaferIsCreated) leaf.leafer.created = true;
324
334
  }
335
+
325
336
  function getMotionPath(leaf) {
326
- const { parent } = leaf;
337
+ const {parent: parent} = leaf;
327
338
  if (!leaf.motionPath && parent) {
328
- const { children } = parent;
339
+ const {children: children} = parent;
329
340
  for (let i = 0; i < children.length; i++) {
330
- if (children[i].motionPath)
331
- return children[i];
341
+ if (children[i].motionPath) return children[i];
332
342
  }
333
343
  }
334
344
  return leaf;
335
345
  }
346
+
336
347
  function getMotionPathData(leaf) {
337
348
  const data = leaf.__;
338
- if (data.__pathForMotion)
339
- return data.__pathForMotion;
349
+ if (data.__pathForMotion) return data.__pathForMotion;
340
350
  return data.__pathForMotion = HighCurveHelper.getMotionPathData(leaf.getPath(true, true));
341
351
  }
342
352
 
@@ -1,2 +1,2 @@
1
- import{BezierHelper as t,PathCommandMap as o,OneRadian as e,UnitConvert as n,PointHelper as a,MatrixHelper as i,decorateLeafAttr as r,attr as s,isNull as c,Plugin as h,Transition as u,UI as l,LeafHelper as f,BranchHelper as g}from"@leafer-ui/draw";const m=[.1488743389,.4333953941,.6794095682,.8650633666,.9739065285],d=[.2955242247,.2692667193,.2190863625,.1494513491,.0666713443],{sqrt:_}=Math,{getDerivative:P}=t,p={getDistance(t,o,e,n,a,i,r,s,c=1){let h,u,l,f,g,p,M=0,b=c/2;for(let c=0;c<m.length;c++)h=b*(1+m[c]),u=b*(1-m[c]),l=P(h,t,e,a,r),f=P(h,o,n,i,s),g=P(u,t,e,a,r),p=P(u,o,n,i,s),M+=d[c]*(_(l*l+f*f)+_(g*g+p*p));return M*b},getRotation(t,o,n,a,i,r,s,c,h){const u=P(t,o,a,r,c),l=P(t,n,i,s,h);return Math.atan2(l,u)/e},getT(t,o,e,n,a,i,r,s,c,h,u=1){let l=0,f=1,g=t/o,m=u/o/3;if(g>=1)return 1;if(g<=0)return 0;for(;f-l>m;)M(e,n,a,i,r,s,c,h,g)<t?l=g:f=g,g=(l+f)/2;return g},cut(t,e,n,a,i,r,s,c,h,u){const l=1-e,f=l*n+e*i,g=l*a+e*r,m=l*i+e*s,d=l*r+e*c,_=l*f+e*m,P=l*g+e*d,p=l*_+e*(l*m+e*(l*s+e*h)),M=l*P+e*(l*d+e*(l*c+e*u));t.push(o.C,f,g,_,P,p,M)}},{getDistance:M}=p,{M:b,L:y,C:D,Z:x}=o,A={},k={},F={transform(t,o){let e,n=0;const a=t.length;for(;n<a;)switch(e=t[n],e){case b:case y:F.transformPoints(t,o,n,1),n+=3;break;case D:F.transformPoints(t,o,n,3),n+=7;break;case x:n+=1}},transformPoints(t,o,e,n){for(let a=e+1,r=a+2*n;a<r;a+=2)A.x=t[a],A.y=t[a+1],i.toOuterPoint(o,A),t[a]=A.x,t[a+1]=A.y},getMotionPathData(t){let o,e,n,i,r=0,s=[],c=0,h=0,u=0;const l=t.length;for(;c<l;){switch(i=t[c],i){case b:case y:e=t[c+1],n=t[c+2],o=i===y&&c>0?a.getDistanceFrom(h,u,e,n):0,h=e,u=n,c+=3;break;case D:e=t[c+5],n=t[c+6],o=p.getDistance(h,u,t[c+1],t[c+2],t[c+3],t[c+4],e,n),h=e,u=n,c+=7;break;case x:c+=1;default:o=0}s.push(o),r+=o}return{total:r,segments:s,data:t}},getDistancePoint(o,e,i){const{segments:r,data:s}=o;e=n.number(e,o.total);let c,h,u,l,f,g,m,d,_,P=0,M={},A=0,F=0,T=0,C=0;const R=s.length;for(;A<R;){switch(l=s[A],l){case b:case y:if(h=s[A+1],u=s[A+2],c=r[F],P+c>=e||!o.total)return A||(T=h,C=u),k.x=T,k.y=C,M.x=h,M.y=u,a.getDistancePoint(k,M,e-P,!0),M.rotation=a.getAngle(k,M),M;T=h,C=u,A+=3;break;case D:if(h=s[A+5],u=s[A+6],c=r[F],P+c>=e)return f=s[A+1],g=s[A+2],m=s[A+3],d=s[A+4],_=p.getT(e-P,c,T,C,f,g,m,d,h,u,i),t.getPointAndSet(_,T,C,f,g,m,d,h,u,M),M.rotation=p.getRotation(_,T,C,f,g,m,d,h,u),M;T=h,C=u,A+=7;break;case x:A+=1;default:c=0}F++,P+=c}return M},getDistancePath(t,o,e){const{segments:i,data:r}=t,s=[];o=n.number(o,t.total);let c,h,u,l,f,g,m,d,_,P=0,M={},A=0,F=0,T=0,C=0;const R=r.length;for(;A<R;){switch(l=r[A],l){case b:case y:if(h=r[A+1],u=r[A+2],c=i[F],P+c>=o||!t.total)return A||(T=h,C=u),k.x=T,k.y=C,M.x=h,M.y=u,a.getDistancePoint(k,M,o-P,!0),s.push(l,M.x,M.y),s;T=h,C=u,A+=3,s.push(l,T,C);break;case D:if(f=r[A+1],g=r[A+2],m=r[A+3],d=r[A+4],h=r[A+5],u=r[A+6],c=i[F],P+c>=o)return _=p.getT(o-P,c,T,C,f,g,m,d,h,u,e),p.cut(s,_,T,C,f,g,m,d,h,u),s;T=h,C=u,A+=7,s.push(l,f,g,m,d,h,u);break;case x:A+=1,s.push(l);default:c=0}F++,P+=c}return s}};function T(t){return r(t,(t=>s({set(o){this.__setAttr(t,o),this.__hasMotionPath=this.motionPath||!c(this.motion),this.__layout.matrixChanged||this.__layout.matrixChange()}})))}h.add("motion-path"),u.register("motion",(function(t,o,e,a){return t?"object"==typeof t&&(t=n.number(t,a.getMotionTotal())):t=0,o?"object"==typeof o&&(o=n.number(o,a.getMotionTotal())):o=0,u.number(t,o,e)})),u.register("motionRotation",(function(t,o,e){return u.number(t,o,e)}));const C=l.prototype,{updateMatrix:R,updateAllMatrix:v}=f,{updateBounds:w}=g;function j(t){const{motion:o,leaferIsCreated:e}=t;if(!c(o)){if(e&&(t.leafer.created=!1),t.motionPath){const e=L(t);e.total&&(t.__.__pathForRender=F.getDistancePath(e,o,t.motionPrecision))}else t.set(t.getMotionPoint(o)),t.__hasAutoLayout||(t.isBranch?(v(t),w(t,t)):R(t));e&&(t.leafer.created=!0)}}function B(t){const{parent:o}=t;if(!t.motionPath&&o){const{children:t}=o;for(let o=0;o<t.length;o++)if(t[o].motionPath)return t[o]}return t}function L(t){const o=t.__;return o.__pathForMotion?o.__pathForMotion:o.__pathForMotion=F.getMotionPathData(t.getPath(!0,!0))}l.addAttr("motionPath",void 0,T),l.addAttr("motionPrecision",1,T),l.addAttr("motion",void 0,T),l.addAttr("motionRotation",!0,T),C.getMotionPathData=function(){return L(B(this))},C.getMotionPoint=function(t){const o=B(this),e=L(o);if(!e.total)return{};const n=F.getDistancePoint(e,t,o.motionPrecision);i.toOuterPoint(o.localTransform,n);const{motionRotation:a}=this;return!1===a?delete n.rotation:"number"==typeof a&&(n.rotation+=a),n},C.getMotionTotal=function(){return this.getMotionPathData().total},C.__updateMotionPath=function(){const t=this.__;if(this.__layout.resized&&t.__pathForMotion&&(t.__pathForMotion=void 0),this.motionPath){let t;const{children:o}=this.parent;for(let e=0;e<o.length;e++)t=o[e],c(t.motion)||t.__layout.matrixChanged||(t!==this&&t.__extraUpdate(),j(t))}else j(this)};export{p as HighBezierHelper,F as HighCurveHelper,T as motionPathType};
1
+ import{BezierHelper as t,PathCommandMap as o,OneRadian as e,UnitConvert as n,PointHelper as a,MatrixHelper as i,decorateLeafAttr as r,attr as s,isNull as c,Plugin as h,Transition as u,isObject as l,UI as f,LeafHelper as g,BranchHelper as m,isNumber as d}from"@leafer-ui/draw";const _=[.1488743389,.4333953941,.6794095682,.8650633666,.9739065285],P=[.2955242247,.2692667193,.2190863625,.1494513491,.0666713443],{sqrt:p}=Math,{getDerivative:M}=t,D={getDistance(t,o,e,n,a,i,r,s,c=1){let h,u,l,f,g,m,d=0,D=c/2;for(let c=0;c<_.length;c++)h=D*(1+_[c]),u=D*(1-_[c]),l=M(h,t,e,a,r),f=M(h,o,n,i,s),g=M(u,t,e,a,r),m=M(u,o,n,i,s),d+=P[c]*(p(l*l+f*f)+p(g*g+m*m));return d*D},getRotation(t,o,n,a,i,r,s,c,h){const u=M(t,o,a,r,c),l=M(t,n,i,s,h);return Math.atan2(l,u)/e},getT(t,o,e,n,a,i,r,s,c,h,u=1){let l=0,f=1,g=t/o,m=u/o/3;if(g>=1)return 1;if(g<=0)return 0;for(;f-l>m;)b(e,n,a,i,r,s,c,h,g)<t?l=g:f=g,g=(l+f)/2;return g},cut(t,e,n,a,i,r,s,c,h,u){const l=1-e,f=l*n+e*i,g=l*a+e*r,m=l*i+e*s,d=l*r+e*c,_=l*f+e*m,P=l*g+e*d,p=l*_+e*(l*m+e*(l*s+e*h)),M=l*P+e*(l*d+e*(l*c+e*u));t.push(o.C,f,g,_,P,p,M)}},{getDistance:b}=D,{M:x,L:y,C:A,Z:k}=o,F={},T={},C={transform(t,o){let e,n=0;const a=t.length;for(;n<a;)switch(e=t[n],e){case x:case y:C.transformPoints(t,o,n,1),n+=3;break;case A:C.transformPoints(t,o,n,3),n+=7;break;case k:n+=1}},transformPoints(t,o,e,n){for(let a=e+1,r=a+2*n;a<r;a+=2)F.x=t[a],F.y=t[a+1],i.toOuterPoint(o,F),t[a]=F.x,t[a+1]=F.y},getMotionPathData(t){let o,e,n,i,r=0,s=[],c=0,h=0,u=0;const l=t.length;for(;c<l;){switch(i=t[c],i){case x:case y:e=t[c+1],n=t[c+2],o=i===y&&c>0?a.getDistanceFrom(h,u,e,n):0,h=e,u=n,c+=3;break;case A:e=t[c+5],n=t[c+6],o=D.getDistance(h,u,t[c+1],t[c+2],t[c+3],t[c+4],e,n),h=e,u=n,c+=7;break;case k:c+=1;default:o=0}s.push(o),r+=o}return{total:r,segments:s,data:t}},getDistancePoint(o,e,i){const{segments:r,data:s}=o;e=n.number(e,o.total);let c,h,u,l,f,g,m,d,_,P=0,p={},M=0,b=0,F=0,C=0;const R=s.length;for(;M<R;){switch(l=s[M],l){case x:case y:if(h=s[M+1],u=s[M+2],c=r[b],P+c>=e||!o.total)return M||(F=h,C=u),T.x=F,T.y=C,p.x=h,p.y=u,a.getDistancePoint(T,p,e-P,!0),p.rotation=a.getAngle(T,p),p;F=h,C=u,M+=3;break;case A:if(h=s[M+5],u=s[M+6],c=r[b],P+c>=e)return f=s[M+1],g=s[M+2],m=s[M+3],d=s[M+4],_=D.getT(e-P,c,F,C,f,g,m,d,h,u,i),t.getPointAndSet(_,F,C,f,g,m,d,h,u,p),p.rotation=D.getRotation(_,F,C,f,g,m,d,h,u),p;F=h,C=u,M+=7;break;case k:M+=1;default:c=0}b++,P+=c}return p},getDistancePath(t,o,e){const{segments:i,data:r}=t,s=[];o=n.number(o,t.total);let c,h,u,l,f,g,m,d,_,P=0,p={},M=0,b=0,F=0,C=0;const R=r.length;for(;M<R;){switch(l=r[M],l){case x:case y:if(h=r[M+1],u=r[M+2],c=i[b],P+c>=o||!t.total)return M||(F=h,C=u),T.x=F,T.y=C,p.x=h,p.y=u,a.getDistancePoint(T,p,o-P,!0),s.push(l,p.x,p.y),s;F=h,C=u,M+=3,s.push(l,F,C);break;case A:if(f=r[M+1],g=r[M+2],m=r[M+3],d=r[M+4],h=r[M+5],u=r[M+6],c=i[b],P+c>=o)return _=D.getT(o-P,c,F,C,f,g,m,d,h,u,e),D.cut(s,_,F,C,f,g,m,d,h,u),s;F=h,C=u,M+=7,s.push(l,f,g,m,d,h,u);break;case k:M+=1,s.push(l);default:c=0}b++,P+=c}return s}};function R(t){return r(t,t=>s({set(o){this.__setAttr(t,o),this.__hasMotionPath=this.motionPath||!c(this.motion),this.__layout.matrixChanged||this.__layout.matrixChange()}}))}h.add("motion-path"),u.register("motion",function(t,o,e,a){return l(t)&&(t=n.number(t,a.getMotionTotal())),l(o)&&(o=n.number(o,a.getMotionTotal())),u.number(t||0,o||0,e)}),u.register("motionRotation",function(t,o,e){return u.number(t,o,e)});const v=f.prototype,{updateMatrix:w,updateAllMatrix:B}=g,{updateBounds:L}=m;function O(t){const{motion:o,leaferIsCreated:e}=t;if(!c(o)){if(e&&(t.leafer.created=!1),t.motionPath){const e=z(t);e.total&&(t.__.__pathForRender=C.getDistancePath(e,o,t.motionPrecision))}else t.set(t.getMotionPoint(o)),t.__hasAutoLayout||(t.isBranch?(B(t),L(t,t)):w(t));e&&(t.leafer.created=!0)}}function q(t){const{parent:o}=t;if(!t.motionPath&&o){const{children:t}=o;for(let o=0;o<t.length;o++)if(t[o].motionPath)return t[o]}return t}function z(t){const o=t.__;return o.__pathForMotion?o.__pathForMotion:o.__pathForMotion=C.getMotionPathData(t.getPath(!0,!0))}f.addAttr("motionPath",void 0,R),f.addAttr("motionPrecision",1,R),f.addAttr("motion",void 0,R),f.addAttr("motionRotation",!0,R),v.getMotionPathData=function(){return z(q(this))},v.getMotionPoint=function(t){const o=q(this),e=z(o);if(!e.total)return{};const n=C.getDistancePoint(e,t,o.motionPrecision);i.toOuterPoint(o.localTransform,n);const{motionRotation:a}=this;return!1===a?delete n.rotation:d(a)&&(n.rotation+=a),n},v.getMotionTotal=function(){return this.getMotionPathData().total},v.__updateMotionPath=function(){const t=this.__;if(this.__layout.resized&&t.__pathForMotion&&(t.__pathForMotion=void 0),this.motionPath){let t;const{children:o}=this.parent;for(let e=0;e<o.length;e++)t=o[e],c(t.motion)||t.__layout.matrixChanged||(t!==this&&t.__extraUpdate(),O(t))}else O(this)};export{D as HighBezierHelper,C as HighCurveHelper,R as motionPathType};
2
2
  //# sourceMappingURL=motion-path.esm.min.js.map