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 | _windowExists = function _windowExists() {
|
20 | return typeof window !== "undefined";
|
21 | },
|
22 | _getGSAP = function _getGSAP() {
|
23 | return gsap || _windowExists() && (gsap = window.gsap) && gsap.registerPlugin && gsap;
|
24 | },
|
25 | _isFunction = function _isFunction(value) {
|
26 | return typeof value === "function";
|
27 | },
|
28 | _warn = function _warn(message) {
|
29 | return console.warn(message);
|
30 | },
|
31 | _idMatrix = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
|
32 | _lumR = 0.212671,
|
33 | _lumG = 0.715160,
|
34 | _lumB = 0.072169,
|
35 | _applyMatrix = function _applyMatrix(m, m2) {
|
36 | var temp = [],
|
37 | i = 0,
|
38 | z = 0,
|
39 | y,
|
40 | x;
|
41 |
|
42 | for (y = 0; y < 4; y++) {
|
43 | for (x = 0; x < 5; x++) {
|
44 | z = x === 4 ? m[i + 4] : 0;
|
45 | 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;
|
46 | }
|
47 |
|
48 | i += 5;
|
49 | }
|
50 |
|
51 | return temp;
|
52 | },
|
53 | _setSaturation = function _setSaturation(m, n) {
|
54 | var inv = 1 - n,
|
55 | r = inv * _lumR,
|
56 | g = inv * _lumG,
|
57 | b = inv * _lumB;
|
58 | 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);
|
59 | },
|
60 | _colorize = function _colorize(m, color, amount) {
|
61 | var c = _splitColor(color),
|
62 | r = c[0] / 255,
|
63 | g = c[1] / 255,
|
64 | b = c[2] / 255,
|
65 | inv = 1 - amount;
|
66 |
|
67 | 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);
|
68 | },
|
69 | _setHue = function _setHue(m, n) {
|
70 | n *= Math.PI / 180;
|
71 | var c = Math.cos(n),
|
72 | s = Math.sin(n);
|
73 | 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);
|
74 | },
|
75 | _setContrast = function _setContrast(m, n) {
|
76 | 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);
|
77 | },
|
78 | _getFilter = function _getFilter(target, type) {
|
79 | var filterClass = _PIXI.filters[type],
|
80 | filters = target.filters || [],
|
81 | i = filters.length,
|
82 | filter;
|
83 |
|
84 | if (!filterClass) {
|
85 | _warn(type + " not found. PixiPlugin.registerPIXI(PIXI)");
|
86 | }
|
87 |
|
88 | while (--i > -1) {
|
89 | if (filters[i] instanceof filterClass) {
|
90 | return filters[i];
|
91 | }
|
92 | }
|
93 |
|
94 | filter = new filterClass();
|
95 |
|
96 | if (type === "BlurFilter") {
|
97 | filter.blur = 0;
|
98 | }
|
99 |
|
100 | filters.push(filter);
|
101 | target.filters = filters;
|
102 | return filter;
|
103 | },
|
104 | _addColorMatrixFilterCacheTween = function _addColorMatrixFilterCacheTween(p, plugin, cache, vars) {
|
105 |
|
106 | plugin.add(cache, p, cache[p], vars[p]);
|
107 |
|
108 | plugin._props.push(p);
|
109 | },
|
110 | _applyBrightnessToMatrix = function _applyBrightnessToMatrix(brightness, matrix) {
|
111 | var temp = new _PIXI.filters.ColorMatrixFilter();
|
112 | temp.matrix = matrix;
|
113 | temp.brightness(brightness, true);
|
114 | return temp.matrix;
|
115 | },
|
116 | _copy = function _copy(obj) {
|
117 | var copy = {},
|
118 | p;
|
119 |
|
120 | for (p in obj) {
|
121 | copy[p] = obj[p];
|
122 | }
|
123 |
|
124 | return copy;
|
125 | },
|
126 | _CMFdefaults = {
|
127 | contrast: 1,
|
128 | saturation: 1,
|
129 | colorizeAmount: 0,
|
130 | colorize: "rgb(255,255,255)",
|
131 | hue: 0,
|
132 | brightness: 1
|
133 | },
|
134 | _parseColorMatrixFilter = function _parseColorMatrixFilter(target, v, pg) {
|
135 | var filter = _getFilter(target, "ColorMatrixFilter"),
|
136 | cache = target._gsColorMatrixFilter = target._gsColorMatrixFilter || _copy(_CMFdefaults),
|
137 | combine = v.combineCMF && !("colorMatrixFilter" in v && !v.colorMatrixFilter),
|
138 | i,
|
139 | matrix,
|
140 | startMatrix;
|
141 |
|
142 | startMatrix = filter.matrix;
|
143 |
|
144 | if (v.resolution) {
|
145 | filter.resolution = v.resolution;
|
146 | }
|
147 |
|
148 | if (v.matrix && v.matrix.length === startMatrix.length) {
|
149 | matrix = v.matrix;
|
150 |
|
151 | if (cache.contrast !== 1) {
|
152 | _addColorMatrixFilterCacheTween("contrast", pg, cache, _CMFdefaults);
|
153 | }
|
154 |
|
155 | if (cache.hue) {
|
156 | _addColorMatrixFilterCacheTween("hue", pg, cache, _CMFdefaults);
|
157 | }
|
158 |
|
159 | if (cache.brightness !== 1) {
|
160 | _addColorMatrixFilterCacheTween("brightness", pg, cache, _CMFdefaults);
|
161 | }
|
162 |
|
163 | if (cache.colorizeAmount) {
|
164 | _addColorMatrixFilterCacheTween("colorize", pg, cache, _CMFdefaults);
|
165 |
|
166 | _addColorMatrixFilterCacheTween("colorizeAmount", pg, cache, _CMFdefaults);
|
167 | }
|
168 |
|
169 | if (cache.saturation !== 1) {
|
170 | _addColorMatrixFilterCacheTween("saturation", pg, cache, _CMFdefaults);
|
171 | }
|
172 | } else {
|
173 | matrix = _idMatrix.slice();
|
174 |
|
175 | if (v.contrast != null) {
|
176 | matrix = _setContrast(matrix, +v.contrast);
|
177 |
|
178 | _addColorMatrixFilterCacheTween("contrast", pg, cache, v);
|
179 | } else if (cache.contrast !== 1) {
|
180 | if (combine) {
|
181 | matrix = _setContrast(matrix, cache.contrast);
|
182 | } else {
|
183 | _addColorMatrixFilterCacheTween("contrast", pg, cache, _CMFdefaults);
|
184 | }
|
185 | }
|
186 |
|
187 | if (v.hue != null) {
|
188 | matrix = _setHue(matrix, +v.hue);
|
189 |
|
190 | _addColorMatrixFilterCacheTween("hue", pg, cache, v);
|
191 | } else if (cache.hue) {
|
192 | if (combine) {
|
193 | matrix = _setHue(matrix, cache.hue);
|
194 | } else {
|
195 | _addColorMatrixFilterCacheTween("hue", pg, cache, _CMFdefaults);
|
196 | }
|
197 | }
|
198 |
|
199 | if (v.brightness != null) {
|
200 | matrix = _applyBrightnessToMatrix(+v.brightness, matrix);
|
201 |
|
202 | _addColorMatrixFilterCacheTween("brightness", pg, cache, v);
|
203 | } else if (cache.brightness !== 1) {
|
204 | if (combine) {
|
205 | matrix = _applyBrightnessToMatrix(cache.brightness, matrix);
|
206 | } else {
|
207 | _addColorMatrixFilterCacheTween("brightness", pg, cache, _CMFdefaults);
|
208 | }
|
209 | }
|
210 |
|
211 | if (v.colorize != null) {
|
212 | v.colorizeAmount = "colorizeAmount" in v ? +v.colorizeAmount : 1;
|
213 | matrix = _colorize(matrix, v.colorize, v.colorizeAmount);
|
214 |
|
215 | _addColorMatrixFilterCacheTween("colorize", pg, cache, v);
|
216 |
|
217 | _addColorMatrixFilterCacheTween("colorizeAmount", pg, cache, v);
|
218 | } else if (cache.colorizeAmount) {
|
219 | if (combine) {
|
220 | matrix = _colorize(matrix, cache.colorize, cache.colorizeAmount);
|
221 | } else {
|
222 | _addColorMatrixFilterCacheTween("colorize", pg, cache, _CMFdefaults);
|
223 |
|
224 | _addColorMatrixFilterCacheTween("colorizeAmount", pg, cache, _CMFdefaults);
|
225 | }
|
226 | }
|
227 |
|
228 | if (v.saturation != null) {
|
229 | matrix = _setSaturation(matrix, +v.saturation);
|
230 |
|
231 | _addColorMatrixFilterCacheTween("saturation", pg, cache, v);
|
232 | } else if (cache.saturation !== 1) {
|
233 | if (combine) {
|
234 | matrix = _setSaturation(matrix, cache.saturation);
|
235 | } else {
|
236 | _addColorMatrixFilterCacheTween("saturation", pg, cache, _CMFdefaults);
|
237 | }
|
238 | }
|
239 | }
|
240 |
|
241 | i = matrix.length;
|
242 |
|
243 | while (--i > -1) {
|
244 | if (matrix[i] !== startMatrix[i]) {
|
245 | pg.add(startMatrix, i, startMatrix[i], matrix[i], "colorMatrixFilter");
|
246 | }
|
247 | }
|
248 |
|
249 | pg._props.push("colorMatrixFilter");
|
250 | },
|
251 | _renderColor = function _renderColor(ratio, _ref) {
|
252 | var t = _ref.t,
|
253 | p = _ref.p,
|
254 | color = _ref.color,
|
255 | set = _ref.set;
|
256 | set(t, p, color[0] << 16 | color[1] << 8 | color[2]);
|
257 | },
|
258 | _renderDirtyCache = function _renderDirtyCache(ratio, _ref2) {
|
259 | var g = _ref2.g;
|
260 |
|
261 | if (g) {
|
262 |
|
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 = _coreInitted = _getGSAP();
|
353 | _PIXI = _PIXI || _win.PIXI;
|
354 |
|
355 | _splitColor = function _splitColor(color) {
|
356 | return gsap.utils.splitColor((color + "").substr(0, 2) === "0x" ? "#" + color.substr(2) : color);
|
357 | };
|
358 |
|
359 | }
|
360 | },
|
361 | i,
|
362 | p;
|
363 |
|
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 | export 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 |
|
416 | _addRotationalPropTween(this, target, p, target[p], value, p === "rotation");
|
417 | } else if (_colorMatrixFilterProps[p]) {
|
418 | if (!colorMatrix) {
|
419 | _parseColorMatrixFilter(target, values.colorMatrixFilter || values, this);
|
420 |
|
421 | colorMatrix = true;
|
422 | }
|
423 | } else if (p === "blur" || p === "blurX" || p === "blurY" || p === "blurPadding") {
|
424 | filter = _getFilter(target, "BlurFilter");
|
425 | this.add(filter, p, filter[p], value);
|
426 |
|
427 | if (values.blurPadding !== 0) {
|
428 | padding = values.blurPadding || Math.max(filter[p], value) * 2;
|
429 | i = target.filters.length;
|
430 |
|
431 | while (--i > -1) {
|
432 | target.filters[i].padding = Math.max(target.filters[i].padding, padding);
|
433 | }
|
434 | }
|
435 | } else if (_colorProps[p]) {
|
436 | if ((p === "lineColor" || p === "fillColor") && target instanceof _PIXI.Graphics) {
|
437 | data = (target.geometry || target).graphicsData;
|
438 |
|
439 | this._pt = new PropTween(this._pt, target, p, 0, 0, _renderDirtyCache, {
|
440 | g: target.geometry || target
|
441 | });
|
442 | i = data.length;
|
443 |
|
444 | while (--i > -1) {
|
445 | _addColorTween(isV4 ? data[i] : data[i][p.substr(0, 4) + "Style"], isV4 ? p : "color", value, this);
|
446 | }
|
447 | } else {
|
448 | _addColorTween(target, p, value, this);
|
449 | }
|
450 | } else if (p === "autoAlpha") {
|
451 | this._pt = new PropTween(this._pt, target, "visible", 0, 0, _renderAutoAlpha);
|
452 | this.add(target, "alpha", target.alpha, value);
|
453 |
|
454 | this._props.push("alpha", "visible");
|
455 | } else if (p !== "resolution") {
|
456 | this.add(target, p, "get", value);
|
457 | }
|
458 |
|
459 | this._props.push(p);
|
460 | }
|
461 | }
|
462 | };
|
463 | _getGSAP() && gsap.registerPlugin(PixiPlugin);
|
464 | export { PixiPlugin as default }; |
\ | No newline at end of file |