UNPKG

@createjs/easeljs

Version:

The Easel JavaScript library provides a full, hierarchical display list, a core interaction model, and helper classes to make working with the HTML5 Canvas element much easier. Part of the CreateJS suite of libraries.

1,601 lines (1,582 loc) 200 kB
/** * @license EaselJS * Visit https://createjs.com for documentation, updates and examples. * * Copyright (c) 2017 gskinner.com, inc. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ (function (exports,tweenjs) { 'use strict'; var Event = function () { function Event(type, bubbles, cancelable) { if (bubbles === void 0) { bubbles = false; } if (cancelable === void 0) { cancelable = false; } this.type = type; this.target = null; this.currentTarget = null; this.eventPhase = 0; this.bubbles = bubbles; this.cancelable = cancelable; this.timeStamp = new Date().getTime(); this.defaultPrevented = false; this.propagationStopped = false; this.immediatePropagationStopped = false; this.removed = false; } var _proto = Event.prototype; _proto.preventDefault = function preventDefault() { this.defaultPrevented = this.cancelable; return this; }; _proto.stopPropagation = function stopPropagation() { this.propagationStopped = true; return this; }; _proto.stopImmediatePropagation = function stopImmediatePropagation() { this.immediatePropagationStopped = this.propagationStopped = true; return this; }; _proto.remove = function remove() { this.removed = true; return this; }; _proto.clone = function clone() { var event = new Event(this.type, this.bubbles, this.cancelable); for (var n in this) { if (this.hasOwnProperty(n)) { event[n] = this[n]; } } return event; }; _proto.set = function set(props) { for (var n in props) { this[n] = props[n]; } return this; }; _proto.toString = function toString() { return "[" + this.constructor.name + " (type=" + this.type + ")]"; }; return Event; }(); var EventDispatcher = function () { EventDispatcher.initialize = function initialize(target) { var p = EventDispatcher.prototype; target.addEventListener = p.addEventListener; target.on = p.on; target.removeEventListener = target.off = p.removeEventListener; target.removeAllEventListeners = p.removeAllEventListeners; target.hasEventListener = p.hasEventListener; target.dispatchEvent = p.dispatchEvent; target._dispatchEvent = p._dispatchEvent; target.willTrigger = p.willTrigger; }; function EventDispatcher() { this._listeners = null; this._captureListeners = null; } var _proto = EventDispatcher.prototype; _proto.addEventListener = function addEventListener(type, listener, useCapture) { if (useCapture === void 0) { useCapture = false; } var listeners; if (useCapture) { listeners = this._captureListeners = this._captureListeners || {}; } else { listeners = this._listeners = this._listeners || {}; } var arr = listeners[type]; if (arr) { this.removeEventListener(type, listener, useCapture); arr = listeners[type]; } if (arr) { arr.push(listener); } else { listeners[type] = [listener]; } return listener; }; _proto.on = function on(type, listener, scope, once, data, useCapture) { if (scope === void 0) { scope = null; } if (once === void 0) { once = false; } if (data === void 0) { data = {}; } if (useCapture === void 0) { useCapture = false; } if (listener.handleEvent) { scope = scope || listener; listener = listener.handleEvent; } scope = scope || this; return this.addEventListener(type, function (evt) { listener.call(scope, evt, data); once && evt.remove(); }, useCapture); }; _proto.removeEventListener = function removeEventListener(type, listener, useCapture) { if (useCapture === void 0) { useCapture = false; } var listeners = useCapture ? this._captureListeners : this._listeners; if (!listeners) { return; } var arr = listeners[type]; if (!arr) { return; } var l = arr.length; for (var i = 0; i < l; i++) { if (arr[i] === listener) { if (l === 1) { delete listeners[type]; } else { arr.splice(i, 1); } break; } } }; _proto.off = function off(type, listener, useCapture) { if (useCapture === void 0) { useCapture = false; } this.removeEventListener(type, listener, useCapture); }; _proto.removeAllEventListeners = function removeAllEventListeners(type) { if (type === void 0) { type = null; } if (type) { if (this._listeners) { delete this._listeners[type]; } if (this._captureListeners) { delete this._captureListeners[type]; } } else { this._listeners = this._captureListeners = null; } }; _proto.dispatchEvent = function dispatchEvent(eventObj, bubbles, cancelable) { if (bubbles === void 0) { bubbles = false; } if (cancelable === void 0) { cancelable = false; } if (typeof eventObj === "string") { var listeners = this._listeners; if (!bubbles && (!listeners || !listeners[eventObj])) { return true; } eventObj = new Event(eventObj, bubbles, cancelable); } else if (eventObj.target && eventObj.clone) { eventObj = eventObj.clone(); } try { eventObj.target = this; } catch (e) {} if (!eventObj.bubbles || !this.parent) { this._dispatchEvent(eventObj, 2); } else { var top = this; var list = [top]; while (top.parent) { list.push(top = top.parent); } var l = list.length; var i; for (i = l - 1; i >= 0 && !eventObj.propagationStopped; i--) { list[i]._dispatchEvent(eventObj, 1 + (i == 0)); } for (i = 1; i < l && !eventObj.propagationStopped; i++) { list[i]._dispatchEvent(eventObj, 3); } } return !eventObj.defaultPrevented; }; _proto.hasEventListener = function hasEventListener(type) { var listeners = this._listeners, captureListeners = this._captureListeners; return !!(listeners && listeners[type] || captureListeners && captureListeners[type]); }; _proto.willTrigger = function willTrigger(type) { var o = this; while (o) { if (o.hasEventListener(type)) { return true; } o = o.parent; } return false; }; _proto.toString = function toString() { return "[" + (this.constructor.name + this.name ? " " + this.name : "") + "]"; }; _proto._dispatchEvent = function _dispatchEvent(eventObj, eventPhase) { var listeners = eventPhase === 1 ? this._captureListeners : this._listeners; if (eventObj && listeners) { var arr = listeners[eventObj.type]; var l; if (!arr || (l = arr.length) === 0) { return; } try { eventObj.currentTarget = this; } catch (e) {} try { eventObj.eventPhase = eventPhase; } catch (e) {} eventObj.removed = false; arr = arr.slice(); for (var i = 0; i < l && !eventObj.immediatePropagationStopped; i++) { var o = arr[i]; if (o.handleEvent) { o.handleEvent(eventObj); } else { o(eventObj); } if (eventObj.removed) { this.off(eventObj.type, o, eventPhase === 1); eventObj.removed = false; } } } }; return EventDispatcher; }(); function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } var Ticker = function (_EventDispatcher) { _inheritsLoose(Ticker, _EventDispatcher); _createClass(Ticker, null, [{ key: "RAF_SYNCHED", get: function get() { return "synched"; } }, { key: "RAF", get: function get() { return "raf"; } }, { key: "TIMEOUT", get: function get() { return "timeout"; } }]); function Ticker(name) { var _this; _this = _EventDispatcher.call(this) || this; _this.name = name; _this.timingMode = Ticker.TIMEOUT; _this.maxDelta = 0; _this.paused = false; _this._inited = false; _this._startTime = 0; _this._pausedTime = 0; _this._ticks = 0; _this._pausedTicks = 0; _this._interval = 50; _this._lastTime = 0; _this._times = null; _this._tickTimes = null; _this._timerId = null; _this._raf = true; return _this; } var _proto = Ticker.prototype; _proto.init = function init() { if (this._inited) { return; } this._inited = true; this._times = []; this._tickTimes = []; this._startTime = this._getTime(); this._times.push(this._lastTime = 0); this._setupTick(); }; _proto.reset = function reset() { if (this._raf) { var f = window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.oCancelAnimationFrame || window.msCancelAnimationFrame; f && f(this._timerId); } else { clearTimeout(this._timerId); } this.removeAllEventListeners("tick"); this._timerId = this._times = this._tickTimes = null; this._startTime = this._lastTime = this._ticks = 0; this._inited = false; }; _proto.addEventListener = function addEventListener(type, listener, useCapture) { !this._inited && this.init(); return _EventDispatcher.prototype.addEventListener.call(this, type, listener, useCapture); }; _proto.getMeasuredTickTime = function getMeasuredTickTime(ticks) { if (ticks === void 0) { ticks = null; } var times = this._tickTimes; if (!times || times.length < 1) { return -1; } ticks = Math.min(times.length, ticks || this.framerate | 0); return times.reduce(function (a, b) { return a + b; }, 0) / ticks; }; _proto.getMeasuredFPS = function getMeasuredFPS(ticks) { if (ticks === void 0) { ticks = null; } var times = this._times; if (!times || times.length < 2) { return -1; } ticks = Math.min(times.length - 1, ticks || this.framerate | 0); return 1000 / ((times[0] - times[ticks]) / ticks); }; _proto.getTime = function getTime(runTime) { if (runTime === void 0) { runTime = false; } return this._startTime ? this._getTime() - (runTime ? this._pausedTime : 0) : -1; }; _proto.getEventTime = function getEventTime(runTime) { if (runTime === void 0) { runTime = false; } return this._startTime ? (this._lastTime || this._startTime) - (runTime ? this._pausedTime : 0) : -1; }; _proto.getTicks = function getTicks(pauseable) { if (pauseable === void 0) { pauseable = false; } return this._ticks - (pauseable ? this._pausedTicks : 0); }; _proto._handleSynch = function _handleSynch() { this._timerId = null; this._setupTick(); if (this._getTime() - this._lastTime >= (this._interval - 1) * 0.97) { this._tick(); } }; _proto._handleRAF = function _handleRAF() { this._timerId = null; this._setupTick(); this._tick(); }; _proto._handleTimeout = function _handleTimeout() { this._timerId = null; this._setupTick(); this._tick(); }; _proto._setupTick = function _setupTick() { if (this._timerId != null) { return; } var mode = this.timingMode || this._raf && Ticker.RAF; if (mode === Ticker.RAF_SYNCHED || mode === Ticker.RAF) { var f = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame; if (f) { this._timerId = f(mode === Ticker.RAF ? this._handleRAF.bind(this) : this._handleSynch.bind(this)); this._raf = true; return; } } this._raf = false; this._timerId = setTimeout(this._handleTimeout.bind(this), this._interval); }; _proto._tick = function _tick() { var paused = this.paused, time = this._getTime(), elapsedTime = time - this._lastTime; this._lastTime = time; this._ticks++; if (paused) { this._pausedTicks++; this._pausedTime += elapsedTime; } if (this.hasEventListener("tick")) { var event = new Event("tick"); var maxDelta = this.maxDelta; event.delta = maxDelta && elapsedTime > maxDelta ? maxDelta : elapsedTime; event.paused = paused; event.time = time; event.runTime = time - this._pausedTime; this.dispatchEvent(event); } this._tickTimes.unshift(this._getTime() - time); while (this._tickTimes.length > 100) { this._tickTimes.pop(); } this._times.unshift(time); while (this._times.length > 100) { this._times.pop(); } }; _proto._getTime = function _getTime() { var now = window.performance && window.performance.now; return (now && now.call(performance) || new Date().getTime()) - this._startTime; }; Ticker.on = function on(type, listener, scope, once, data, useCapture) { return _instance.on(type, listener, scope, once, data, useCapture); }; Ticker.removeEventListener = function removeEventListener(type, listener, useCapture) { _instance.removeEventListener(type, listener, useCapture); }; Ticker.off = function off(type, listener, useCapture) { _instance.off(type, listener, useCapture); }; Ticker.removeAllEventListeners = function removeAllEventListeners(type) { _instance.removeAllEventListeners(type); }; Ticker.dispatchEvent = function dispatchEvent(eventObj, bubbles, cancelable) { return _instance.dispatchEvent(eventObj, bubbles, cancelable); }; Ticker.hasEventListener = function hasEventListener(type) { return _instance.hasEventListener(type); }; Ticker.willTrigger = function willTrigger(type) { return _instance.willTrigger(type); }; Ticker.toString = function toString() { return _instance.toString(); }; Ticker.init = function init() { _instance.init(); }; Ticker.reset = function reset() { _instance.reset(); }; Ticker.addEventListener = function addEventListener(type, listener, useCapture) { _instance.addEventListener(type, listener, useCapture); }; Ticker.getMeasuredTickTime = function getMeasuredTickTime(ticks) { return _instance.getMeasuredTickTime(ticks); }; Ticker.getMeasuredFPS = function getMeasuredFPS(ticks) { return _instance.getMeasuredFPS(ticks); }; Ticker.getTime = function getTime(runTime) { return _instance.getTime(runTime); }; Ticker.getEventTime = function getEventTime(runTime) { return _instance.getEventTime(runTime); }; Ticker.getTicks = function getTicks(pauseable) { return _instance.getTicks(pauseable); }; _createClass(Ticker, [{ key: "interval", get: function get() { return this._interval; }, set: function set(interval) { this._interval = interval; if (!this._inited) { return; } this._setupTick(); } }, { key: "framerate", get: function get() { return 1000 / this._interval; }, set: function set(framerate) { this.interval = 1000 / framerate; } }], [{ key: "interval", get: function get() { return _instance.interval; }, set: function set(interval) { _instance.interval = interval; } }, { key: "framerate", get: function get() { return _instance.framerate; }, set: function set(framerate) { _instance.framerate = framerate; } }, { key: "name", get: function get() { return _instance.name; }, set: function set(name) { _instance.name = name; } }, { key: "timingMode", get: function get() { return _instance.timingMode; }, set: function set(timingMode) { _instance.timingMode = timingMode; } }, { key: "maxDelta", get: function get() { return _instance.maxDelta; }, set: function set(maxDelta) { _instance.maxDelta = maxDelta; } }, { key: "paused", get: function get() { return _instance.paused; }, set: function set(paused) { _instance.paused = paused; } }]); return Ticker; }(EventDispatcher); var _instance = new Ticker("createjs.global"); var StageGL = function StageGL() { throw new Error("\n\t\t\tStageGL is not currently supported on the EaselJS 2.0 branch.\n\t\t\tEnd of Q1 2018 is targetted for StageGL support.\n\t\t\tFollow @CreateJS on Twitter for updates.\n\t\t"); }; var Shadow = function () { function Shadow(color, offsetX, offsetY, blur) { if (color === void 0) { color = "black"; } if (offsetX === void 0) { offsetX = 0; } if (offsetY === void 0) { offsetY = 0; } if (blur === void 0) { blur = 0; } this.color = color; this.offsetX = offsetX; this.offsetY = offsetY; this.blur = blur; } var _proto = Shadow.prototype; _proto.toString = function toString() { return "[" + this.constructor.name + "]"; }; _proto.clone = function clone() { return new Shadow(this.color, this.offsetX, this.offsetY, this.blur); }; return Shadow; }(); Shadow.identity = new Shadow("transparent"); var _nextId = 0; function uid() { return _nextId++; } var Point = function () { function Point(x, y) { this.setValues(x, y); } var _proto = Point.prototype; _proto.setValues = function setValues(x, y) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } this.x = x; this.y = y; return this; }; _proto.copy = function copy(point) { this.x = point.x; this.y = point.y; return this; }; _proto.clone = function clone() { return new Point(this.x, this.y); }; _proto.toString = function toString() { return "[" + this.constructor.name + " (x=" + this.x + " y=" + this.y + ")]"; }; return Point; }(); var Matrix2D = function () { function Matrix2D(a, b, c, d, tx, ty) { this.setValues(a, b, c, d, tx, ty); } var _proto = Matrix2D.prototype; _proto.setValues = function setValues(a, b, c, d, tx, ty) { if (a === void 0) { a = 1; } if (b === void 0) { b = 0; } if (c === void 0) { c = 0; } if (d === void 0) { d = 1; } if (tx === void 0) { tx = 0; } if (ty === void 0) { ty = 0; } this.a = a; this.b = b; this.c = c; this.d = d; this.tx = tx; this.ty = ty; return this; }; _proto.append = function append(a, b, c, d, tx, ty) { var a1 = this.a; var b1 = this.b; var c1 = this.c; var d1 = this.d; if (a != 1 || b != 0 || c != 0 || d != 1) { this.a = a1 * a + c1 * b; this.b = b1 * a + d1 * b; this.c = a1 * c + c1 * d; this.d = b1 * c + d1 * d; } this.tx = a1 * tx + c1 * ty + this.tx; this.ty = b1 * tx + d1 * ty + this.ty; return this; }; _proto.prepend = function prepend(a, b, c, d, tx, ty) { var a1 = this.a; var c1 = this.c; var tx1 = this.tx; this.a = a * a1 + c * this.b; this.b = b * a1 + d * this.b; this.c = a * c1 + c * this.d; this.d = b * c1 + d * this.d; this.tx = a * tx1 + c * this.ty + tx; this.ty = b * tx1 + d * this.ty + ty; return this; }; _proto.appendMatrix = function appendMatrix(matrix) { return this.append(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty); }; _proto.prependMatrix = function prependMatrix(matrix) { return this.prepend(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty); }; _proto.appendTransform = function appendTransform(x, y, scaleX, scaleY, rotation, skewX, skewY, regX, regY) { var r, cos, sin; if (rotation % 360) { r = rotation * Matrix2D.DEG_TO_RAD; cos = Math.cos(r); sin = Math.sin(r); } else { cos = 1; sin = 0; } if (skewX || skewY) { skewX *= Matrix2D.DEG_TO_RAD; skewY *= Matrix2D.DEG_TO_RAD; this.append(Math.cos(skewY), Math.sin(skewY), -Math.sin(skewX), Math.cos(skewX), x, y); this.append(cos * scaleX, sin * scaleX, -sin * scaleY, cos * scaleY, 0, 0); } else { this.append(cos * scaleX, sin * scaleX, -sin * scaleY, cos * scaleY, x, y); } if (regX || regY) { this.tx -= regX * this.a + regY * this.c; this.ty -= regX * this.b + regY * this.d; } return this; }; _proto.prependTransform = function prependTransform(x, y, scaleX, scaleY, rotation, skewX, skewY, regX, regY) { var r, cos, sin; if (rotation % 360) { r = rotation * Matrix2D.DEG_TO_RAD; cos = Math.cos(r); sin = Math.sin(r); } else { cos = 1; sin = 0; } if (regX || regY) { this.tx -= regX; this.ty -= regY; } if (skewX || skewY) { skewX *= Matrix2D.DEG_TO_RAD; skewY *= Matrix2D.DEG_TO_RAD; this.prepend(cos * scaleX, sin * scaleX, -sin * scaleY, cos * scaleY, 0, 0); this.prepend(Math.cos(skewY), Math.sin(skewY), -Math.sin(skewX), Math.cos(skewX), x, y); } else { this.prepend(cos * scaleX, sin * scaleX, -sin * scaleY, cos * scaleY, x, y); } return this; }; _proto.rotate = function rotate(angle) { angle *= Matrix2D.DEG_TO_RAD; var cos = Math.cos(angle); var sin = Math.sin(angle); var a1 = this.a; var b1 = this.b; this.a = a1 * cos + this.c * sin; this.b = b1 * cos + this.d * sin; this.c = -a1 * sin + this.c * cos; this.d = -b1 * sin + this.d * cos; return this; }; _proto.skew = function skew(skewX, skewY) { skewX *= Matrix2D.DEG_TO_RAD; skewY *= Matrix2D.DEG_TO_RAD; this.append(Math.cos(skewY), Math.sin(skewY), -Math.sin(skewX), Math.cos(skewX), 0, 0); return this; }; _proto.scale = function scale(x, y) { this.a *= x; this.b *= x; this.c *= y; this.d *= y; return this; }; _proto.translate = function translate(x, y) { this.tx += this.a * x + this.c * y; this.ty += this.b * x + this.d * y; return this; }; _proto.identity = function identity() { this.a = this.d = 1; this.b = this.c = this.tx = this.ty = 0; return this; }; _proto.invert = function invert() { var a1 = this.a; var b1 = this.b; var c1 = this.c; var d1 = this.d; var tx1 = this.tx; var n = a1 * d1 - b1 * c1; this.a = d1 / n; this.b = -b1 / n; this.c = -c1 / n; this.d = a1 / n; this.tx = (c1 * this.ty - d1 * tx1) / n; this.ty = -(a1 * this.ty - b1 * tx1) / n; return this; }; _proto.isIdentity = function isIdentity() { return this.tx === 0 && this.ty === 0 && this.a === 1 && this.b === 0 && this.c === 0 && this.d === 1; }; _proto.equals = function equals(matrix) { return this.tx === matrix.tx && this.ty === matrix.ty && this.a === matrix.a && this.b === matrix.b && this.c === matrix.c && this.d === matrix.d; }; _proto.transformPoint = function transformPoint(x, y, pt) { if (pt === void 0) { pt = new Point(); } pt.x = x * this.a + y * this.c + this.tx; pt.y = x * this.b + y * this.d + this.ty; return pt; }; _proto.decompose = function decompose(target) { if (target === void 0) { target = {}; } target.x = this.tx; target.y = this.ty; target.scaleX = Math.sqrt(this.a * this.a + this.b * this.b); target.scaleY = Math.sqrt(this.c * this.c + this.d * this.d); var skewX = Math.atan2(-this.c, this.d); var skewY = Math.atan2(this.b, this.a); var delta = Math.abs(1 - skewX / skewY); if (delta < 0.00001) { target.rotation = skewY / Matrix2D.DEG_TO_RAD; if (this.a < 0 && this.d >= 0) { target.rotation += target.rotation <= 0 ? 180 : -180; } target.skewX = target.skewY = 0; } else { target.skewX = skewX / Matrix2D.DEG_TO_RAD; target.skewY = skewY / Matrix2D.DEG_TO_RAD; } return target; }; _proto.copy = function copy(matrix) { return this.setValues(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty); }; _proto.clone = function clone() { return new Matrix2D(this.a, this.b, this.c, this.d, this.tx, this.ty); }; _proto.toString = function toString() { return "[" + this.constructor.name + " (a=" + this.a + " b=" + this.b + " c=" + this.c + " d=" + this.d + " tx=" + this.tx + " ty=" + this.ty + ")]"; }; return Matrix2D; }(); Matrix2D.DEG_TO_RAD = Math.PI / 180; Matrix2D.identity = new Matrix2D(); var DisplayProps = function () { function DisplayProps(visible, alpha, shadow, compositeOperation, matrix) { this.setValues(visible, alpha, shadow, compositeOperation, matrix); } var _proto = DisplayProps.prototype; _proto.setValues = function setValues(visible, alpha, shadow, compositeOperation, matrix) { if (visible === void 0) { visible = true; } if (alpha === void 0) { alpha = 1; } this.visible = visible; this.alpha = alpha; this.shadow = shadow; this.compositeOperation = compositeOperation; this.matrix = matrix || this.matrix && this.matrix.identity() || new Matrix2D(); return this; }; _proto.append = function append(visible, alpha, shadow, compositeOperation, matrix) { this.alpha *= alpha; this.shadow = shadow || this.shadow; this.compositeOperation = compositeOperation || this.compositeOperation; this.visible = this.visible && visible; matrix && this.matrix.appendMatrix(matrix); return this; }; _proto.prepend = function prepend(visible, alpha, shadow, compositeOperation, matrix) { this.alpha *= alpha; this.shadow = this.shadow || shadow; this.compositeOperation = this.compositeOperation || compositeOperation; this.visible = this.visible && visible; matrix && this.matrix.prependMatrix(matrix); return this; }; _proto.identity = function identity() { this.visible = true; this.alpha = 1; this.shadow = this.compositeOperation = null; this.matrix.identity(); return this; }; _proto.clone = function clone() { return new DisplayProps(this.alpha, this.shadow, this.compositeOperation, this.visible, this.matrix.clone()); }; return DisplayProps; }(); var Rectangle = function () { function Rectangle(x, y, width, height) { this.setValues(x, y, width, height); } var _proto = Rectangle.prototype; _proto.setValues = function setValues(x, y, width, height) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (width === void 0) { width = 0; } if (height === void 0) { height = 0; } this.x = x; this.y = y; this.width = width; this.height = height; return this; }; _proto.extend = function extend(x, y, width, height) { if (width === void 0) { width = 0; } if (height === void 0) { height = 0; } if (x + width > this.x + this.width) { this.width = x + width - this.x; } if (y + height > this.y + this.height) { this.height = y + height - this.y; } if (x < this.x) { this.width += this.x - x; this.x = x; } if (y < this.y) { this.height += this.y - y; this.y = y; } return this; }; _proto.pad = function pad(top, left, bottom, right) { this.x -= left; this.y -= top; this.width += left + right; this.height += top + bottom; return this; }; _proto.copy = function copy(rect) { return this.setValues(rect.x, rect.y, rect.width, rect.height); }; _proto.contains = function contains(x, y, width, height) { if (width === void 0) { width = 0; } if (height === void 0) { height = 0; } return x >= this.x && x + width <= this.x + this.width && y >= this.y && y + height <= this.y + this.height; }; _proto.union = function union(rect) { return this.clone().extend(rect.x, rect.y, rect.width, rect.height); }; _proto.intersection = function intersection(rect) { var x1 = rect.x, y1 = rect.y, x2 = x1 + rect.width, y2 = y1 + rect.height; if (this.x > x1) { x1 = this.x; } if (this.y > y1) { y1 = this.y; } if (this.x + this.width < x2) { x2 = this.x + this.width; } if (this.y + this.height < y2) { y2 = this.y + this.height; } return x2 <= x1 || y2 <= y1 ? null : new Rectangle(x1, y1, x2 - x1, y2 - y1); }; _proto.intersects = function intersects(rect) { return rect.x <= this.x + this.width && this.x <= rect.x + rect.width && rect.y <= this.y + this.height && this.y <= rect.y + rect.height; }; _proto.isEmpty = function isEmpty() { return this.width <= 0 || this.height <= 0; }; _proto.clone = function clone() { return new Rectangle(this.x, this.y, this.width, this.height); }; _proto.toString = function toString() { return "[" + this.constructor.name + " (x=" + this.x + " y=" + this.y + " width=" + this.width + " height=" + this.height + ")]"; }; return Rectangle; }(); var Filter = function () { function Filter() { this.usesContext = false; this._multiPass = null; this.VTX_SHADER_BODY = null; this.FRAG_SHADER_BODY = null; } var _proto = Filter.prototype; _proto.getBounds = function getBounds(rect) {}; _proto.shaderParamSetup = function shaderParamSetup(gl, stage, shaderProgram) {}; _proto.applyFilter = function applyFilter(ctx, x, y, width, height, targetCtx, targetX, targetY) { targetCtx = targetCtx || ctx; if (targetX == null) { targetX = x; } if (targetY == null) { targetY = y; } try { var imageData = ctx.getImageData(x, y, width, height); if (this._applyFilter(imageData)) { targetCtx.putImageData(imageData, targetX, targetY); return true; } } catch (e) {} return false; }; _proto.toString = function toString() { return "[" + this.constructor.name + "]"; }; _proto.clone = function clone() { return new Filter(); }; _proto._applyFilter = function _applyFilter(imageData) {}; return Filter; }(); var BitmapCache = function (_Filter) { _inheritsLoose(BitmapCache, _Filter); function BitmapCache() { var _this; _this = _Filter.call(this) || this; _this.width = undefined; _this.height = undefined; _this.x = undefined; _this.y = undefined; _this.scale = 1; _this.offX = 0; _this.offY = 0; _this.cacheID = 0; _this._filterOffX = 0; _this._filterOffY = 0; _this._cacheDataURLID = 0; _this._cacheDataURL = null; _this._drawWidth = 0; _this._drawHeight = 0; _this._boundRect = new Rectangle(); return _this; } BitmapCache.getFilterBounds = function getFilterBounds(target, output) { if (output === void 0) { output = new Rectangle(); } var filters = target.filters; var filterCount = filters && filters.length; if (!!filterCount <= 0) { return output; } for (var i = 0; i < filterCount; i++) { var f = filters[i]; if (!f || !f.getBounds) { continue; } var test = f.getBounds(); if (!test) { continue; } if (i == 0) { output.setValues(test.x, test.y, test.width, test.height); } else { output.extend(test.x, test.y, test.width, test.height); } } return output; }; var _proto = BitmapCache.prototype; _proto.define = function define(target, x, y, width, height, scale, options) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (width === void 0) { width = 1; } if (height === void 0) { height = 1; } if (scale === void 0) { scale = 1; } if (!target) { throw "No symbol to cache"; } this._options = options; this._useWebGL = options !== undefined; this.target = target; this.width = width >= 1 ? width : 1; this.height = height >= 1 ? height : 1; this.x = x; this.y = y; this.scale = scale; this.update(); }; _proto.update = function update(compositeOperation) { if (!this.target) { throw "define() must be called before update()"; } var filterBounds = BitmapCache.getFilterBounds(this.target); var surface = this.target.cacheCanvas; this._drawWidth = Math.ceil(this.width * this.scale) + filterBounds.width; this._drawHeight = Math.ceil(this.height * this.scale) + filterBounds.height; if (!surface || this._drawWidth != surface.width || this._drawHeight != surface.height) { this._updateSurface(); } this._filterOffX = filterBounds.x; this._filterOffY = filterBounds.y; this.offX = this.x * this.scale + this._filterOffX; this.offY = this.y * this.scale + this._filterOffY; this._drawToCache(compositeOperation); this.cacheID = this.cacheID ? this.cacheID + 1 : 1; }; _proto.release = function release() { var stage = this.target.stage; if (this._useWebGL && this._webGLCache) { if (!this._webGLCache.isCacheControlled) { if (this.__lastRT) { this.__lastRT = undefined; } if (this.__rtA) { this._webGLCache._killTextureObject(this.__rtA); } if (this.__rtB) { this._webGLCache._killTextureObject(this.__rtB); } if (this.target && this.target.cacheCanvas) { this._webGLCache._killTextureObject(this.target.cacheCanvas); } } this._webGLCache = false; } else if (stage instanceof StageGL) { stage.releaseTexture(this.target.cacheCanvas); } this.target = this.target.cacheCanvas = null; this.cacheID = this._cacheDataURLID = this._cacheDataURL = undefined; this.width = this.height = this.x = this.y = this.offX = this.offY = 0; this.scale = 1; }; _proto.getCacheDataURL = function getCacheDataURL() { var cacheCanvas = this.target && this.target.cacheCanvas; if (!cacheCanvas) { return null; } if (this.cacheID != this._cacheDataURLID) { this._cacheDataURLID = this.cacheID; this._cacheDataURL = cacheCanvas.toDataURL ? cacheCanvas.toDataURL() : null; } return this._cacheDataURL; }; _proto.draw = function draw(ctx) { if (!this.target) { return false; } ctx.drawImage(this.target.cacheCanvas, this.x + this._filterOffX / this.scale, this.y + this._filterOffY / this.scale, this._drawWidth / this.scale, this._drawHeight / this.scale); return true; }; _proto.getBounds = function getBounds() { var scale = this.scale; return this._boundRect.setValue(this._filterOffX / scale, this._filterOffY / scale, this.width / scale, this.height / scale); }; _proto._updateSurface = function _updateSurface() { var surface; if (!this._useWebGL) { surface = this.target.cacheCanvas; if (!surface) { surface = this.target.cacheCanvas = window.createjs && createjs.createCanvas ? createjs.createCanvas() : document.createElement("canvas"); } surface.width = this._drawWidth; surface.height = this._drawHeight; return; } if (!this._webGLCache) { if (this._options.useGL === "stage") { if (!(this.target.stage != null && this.target.stage.isWebGL)) { throw "Cannot use 'stage' for cache because the object's parent stage is " + (this.target.stage != null ? "non WebGL." : "not set, please addChild to the correct stage."); } this.target.cacheCanvas = true; this._webGLCache = this.target.stage; } else if (this._options.useGL === "new") { this.target.cacheCanvas = document.createElement("canvas"); this._webGLCache = new StageGL(this.target.cacheCanvas, { antialias: true, transparent: true, autoPurge: -1 }); this._webGLCache.isCacheControlled = true; } else { throw "Invalid option provided to useGL, expected ['stage', 'new', StageGL, undefined], got " + this._options.useGL; } } var stageGL = this._webGLCache; surface = this.target.cacheCanvas; if (stageGL.isCacheControlled) { surface.width = this._drawWidth; surface.height = this._drawHeight; stageGL.updateViewport(this._drawWidth, this._drawHeight); } if (this.target.filters) { stageGL.getTargetRenderTexture(this.target, this._drawWidth, this._drawHeight); stageGL.getTargetRenderTexture(this.target, this._drawWidth, this._drawHeight); } else if (!stageGL.isCacheControlled) { stageGL.getTargetRenderTexture(this.target, this._drawWidth, this._drawHeight); } }; _proto._drawToCache = function _drawToCache(compositeOperation) { var target = this.target; var surface = target.cacheCanvas; var webGL = this._webGLCache; if (!this._useWebGL || !webGL) { var ctx = surface.getContext("2d"); if (!compositeOperation) { ctx.clearRect(0, 0, this._drawWidth + 1, this._drawHeight + 1); } ctx.save(); ctx.globalCompositeOperation = compositeOperation; ctx.setTransform(this.scale, 0, 0, this.scale, -this._filterOffX, -this._filterOffY); ctx.translate(-this.x, -this.y); target.draw(ctx, true); ctx.restore(); if (target.filters && target.filters.length) { this._applyFilters(target); } surface._invalid = true; return; } this._webGLCache.cacheDraw(target, target.filters, this); surface = this.target.cacheCanvas; surface.width = this._drawWidth; surface.height = this._drawHeight; surface._invalid = true; }; _proto._applyFilters = function _applyFilters() { var surface = this.target.cacheCanvas; var filters = this.target.filters; var w = this._drawWidth; var h = this._drawHeight; var data = surface.getContext("2d").getImageData(0, 0, w, h); var l = filters.length; for (var i = 0; i < l; i++) { filters[i]._applyFilter(data); } surface.getContext("2d").putImageData(data, 0, 0); }; return BitmapCache; }(Filter); var DisplayObject = function (_EventDispatcher) { _inheritsLoose(DisplayObject, _EventDispatcher); function DisplayObject() { var _this; _this = _EventDispatcher.call(this) || this; _this.alpha = 1; _this.cacheCanvas = null; _this.bitmapCache = null; _this.id = uid(); _this.mouseEnabled = true; _this.tickEnabled = true; _this.name = null; _this.parent = null; _this.regX = 0; _this.regY = 0; _this.rotation = 0; _this.scaleX = 1; _this.scaleY = 1; _this.skewX = 0; _this.skewY = 0; _this.shadow = null; _this.visible = true; _this.x = 0; _this.y = 0; _this.transformMatrix = null; _this.compositeOperation = null; _this.snapToPixel = true; _this.filters = null; _this.mask = null; _this.hitArea = null; _this.cursor = null; _this._props = new DisplayProps(); _this._rectangle = new Rectangle(); _this._bounds = null; _this._webGLRenderStyle = DisplayObject._StageGL_NONE; return _this; } var _proto = DisplayObject.prototype; _proto.isVisible = function isVisible() { return !!(this.visible && this.alpha > 0 && this.scaleX != 0 && this.scaleY != 0); }; _proto.draw = function draw(ctx, ignoreCache) { if (ignoreCache === void 0) { ignoreCache = false; } return this.drawCache(ctx, ignoreCache); }; _proto.drawCache = function drawCache(ctx, ignoreCache) { if (ignoreCache === void 0) { ignoreCache = false; } var cache = this.bitmapCache; if (cache && !ignoreCache) { return cache.draw(ctx); } return false; }; _proto.updateContext = function updateContext(ctx) { var o = this, mask = o.mask, mtx = o._props.matrix; if (mask && mask.graphics && !mask.graphics.isEmpty()) { mask.getMatrix(mtx); ctx.transform(mtx.a, mtx.b, mtx.c, mtx.d, mtx.tx, mtx.ty); mask.graphics.drawAsPath(ctx); ctx.clip(); mtx.invert(); ctx.transform(mtx.a, mtx.b, mtx.c, mtx.d, mtx.tx, mtx.ty); } this.getMatrix(mtx); var tx = mtx.tx, ty = mtx.ty; if (DisplayObject._snapToPixelEnabled && o.snapToPixel) { tx = tx + (tx < 0 ? -0.5 : 0.5) | 0; ty = ty + (ty < 0 ? -0.5 : 0.5) | 0; } ctx.transform(mtx.a, mtx.b, mtx.c, mtx.d, tx, ty); ctx.globalAlpha *= o.alpha; if (o.compositeOperation) { ctx.globalCompositeOperation = o.compositeOperation; } if (o.shadow) { this._applyShadow(ctx, o.shadow); } }; _proto.cache = function cache(x, y, width, height, scale, options) { if (scale === void 0) { scale = 1; } if (!this.bitmapCache) { this.bitmapCache = new BitmapCache(); } this.bitmapCache.define(this, x, y, width, height, scale, options); }; _proto.updateCache = function updateCache(compositeOperation) { if (!this.bitmapCache) { throw "No cache found. cache() must be called before updateCache()"; } this.bitmapCache.update(compositeOperation); }; _proto.uncache = function uncache() { if (this.bitmapCache) { this.bitmapCache.release(); this.bitmapCache = undefined; } }; _proto.getCacheDataURL = function getCacheDataURL() { return this.bitmapCache ? this.bitmapCache.getDataURL() : null; }; _proto.localToGlobal = function localToGlobal(x, y, pt) { if (pt === void 0) { pt = new Point(); } return this.getConcatenatedMatrix(this._props.matrix).transformPoint(x, y, pt); }; _proto.globalToLocal = function globalToLocal(x, y, pt) { if (pt === void 0) { pt = new Point(); } return this.getConcatenatedMatrix(this._props.matrix).invert().transformPoint(x, y, pt); }; _proto.localToLocal = function localToLocal(x, y, target, pt) { pt = this.localToGlobal(x, y, pt); return target.globalToLocal(pt.x, pt.y, pt); }; _proto.setTransform = function setTransform(x, y, scaleX, scaleY, rotation, skewX, skewY, regX, regY) { if (x === void 0) { x = 0; } if (y === void 0) { y = 0; } if (scaleX === void 0) { scaleX = 1; } if (scaleY === void 0) { scaleY = 1; } if (rotation === void 0) { rotation = 0; } if (skewX === void 0) { skewX = 0; } if (skewY === void 0) { skewY = 0; } if (regX === void 0) { regX = 0; } if (regY === void 0) { regY = 0; } this.x = x; this.y = y; this.scaleX = scaleX; this.scaleY = scaleY; this.rotation = rotation; this.skewX = skewX; this.skewY = skewY; this.regX = regX; this.regY = regY; return this; }; _proto.getMatrix = function getMatrix(matrix) { var o = this, mtx = matrix && matrix.identity() || new Matrix2D(); return o.transformMatrix ? mtx.copy(o.transformMatrix) : mtx.appendTransform(o.x, o.y, o.scaleX, o.scaleY, o.rotation, o.skewX, o.skewY, o.regX, o.regY); }; _proto.getConcatenatedMatrix = function getConcatenatedMatrix(matrix) { var o = this, mtx = this.getMatrix(matrix); while (o = o.parent) { mtx.prependMatrix(o.getMatrix(o._props.matrix)); } return mtx; }; _proto.getConcatenatedDisplayProps = function getConcatenatedDisplayProps(props) { props = props ? props.identity() : new DisplayProps(); var o = this, mtx = o.getMatrix(props.matrix); do { props.prepend(o.visible, o.alpha, o.shadow, o.compositeOperation); if (o != this) { mtx.prependMatrix(o.getMatrix(o._props.matrix)); } } while (o = o.parent); return props; }; _proto.hitTest = function hitTest(x, y) { var ctx = DisplayObject._hitTestContext; ctx.setTransform(1, 0, 0, 1, -x, -y); this.draw(ctx); var hit = this._testHit(ctx); ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, 2, 2); return hit; }; _proto.set = function set(props) { for (var n in props) { this[n] = props[n]; } return this; }; _proto.getBounds = function getBounds() { if (this._bounds) { return this._rectangle.copy(this._bounds); } var cacheCanvas = this.cacheCanvas; if (cacheCanvas) { var scale = this._cacheScale; return this._rectangle.setValues(this._cacheOffsetX, this._cacheOffsetY, cacheCanvas.width / scale, cacheCanvas.height / scale); } return null; }; _proto.getTransformedBounds = function getTransformedBounds() { return this._getBounds(); }; _proto.setBounds = function setBounds(x, y, width, height) { if (x == null) { this._bounds = null; } this._bounds = (this._bounds || new Rectangle()).setValues(x, y, width, height); }; _proto.clone = function clone() { return this._cloneProps(new DisplayObject()); }; _proto.toString = function toString() { return "[" + this.constructor.name + (this.name ? " (name=" + this.name + ")" : "") + "]"; }; _proto._cloneProps = function _cloneProps(o) { o.alpha = this.alpha; o.mouseEnabled = this.mouseEnabled; o.tickEnabled = this.tickEnabled; o.name = this.name; o.regX = this.regX; o.regY = this.regY; o.rotation = this.rotation; o.scaleX = this.scaleX; o.scaleY = this.scaleY; o.shadow = this.shadow; o.skewX = this.skewX; o.skewY = this.skewY; o.visible = this.visible; o.x = this.x; o.y = this.y; o.compositeOperation = this.compositeOperation; o.snapToPixel = this.snapToPixel; o.filters = this.filters == null ? null : this.filters.slice(0); o.mask = this.mask; o.hitArea = this.hitArea; o.cursor = this.cursor; o._bounds = this._bounds; return o; }; _proto._applyShadow = function _applyShadow(ctx, shadow) { if (shadow === void 0) { shadow = Shadow.identity; } shadow = shadow; ctx.shadowColor = shadow.color; ctx.shadowOffsetX = shadow.offsetX; ctx.shadowOffsetY = shadow.offsetY; ctx.shadowBlur = shadow.blur; }; _proto._tick = function _tick(evtObj) { var ls = this._listeners; if (ls && ls["tick"]) { evtObj.target = null; evtObj.propagationStopped = evtObj.immediatePropagationStopped = false; this.dispatchEvent(evtObj); } }; _proto._testHit = function _testHit(ctx) { try { return ctx.getImageData(0, 0, 1, 1).data[3] > 1; } catch (e) { if (!DisplayObject.suppressCrossDomainErrors) { throw "An error has occurred. This is most li