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