/
swirl.js
226 lines (194 loc) · 7.57 KB
/
swirl.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
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
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }
var _transit = require('./transit');
var _transit2 = _interopRequireDefault(_transit);
var _h = require('./h');
/*
*TODO:*
---
- tweak then chains
*/
var _h2 = _interopRequireDefault(_h);
var Swirl = (function (_Transit) {
_inherits(Swirl, _Transit);
function Swirl() {
_classCallCheck(this, Swirl);
_get(Object.getPrototypeOf(Swirl.prototype), 'constructor', this).apply(this, arguments);
}
_createClass(Swirl, [{
key: '_declareDefaults',
/*
Method to declare _defaults and other default objects.
@private
@override @ Transit
*/
value: function _declareDefaults() {
_get(Object.getPrototypeOf(Swirl.prototype), '_declareDefaults', this).call(this);
/* _DEFAULTS ARE - TRANSIT DEFAULTS + THESE: */
/* [boolean] :: If shape should follow sinusoidal path. */
this._defaults.isSwirl = true;
/* ∆ :: [number > 0] :: Degree size of the sinusoidal path. */
this._defaults.swirlSize = 10;
/* ∆ :: [number > 0] :: Frequency of the sinusoidal path. */
this._defaults.swirlFrequency = 3;
/* ∆ :: [number > 0] :: Sinusoidal path length scale. */
this._defaults.pathScale = 1;
/* ∆ :: [number] :: Degree shift for the sinusoidal path. */
this._defaults.degreeShift = 0;
/* ∆ :: [number] :: Radius of the shape. */
this._defaults.radius = { 5: 0 };
/* [number: -1, 1] :: Directon of Swirl. */
this._defaults.direction = 1;
/* technical ones: */
/* [boolean] :: If should have child shape. */
this._defaults.isWithShape = true;
}
// ^ PUBLIC METHOD(S) ^
// v PRIVATE METHOD(S) v
/*
Method to copy _o options to _props with
fallback to _defaults.
@private
@override @ Module
*/
}, {
key: '_extendDefaults',
value: function _extendDefaults() {
_get(Object.getPrototypeOf(Swirl.prototype), '_extendDefaults', this).call(this);
this._calcPosData();
}
/*
Method to tune new oprions to _o and _props object.
@private
@overrides @ Module
@param {Object} Options object to tune to.
*/
}, {
key: '_tuneNewOptions',
value: function _tuneNewOptions(o) {
if (o == null) {
return;
}
_get(Object.getPrototypeOf(Swirl.prototype), '_tuneNewOptions', this).call(this, o);
if (o.x != null || o.y != null) {
this._calcPosData();
}
}
/*
Method to calculate Swirl's position data.
@private
*/
}, {
key: '_calcPosData',
value: function _calcPosData() {
var x = this._getPosValue('x'),
y = this._getPosValue('y'),
angle = 90 + Math.atan(y.delta / x.delta || 0) * _h2['default'].RAD_TO_DEG;
this._posData = {
radius: Math.sqrt(x.delta * x.delta + y.delta * y.delta),
angle: x.delta < 0 ? angle + 180 : angle,
x: x, y: y
};
// set the last position to _props
// this._calcSwirlXY( 1 );
}
/*
Gets `x` or `y` position value.
@private
@param {String} Name of the property.
*/
}, {
key: '_getPosValue',
value: function _getPosValue(name) {
var delta = this._deltas[name];
if (delta) {
// delete from deltas to prevent normal
delete this._deltas[name];
return {
start: delta.start.value,
end: delta.end.value,
delta: delta.delta,
units: delta.end.unit
};
} else {
var pos = _h2['default'].parseUnit(this._props[name]);
return { start: pos.value, end: pos.value, delta: 0, units: pos.unit };
}
}
/*
Method to calculate the progress of the Swirl.
@private
@overrides @ Transit
@param {Numer} Progress of the Swirl in range of [0..1]
*/
}, {
key: '_setProgress',
value: function _setProgress(proc) {
this._progress = proc;
this._calcCurrentProps(proc);
this._calcSwirlXY(proc);
this._calcOrigin();
this._draw(proc);
}
/*
Method to calculate x/y for Swirl's progress
@private
@mutates _props
@param {Number} Current progress in [0...1]
*/
}, {
key: '_calcSwirlXY',
value: function _calcSwirlXY(proc) {
var p = this._props,
angle = this._posData.angle + p.degreeShift,
point = _h2['default'].getRadialPoint({
angle: p.isSwirl ? angle + this._getSwirl(proc) : angle,
radius: proc * this._posData.radius * p.pathScale,
center: {
x: this._posData.x.start,
y: this._posData.y.start
}
});
// if foreign svg canvas - set position without units
var x = point.x,
y = point.y;
p.x = this._o.ctx ? x : x + this._posData.x.units;
p.y = this._o.ctx ? y : y + this._posData.y.units;
}
/*
Method to get progress of the swirl.
@private
@param {Number} Progress of the Swirl.
@returns {Number} Progress of the swirl.
*/
}, {
key: '_getSwirl',
value: function _getSwirl(proc) {
var p = this._props;
return p.direction * p.swirlSize * Math.sin(p.swirlFrequency * proc);
}
/*
Method to draw shape.
If !isWithShape - draw self el only, but not shape.
@private
@overrides @ Transit.
*/
}, {
key: '_draw',
value: function _draw() {
// call _draw or just _drawEl @ Transit depending if there is `shape`
var methodName = this._props.isWithShape ? '_draw' : '_drawEl';
_transit2['default'].prototype[methodName].call(this);
}
}]);
return Swirl;
})(_transit2['default']);
exports['default'] = Swirl;
module.exports = exports['default'];