UNPKG

8.76 kBJavaScriptView Raw
1/*!
2 * EaselPlugin 3.10.3
3 * https://greensock.com
4 *
5 * @license Copyright 2008-2022, GreenSock. All rights reserved.
6 * Subject to the terms at https://greensock.com/standard-license or for
7 * Club GreenSock members, the agreement issued with that membership.
8 * @author: Jack Doyle, jack@greensock.com
9*/
10
11/* eslint-disable */
12var gsap,
13 _coreInitted,
14 _win,
15 _createJS,
16 _ColorFilter,
17 _ColorMatrixFilter,
18 _colorProps = "redMultiplier,greenMultiplier,blueMultiplier,alphaMultiplier,redOffset,greenOffset,blueOffset,alphaOffset".split(","),
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 _getCreateJS = function _getCreateJS() {
26 return _createJS || _win && _win.createjs || _win || {};
27},
28 _warn = function _warn(message) {
29 return console.warn(message);
30},
31 _cache = function _cache(target) {
32 var b = target.getBounds && target.getBounds();
33
34 if (!b) {
35 b = target.nominalBounds || {
36 x: 0,
37 y: 0,
38 width: 100,
39 height: 100
40 };
41 target.setBounds && target.setBounds(b.x, b.y, b.width, b.height);
42 }
43
44 target.cache && target.cache(b.x, b.y, b.width, b.height);
45
46 _warn("EaselPlugin: for filters to display in EaselJS, you must call the object's cache() method first. GSAP attempted to use the target's getBounds() for the cache but that may not be completely accurate. " + target);
47},
48 _parseColorFilter = function _parseColorFilter(target, v, plugin) {
49 if (!_ColorFilter) {
50 _ColorFilter = _getCreateJS().ColorFilter;
51
52 if (!_ColorFilter) {
53 _warn("EaselPlugin error: The EaselJS ColorFilter JavaScript file wasn't loaded.");
54 }
55 }
56
57 var filters = target.filters || [],
58 i = filters.length,
59 c,
60 s,
61 e,
62 a,
63 p,
64 pt;
65
66 while (i--) {
67 if (filters[i] instanceof _ColorFilter) {
68 s = filters[i];
69 break;
70 }
71 }
72
73 if (!s) {
74 s = new _ColorFilter();
75 filters.push(s);
76 target.filters = filters;
77 }
78
79 e = s.clone();
80
81 if (v.tint != null) {
82 c = gsap.utils.splitColor(v.tint);
83 a = v.tintAmount != null ? +v.tintAmount : 1;
84 e.redOffset = +c[0] * a;
85 e.greenOffset = +c[1] * a;
86 e.blueOffset = +c[2] * a;
87 e.redMultiplier = e.greenMultiplier = e.blueMultiplier = 1 - a;
88 } else {
89 for (p in v) {
90 if (p !== "exposure") if (p !== "brightness") {
91 e[p] = +v[p];
92 }
93 }
94 }
95
96 if (v.exposure != null) {
97 e.redOffset = e.greenOffset = e.blueOffset = 255 * (+v.exposure - 1);
98 e.redMultiplier = e.greenMultiplier = e.blueMultiplier = 1;
99 } else if (v.brightness != null) {
100 a = +v.brightness - 1;
101 e.redOffset = e.greenOffset = e.blueOffset = a > 0 ? a * 255 : 0;
102 e.redMultiplier = e.greenMultiplier = e.blueMultiplier = 1 - Math.abs(a);
103 }
104
105 i = 8;
106
107 while (i--) {
108 p = _colorProps[i];
109
110 if (s[p] !== e[p]) {
111 pt = plugin.add(s, p, s[p], e[p]);
112
113 if (pt) {
114 pt.op = "easel_colorFilter";
115 }
116 }
117 }
118
119 plugin._props.push("easel_colorFilter");
120
121 if (!target.cacheID) {
122 _cache(target);
123 }
124},
125 _idMatrix = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
126 _lumR = 0.212671,
127 _lumG = 0.715160,
128 _lumB = 0.072169,
129 _applyMatrix = function _applyMatrix(m, m2) {
130 if (!(m instanceof Array) || !(m2 instanceof Array)) {
131 return m2;
132 }
133
134 var temp = [],
135 i = 0,
136 z = 0,
137 y,
138 x;
139
140 for (y = 0; y < 4; y++) {
141 for (x = 0; x < 5; x++) {
142 z = x === 4 ? m[i + 4] : 0;
143 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;
144 }
145
146 i += 5;
147 }
148
149 return temp;
150},
151 _setSaturation = function _setSaturation(m, n) {
152 if (isNaN(n)) {
153 return m;
154 }
155
156 var inv = 1 - n,
157 r = inv * _lumR,
158 g = inv * _lumG,
159 b = inv * _lumB;
160 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);
161},
162 _colorize = function _colorize(m, color, amount) {
163 if (isNaN(amount)) {
164 amount = 1;
165 }
166
167 var c = gsap.utils.splitColor(color),
168 r = c[0] / 255,
169 g = c[1] / 255,
170 b = c[2] / 255,
171 inv = 1 - amount;
172 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);
173},
174 _setHue = function _setHue(m, n) {
175 if (isNaN(n)) {
176 return m;
177 }
178
179 n *= Math.PI / 180;
180 var c = Math.cos(n),
181 s = Math.sin(n);
182 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);
183},
184 _setContrast = function _setContrast(m, n) {
185 if (isNaN(n)) {
186 return m;
187 }
188
189 n += 0.01;
190 return _applyMatrix([n, 0, 0, 0, 128 * (1 - n), 0, n, 0, 0, 128 * (1 - n), 0, 0, n, 0, 128 * (1 - n), 0, 0, 0, 1, 0], m);
191},
192 _parseColorMatrixFilter = function _parseColorMatrixFilter(target, v, plugin) {
193 if (!_ColorMatrixFilter) {
194 _ColorMatrixFilter = _getCreateJS().ColorMatrixFilter;
195
196 if (!_ColorMatrixFilter) {
197 _warn("EaselPlugin: The EaselJS ColorMatrixFilter JavaScript file wasn't loaded.");
198 }
199 }
200
201 var filters = target.filters || [],
202 i = filters.length,
203 matrix,
204 startMatrix,
205 s,
206 pg;
207
208 while (--i > -1) {
209 if (filters[i] instanceof _ColorMatrixFilter) {
210 s = filters[i];
211 break;
212 }
213 }
214
215 if (!s) {
216 s = new _ColorMatrixFilter(_idMatrix.slice());
217 filters.push(s);
218 target.filters = filters;
219 }
220
221 startMatrix = s.matrix;
222 matrix = _idMatrix.slice();
223
224 if (v.colorize != null) {
225 matrix = _colorize(matrix, v.colorize, Number(v.colorizeAmount));
226 }
227
228 if (v.contrast != null) {
229 matrix = _setContrast(matrix, Number(v.contrast));
230 }
231
232 if (v.hue != null) {
233 matrix = _setHue(matrix, Number(v.hue));
234 }
235
236 if (v.saturation != null) {
237 matrix = _setSaturation(matrix, Number(v.saturation));
238 }
239
240 i = matrix.length;
241
242 while (--i > -1) {
243 if (matrix[i] !== startMatrix[i]) {
244 pg = plugin.add(startMatrix, i, startMatrix[i], matrix[i]);
245
246 if (pg) {
247 pg.op = "easel_colorMatrixFilter";
248 }
249 }
250 }
251
252 plugin._props.push("easel_colorMatrixFilter");
253
254 if (!target.cacheID) {
255 _cache();
256 }
257
258 plugin._matrix = startMatrix;
259},
260 _initCore = function _initCore(core) {
261 gsap = core || _getGSAP();
262
263 if (_windowExists()) {
264 _win = window;
265 }
266
267 if (gsap) {
268 _coreInitted = 1;
269 }
270};
271
272export var EaselPlugin = {
273 version: "3.10.3",
274 name: "easel",
275 init: function init(target, value, tween, index, targets) {
276 if (!_coreInitted) {
277 _initCore();
278
279 if (!gsap) {
280 _warn("Please gsap.registerPlugin(EaselPlugin)");
281 }
282 }
283
284 this.target = target;
285 var p, pt, tint, colorMatrix, end, labels, i;
286
287 for (p in value) {
288 end = value[p];
289
290 if (p === "colorFilter" || p === "tint" || p === "tintAmount" || p === "exposure" || p === "brightness") {
291 if (!tint) {
292 _parseColorFilter(target, value.colorFilter || value, this);
293
294 tint = true;
295 }
296 } else if (p === "saturation" || p === "contrast" || p === "hue" || p === "colorize" || p === "colorizeAmount") {
297 if (!colorMatrix) {
298 _parseColorMatrixFilter(target, value.colorMatrixFilter || value, this);
299
300 colorMatrix = true;
301 }
302 } else if (p === "frame") {
303 if (typeof end === "string" && end.charAt(1) !== "=" && (labels = target.labels)) {
304 for (i = 0; i < labels.length; i++) {
305 if (labels[i].label === end) {
306 end = labels[i].position;
307 }
308 }
309 }
310
311 pt = this.add(target, "gotoAndStop", target.currentFrame, end, index, targets, Math.round);
312
313 if (pt) {
314 pt.op = p;
315 }
316 } else if (target[p] != null) {
317 this.add(target, p, "get", end);
318 }
319 }
320 },
321 render: function render(ratio, data) {
322 var pt = data._pt;
323
324 while (pt) {
325 pt.r(ratio, pt.d);
326 pt = pt._next;
327 }
328
329 if (data.target.cacheID) {
330 data.target.updateCache();
331 }
332 },
333 register: _initCore
334};
335
336EaselPlugin.registerCreateJS = function (createjs) {
337 _createJS = createjs;
338};
339
340_getGSAP() && gsap.registerPlugin(EaselPlugin);
341export { EaselPlugin as default };
\No newline at end of file