1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var gsap,
|
13 | _win,
|
14 | _splitColor,
|
15 | _coreInitted,
|
16 | _PIXI,
|
17 | PropTween,
|
18 | _getSetter,
|
19 | _isV4,
|
20 | _windowExists = function _windowExists() {
|
21 | return typeof window !== "undefined";
|
22 | },
|
23 | _getGSAP = function _getGSAP() {
|
24 | return gsap || _windowExists() && (gsap = window.gsap) && gsap.registerPlugin && gsap;
|
25 | },
|
26 | _isFunction = function _isFunction(value) {
|
27 | return typeof value === "function";
|
28 | },
|
29 | _warn = function _warn(message) {
|
30 | return console.warn(message);
|
31 | },
|
32 | _idMatrix = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
|
33 | _lumR = 0.212671,
|
34 | _lumG = 0.715160,
|
35 | _lumB = 0.072169,
|
36 | _applyMatrix = function _applyMatrix(m, m2) {
|
37 | var temp = [],
|
38 | i = 0,
|
39 | z = 0,
|
40 | y,
|
41 | x;
|
42 |
|
43 | for (y = 0; y < 4; y++) {
|
44 | for (x = 0; x < 5; x++) {
|
45 | z = x === 4 ? m[i + 4] : 0;
|
46 | temp[i + x] = m[i] * m2[x] + m[i + 1] * m2[x + 5] + m[i + 2] * m2[x + 10] + m[i + 3] * m2[x + 15] + z;
|
47 | }
|
48 |
|
49 | i += 5;
|
50 | }
|
51 |
|
52 | return temp;
|
53 | },
|
54 | _setSaturation = function _setSaturation(m, n) {
|
55 | var inv = 1 - n,
|
56 | r = inv * _lumR,
|
57 | g = inv * _lumG,
|
58 | b = inv * _lumB;
|
59 | return _applyMatrix([r + n, g, b, 0, 0, r, g + n, b, 0, 0, r, g, b + n, 0, 0, 0, 0, 0, 1, 0], m);
|
60 | },
|
61 | _colorize = function _colorize(m, color, amount) {
|
62 | var c = _splitColor(color),
|
63 | r = c[0] / 255,
|
64 | g = c[1] / 255,
|
65 | b = c[2] / 255,
|
66 | inv = 1 - amount;
|
67 |
|
68 | return _applyMatrix([inv + amount * r * _lumR, amount * r * _lumG, amount * r * _lumB, 0, 0, amount * g * _lumR, inv + amount * g * _lumG, amount * g * _lumB, 0, 0, amount * b * _lumR, amount * b * _lumG, inv + amount * b * _lumB, 0, 0, 0, 0, 0, 1, 0], m);
|
69 | },
|
70 | _setHue = function _setHue(m, n) {
|
71 | n *= Math.PI / 180;
|
72 | var c = Math.cos(n),
|
73 | s = Math.sin(n);
|
74 | return _applyMatrix([_lumR + c * (1 - _lumR) + s * -_lumR, _lumG + c * -_lumG + s * -_lumG, _lumB + c * -_lumB + s * (1 - _lumB), 0, 0, _lumR + c * -_lumR + s * 0.143, _lumG + c * (1 - _lumG) + s * 0.14, _lumB + c * -_lumB + s * -0.283, 0, 0, _lumR + c * -_lumR + s * -(1 - _lumR), _lumG + c * -_lumG + s * _lumG, _lumB + c * (1 - _lumB) + s * _lumB, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], m);
|
75 | },
|
76 | _setContrast = function _setContrast(m, n) {
|
77 | return _applyMatrix([n, 0, 0, 0, 0.5 * (1 - n), 0, n, 0, 0, 0.5 * (1 - n), 0, 0, n, 0, 0.5 * (1 - n), 0, 0, 0, 1, 0], m);
|
78 | },
|
79 | _getFilter = function _getFilter(target, type) {
|
80 | var filterClass = _PIXI.filters[type],
|
81 | filters = target.filters || [],
|
82 | i = filters.length,
|
83 | filter;
|
84 | filterClass || _warn(type + " not found. PixiPlugin.registerPIXI(PIXI)");
|
85 |
|
86 | while (--i > -1) {
|
87 | if (filters[i] instanceof filterClass) {
|
88 | return filters[i];
|
89 | }
|
90 | }
|
91 |
|
92 | filter = new filterClass();
|
93 |
|
94 | if (type === "BlurFilter") {
|
95 | filter.blur = 0;
|
96 | }
|
97 |
|
98 | filters.push(filter);
|
99 | target.filters = filters;
|
100 | return filter;
|
101 | },
|
102 | _addColorMatrixFilterCacheTween = function _addColorMatrixFilterCacheTween(p, plugin, cache, vars) {
|
103 |
|
104 | plugin.add(cache, p, cache[p], vars[p]);
|
105 |
|
106 | plugin._props.push(p);
|
107 | },
|
108 | _applyBrightnessToMatrix = function _applyBrightnessToMatrix(brightness, matrix) {
|
109 | var temp = new _PIXI.filters.ColorMatrixFilter();
|
110 | temp.matrix = matrix;
|
111 | temp.brightness(brightness, true);
|
112 | return temp.matrix;
|
113 | },
|
114 | _copy = function _copy(obj) {
|
115 | var copy = {},
|
116 | p;
|
117 |
|
118 | for (p in obj) {
|
119 | copy[p] = obj[p];
|
120 | }
|
121 |
|
122 | return copy;
|
123 | },
|
124 | _CMFdefaults = {
|
125 | contrast: 1,
|
126 | saturation: 1,
|
127 | colorizeAmount: 0,
|
128 | colorize: "rgb(255,255,255)",
|
129 | hue: 0,
|
130 | brightness: 1
|
131 | },
|
132 | _parseColorMatrixFilter = function _parseColorMatrixFilter(target, v, pg) {
|
133 | var filter = _getFilter(target, "ColorMatrixFilter"),
|
134 | cache = target._gsColorMatrixFilter = target._gsColorMatrixFilter || _copy(_CMFdefaults),
|
135 | combine = v.combineCMF && !("colorMatrixFilter" in v && !v.colorMatrixFilter),
|
136 | i,
|
137 | matrix,
|
138 | startMatrix;
|
139 |
|
140 | startMatrix = filter.matrix;
|
141 |
|
142 | if (v.resolution) {
|
143 | filter.resolution = v.resolution;
|
144 | }
|
145 |
|
146 | if (v.matrix && v.matrix.length === startMatrix.length) {
|
147 | matrix = v.matrix;
|
148 |
|
149 | if (cache.contrast !== 1) {
|
150 | _addColorMatrixFilterCacheTween("contrast", pg, cache, _CMFdefaults);
|
151 | }
|
152 |
|
153 | if (cache.hue) {
|
154 | _addColorMatrixFilterCacheTween("hue", pg, cache, _CMFdefaults);
|
155 | }
|
156 |
|
157 | if (cache.brightness !== 1) {
|
158 | _addColorMatrixFilterCacheTween("brightness", pg, cache, _CMFdefaults);
|
159 | }
|
160 |
|
161 | if (cache.colorizeAmount) {
|
162 | _addColorMatrixFilterCacheTween("colorize", pg, cache, _CMFdefaults);
|
163 |
|
164 | _addColorMatrixFilterCacheTween("colorizeAmount", pg, cache, _CMFdefaults);
|
165 | }
|
166 |
|
167 | if (cache.saturation !== 1) {
|
168 | _addColorMatrixFilterCacheTween("saturation", pg, cache, _CMFdefaults);
|
169 | }
|
170 | } else {
|
171 | matrix = _idMatrix.slice();
|
172 |
|
173 | if (v.contrast != null) {
|
174 | matrix = _setContrast(matrix, +v.contrast);
|
175 |
|
176 | _addColorMatrixFilterCacheTween("contrast", pg, cache, v);
|
177 | } else if (cache.contrast !== 1) {
|
178 | if (combine) {
|
179 | matrix = _setContrast(matrix, cache.contrast);
|
180 | } else {
|
181 | _addColorMatrixFilterCacheTween("contrast", pg, cache, _CMFdefaults);
|
182 | }
|
183 | }
|
184 |
|
185 | if (v.hue != null) {
|
186 | matrix = _setHue(matrix, +v.hue);
|
187 |
|
188 | _addColorMatrixFilterCacheTween("hue", pg, cache, v);
|
189 | } else if (cache.hue) {
|
190 | if (combine) {
|
191 | matrix = _setHue(matrix, cache.hue);
|
192 | } else {
|
193 | _addColorMatrixFilterCacheTween("hue", pg, cache, _CMFdefaults);
|
194 | }
|
195 | }
|
196 |
|
197 | if (v.brightness != null) {
|
198 | matrix = _applyBrightnessToMatrix(+v.brightness, matrix);
|
199 |
|
200 | _addColorMatrixFilterCacheTween("brightness", pg, cache, v);
|
201 | } else if (cache.brightness !== 1) {
|
202 | if (combine) {
|
203 | matrix = _applyBrightnessToMatrix(cache.brightness, matrix);
|
204 | } else {
|
205 | _addColorMatrixFilterCacheTween("brightness", pg, cache, _CMFdefaults);
|
206 | }
|
207 | }
|
208 |
|
209 | if (v.colorize != null) {
|
210 | v.colorizeAmount = "colorizeAmount" in v ? +v.colorizeAmount : 1;
|
211 | matrix = _colorize(matrix, v.colorize, v.colorizeAmount);
|
212 |
|
213 | _addColorMatrixFilterCacheTween("colorize", pg, cache, v);
|
214 |
|
215 | _addColorMatrixFilterCacheTween("colorizeAmount", pg, cache, v);
|
216 | } else if (cache.colorizeAmount) {
|
217 | if (combine) {
|
218 | matrix = _colorize(matrix, cache.colorize, cache.colorizeAmount);
|
219 | } else {
|
220 | _addColorMatrixFilterCacheTween("colorize", pg, cache, _CMFdefaults);
|
221 |
|
222 | _addColorMatrixFilterCacheTween("colorizeAmount", pg, cache, _CMFdefaults);
|
223 | }
|
224 | }
|
225 |
|
226 | if (v.saturation != null) {
|
227 | matrix = _setSaturation(matrix, +v.saturation);
|
228 |
|
229 | _addColorMatrixFilterCacheTween("saturation", pg, cache, v);
|
230 | } else if (cache.saturation !== 1) {
|
231 | if (combine) {
|
232 | matrix = _setSaturation(matrix, cache.saturation);
|
233 | } else {
|
234 | _addColorMatrixFilterCacheTween("saturation", pg, cache, _CMFdefaults);
|
235 | }
|
236 | }
|
237 | }
|
238 |
|
239 | i = matrix.length;
|
240 |
|
241 | while (--i > -1) {
|
242 | if (matrix[i] !== startMatrix[i]) {
|
243 | pg.add(startMatrix, i, startMatrix[i], matrix[i], "colorMatrixFilter");
|
244 | }
|
245 | }
|
246 |
|
247 | pg._props.push("colorMatrixFilter");
|
248 | },
|
249 | _renderColor = function _renderColor(ratio, _ref) {
|
250 | var t = _ref.t,
|
251 | p = _ref.p,
|
252 | color = _ref.color,
|
253 | set = _ref.set;
|
254 | set(t, p, color[0] << 16 | color[1] << 8 | color[2]);
|
255 | },
|
256 | _renderDirtyCache = function _renderDirtyCache(ratio, _ref2) {
|
257 | var g = _ref2.g;
|
258 |
|
259 | if (g) {
|
260 |
|
261 | g.dirty++;
|
262 | g.clearDirty++;
|
263 | }
|
264 | },
|
265 | _renderAutoAlpha = function _renderAutoAlpha(ratio, data) {
|
266 | data.t.visible = !!data.t.alpha;
|
267 | },
|
268 | _addColorTween = function _addColorTween(target, p, value, plugin) {
|
269 | var currentValue = target[p],
|
270 | startColor = _splitColor(_isFunction(currentValue) ? target[p.indexOf("set") || !_isFunction(target["get" + p.substr(3)]) ? p : "get" + p.substr(3)]() : currentValue),
|
271 | endColor = _splitColor(value);
|
272 |
|
273 | plugin._pt = new PropTween(plugin._pt, target, p, 0, 0, _renderColor, {
|
274 | t: target,
|
275 | p: p,
|
276 | color: startColor,
|
277 | set: _getSetter(target, p)
|
278 | });
|
279 | plugin.add(startColor, 0, startColor[0], endColor[0]);
|
280 | plugin.add(startColor, 1, startColor[1], endColor[1]);
|
281 | plugin.add(startColor, 2, startColor[2], endColor[2]);
|
282 | },
|
283 | _colorProps = {
|
284 | tint: 1,
|
285 | lineColor: 1,
|
286 | fillColor: 1
|
287 | },
|
288 | _xyContexts = "position,scale,skew,pivot,anchor,tilePosition,tileScale".split(","),
|
289 | _contexts = {
|
290 | x: "position",
|
291 | y: "position",
|
292 | tileX: "tilePosition",
|
293 | tileY: "tilePosition"
|
294 | },
|
295 | _colorMatrixFilterProps = {
|
296 | colorMatrixFilter: 1,
|
297 | saturation: 1,
|
298 | contrast: 1,
|
299 | hue: 1,
|
300 | colorize: 1,
|
301 | colorizeAmount: 1,
|
302 | brightness: 1,
|
303 | combineCMF: 1
|
304 | },
|
305 | _DEG2RAD = Math.PI / 180,
|
306 | _isString = function _isString(value) {
|
307 | return typeof value === "string";
|
308 | },
|
309 | _degreesToRadians = function _degreesToRadians(value) {
|
310 | return _isString(value) && value.charAt(1) === "=" ? value.substr(0, 2) + parseFloat(value.substr(2)) * _DEG2RAD : value * _DEG2RAD;
|
311 | },
|
312 | _renderPropWithEnd = function _renderPropWithEnd(ratio, data) {
|
313 | return data.set(data.t, data.p, ratio === 1 ? data.e : Math.round((data.s + data.c * ratio) * 100000) / 100000, data);
|
314 | },
|
315 | _addRotationalPropTween = function _addRotationalPropTween(plugin, target, property, startNum, endValue, radians) {
|
316 | var cap = 360 * (radians ? _DEG2RAD : 1),
|
317 | isString = _isString(endValue),
|
318 | relative = isString && endValue.charAt(1) === "=" ? +(endValue.charAt(0) + "1") : 0,
|
319 | endNum = parseFloat(relative ? endValue.substr(2) : endValue) * (radians ? _DEG2RAD : 1),
|
320 | change = relative ? endNum * relative : endNum - startNum,
|
321 | finalValue = startNum + change,
|
322 | direction,
|
323 | pt;
|
324 |
|
325 | if (isString) {
|
326 | direction = endValue.split("_")[1];
|
327 |
|
328 | if (direction === "short") {
|
329 | change %= cap;
|
330 |
|
331 | if (change !== change % (cap / 2)) {
|
332 | change += change < 0 ? cap : -cap;
|
333 | }
|
334 | }
|
335 |
|
336 | if (direction === "cw" && change < 0) {
|
337 | change = (change + cap * 1e10) % cap - ~~(change / cap) * cap;
|
338 | } else if (direction === "ccw" && change > 0) {
|
339 | change = (change - cap * 1e10) % cap - ~~(change / cap) * cap;
|
340 | }
|
341 | }
|
342 |
|
343 | plugin._pt = pt = new PropTween(plugin._pt, target, property, startNum, change, _renderPropWithEnd);
|
344 | pt.e = finalValue;
|
345 | return pt;
|
346 | },
|
347 | _initCore = function _initCore() {
|
348 | if (_windowExists()) {
|
349 | _win = window;
|
350 | gsap = _getGSAP();
|
351 | _PIXI = _coreInitted = _PIXI || _win.PIXI;
|
352 | _isV4 = _PIXI && _PIXI.VERSION && _PIXI.VERSION.charAt(0) === "4";
|
353 |
|
354 | _splitColor = function _splitColor(color) {
|
355 | return gsap.utils.splitColor((color + "").substr(0, 2) === "0x" ? "#" + color.substr(2) : color);
|
356 | };
|
357 |
|
358 | }
|
359 | },
|
360 | i,
|
361 | p;
|
362 |
|
363 |
|
364 | for (i = 0; i < _xyContexts.length; i++) {
|
365 | p = _xyContexts[i];
|
366 | _contexts[p + "X"] = p;
|
367 | _contexts[p + "Y"] = p;
|
368 | }
|
369 |
|
370 | export var PixiPlugin = {
|
371 | version: "3.10.3",
|
372 | name: "pixi",
|
373 | register: function register(core, Plugin, propTween) {
|
374 | gsap = core;
|
375 | PropTween = propTween;
|
376 | _getSetter = Plugin.getSetter;
|
377 |
|
378 | _initCore();
|
379 | },
|
380 | registerPIXI: function registerPIXI(pixi) {
|
381 | _PIXI = pixi;
|
382 | },
|
383 | init: function init(target, values, tween, index, targets) {
|
384 | _PIXI || _initCore();
|
385 |
|
386 | if (!_PIXI || !(target instanceof _PIXI.DisplayObject)) {
|
387 | console.warn(target, "is not a DisplayObject or PIXI was not found. PixiPlugin.registerPIXI(PIXI);");
|
388 | return false;
|
389 | }
|
390 |
|
391 | var context, axis, value, colorMatrix, filter, p, padding, i, data;
|
392 |
|
393 | for (p in values) {
|
394 | context = _contexts[p];
|
395 | value = values[p];
|
396 |
|
397 | if (context) {
|
398 | axis = ~p.charAt(p.length - 1).toLowerCase().indexOf("x") ? "x" : "y";
|
399 | this.add(target[context], axis, target[context][axis], context === "skew" ? _degreesToRadians(value) : value);
|
400 | } else if (p === "scale" || p === "anchor" || p === "pivot" || p === "tileScale") {
|
401 | this.add(target[p], "x", target[p].x, value);
|
402 | this.add(target[p], "y", target[p].y, value);
|
403 | } else if (p === "rotation" || p === "angle") {
|
404 |
|
405 | _addRotationalPropTween(this, target, p, target[p], value, p === "rotation");
|
406 | } else if (_colorMatrixFilterProps[p]) {
|
407 | if (!colorMatrix) {
|
408 | _parseColorMatrixFilter(target, values.colorMatrixFilter || values, this);
|
409 |
|
410 | colorMatrix = true;
|
411 | }
|
412 | } else if (p === "blur" || p === "blurX" || p === "blurY" || p === "blurPadding") {
|
413 | filter = _getFilter(target, "BlurFilter");
|
414 | this.add(filter, p, filter[p], value);
|
415 |
|
416 | if (values.blurPadding !== 0) {
|
417 | padding = values.blurPadding || Math.max(filter[p], value) * 2;
|
418 | i = target.filters.length;
|
419 |
|
420 | while (--i > -1) {
|
421 | target.filters[i].padding = Math.max(target.filters[i].padding, padding);
|
422 | }
|
423 | }
|
424 | } else if (_colorProps[p]) {
|
425 | if ((p === "lineColor" || p === "fillColor") && target instanceof _PIXI.Graphics) {
|
426 | data = (target.geometry || target).graphicsData;
|
427 |
|
428 | this._pt = new PropTween(this._pt, target, p, 0, 0, _renderDirtyCache, {
|
429 | g: target.geometry || target
|
430 | });
|
431 | i = data.length;
|
432 |
|
433 | while (--i > -1) {
|
434 | _addColorTween(_isV4 ? data[i] : data[i][p.substr(0, 4) + "Style"], _isV4 ? p : "color", value, this);
|
435 | }
|
436 | } else {
|
437 | _addColorTween(target, p, value, this);
|
438 | }
|
439 | } else if (p === "autoAlpha") {
|
440 | this._pt = new PropTween(this._pt, target, "visible", 0, 0, _renderAutoAlpha);
|
441 | this.add(target, "alpha", target.alpha, value);
|
442 |
|
443 | this._props.push("alpha", "visible");
|
444 | } else if (p !== "resolution") {
|
445 | this.add(target, p, "get", value);
|
446 | }
|
447 |
|
448 | this._props.push(p);
|
449 | }
|
450 | }
|
451 | };
|
452 | _getGSAP() && gsap.registerPlugin(PixiPlugin);
|
453 | export { PixiPlugin as default }; |
\ | No newline at end of file |