UNPKG

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