forked from 15001217168/mojs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.babel.js
More file actions
536 lines (514 loc) · 16.1 KB
/
Copy pathshape.babel.js
File metadata and controls
536 lines (514 loc) · 16.1 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
const h = require('./h');
const Bit = require('./shapes/bit');
const shapesMap = require('./shapes/shapesMap');
import Module from './module';
import Thenable from './thenable';
import Tunable from './tunable';
import Tweenable from './tween/tweenable';
import Tween from './tween/tween';
import Timeline from './tween/timeline';
// TODO
// - refactor
// - add setIfChanged to Module
// --
// - tween for every prop
class Shape extends Tunable {
/*
Method to declare module's defaults.
@private
*/
_declareDefaults () {
// DEFAULTS / APIs
this._defaults = {
// where to append the module to [selector, HTMLElement]
parent: document.body,
// class name for the `el`
className: '',
// Possible values: [circle, line, zigzag, rect, polygon, cross, equal ]
shape: 'circle',
// ∆ :: Possible values: [color name, rgb, rgba, hex]
stroke: 'transparent',
// ∆ :: Possible values: [ 0..1 ]
strokeOpacity: 1,
// Possible values: ['butt' | 'round' | 'square']
strokeLinecap: '',
// ∆ :: Possible values: [ number ]
strokeWidth: 2,
// ∆ :: Units :: Possible values: [ number, string ]
strokeDasharray: 0,
// ∆ :: Units :: Possible values: [ number, string ]
strokeDashoffset: 0,
// ∆ :: Possible values: [color name, rgb, rgba, hex]
fill: 'deeppink',
// ∆ :: Possible values: [ 0..1 ]
fillOpacity: 1,
// {Boolean} - if should hide module with `opacity` instead of `display`
isSoftHide: true,
// {Boolean} - if should trigger composite layer for the `el`
isForce3d: false,
// ∆ :: Units :: Possible values: [ number, string ]
left: '50%',
// ∆ :: Units :: Possible values: [ number, string ]
top: '50%',
// ∆ :: Units :: Possible values: [ number, string ]
x: 0,
// ∆ :: Units :: Possible values: [ number, string ]
y: 0,
// ∆ :: Possible values: [ number ]
angle: 0,
// ∆ :: Possible values: [ number ]
scale: 1,
// ∆ :: Possible values: [ number ] Fallbacks to `scale`.
scaleX: null,
// ∆ :: Possible values: [ number ] Fallbacks to `scale`.
scaleY: null,
// ∆ :: Possible values: [ number, string ]
origin: '50% 50%',
// ∆ :: Possible values: [ 0..1 ]
opacity: 1,
// ∆ :: Units :: Possible values: [ number, string ]
rx: 0,
// ∆ :: Units :: Possible values: [ number, string ]
ry: 0,
// ∆ :: Possible values: [ number ]
points: 3,
// ∆ :: Possible values: [ number ]
radius: 50,
// ∆ :: Possible values: [ number ]
radiusX: null,
// ∆ :: Possible values: [ number ]
radiusY: null,
// Possible values: [ boolean ]
isShowStart: false,
// Possible values: [ boolean ]
isShowEnd: true,
// Possible values: [ boolean ]
isRefreshState: true,
// Possible values: [ number > 0 ]
duration: 400,
// Possible values: [ number ]
/* technical ones: */
// explicit width of the module canvas
width: null,
// explicit height of the module canvas
height: null,
// Possible values: [ number ]
// sizeGap: 0,
/* [boolean] :: If should have child shape. */
isWithShape: true,
// context for all the callbacks
callbacksContext: this
}
}
/*
Method to start the animation with optional new options.
@public
@overrides @ Tunable
@param {Object} New options to set on the run.
@returns {Object} this.
*/
tune (o) {
super.tune( o );
// update shapeModule's size to the max in `then` chain
this._getMaxSizeInChain();
return this;
}
/*
Method to create a then record for the module.
@public
@overrides @ Thenable
@param {Object} Options for the next animation.
@returns {Object} this.
*/
then (o) {
// this._makeTimeline()
super.then( o );
// update shapeModule's size to the max in `then` chain
this._getMaxSizeInChain();
return this;
}
// ^ PUBLIC METHOD(S) ^
// v PRIVATE METHOD(S) v
/*
Method to declare variables.
@overrides Thenable
*/
_vars () {
// call _vars method on Thenable
super._vars();
this._lastSet = {};
// save previous module in the chain
this._prevChainModule = this._o.prevChainModule;
// should draw on foreign svg canvas
this.isForeign = !!this._o.ctx;
// this._o.isTimelineLess = true;
// should take an svg element as self bit
return this.isForeignBit = !!this._o.shape;
}
/*
Method to initialize modules presentation.
@private
@overrides Module
*/
_render () {
if (!this._isRendered && !this._isChained) {
// create `mojs` shape element
this.el = document.createElement('div');
// set name on the `el`
this.el.setAttribute( 'data-name', 'mojs-shape' );
// set class on the `el`
this.el.setAttribute( 'class', this._props.className );
// create shape module
this._createShape();
// append `el` to parent
this._props.parent.appendChild( this.el );
// set position styles on the el
this._setElStyles();
// set initial position for the first module in the chain
this._setProgress(0, 0);
// show at start if `isShowStart`
if (this._props.isShowStart) { this._show(); } else { this._hide(); }
// set `_isRendered` hatch
this._isRendered = true;
} else if ( this._isChained ) {
// save elements from master module
this.el = this._masterModule.el;
this.shapeModule = this._masterModule.shapeModule;
}
return this;
}
/*
Method to set el styles on initialization.
@private
*/
_setElStyles () {
if ( !this.el ) { return; }
// if (!this.isForeign) {
var p = this._props,
style = this.el.style,
width = p.shapeWidth,
height = p.shapeHeight;
style.position = 'absolute';
this._setElSizeStyles( width, height );
if ( p.isForce3d ) {
let name = 'backface-visibility';
style[ `${name}` ] = 'hidden';
style[ `${h.prefix.css}${name}` ] = 'hidden';
}
// }
}
/*
Method to set `width`/`height`/`margins` to the `el` styles.
@param {Number} Width.
@param {height} Height.
*/
_setElSizeStyles ( width, height ) {
var style = this.el.style;
style.width = `${ width }px`;
style.height = `${ height }px`;
style[ 'margin-left' ] = `${ - width/2 }px`;
style[ 'margin-top' ] = `${ - height/2 }px`;
}
/*
Method to draw shape.
@private
*/
_draw () {
if (!this.shapeModule) { return; }
var p = this._props,
bP = this.shapeModule._props;
// set props on bit
// bP.x = this._origin.x;
// bP.y = this._origin.y;
bP.rx = p.rx;
bP.ry = p.ry;
bP.stroke = p.stroke;
bP['stroke-width'] = p.strokeWidth;
bP['stroke-opacity'] = p.strokeOpacity;
bP['stroke-dasharray'] = p.strokeDasharray;
bP['stroke-dashoffset'] = p.strokeDashoffset;
bP['stroke-linecap'] = p.strokeLinecap;
bP['fill'] = p.fill;
bP['fill-opacity'] = p.fillOpacity;
bP.radius = p.radius;
bP.radiusX = p.radiusX;
bP.radiusY = p.radiusY;
bP.points = p.points;
this.shapeModule._draw();
this._drawEl();
}
/*
Method to set current modules props to main div el.
@private
*/
_drawEl () {
// return;
if (this.el == null) { return true; }
var p = this._props;
var style = this.el.style;
// style.opacity = p.opacity;
this._isPropChanged('opacity') && (style.opacity = p.opacity);
if (!this.isForeign) {
this._isPropChanged('left') && (style.left = p.left);
this._isPropChanged('top') && (style.top = p.top);
var isX = this._isPropChanged('x'),
isY = this._isPropChanged('y'),
isTranslate = isX || isY,
isScaleX = this._isPropChanged('scaleX'),
isScaleY = this._isPropChanged('scaleY'),
isScale = this._isPropChanged('scale'),
isScale = isScale || isScaleX || isScaleY,
isRotate = this._isPropChanged('angle');
if ( isTranslate || isScale || isRotate ) {
var transform = this._fillTransform();
style[`${ h.prefix.css }transform`] = transform;
style['transform'] = transform;
}
if ( this._isPropChanged('origin') || this._deltas[ 'origin' ] ) {
var origin = this._fillOrigin();
style[`${ h.prefix.css }transform-origin`] = origin;
style['transform-origin'] = origin;
}
}
}
/*
Method to check if property changed after the latest check.
@private
@param {String} Name of the property to check.
@returns {Boolean}
*/
_isPropChanged ( name ) {
// if there is no recod for the property - create it
if (this._lastSet[name] == null) { this._lastSet[name] = {}; }
if (this._lastSet[name].value !== this._props[name]) {
this._lastSet[name].value = this._props[name];
return true;
} else { return false; }
}
/*
Method to tune new option on run.
@private
@override @ Module
@param {Object} Option to tune on run.
*/
_tuneNewOptions (o) {
// call super on Module
super._tuneNewOptions(o);
// return if empty object
if ( !((o != null) && Object.keys(o).length) ) { return 1; }
// this._calcSize();
this._setElStyles();
}
/*
Method to get max radiusX value.
@private
@param {String} Radius name.
*/
_getMaxRadius( name ) {
var selfSize, selfSizeX;
selfSize = this._getRadiusSize('radius');
return this._getRadiusSize(name, selfSize );
}
/*
Method to increase calculated size based on easing.
@private
*/
_increaseSizeWithEasing () {
var p = this._props,
easing = this._o.easing,
isStringEasing = easing && typeof easing === 'string';
switch ( isStringEasing && easing.toLowerCase() ) {
case 'elastic.out':
case 'elastic.inout':
p.size *= 1.25;
break;
case 'back.out':
case 'back.inout':
p.size *= 1.1;
}
}
/*
Method to increase calculated size based on bit ratio.
@private
*/
// _increaseSizeWithBitRatio () {
// var p = this._props;
// // p.size *= this.shape._props.ratio;
// p.size += 2 * p.sizeGap;
// }
/*
Method to get maximum radius size with optional fallback.
@private
@param {Object}
@param key {String} Name of the radius - [radius|radiusX|radiusY].
@param @optional fallback {Number} Optional number to set if there
is no value for the key.
*/
_getRadiusSize ( name, fallback = 0 ) {
var delta = this._deltas[name];
// if value is delta value
if (delta != null) {
// get maximum number between start and end values of the delta
return Math.max(Math.abs(delta.end), Math.abs(delta.start));
} else if (this._props[name] != null) {
// else get the value from props object
return parseFloat(this._props[name]);
} else { return fallback; }
}
/*
Method to get max shape canvas size and save it to _props.
@private
*/
_getShapeSize () {
var p = this._props,
// get maximum stroke value
stroke = this._getMaxStroke();
// save shape `width` and `height` to `_props`
p.shapeWidth = (p.width != null)
? p.width
: 2*this._getMaxRadius( 'radiusX' ) + stroke;
p.shapeHeight = (p.height != null)
? p.height
: 2*this._getMaxRadius( 'radiusY' ) + stroke;
}
/*
Method to create shape.
@private
*/
_createShape () {
// calculate max shape canvas size and set to _props
this._getShapeSize();
// don't create actual shape if !`isWithShape`
if ( !this._props.isWithShape ) { return; }
var p = this._props;
// get shape's class
var Shape = shapesMap.getShape(this._props.shape);
// create `_shape` module
this.shapeModule = new Shape({
width: p.shapeWidth,
height: p.shapeHeight,
parent: this.el
});
}
/*
Method to get max size in `then` chain
@private
*/
_getMaxSizeInChain () {
let p = this._props,
maxW = 0,
maxH = 0;
for (var i = 0; i < this._modules.length; i++) {
this._modules[i]._getShapeSize();
maxW = Math.max( maxW, this._modules[i]._props.shapeWidth );
maxH = Math.max( maxH, this._modules[i]._props.shapeHeight );
}
this.shapeModule && this.shapeModule._setSize( maxW, maxH );
this._setElSizeStyles( maxW, maxH );
}
/*
Method to get max value of the strokeWidth.
@private
*/
_getMaxStroke () {
var p = this._props;
var dStroke = this._deltas[ 'strokeWidth' ];
return (dStroke != null)
? Math.max(dStroke.start, dStroke.end)
: p.strokeWidth;
}
/*
Method to draw current progress of the deltas.
@private
@override @ Module
@param {Number} EasedProgress to set - [0..1].
@param {Number} Progress to set - [0..1].
*/
_setProgress ( easedProgress, progress ) {
// call the super on Module
Module.prototype._setProgress.call(this, easedProgress, progress);
// draw current progress
this._draw(easedProgress);
}
/*
Method to add callback overrides to passed object.
@private
@param {Object} Object to add the overrides to.
*/
_applyCallbackOverrides (obj) {
var it = this,
p = this._props;
// specify control functions for the module
obj.callbackOverrides = {
onUpdate: function (ep, p) { return it._setProgress(ep, p); },
onStart: function (isFwd) {
// don't touch main `el` onStart in chained elements
if ( it._isChained ) { return };
if ( isFwd ) { it._show(); }
else { if ( !p.isShowStart ) { it._hide(); } }
},
onComplete: function (isFwd) {
// don't touch main `el` if not the last in `then` chain
if ( !it._isLastInChain() ) { return; }
if ( isFwd ) { if ( !p.isShowEnd ) { it._hide(); } }
else { it._show(); }
},
onRefresh: function (isBefore) {
p.isRefreshState && isBefore && it._refreshBefore();
}
}
}
/*
Method to setup tween and timeline options before creating them.
@override @ Tweenable
@private
*/
_transformTweenOptions () { this._applyCallbackOverrides( this._o ); }
/*
Method to create transform string.
@private
@returns {String} Transform string.
*/
_fillTransform () {
var p = this._props,
scaleX = ( p.scaleX != null ) ? p.scaleX : p.scale,
scaleY = ( p.scaleY != null ) ? p.scaleY : p.scale,
scale = `${ scaleX }, ${scaleY}`;
return `translate(${p.x}, ${p.y}) rotate(${p.angle}deg) scale(${scale})`;
}
/*
Method to create transform-origin string.
@private
@returns {String} Transform string.
*/
_fillOrigin () {
var p = this._props,
str = '';
for (var i = 0; i < p.origin.length; i++) {
str += `${ p.origin[i].string } `;
}
return str;
}
/*
Method to refresh state befor startTime.
@private
*/
_refreshBefore () {
// call setProgress with eased and normal progress
this._setProgress( this.tween._props.easing(0), 0 );
if ( this._props.isShowStart ) { this._show(); } else { this._hide(); }
}
/*
Method that gets called on `soft` show of the module,
it should restore transform styles of the module.
@private
@overrides @ Module
*/
_showByTransform () {
// reset the cache of the scale prop
this._lastSet.scale = null;
// draw el accroding to it's props
this._drawEl();
}
}
export default Shape;