diff --git a/CHANGELOG.md b/CHANGELOG.md index e36135e..5d62efe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ * remove relative path resolver for texture image loader * remove cordova/fastcontext loader and build, use web by default * remove _create, _extend functions with js functions -* rename lib to src, make folder structre flat +* rename lib to src, make folder structure flat +* typescript definition +* Stage.internal.* is moved to Stage.* #### v0.8.2 * `render.js` renamed to `loop.js` diff --git a/dist/stage.cjs b/dist/stage.cjs new file mode 100644 index 0000000..d22405c --- /dev/null +++ b/dist/stage.cjs @@ -0,0 +1,2951 @@ +"use strict"; +function getDefaultExportFromCjs(x) { + return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x; +} +/**! + * is + * the definitive JavaScript type testing library + * + * @copyright 2013-2014 Enrico Marino / Jordan Harband + * @license MIT + */ +var objProto = Object.prototype; +var owns = objProto.hasOwnProperty; +var toStr = objProto.toString; +var symbolValueOf; +if (typeof Symbol === "function") { + symbolValueOf = Symbol.prototype.valueOf; +} +var bigIntValueOf; +if (typeof BigInt === "function") { + bigIntValueOf = BigInt.prototype.valueOf; +} +var isActualNaN = function(value) { + return value !== value; +}; +var NON_HOST_TYPES = { + "boolean": 1, + number: 1, + string: 1, + undefined: 1 +}; +var base64Regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/; +var hexRegex = /^[A-Fa-f0-9]+$/; +var is = {}; +is.a = is.type = function(value, type) { + return typeof value === type; +}; +is.defined = function(value) { + return typeof value !== "undefined"; +}; +is.empty = function(value) { + var type = toStr.call(value); + var key; + if (type === "[object Array]" || type === "[object Arguments]" || type === "[object String]") { + return value.length === 0; + } + if (type === "[object Object]") { + for (key in value) { + if (owns.call(value, key)) { + return false; + } + } + return true; + } + return !value; +}; +is.equal = function equal(value, other) { + if (value === other) { + return true; + } + var type = toStr.call(value); + var key; + if (type !== toStr.call(other)) { + return false; + } + if (type === "[object Object]") { + for (key in value) { + if (!is.equal(value[key], other[key]) || !(key in other)) { + return false; + } + } + for (key in other) { + if (!is.equal(value[key], other[key]) || !(key in value)) { + return false; + } + } + return true; + } + if (type === "[object Array]") { + key = value.length; + if (key !== other.length) { + return false; + } + while (key--) { + if (!is.equal(value[key], other[key])) { + return false; + } + } + return true; + } + if (type === "[object Function]") { + return value.prototype === other.prototype; + } + if (type === "[object Date]") { + return value.getTime() === other.getTime(); + } + return false; +}; +is.hosted = function(value, host) { + var type = typeof host[value]; + return type === "object" ? !!host[value] : !NON_HOST_TYPES[type]; +}; +is.instance = is["instanceof"] = function(value, constructor) { + return value instanceof constructor; +}; +is.nil = is["null"] = function(value) { + return value === null; +}; +is.undef = is.undefined = function(value) { + return typeof value === "undefined"; +}; +is.args = is.arguments = function(value) { + var isStandardArguments = toStr.call(value) === "[object Arguments]"; + var isOldArguments = !is.array(value) && is.arraylike(value) && is.object(value) && is.fn(value.callee); + return isStandardArguments || isOldArguments; +}; +is.array = Array.isArray || function(value) { + return toStr.call(value) === "[object Array]"; +}; +is.args.empty = function(value) { + return is.args(value) && value.length === 0; +}; +is.array.empty = function(value) { + return is.array(value) && value.length === 0; +}; +is.arraylike = function(value) { + return !!value && !is.bool(value) && owns.call(value, "length") && isFinite(value.length) && is.number(value.length) && value.length >= 0; +}; +is.bool = is["boolean"] = function(value) { + return toStr.call(value) === "[object Boolean]"; +}; +is["false"] = function(value) { + return is.bool(value) && Boolean(Number(value)) === false; +}; +is["true"] = function(value) { + return is.bool(value) && Boolean(Number(value)) === true; +}; +is.date = function(value) { + return toStr.call(value) === "[object Date]"; +}; +is.date.valid = function(value) { + return is.date(value) && !isNaN(Number(value)); +}; +is.element = function(value) { + return value !== void 0 && typeof HTMLElement !== "undefined" && value instanceof HTMLElement && value.nodeType === 1; +}; +is.error = function(value) { + return toStr.call(value) === "[object Error]"; +}; +is.fn = is["function"] = function(value) { + var isAlert = typeof window !== "undefined" && value === window.alert; + if (isAlert) { + return true; + } + var str = toStr.call(value); + return str === "[object Function]" || str === "[object GeneratorFunction]" || str === "[object AsyncFunction]"; +}; +is.number = function(value) { + return toStr.call(value) === "[object Number]"; +}; +is.infinite = function(value) { + return value === Infinity || value === -Infinity; +}; +is.decimal = function(value) { + return is.number(value) && !isActualNaN(value) && !is.infinite(value) && value % 1 !== 0; +}; +is.divisibleBy = function(value, n) { + var isDividendInfinite = is.infinite(value); + var isDivisorInfinite = is.infinite(n); + var isNonZeroNumber = is.number(value) && !isActualNaN(value) && is.number(n) && !isActualNaN(n) && n !== 0; + return isDividendInfinite || isDivisorInfinite || isNonZeroNumber && value % n === 0; +}; +is.integer = is["int"] = function(value) { + return is.number(value) && !isActualNaN(value) && value % 1 === 0; +}; +is.maximum = function(value, others) { + if (isActualNaN(value)) { + throw new TypeError("NaN is not a valid value"); + } else if (!is.arraylike(others)) { + throw new TypeError("second argument must be array-like"); + } + var len = others.length; + while (--len >= 0) { + if (value < others[len]) { + return false; + } + } + return true; +}; +is.minimum = function(value, others) { + if (isActualNaN(value)) { + throw new TypeError("NaN is not a valid value"); + } else if (!is.arraylike(others)) { + throw new TypeError("second argument must be array-like"); + } + var len = others.length; + while (--len >= 0) { + if (value > others[len]) { + return false; + } + } + return true; +}; +is.nan = function(value) { + return !is.number(value) || value !== value; +}; +is.even = function(value) { + return is.infinite(value) || is.number(value) && value === value && value % 2 === 0; +}; +is.odd = function(value) { + return is.infinite(value) || is.number(value) && value === value && value % 2 !== 0; +}; +is.ge = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { + throw new TypeError("NaN is not a valid value"); + } + return !is.infinite(value) && !is.infinite(other) && value >= other; +}; +is.gt = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { + throw new TypeError("NaN is not a valid value"); + } + return !is.infinite(value) && !is.infinite(other) && value > other; +}; +is.le = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { + throw new TypeError("NaN is not a valid value"); + } + return !is.infinite(value) && !is.infinite(other) && value <= other; +}; +is.lt = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { + throw new TypeError("NaN is not a valid value"); + } + return !is.infinite(value) && !is.infinite(other) && value < other; +}; +is.within = function(value, start, finish) { + if (isActualNaN(value) || isActualNaN(start) || isActualNaN(finish)) { + throw new TypeError("NaN is not a valid value"); + } else if (!is.number(value) || !is.number(start) || !is.number(finish)) { + throw new TypeError("all arguments must be numbers"); + } + var isAnyInfinite = is.infinite(value) || is.infinite(start) || is.infinite(finish); + return isAnyInfinite || value >= start && value <= finish; +}; +is.object = function(value) { + return toStr.call(value) === "[object Object]"; +}; +is.primitive = function isPrimitive(value) { + if (!value) { + return true; + } + if (typeof value === "object" || is.object(value) || is.fn(value) || is.array(value)) { + return false; + } + return true; +}; +is.hash = function(value) { + return is.object(value) && value.constructor === Object && !value.nodeType && !value.setInterval; +}; +is.regexp = function(value) { + return toStr.call(value) === "[object RegExp]"; +}; +is.string = function(value) { + return toStr.call(value) === "[object String]"; +}; +is.base64 = function(value) { + return is.string(value) && (!value.length || base64Regex.test(value)); +}; +is.hex = function(value) { + return is.string(value) && (!value.length || hexRegex.test(value)); +}; +is.symbol = function(value) { + return typeof Symbol === "function" && toStr.call(value) === "[object Symbol]" && typeof symbolValueOf.call(value) === "symbol"; +}; +is.bigint = function(value) { + return typeof BigInt === "function" && toStr.call(value) === "[object BigInt]" && typeof bigIntValueOf.call(value) === "bigint"; +}; +var is_1 = is; +const is$1 = /* @__PURE__ */ getDefaultExportFromCjs(is_1); +const stats = {}; +function _await() { + var count = 0; + function fork(fn, n) { + count += n = typeof n === "number" && n >= 1 ? n : 1; + return function() { + fn && fn.apply(this, arguments); + if (n > 0) { + n--, count--, call(); + } + }; + } + var then = []; + function call() { + if (count === 0) { + while (then.length) { + setTimeout(then.shift(), 0); + } + } + } + fork.then = function(fn) { + if (count === 0) { + setTimeout(fn, 0); + } else { + then.push(fn); + } + }; + return fork; +} +stats.create = 0; +function Stage(arg) { + if (!(this instanceof Stage)) { + if (is$1.fn(arg)) { + return Stage.app.apply(Stage, arguments); + } else if (is$1.object(arg)) { + return Stage.atlas.apply(Stage, arguments); + } else { + return arg; + } + } + stats.create++; + for (var i = 0; i < _init.length; i++) { + _init[i].call(this); + } +} +var _init = []; +Stage._init = function(fn) { + _init.push(fn); +}; +var _load = []; +Stage._load = function(fn) { + _load.push(fn); +}; +var _config = {}; +Stage.config = function() { + if (arguments.length === 1 && is$1.string(arguments[0])) { + return _config[arguments[0]]; + } + if (arguments.length === 1 && is$1.object(arguments[0])) { + Object.assign(_config, arguments[0]); + } + if (arguments.length === 2 && is$1.string(arguments[0])) { + _config[arguments[1]]; + } +}; +var _app_queue = []; +var _stages = []; +var _loaded = false; +var _paused = false; +Stage.app = function(app, opts) { + if (!_loaded) { + _app_queue.push(arguments); + return; + } + console.log("Creating app..."); + var loader = Stage.config("app-loader"); + loader(function(stage, canvas) { + console.log("Initing app..."); + for (var i = 0; i < _load.length; i++) { + _load[i].call(this, stage, canvas); + } + app(stage, canvas); + _stages.push(stage); + console.log("Starting app..."); + stage.start(); + }, opts); +}; +var loading = _await(); +Stage.preload = function(load) { + if (typeof load !== "function") { + return; + } + load(loading()); +}; +Stage.start = function(config) { + console.log("Starting..."); + Stage.config(config); + loading.then(function() { + console.log("Loading apps..."); + _loaded = true; + while (_app_queue.length) { + var args = _app_queue.shift(); + Stage.app.apply(Stage, args); + } + }); +}; +Stage.pause = function() { + if (!_paused) { + _paused = true; + for (var i = _stages.length - 1; i >= 0; i--) { + _stages[i].pause(); + } + } +}; +Stage.resume = function() { + if (_paused) { + _paused = false; + for (var i = _stages.length - 1; i >= 0; i--) { + _stages[i].resume(); + } + } +}; +Stage.create = function() { + return new Stage(); +}; +function Matrix(a, b, c, d, e, f) { + this.reset(a, b, c, d, e, f); +} +Matrix.prototype.toString = function() { + return "[" + this.a + ", " + this.b + ", " + this.c + ", " + this.d + ", " + this.e + ", " + this.f + "]"; +}; +Matrix.prototype.clone = function() { + return new Matrix(this.a, this.b, this.c, this.d, this.e, this.f); +}; +Matrix.prototype.reset = function(a, b, c, d, e, f) { + this._dirty = true; + if (typeof a === "object") { + this.a = a.a, this.d = a.d; + this.b = a.b, this.c = a.c; + this.e = a.e, this.f = a.f; + } else { + this.a = a || 1, this.d = d || 1; + this.b = b || 0, this.c = c || 0; + this.e = e || 0, this.f = f || 0; + } + return this; +}; +Matrix.prototype.identity = function() { + this._dirty = true; + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.e = 0; + this.f = 0; + return this; +}; +Matrix.prototype.rotate = function(angle) { + if (!angle) { + return this; + } + this._dirty = true; + var u = angle ? Math.cos(angle) : 1; + var v = angle ? Math.sin(angle) : 0; + var a = u * this.a - v * this.b; + var b = u * this.b + v * this.a; + var c = u * this.c - v * this.d; + var d = u * this.d + v * this.c; + var e = u * this.e - v * this.f; + var f = u * this.f + v * this.e; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + return this; +}; +Matrix.prototype.translate = function(x, y) { + if (!x && !y) { + return this; + } + this._dirty = true; + this.e += x; + this.f += y; + return this; +}; +Matrix.prototype.scale = function(x, y) { + if (!(x - 1) && !(y - 1)) { + return this; + } + this._dirty = true; + this.a *= x; + this.b *= y; + this.c *= x; + this.d *= y; + this.e *= x; + this.f *= y; + return this; +}; +Matrix.prototype.skew = function(x, y) { + if (!x && !y) { + return this; + } + this._dirty = true; + var a = this.a + this.b * x; + var b = this.b + this.a * y; + var c = this.c + this.d * x; + var d = this.d + this.c * y; + var e = this.e + this.f * x; + var f = this.f + this.e * y; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + return this; +}; +Matrix.prototype.concat = function(m) { + this._dirty = true; + var n = this; + var a = n.a * m.a + n.b * m.c; + var b = n.b * m.d + n.a * m.b; + var c = n.c * m.a + n.d * m.c; + var d = n.d * m.d + n.c * m.b; + var e = n.e * m.a + m.e + n.f * m.c; + var f = n.f * m.d + m.f + n.e * m.b; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + return this; +}; +Matrix.prototype.inverse = Matrix.prototype.reverse = function() { + if (this._dirty) { + this._dirty = false; + this.inversed = this.inversed || new Matrix(); + var z = this.a * this.d - this.b * this.c; + this.inversed.a = this.d / z; + this.inversed.b = -this.b / z; + this.inversed.c = -this.c / z; + this.inversed.d = this.a / z; + this.inversed.e = (this.c * this.f - this.e * this.d) / z; + this.inversed.f = (this.e * this.b - this.a * this.f) / z; + } + return this.inversed; +}; +Matrix.prototype.map = function(p, q) { + q = q || {}; + q.x = this.a * p.x + this.c * p.y + this.e; + q.y = this.b * p.x + this.d * p.y + this.f; + return q; +}; +Matrix.prototype.mapX = function(x, y) { + if (typeof x === "object") + y = x.y, x = x.x; + return this.a * x + this.c * y + this.e; +}; +Matrix.prototype.mapY = function(x, y) { + if (typeof x === "object") + y = x.y, x = x.x; + return this.b * x + this.d * y + this.f; +}; +var native = Math; +const math = Object.create(Math); +math.random = function(min, max) { + if (typeof min === "undefined") { + max = 1, min = 0; + } else if (typeof max === "undefined") { + max = min, min = 0; + } + return min == max ? min : native.random() * (max - min) + min; +}; +math.rotate = function(num, min, max) { + if (typeof min === "undefined") { + max = 1, min = 0; + } else if (typeof max === "undefined") { + max = min, min = 0; + } + if (max > min) { + num = (num - min) % (max - min); + return num + (num < 0 ? max : min); + } else { + num = (num - max) % (min - max); + return num + (num <= 0 ? min : max); + } +}; +math.limit = function(num, min, max) { + if (num < min) { + return min; + } else if (num > max) { + return max; + } else { + return num; + } +}; +math.length = function(x, y) { + return native.sqrt(x * x + y * y); +}; +function Texture(image, ratio) { + if (typeof image === "object") { + this.src(image, ratio); + } +} +Texture.prototype.pipe = function() { + return new Texture(this); +}; +Texture.prototype.src = function(x, y, w, h) { + if (typeof x === "object") { + var image = x, ratio = y || 1; + this._image = image; + this._sx = this._dx = 0; + this._sy = this._dy = 0; + this._sw = this._dw = image.width / ratio; + this._sh = this._dh = image.height / ratio; + this.width = image.width / ratio; + this.height = image.height / ratio; + this.ratio = ratio; + } else { + if (typeof w === "undefined") { + w = x, h = y; + } else { + this._sx = x, this._sy = y; + } + this._sw = this._dw = w; + this._sh = this._dh = h; + this.width = w; + this.height = h; + } + return this; +}; +Texture.prototype.dest = function(x, y, w, h) { + this._dx = x, this._dy = y; + this._dx = x, this._dy = y; + if (typeof w !== "undefined") { + this._dw = w, this._dh = h; + this.width = w, this.height = h; + } + return this; +}; +Texture.prototype.draw = function(context, x1, y1, x2, y2, x3, y3, x4, y4) { + var image = this._image; + if (image === null || typeof image !== "object") { + return; + } + var sx = this._sx, sy = this._sy; + var sw = this._sw, sh = this._sh; + var dx = this._dx, dy = this._dy; + var dw = this._dw, dh = this._dh; + if (typeof x3 !== "undefined") { + x1 = math.limit(x1, 0, this._sw), x2 = math.limit(x2, 0, this._sw - x1); + y1 = math.limit(y1, 0, this._sh), y2 = math.limit(y2, 0, this._sh - y1); + sx += x1, sy += y1, sw = x2, sh = y2; + dx = x3, dy = y3, dw = x4, dh = y4; + } else if (typeof x2 !== "undefined") { + dx = x1, dy = y1, dw = x2, dh = y2; + } else if (typeof x1 !== "undefined") { + dw = x1, dh = y1; + } + var ratio = this.ratio || 1; + sx *= ratio, sy *= ratio, sw *= ratio, sh *= ratio; + try { + if (typeof image.draw === "function") { + image.draw(context, sx, sy, sw, sh, dx, dy, dw, dh); + } else { + stats.draw++; + context.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh); + } + } catch (ex) { + if (!image._draw_failed) { + console.log("Unable to draw: ", image); + console.log(ex); + image._draw_failed = true; + } + } +}; +var _atlases_map = {}; +var _atlases_arr = []; +Stage.atlas = function(def) { + var atlas = is$1.fn(def.draw) ? def : new Atlas(def); + if (def.name) { + _atlases_map[def.name] = atlas; + } + _atlases_arr.push(atlas); + deprecated(def, "imagePath"); + deprecated(def, "imageRatio"); + var url = def.imagePath; + var ratio = def.imageRatio || 1; + if (is$1.string(def.image)) { + url = def.image; + } else if (is$1.hash(def.image)) { + url = def.image.src || def.image.url; + ratio = def.image.ratio || ratio; + } + url && Stage.preload(function(done) { + console.log("Loading atlas: " + url); + var imageloader = Stage.config("image-loader"); + imageloader(url, function(image) { + console.log("Image loaded: " + url); + atlas.src(image, ratio); + done(); + }, function(err) { + console.log("Error loading atlas: " + url, err); + done(); + }); + }); + return atlas; +}; +Atlas._super = Texture; +Atlas.prototype = Object.create(Atlas._super.prototype); +function Atlas(def) { + Atlas._super.call(this); + var atlas = this; + deprecated(def, "filter"); + deprecated(def, "cutouts"); + deprecated(def, "sprites"); + deprecated(def, "factory"); + var map = def.map || def.filter; + var ppu = def.ppu || def.ratio || 1; + var trim = def.trim || 0; + var textures = def.textures; + var factory = def.factory; + var cutouts = def.cutouts || def.sprites; + function make(def2) { + if (!def2 || is$1.fn(def2.draw)) { + return def2; + } + def2 = Object.assign({}, def2); + if (is$1.fn(map)) { + def2 = map(def2); + } + if (ppu != 1) { + def2.x *= ppu, def2.y *= ppu; + def2.width *= ppu, def2.height *= ppu; + def2.top *= ppu, def2.bottom *= ppu; + def2.left *= ppu, def2.right *= ppu; + } + if (trim != 0) { + def2.x += trim, def2.y += trim; + def2.width -= 2 * trim, def2.height -= 2 * trim; + def2.top -= trim, def2.bottom -= trim; + def2.left -= trim, def2.right -= trim; + } + var texture = atlas.pipe(); + texture.top = def2.top, texture.bottom = def2.bottom; + texture.left = def2.left, texture.right = def2.right; + texture.src(def2.x, def2.y, def2.width, def2.height); + return texture; + } + function find(query) { + if (textures) { + if (is$1.fn(textures)) { + return textures(query); + } else if (is$1.hash(textures)) { + return textures[query]; + } + } + if (cutouts) { + var result = null, n = 0; + for (var i = 0; i < cutouts.length; i++) { + if (string.startsWith(cutouts[i].name, query)) { + if (n === 0) { + result = cutouts[i]; + } else if (n === 1) { + result = [result, cutouts[i]]; + } else { + result.push(cutouts[i]); + } + n++; + } + } + if (n === 0 && is$1.fn(factory)) { + result = function(subquery) { + return factory(query + (subquery ? subquery : "")); + }; + } + return result; + } + } + this.select = function(query) { + if (!query) { + return new Selection(this.pipe()); + } + var found = find(query); + if (found) { + return new Selection(found, find, make); + } + }; +} +var nfTexture = new Texture(); +nfTexture.x = nfTexture.y = nfTexture.width = nfTexture.height = 0; +nfTexture.pipe = nfTexture.src = nfTexture.dest = function() { + return this; +}; +nfTexture.draw = function() { +}; +var nfSelection = new Selection(nfTexture); +function Selection(result, find, make) { + function link(result2, subquery) { + if (!result2) { + return nfTexture; + } else if (is$1.fn(result2.draw)) { + return result2; + } else if (is$1.hash(result2) && is$1.number(result2.width) && is$1.number(result2.height) && is$1.fn(make)) { + return make(result2); + } else if (is$1.hash(result2) && is$1.defined(subquery)) { + return link(result2[subquery]); + } else if (is$1.fn(result2)) { + return link(result2(subquery)); + } else if (is$1.array(result2)) { + return link(result2[0]); + } else if (is$1.string(result2) && is$1.fn(find)) { + return link(find(result2)); + } + } + this.one = function(subquery) { + return link(result, subquery); + }; + this.array = function(arr) { + var array = is$1.array(arr) ? arr : []; + if (is$1.array(result)) { + for (var i = 0; i < result.length; i++) { + array[i] = link(result[i]); + } + } else { + array[0] = link(result); + } + return array; + }; +} +Stage.texture = function(query) { + if (!is$1.string(query)) { + return new Selection(query); + } + var result = null, atlas, i; + if ((i = query.indexOf(":")) > 0 && query.length > i + 1) { + atlas = _atlases_map[query.slice(0, i)]; + result = atlas && atlas.select(query.slice(i + 1)); + } + if (!result && (atlas = _atlases_map[query])) { + result = atlas.select(); + } + for (i = 0; !result && i < _atlases_arr.length; i++) { + result = _atlases_arr[i].select(query); + } + if (!result) { + console.error("Texture not found: " + query); + result = nfSelection; + } + return result; +}; +function deprecated(hash, name, msg) { + if (name in hash) + console.log(msg ? msg.replace("%name", name) : "'" + name + "' field of texture atlas is deprecated."); +} +var iid$1 = 0; +Stage.prototype._label = ""; +Stage.prototype._visible = true; +Stage.prototype._parent = null; +Stage.prototype._next = null; +Stage.prototype._prev = null; +Stage.prototype._first = null; +Stage.prototype._last = null; +Stage.prototype._attrs = null; +Stage.prototype._flags = null; +Stage.prototype.toString = function() { + return "[" + this._label + "]"; +}; +Stage.prototype.id = function(id) { + return this.label(id); +}; +Stage.prototype.label = function(label) { + if (typeof label === "undefined") { + return this._label; + } + this._label = label; + return this; +}; +Stage.prototype.attr = function(name, value) { + if (typeof value === "undefined") { + return this._attrs !== null ? this._attrs[name] : void 0; + } + (this._attrs !== null ? this._attrs : this._attrs = {})[name] = value; + return this; +}; +Stage.prototype.visible = function(visible) { + if (typeof visible === "undefined") { + return this._visible; + } + this._visible = visible; + this._parent && (this._parent._ts_children = ++iid$1); + this._ts_pin = ++iid$1; + this.touch(); + return this; +}; +Stage.prototype.hide = function() { + return this.visible(false); +}; +Stage.prototype.show = function() { + return this.visible(true); +}; +Stage.prototype.parent = function() { + return this._parent; +}; +Stage.prototype.next = function(visible) { + var next = this._next; + while (next && visible && !next._visible) { + next = next._next; + } + return next; +}; +Stage.prototype.prev = function(visible) { + var prev = this._prev; + while (prev && visible && !prev._visible) { + prev = prev._prev; + } + return prev; +}; +Stage.prototype.first = function(visible) { + var next = this._first; + while (next && visible && !next._visible) { + next = next._next; + } + return next; +}; +Stage.prototype.last = function(visible) { + var prev = this._last; + while (prev && visible && !prev._visible) { + prev = prev._prev; + } + return prev; +}; +Stage.prototype.visit = function(visitor, data) { + var reverse = visitor.reverse; + var visible = visitor.visible; + if (visitor.start && visitor.start(this, data)) { + return; + } + var child, next = reverse ? this.last(visible) : this.first(visible); + while (child = next) { + next = reverse ? child.prev(visible) : child.next(visible); + if (child.visit(visitor, data)) { + return true; + } + } + return visitor.end && visitor.end(this, data); +}; +Stage.prototype.append = function(child, more) { + if (is$1.array(child)) + for (var i = 0; i < child.length; i++) + append(this, child[i]); + else if (typeof more !== "undefined") + for (var i = 0; i < arguments.length; i++) + append(this, arguments[i]); + else if (typeof child !== "undefined") + append(this, child); + return this; +}; +Stage.prototype.prepend = function(child, more) { + if (is$1.array(child)) + for (var i = child.length - 1; i >= 0; i--) + prepend(this, child[i]); + else if (typeof more !== "undefined") + for (var i = arguments.length - 1; i >= 0; i--) + prepend(this, arguments[i]); + else if (typeof child !== "undefined") + prepend(this, child); + return this; +}; +Stage.prototype.appendTo = function(parent) { + append(parent, this); + return this; +}; +Stage.prototype.prependTo = function(parent) { + prepend(parent, this); + return this; +}; +Stage.prototype.insertNext = function(sibling, more) { + if (is$1.array(sibling)) + for (var i = 0; i < sibling.length; i++) + insertAfter(sibling[i], this); + else if (typeof more !== "undefined") + for (var i = 0; i < arguments.length; i++) + insertAfter(arguments[i], this); + else if (typeof sibling !== "undefined") + insertAfter(sibling, this); + return this; +}; +Stage.prototype.insertPrev = function(sibling, more) { + if (is$1.array(sibling)) + for (var i = sibling.length - 1; i >= 0; i--) + insertBefore(sibling[i], this); + else if (typeof more !== "undefined") + for (var i = arguments.length - 1; i >= 0; i--) + insertBefore(arguments[i], this); + else if (typeof sibling !== "undefined") + insertBefore(sibling, this); + return this; +}; +Stage.prototype.insertAfter = function(prev) { + insertAfter(this, prev); + return this; +}; +Stage.prototype.insertBefore = function(next) { + insertBefore(this, next); + return this; +}; +function append(parent, child) { + _ensure(child); + _ensure(parent); + child.remove(); + if (parent._last) { + parent._last._next = child; + child._prev = parent._last; + } + child._parent = parent; + parent._last = child; + if (!parent._first) { + parent._first = child; + } + child._parent._flag(child, true); + child._ts_parent = ++iid$1; + parent._ts_children = ++iid$1; + parent.touch(); +} +function prepend(parent, child) { + _ensure(child); + _ensure(parent); + child.remove(); + if (parent._first) { + parent._first._prev = child; + child._next = parent._first; + } + child._parent = parent; + parent._first = child; + if (!parent._last) { + parent._last = child; + } + child._parent._flag(child, true); + child._ts_parent = ++iid$1; + parent._ts_children = ++iid$1; + parent.touch(); +} +function insertBefore(self, next) { + _ensure(self); + _ensure(next); + self.remove(); + var parent = next._parent; + var prev = next._prev; + next._prev = self; + prev && (prev._next = self) || parent && (parent._first = self); + self._parent = parent; + self._prev = prev; + self._next = next; + self._parent._flag(self, true); + self._ts_parent = ++iid$1; + self.touch(); +} +function insertAfter(self, prev) { + _ensure(self); + _ensure(prev); + self.remove(); + var parent = prev._parent; + var next = prev._next; + prev._next = self; + next && (next._prev = self) || parent && (parent._last = self); + self._parent = parent; + self._prev = prev; + self._next = next; + self._parent._flag(self, true); + self._ts_parent = ++iid$1; + self.touch(); +} +Stage.prototype.remove = function(child, more) { + if (typeof child !== "undefined") { + if (is$1.array(child)) { + for (var i = 0; i < child.length; i++) + _ensure(child[i]).remove(); + } else if (typeof more !== "undefined") { + for (var i = 0; i < arguments.length; i++) + _ensure(arguments[i]).remove(); + } else { + _ensure(child).remove(); + } + return this; + } + if (this._prev) { + this._prev._next = this._next; + } + if (this._next) { + this._next._prev = this._prev; + } + if (this._parent) { + if (this._parent._first === this) { + this._parent._first = this._next; + } + if (this._parent._last === this) { + this._parent._last = this._prev; + } + this._parent._flag(this, false); + this._parent._ts_children = ++iid$1; + this._parent.touch(); + } + this._prev = this._next = this._parent = null; + this._ts_parent = ++iid$1; + return this; +}; +Stage.prototype.empty = function() { + var child, next = this._first; + while (child = next) { + next = child._next; + child._prev = child._next = child._parent = null; + this._flag(child, false); + } + this._first = this._last = null; + this._ts_children = ++iid$1; + this.touch(); + return this; +}; +Stage.prototype.touch = function() { + this._ts_touch = ++iid$1; + this._parent && this._parent.touch(); + return this; +}; +Stage.prototype._flag = function(obj, name) { + if (typeof name === "undefined") { + return this._flags !== null && this._flags[obj] || 0; + } + if (typeof obj === "string") { + if (name) { + this._flags = this._flags || {}; + if (!this._flags[obj] && this._parent) { + this._parent._flag(obj, true); + } + this._flags[obj] = (this._flags[obj] || 0) + 1; + } else if (this._flags && this._flags[obj] > 0) { + if (this._flags[obj] == 1 && this._parent) { + this._parent._flag(obj, false); + } + this._flags[obj] = this._flags[obj] - 1; + } + } + if (typeof obj === "object") { + if (obj._flags) { + for (var type in obj._flags) { + if (obj._flags[type] > 0) { + this._flag(type, name); + } + } + } + } + return this; +}; +Stage.prototype.hitTest = function(hit) { + var width = this._pin._width; + var height = this._pin._height; + return hit.x >= 0 && hit.x <= width && hit.y >= 0 && hit.y <= height; +}; +function _ensure(obj) { + if (obj && obj instanceof Stage) { + return obj; + } + throw "Invalid node: " + obj; +} +function listenable(prototype, callback) { + prototype._listeners = null; + prototype.on = prototype.listen = function(types, listener) { + if (!types || !types.length || typeof listener !== "function") { + return this; + } + if (this._listeners === null) { + this._listeners = {}; + } + var isarray = typeof types !== "string" && typeof types.join === "function"; + if (types = (isarray ? types.join(" ") : types).match(/\S+/g)) { + for (var i = 0; i < types.length; i++) { + var type = types[i]; + this._listeners[type] = this._listeners[type] || []; + this._listeners[type].push(listener); + if (typeof callback === "function") { + callback(this, type, true); + } + } + } + return this; + }; + prototype.off = function(types, listener) { + if (!types || !types.length || typeof listener !== "function") { + return this; + } + if (this._listeners === null) { + return this; + } + var isarray = typeof types !== "string" && typeof types.join === "function"; + if (types = (isarray ? types.join(" ") : types).match(/\S+/g)) { + for (var i = 0; i < types.length; i++) { + var type = types[i], all = this._listeners[type], index; + if (all && (index = all.indexOf(listener)) >= 0) { + all.splice(index, 1); + if (!all.length) { + delete this._listeners[type]; + } + if (typeof callback === "function") { + callback(this, type, false); + } + } + } + } + return this; + }; + prototype.listeners = function(type) { + return this._listeners && this._listeners[type]; + }; + prototype.publish = function(name, args) { + var listeners = this.listeners(name); + if (!listeners || !listeners.length) { + return 0; + } + for (var l = 0; l < listeners.length; l++) { + listeners[l].apply(this, args); + } + return listeners.length; + }; + prototype.trigger = function(name, args) { + this.publish(name, args); + return this; + }; +} +var iid = 0; +Stage._init(function() { + this._pin = new Pin(this); +}); +Stage.prototype.matrix = function(relative) { + if (relative === true) { + return this._pin.relativeMatrix(); + } + return this._pin.absoluteMatrix(); +}; +Stage.prototype.pin = function(a, b) { + if (typeof a === "object") { + this._pin.set(a); + return this; + } else if (typeof a === "string") { + if (typeof b === "undefined") { + return this._pin.get(a); + } else { + this._pin.set(a, b); + return this; + } + } else if (typeof a === "undefined") { + return this._pin; + } +}; +function Pin(owner) { + this._owner = owner; + this._parent = null; + this._relativeMatrix = new Matrix(); + this._absoluteMatrix = new Matrix(); + this.reset(); +} +Pin.prototype.reset = function() { + this._textureAlpha = 1; + this._alpha = 1; + this._width = 0; + this._height = 0; + this._scaleX = 1; + this._scaleY = 1; + this._skewX = 0; + this._skewY = 0; + this._rotation = 0; + this._pivoted = false; + this._pivotX = null; + this._pivotY = null; + this._handled = false; + this._handleX = 0; + this._handleY = 0; + this._aligned = false; + this._alignX = 0; + this._alignY = 0; + this._offsetX = 0; + this._offsetY = 0; + this._boxX = 0; + this._boxY = 0; + this._boxWidth = this._width; + this._boxHeight = this._height; + this._ts_translate = ++iid; + this._ts_transform = ++iid; + this._ts_matrix = ++iid; +}; +Pin.prototype._update = function() { + this._parent = this._owner._parent && this._owner._parent._pin; + if (this._handled && this._mo_handle != this._ts_transform) { + this._mo_handle = this._ts_transform; + this._ts_translate = ++iid; + } + if (this._aligned && this._parent && this._mo_align != this._parent._ts_transform) { + this._mo_align = this._parent._ts_transform; + this._ts_translate = ++iid; + } + return this; +}; +Pin.prototype.toString = function() { + return this._owner + " (" + (this._parent ? this._parent._owner : null) + ")"; +}; +Pin.prototype.absoluteMatrix = function() { + this._update(); + var ts = Math.max( + this._ts_transform, + this._ts_translate, + this._parent ? this._parent._ts_matrix : 0 + ); + if (this._mo_abs == ts) { + return this._absoluteMatrix; + } + this._mo_abs = ts; + var abs2 = this._absoluteMatrix; + abs2.reset(this.relativeMatrix()); + this._parent && abs2.concat(this._parent._absoluteMatrix); + this._ts_matrix = ++iid; + return abs2; +}; +Pin.prototype.relativeMatrix = function() { + this._update(); + var ts = Math.max( + this._ts_transform, + this._ts_translate, + this._parent ? this._parent._ts_transform : 0 + ); + if (this._mo_rel == ts) { + return this._relativeMatrix; + } + this._mo_rel = ts; + var rel2 = this._relativeMatrix; + rel2.identity(); + if (this._pivoted) { + rel2.translate(-this._pivotX * this._width, -this._pivotY * this._height); + } + rel2.scale(this._scaleX, this._scaleY); + rel2.skew(this._skewX, this._skewY); + rel2.rotate(this._rotation); + if (this._pivoted) { + rel2.translate(this._pivotX * this._width, this._pivotY * this._height); + } + if (this._pivoted) { + this._boxX = 0; + this._boxY = 0; + this._boxWidth = this._width; + this._boxHeight = this._height; + } else { + var p, q; + if (rel2.a > 0 && rel2.c > 0 || rel2.a < 0 && rel2.c < 0) { + p = 0, q = rel2.a * this._width + rel2.c * this._height; + } else { + p = rel2.a * this._width, q = rel2.c * this._height; + } + if (p > q) { + this._boxX = q; + this._boxWidth = p - q; + } else { + this._boxX = p; + this._boxWidth = q - p; + } + if (rel2.b > 0 && rel2.d > 0 || rel2.b < 0 && rel2.d < 0) { + p = 0, q = rel2.b * this._width + rel2.d * this._height; + } else { + p = rel2.b * this._width, q = rel2.d * this._height; + } + if (p > q) { + this._boxY = q; + this._boxHeight = p - q; + } else { + this._boxY = p; + this._boxHeight = q - p; + } + } + this._x = this._offsetX; + this._y = this._offsetY; + this._x -= this._boxX + this._handleX * this._boxWidth; + this._y -= this._boxY + this._handleY * this._boxHeight; + if (this._aligned && this._parent) { + this._parent.relativeMatrix(); + this._x += this._alignX * this._parent._width; + this._y += this._alignY * this._parent._height; + } + rel2.translate(this._x, this._y); + return this._relativeMatrix; +}; +Pin.prototype.get = function(key) { + if (typeof getters[key] === "function") { + return getters[key](this); + } +}; +Pin.prototype.set = function(a, b) { + if (typeof a === "string") { + if (typeof setters[a] === "function" && typeof b !== "undefined") { + setters[a](this, b); + } + } else if (typeof a === "object") { + for (b in a) { + if (typeof setters[b] === "function" && typeof a[b] !== "undefined") { + setters[b](this, a[b], a); + } + } + } + if (this._owner) { + this._owner._ts_pin = ++iid; + this._owner.touch(); + } + return this; +}; +var getters = { + alpha: function(pin) { + return pin._alpha; + }, + textureAlpha: function(pin) { + return pin._textureAlpha; + }, + width: function(pin) { + return pin._width; + }, + height: function(pin) { + return pin._height; + }, + boxWidth: function(pin) { + return pin._boxWidth; + }, + boxHeight: function(pin) { + return pin._boxHeight; + }, + // scale : function(pin) { + // }, + scaleX: function(pin) { + return pin._scaleX; + }, + scaleY: function(pin) { + return pin._scaleY; + }, + // skew : function(pin) { + // }, + skewX: function(pin) { + return pin._skewX; + }, + skewY: function(pin) { + return pin._skewY; + }, + rotation: function(pin) { + return pin._rotation; + }, + // pivot : function(pin) { + // }, + pivotX: function(pin) { + return pin._pivotX; + }, + pivotY: function(pin) { + return pin._pivotY; + }, + // offset : function(pin) { + // }, + offsetX: function(pin) { + return pin._offsetX; + }, + offsetY: function(pin) { + return pin._offsetY; + }, + // align : function(pin) { + // }, + alignX: function(pin) { + return pin._alignX; + }, + alignY: function(pin) { + return pin._alignY; + }, + // handle : function(pin) { + // }, + handleX: function(pin) { + return pin._handleX; + }, + handleY: function(pin) { + return pin._handleY; + } +}; +var setters = { + alpha: function(pin, value) { + pin._alpha = value; + }, + textureAlpha: function(pin, value) { + pin._textureAlpha = value; + }, + width: function(pin, value) { + pin._width_ = value; + pin._width = value; + pin._ts_transform = ++iid; + }, + height: function(pin, value) { + pin._height_ = value; + pin._height = value; + pin._ts_transform = ++iid; + }, + scale: function(pin, value) { + pin._scaleX = value; + pin._scaleY = value; + pin._ts_transform = ++iid; + }, + scaleX: function(pin, value) { + pin._scaleX = value; + pin._ts_transform = ++iid; + }, + scaleY: function(pin, value) { + pin._scaleY = value; + pin._ts_transform = ++iid; + }, + skew: function(pin, value) { + pin._skewX = value; + pin._skewY = value; + pin._ts_transform = ++iid; + }, + skewX: function(pin, value) { + pin._skewX = value; + pin._ts_transform = ++iid; + }, + skewY: function(pin, value) { + pin._skewY = value; + pin._ts_transform = ++iid; + }, + rotation: function(pin, value) { + pin._rotation = value; + pin._ts_transform = ++iid; + }, + pivot: function(pin, value) { + pin._pivotX = value; + pin._pivotY = value; + pin._pivoted = true; + pin._ts_transform = ++iid; + }, + pivotX: function(pin, value) { + pin._pivotX = value; + pin._pivoted = true; + pin._ts_transform = ++iid; + }, + pivotY: function(pin, value) { + pin._pivotY = value; + pin._pivoted = true; + pin._ts_transform = ++iid; + }, + offset: function(pin, value) { + pin._offsetX = value; + pin._offsetY = value; + pin._ts_translate = ++iid; + }, + offsetX: function(pin, value) { + pin._offsetX = value; + pin._ts_translate = ++iid; + }, + offsetY: function(pin, value) { + pin._offsetY = value; + pin._ts_translate = ++iid; + }, + align: function(pin, value) { + this.alignX(pin, value); + this.alignY(pin, value); + }, + alignX: function(pin, value) { + pin._alignX = value; + pin._aligned = true; + pin._ts_translate = ++iid; + this.handleX(pin, value); + }, + alignY: function(pin, value) { + pin._alignY = value; + pin._aligned = true; + pin._ts_translate = ++iid; + this.handleY(pin, value); + }, + handle: function(pin, value) { + this.handleX(pin, value); + this.handleY(pin, value); + }, + handleX: function(pin, value) { + pin._handleX = value; + pin._handled = true; + pin._ts_translate = ++iid; + }, + handleY: function(pin, value) { + pin._handleY = value; + pin._handled = true; + pin._ts_translate = ++iid; + }, + resizeMode: function(pin, value, all) { + if (all) { + if (value == "in") { + value = "in-pad"; + } else if (value == "out") { + value = "out-crop"; + } + scaleTo(pin, all.resizeWidth, all.resizeHeight, value); + } + }, + resizeWidth: function(pin, value, all) { + if (!all || !all.resizeMode) { + scaleTo(pin, value, null); + } + }, + resizeHeight: function(pin, value, all) { + if (!all || !all.resizeMode) { + scaleTo(pin, null, value); + } + }, + scaleMode: function(pin, value, all) { + if (all) { + scaleTo(pin, all.scaleWidth, all.scaleHeight, value); + } + }, + scaleWidth: function(pin, value, all) { + if (!all || !all.scaleMode) { + scaleTo(pin, value, null); + } + }, + scaleHeight: function(pin, value, all) { + if (!all || !all.scaleMode) { + scaleTo(pin, null, value); + } + }, + matrix: function(pin, value) { + this.scaleX(pin, value.a); + this.skewX(pin, value.c / value.d); + this.skewY(pin, value.b / value.a); + this.scaleY(pin, value.d); + this.offsetX(pin, value.e); + this.offsetY(pin, value.f); + this.rotation(pin, 0); + } +}; +function scaleTo(pin, width, height, mode) { + var w = typeof width === "number"; + var h = typeof height === "number"; + var m = typeof mode === "string"; + pin._ts_transform = ++iid; + if (w) { + pin._scaleX = width / pin._width_; + pin._width = pin._width_; + } + if (h) { + pin._scaleY = height / pin._height_; + pin._height = pin._height_; + } + if (w && h && m) { + if (mode == "out" || mode == "out-crop") { + pin._scaleX = pin._scaleY = Math.max(pin._scaleX, pin._scaleY); + } else if (mode == "in" || mode == "in-pad") { + pin._scaleX = pin._scaleY = Math.min(pin._scaleX, pin._scaleY); + } + if (mode == "out-crop" || mode == "in-pad") { + pin._width = width / pin._scaleX; + pin._height = height / pin._scaleY; + } + } +} +Stage.prototype.scaleTo = function(a, b, c) { + if (typeof a === "object") + c = b, b = a.y, a = a.x; + scaleTo(this._pin, a, b, c); + return this; +}; +Pin._add_shortcuts = function(Stage2) { + Stage2.prototype.size = function(w, h) { + this.pin("width", w); + this.pin("height", h); + return this; + }; + Stage2.prototype.width = function(w) { + if (typeof w === "undefined") { + return this.pin("width"); + } + this.pin("width", w); + return this; + }; + Stage2.prototype.height = function(h) { + if (typeof h === "undefined") { + return this.pin("height"); + } + this.pin("height", h); + return this; + }; + Stage2.prototype.offset = function(a, b) { + if (typeof a === "object") + b = a.y, a = a.x; + this.pin("offsetX", a); + this.pin("offsetY", b); + return this; + }; + Stage2.prototype.rotate = function(a) { + this.pin("rotation", a); + return this; + }; + Stage2.prototype.skew = function(a, b) { + if (typeof a === "object") + b = a.y, a = a.x; + else if (typeof b === "undefined") + b = a; + this.pin("skewX", a); + this.pin("skewY", b); + return this; + }; + Stage2.prototype.scale = function(a, b) { + if (typeof a === "object") + b = a.y, a = a.x; + else if (typeof b === "undefined") + b = a; + this.pin("scaleX", a); + this.pin("scaleY", b); + return this; + }; + Stage2.prototype.alpha = function(a, ta) { + this.pin("alpha", a); + if (typeof ta !== "undefined") { + this.pin("textureAlpha", ta); + } + return this; + }; +}; +Pin._add_shortcuts(Stage); +Stage.prototype._textures = null; +Stage.prototype._alpha = 1; +Stage.prototype.render = function(context) { + if (!this._visible) { + return; + } + stats.node++; + var m = this.matrix(); + context.setTransform(m.a, m.b, m.c, m.d, m.e, m.f); + this._alpha = this._pin._alpha * (this._parent ? this._parent._alpha : 1); + var alpha = this._pin._textureAlpha * this._alpha; + if (context.globalAlpha != alpha) { + context.globalAlpha = alpha; + } + if (this._textures !== null) { + for (var i = 0, n = this._textures.length; i < n; i++) { + this._textures[i].draw(context); + } + } + if (context.globalAlpha != this._alpha) { + context.globalAlpha = this._alpha; + } + var child, next = this._first; + while (child = next) { + next = child._next; + child.render(context); + } +}; +Stage.prototype._tickBefore = null; +Stage.prototype._tickAfter = null; +Stage.prototype.MAX_ELAPSE = Infinity; +Stage.prototype._tick = function(elapsed, now, last) { + if (!this._visible) { + return; + } + if (elapsed > this.MAX_ELAPSE) { + elapsed = this.MAX_ELAPSE; + } + var ticked = false; + if (this._tickBefore !== null) { + for (var i = 0; i < this._tickBefore.length; i++) { + stats.tick++; + var tickFn = this._tickBefore[i]; + ticked = tickFn.call(this, elapsed, now, last) === true || ticked; + } + } + var child, next = this._first; + while (child = next) { + next = child._next; + if (child._flag("_tick")) { + ticked = child._tick(elapsed, now, last) === true ? true : ticked; + } + } + if (this._tickAfter !== null) { + for (var i = 0; i < this._tickAfter.length; i++) { + stats.tick++; + var tickFn = this._tickAfter[i]; + ticked = tickFn.call(this, elapsed, now, last) === true || ticked; + } + } + return ticked; +}; +Stage.prototype.tick = function(ticker, before) { + if (typeof ticker !== "function") { + return; + } + if (before) { + if (this._tickBefore === null) { + this._tickBefore = []; + } + this._tickBefore.push(ticker); + } else { + if (this._tickAfter === null) { + this._tickAfter = []; + } + this._tickAfter.push(ticker); + } + this._flag("_tick", this._tickAfter !== null && this._tickAfter.length > 0 || this._tickBefore !== null && this._tickBefore.length > 0); +}; +Stage.prototype.untick = function(ticker) { + if (typeof ticker !== "function") { + return; + } + var i; + if (this._tickBefore !== null && (i = this._tickBefore.indexOf(ticker)) >= 0) { + this._tickBefore.splice(i, 1); + } + if (this._tickAfter !== null && (i = this._tickAfter.indexOf(ticker)) >= 0) { + this._tickAfter.splice(i, 1); + } +}; +Stage.prototype.timeout = function(fn, time) { + this.setTimeout(fn, time); +}; +Stage.prototype.setTimeout = function(fn, time) { + function timer(t) { + if ((time -= t) < 0) { + this.untick(timer); + fn.call(this); + } else { + return true; + } + } + this.tick(timer); + return timer; +}; +Stage.prototype.clearTimeout = function(timer) { + this.untick(timer); +}; +Root._super = Stage; +Root.prototype = Object.create(Root._super.prototype); +Stage.root = function(request, render) { + return new Root(request, render); +}; +function Root(request, render) { + Root._super.call(this); + this.label("Root"); + var paused = true; + var stopped = true; + var self = this; + var lastTime = 0; + var loop = function(now) { + if (paused === true || stopped === true) { + return; + } + stats.tick = stats.node = stats.draw = 0; + var last = lastTime || now; + var elapsed = now - last; + lastTime = now; + var ticked = self._tick(elapsed, now, last); + if (self._mo_touch != self._ts_touch) { + self._mo_touch = self._ts_touch; + render(self); + request(loop); + } else if (ticked) { + request(loop); + } else { + paused = true; + } + stats.fps = elapsed ? 1e3 / elapsed : 0; + }; + this.start = function() { + stopped = false; + return this.resume(); + }; + this.resume = function() { + if (paused) { + this.publish("resume"); + paused = false; + request(loop); + } + return this; + }; + this.pause = function() { + if (!paused) { + this.publish("pause"); + } + paused = true; + return this; + }; + this.touch_root = this.touch; + this.touch = function() { + this.resume(); + return this.touch_root(); + }; + this.stop = function() { + stopped = true; + return this; + }; +} +Root.prototype.background = function(color) { + return this; +}; +Root.prototype.viewport = function(width, height, ratio) { + if (typeof width === "undefined") { + return Object.assign({}, this._viewport); + } + this._viewport = { + width, + height, + ratio: ratio || 1 + }; + this.viewbox(); + var data = Object.assign({}, this._viewport); + this.visit({ + start: function(node) { + if (!node._flag("viewport")) { + return true; + } + node.publish("viewport", [data]); + } + }); + return this; +}; +Root.prototype.viewbox = function(width, height, mode) { + if (typeof width === "number" && typeof height === "number") { + this._viewbox = { + width, + height, + mode: /^(in|out|in-pad|out-crop)$/.test(mode) ? mode : "in-pad" + }; + } + var box = this._viewbox; + var size = this._viewport; + if (size && box) { + this.pin({ + width: box.width, + height: box.height + }); + this.scaleTo(size.width, size.height, box.mode); + } else if (size) { + this.pin({ + width: size.width, + height: size.height + }); + } + return this; +}; +Stage.canvas = function(type, attributes, drawFn) { + if (typeof type === "string") { + if (typeof attributes === "object") + ; + else { + if (typeof attributes === "function") { + drawFn = attributes; + } + attributes = {}; + } + } else { + if (typeof type === "function") { + drawFn = type; + } + attributes = {}; + type = "2d"; + } + var canvas = document.createElement("canvas"); + var context = canvas.getContext(type, attributes); + var texture = new Texture(canvas); + texture.context = function() { + return context; + }; + texture.size = function(width, height, ratio) { + ratio = ratio || 1; + canvas.width = width * ratio; + canvas.height = height * ratio; + this.src(canvas, ratio); + return this; + }; + texture.canvas = function(fn) { + if (typeof fn === "function") { + fn.call(this, context); + } else if (typeof fn === "undefined" && typeof drawFn === "function") { + drawFn.call(this, context); + } + return this; + }; + if (typeof drawFn === "function") { + drawFn.call(texture, context); + } + return texture; +}; +function repeat(img, owidth, oheight, stretch, inner, insert) { + var width = img.width; + var height = img.height; + var left = img.left; + var right = img.right; + var top = img.top; + var bottom = img.bottom; + left = typeof left === "number" && left === left ? left : 0; + right = typeof right === "number" && right === right ? right : 0; + top = typeof top === "number" && top === top ? top : 0; + bottom = typeof bottom === "number" && bottom === bottom ? bottom : 0; + width = width - left - right; + height = height - top - bottom; + if (!inner) { + owidth = Math.max(owidth - left - right, 0); + oheight = Math.max(oheight - top - bottom, 0); + } + var i = 0; + if (top > 0 && left > 0) + insert(i++, 0, 0, left, top, 0, 0, left, top); + if (bottom > 0 && left > 0) + insert(i++, 0, height + top, left, bottom, 0, oheight + top, left, bottom); + if (top > 0 && right > 0) + insert(i++, width + left, 0, right, top, owidth + left, 0, right, top); + if (bottom > 0 && right > 0) + insert( + i++, + width + left, + height + top, + right, + bottom, + owidth + left, + oheight + top, + right, + bottom + ); + if (stretch) { + if (top > 0) + insert(i++, left, 0, width, top, left, 0, owidth, top); + if (bottom > 0) + insert( + i++, + left, + height + top, + width, + bottom, + left, + oheight + top, + owidth, + bottom + ); + if (left > 0) + insert(i++, 0, top, left, height, 0, top, left, oheight); + if (right > 0) + insert( + i++, + width + left, + top, + right, + height, + owidth + left, + top, + right, + oheight + ); + insert(i++, left, top, width, height, left, top, owidth, oheight); + } else { + var l = left, r = owidth, w; + while (r > 0) { + w = Math.min(width, r), r -= width; + var t = top, b = oheight, h; + while (b > 0) { + h = Math.min(height, b), b -= height; + insert(i++, left, top, w, h, l, t, w, h); + if (r <= 0) { + if (left) + insert(i++, 0, top, left, h, 0, t, left, h); + if (right) + insert(i++, width + left, top, right, h, l + w, t, right, h); + } + t += h; + } + if (top) + insert(i++, left, 0, w, top, l, 0, w, top); + if (bottom) + insert(i++, left, height + top, w, bottom, l, t, w, bottom); + l += w; + } + } + return i; +} +Stage.image = function(image) { + var img = new Image$1(); + image && img.image(image); + return img; +}; +Image$1._super = Stage; +Image$1.prototype = Object.create(Image$1._super.prototype); +function Image$1() { + Image$1._super.call(this); + this.label("Image"); + this._textures = []; + this._image = null; +} +Image$1.prototype.image = function(image) { + this._image = Stage.texture(image).one(); + this.pin("width", this._image ? this._image.width : 0); + this.pin("height", this._image ? this._image.height : 0); + this._textures[0] = this._image.pipe(); + this._textures.length = 1; + return this; +}; +Image$1.prototype.tile = function(inner) { + this._repeat(false, inner); + return this; +}; +Image$1.prototype.stretch = function(inner) { + this._repeat(true, inner); + return this; +}; +Image$1.prototype._repeat = function(stretch, inner) { + var self = this; + this.untick(this._repeatTicker); + this.tick(this._repeatTicker = function() { + if (this._mo_stretch == this._pin._ts_transform) { + return; + } + this._mo_stretch = this._pin._ts_transform; + var width = this.pin("width"); + var height = this.pin("height"); + this._textures.length = repeat(this._image, width, height, stretch, inner, insert); + }); + function insert(i, sx, sy, sw, sh, dx, dy, dw, dh) { + var repeat2 = self._textures.length > i ? self._textures[i] : self._textures[i] = self._image.pipe(); + repeat2.src(sx, sy, sw, sh); + repeat2.dest(dx, dy, dw, dh); + } +}; +Stage.anim = function(frames, fps) { + var anim = new Anim(); + anim.frames(frames).gotoFrame(0); + fps && anim.fps(fps); + return anim; +}; +Anim._super = Stage; +Anim.prototype = Object.create(Anim._super.prototype); +Stage.Anim = { + FPS: 15 +}; +function Anim() { + Anim._super.call(this); + this.label("Anim"); + this._textures = []; + this._fps = Stage.Anim.FPS; + this._ft = 1e3 / this._fps; + this._time = -1; + this._repeat = 0; + this._index = 0; + this._frames = []; + var lastTime = 0; + this.tick(function(t, now, last) { + if (this._time < 0 || this._frames.length <= 1) { + return; + } + var ignore = lastTime != last; + lastTime = now; + if (ignore) { + return true; + } + this._time += t; + if (this._time < this._ft) { + return true; + } + var n = this._time / this._ft | 0; + this._time -= n * this._ft; + this.moveFrame(n); + if (this._repeat > 0 && (this._repeat -= n) <= 0) { + this.stop(); + this._callback && this._callback(); + return false; + } + return true; + }, false); +} +Anim.prototype.fps = function(fps) { + if (typeof fps === "undefined") { + return this._fps; + } + this._fps = fps > 0 ? fps : Stage.Anim.FPS; + this._ft = 1e3 / this._fps; + return this; +}; +Anim.prototype.setFrames = function(a, b, c) { + return this.frames(a, b, c); +}; +Anim.prototype.frames = function(frames) { + this._index = 0; + this._frames = Stage.texture(frames).array(); + this.touch(); + return this; +}; +Anim.prototype.length = function() { + return this._frames ? this._frames.length : 0; +}; +Anim.prototype.gotoFrame = function(frame, resize) { + this._index = math.rotate(frame, this._frames.length) | 0; + resize = resize || !this._textures[0]; + this._textures[0] = this._frames[this._index]; + if (resize) { + this.pin("width", this._textures[0].width); + this.pin("height", this._textures[0].height); + } + this.touch(); + return this; +}; +Anim.prototype.moveFrame = function(move) { + return this.gotoFrame(this._index + move); +}; +Anim.prototype.repeat = function(repeat2, callback) { + this._repeat = repeat2 * this._frames.length - 1; + this._callback = callback; + this.play(); + return this; +}; +Anim.prototype.play = function(frame) { + if (typeof frame !== "undefined") { + this.gotoFrame(frame); + this._time = 0; + } else if (this._time < 0) { + this._time = 0; + } + this.touch(); + return this; +}; +Anim.prototype.stop = function(frame) { + this._time = -1; + if (typeof frame !== "undefined") { + this.gotoFrame(frame); + } + return this; +}; +Stage.string = function(frames) { + return new Str().frames(frames); +}; +Str._super = Stage; +Str.prototype = Object.create(Str._super.prototype); +function Str() { + Str._super.call(this); + this.label("String"); + this._textures = []; +} +Str.prototype.setFont = function(a, b, c) { + return this.frames(a, b, c); +}; +Str.prototype.frames = function(frames) { + this._textures = []; + if (typeof frames == "string") { + frames = Stage.texture(frames); + this._item = function(value) { + return frames.one(value); + }; + } else if (typeof frames === "object") { + this._item = function(value) { + return frames[value]; + }; + } else if (typeof frames === "function") { + this._item = frames; + } + return this; +}; +Str.prototype.setValue = function(a, b, c) { + return this.value(a, b, c); +}; +Str.prototype.value = function(value) { + if (typeof value === "undefined") { + return this._value; + } + if (this._value === value) { + return this; + } + this._value = value; + if (value === null) { + value = ""; + } else if (typeof value !== "string" && !is$1.array(value)) { + value = value.toString(); + } + this._spacing = this._spacing || 0; + var width = 0, height = 0; + for (var i = 0; i < value.length; i++) { + var image = this._textures[i] = this._item(value[i]); + width += i > 0 ? this._spacing : 0; + image.dest(width, 0); + width = width + image.width; + height = Math.max(height, image.height); + } + this.pin("width", width); + this.pin("height", height); + this._textures.length = value.length; + return this; +}; +Stage.row = function(align) { + return Stage.create().row(align).label("Row"); +}; +Stage.prototype.row = function(align) { + this.sequence("row", align); + return this; +}; +Stage.column = function(align) { + return Stage.create().column(align).label("Row"); +}; +Stage.prototype.column = function(align) { + this.sequence("column", align); + return this; +}; +Stage.sequence = function(type, align) { + return Stage.create().sequence(type, align).label("Sequence"); +}; +Stage.prototype.sequence = function(type, align) { + this._padding = this._padding || 0; + this._spacing = this._spacing || 0; + this.untick(this._layoutTiker); + this.tick(this._layoutTiker = function() { + if (this._mo_seq == this._ts_touch) { + return; + } + this._mo_seq = this._ts_touch; + var alignChildren = this._mo_seqAlign != this._ts_children; + this._mo_seqAlign = this._ts_children; + var width = 0, height = 0; + var child, next = this.first(true); + var first = true; + while (child = next) { + next = child.next(true); + child.matrix(true); + var w = child.pin("boxWidth"); + var h = child.pin("boxHeight"); + if (type == "column") { + !first && (height += this._spacing); + child.pin("offsetY") != height && child.pin("offsetY", height); + width = Math.max(width, w); + height = height + h; + alignChildren && child.pin("alignX", align); + } else if (type == "row") { + !first && (width += this._spacing); + child.pin("offsetX") != width && child.pin("offsetX", width); + width = width + w; + height = Math.max(height, h); + alignChildren && child.pin("alignY", align); + } + first = false; + } + width += 2 * this._padding; + height += 2 * this._padding; + this.pin("width") != width && this.pin("width", width); + this.pin("height") != height && this.pin("height", height); + }); + return this; +}; +Stage.box = function() { + return Stage.create().box().label("Box"); +}; +Stage.prototype.box = function() { + this._padding = this._padding || 0; + this.untick(this._layoutTiker); + this.tick(this._layoutTiker = function() { + if (this._mo_box == this._ts_touch) { + return; + } + this._mo_box = this._ts_touch; + var width = 0, height = 0; + var child, next = this.first(true); + while (child = next) { + next = child.next(true); + child.matrix(true); + var w = child.pin("boxWidth"); + var h = child.pin("boxHeight"); + width = Math.max(width, w); + height = Math.max(height, h); + } + width += 2 * this._padding; + height += 2 * this._padding; + this.pin("width") != width && this.pin("width", width); + this.pin("height") != height && this.pin("height", height); + }); + return this; +}; +Stage.layer = function() { + return Stage.create().layer().label("Layer"); +}; +Stage.prototype.layer = function() { + this.untick(this._layoutTiker); + this.tick(this._layoutTiker = function() { + var parent = this.parent(); + if (parent) { + var width = parent.pin("width"); + if (this.pin("width") != width) { + this.pin("width", width); + } + var height = parent.pin("height"); + if (this.pin("height") != height) { + this.pin("height", height); + } + } + }, true); + return this; +}; +Stage.prototype.padding = function(pad) { + this._padding = pad; + return this; +}; +Stage.prototype.spacing = function(space) { + this._spacing = space; + return this; +}; +function _identity(x) { + return x; +} +var _cache = {}; +var _modes = {}; +var _easings = {}; +function Easing(token) { + if (typeof token === "function") { + return token; + } + if (typeof token !== "string") { + return _identity; + } + var fn = _cache[token]; + if (fn) { + return fn; + } + var match = /^(\w+)(-(in|out|in-out|out-in))?(\((.*)\))?$/i.exec(token); + if (!match || !match.length) { + return _identity; + } + var easing = _easings[match[1]]; + var mode = _modes[match[3]]; + var params = match[5]; + if (easing && easing.fn) { + fn = easing.fn; + } else if (easing && easing.fc) { + fn = easing.fc.apply(easing.fc, params && params.replace(/\s+/, "").split(",")); + } else { + fn = _identity; + } + if (mode) { + fn = mode.fn(fn); + } + _cache[token] = fn; + return fn; +} +Easing.add = function(data) { + var names = (data.name || data.mode).split(/\s+/); + for (var i = 0; i < names.length; i++) { + var name = names[i]; + if (name) { + (data.name ? _easings : _modes)[name] = data; + } + } +}; +Easing.add({ + mode: "in", + fn: function(f) { + return f; + } +}); +Easing.add({ + mode: "out", + fn: function(f) { + return function(t) { + return 1 - f(1 - t); + }; + } +}); +Easing.add({ + mode: "in-out", + fn: function(f) { + return function(t) { + return t < 0.5 ? f(2 * t) / 2 : 1 - f(2 * (1 - t)) / 2; + }; + } +}); +Easing.add({ + mode: "out-in", + fn: function(f) { + return function(t) { + return t < 0.5 ? 1 - f(2 * (1 - t)) / 2 : f(2 * t) / 2; + }; + } +}); +Easing.add({ + name: "linear", + fn: function(t) { + return t; + } +}); +Easing.add({ + name: "quad", + fn: function(t) { + return t * t; + } +}); +Easing.add({ + name: "cubic", + fn: function(t) { + return t * t * t; + } +}); +Easing.add({ + name: "quart", + fn: function(t) { + return t * t * t * t; + } +}); +Easing.add({ + name: "quint", + fn: function(t) { + return t * t * t * t * t; + } +}); +Easing.add({ + name: "sin sine", + fn: function(t) { + return 1 - Math.cos(t * Math.PI / 2); + } +}); +Easing.add({ + name: "exp expo", + fn: function(t) { + return t == 0 ? 0 : Math.pow(2, 10 * (t - 1)); + } +}); +Easing.add({ + name: "circle circ", + fn: function(t) { + return 1 - Math.sqrt(1 - t * t); + } +}); +Easing.add({ + name: "bounce", + fn: function(t) { + return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + 0.75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + 0.9375 : 7.5625 * (t -= 2.625 / 2.75) * t + 0.984375; + } +}); +Easing.add({ + name: "poly", + fc: function(e) { + return function(t) { + return Math.pow(t, e); + }; + } +}); +Easing.add({ + name: "elastic", + fc: function(a, p) { + p = p || 0.45; + a = a || 1; + var s = p / (2 * Math.PI) * Math.asin(1 / a); + return function(t) { + return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / p); + }; + } +}); +Easing.add({ + name: "back", + fc: function(s) { + s = typeof s !== "undefined" ? s : 1.70158; + return function(t) { + return t * t * ((s + 1) * t - s); + }; + } +}); +Stage.prototype.tween = function(duration, delay, append2) { + if (typeof duration !== "number") { + append2 = duration, delay = 0, duration = 0; + } else if (typeof delay !== "number") { + append2 = delay, delay = 0; + } + if (!this._tweens) { + this._tweens = []; + var ticktime = 0; + this.tick(function(elapsed, now, last) { + if (!this._tweens.length) { + return; + } + var ignore = ticktime != last; + ticktime = now; + if (ignore) { + return true; + } + var head = this._tweens[0]; + var next = head.tick(this, elapsed, now, last); + if (next && head === this._tweens[0]) { + this._tweens.shift(); + } + if (Array.isArray(next)) { + for (var i = 0; i < next.length; i++) { + try { + next[i].call(this); + } catch (e) { + console.log(e); + } + } + } else if (typeof next === "object") { + this._tweens.unshift(next); + } + return true; + }, true); + } + this.touch(); + if (!append2) { + this._tweens.length = 0; + } + var tween = new Tween(this, duration, delay); + this._tweens.push(tween); + return tween; +}; +function Tween(owner, duration, delay) { + this._end = {}; + this._duration = duration || 400; + this._delay = delay || 0; + this._owner = owner; + this._time = 0; +} +Tween.prototype.tick = function(node, elapsed, now, last) { + this._time += elapsed; + if (this._time < this._delay) { + return; + } + var time = this._time - this._delay; + if (!this._start) { + this._start = {}; + for (var key in this._end) { + this._start[key] = this._owner.pin(key); + } + } + var p, over; + if (time < this._duration) { + p = time / this._duration; + over = false; + } else { + p = 1; + over = true; + } + if (typeof this._easing == "function") { + p = this._easing(p); + } + var q = 1 - p; + for (var key in this._end) { + this._owner.pin(key, this._start[key] * q + this._end[key] * p); + } + if (over) { + var actions = [this._hide, this._remove, this._done]; + actions = actions.filter(function(element) { + return typeof element === "function"; + }); + return this._next || actions; + } +}; +Tween.prototype.tween = function(duration, delay) { + return this._next = new Tween(this._owner, duration, delay); +}; +Tween.prototype.duration = function(duration) { + this._duration = duration; + return this; +}; +Tween.prototype.delay = function(delay) { + this._delay = delay; + return this; +}; +Tween.prototype.ease = function(easing) { + this._easing = Easing(easing); + return this; +}; +Tween.prototype.done = function(fn) { + this._done = fn; + return this; +}; +Tween.prototype.hide = function() { + this._hide = function() { + this.hide(); + }; + return this; +}; +Tween.prototype.remove = function() { + this._remove = function() { + this.remove(); + }; + return this; +}; +Tween.prototype.pin = function(a, b) { + if (typeof a === "object") { + for (var attr in a) { + pinning(this._owner, this._end, attr, a[attr]); + } + } else if (typeof b !== "undefined") { + pinning(this._owner, this._end, a, b); + } + return this; +}; +function pinning(node, map, key, value) { + if (typeof node.pin(key) === "number") { + map[key] = value; + } else if (typeof node.pin(key + "X") === "number" && typeof node.pin(key + "Y") === "number") { + map[key + "X"] = value; + map[key + "Y"] = value; + } +} +Pin._add_shortcuts(Tween); +Tween.prototype.then = function(fn) { + this.done(fn); + return this; +}; +Tween.prototype.clear = function(forward) { + return this; +}; +Stage._load(function(stage, elem) { + Mouse.subscribe(stage, elem); +}); +Mouse.CLICK = "click"; +Mouse.START = "touchstart mousedown"; +Mouse.MOVE = "touchmove mousemove"; +Mouse.END = "touchend mouseup"; +Mouse.CANCEL = "touchcancel mousecancel"; +Mouse.subscribe = function(stage, elem) { + if (stage.mouse) { + return; + } + stage.mouse = new Mouse(stage, elem); + elem.addEventListener("touchstart", handleStart); + elem.addEventListener("touchend", handleEnd); + elem.addEventListener("touchmove", handleMove); + elem.addEventListener("touchcancel", handleCancel); + elem.addEventListener("mousedown", handleStart); + elem.addEventListener("mouseup", handleEnd); + elem.addEventListener("mousemove", handleMove); + document.addEventListener("mouseup", handleCancel); + window.addEventListener("blur", handleCancel); + var clicklist = [], cancellist = []; + function handleStart(event) { + event.preventDefault(); + stage.mouse.locate(event); + stage.mouse.publish(event.type, event); + stage.mouse.lookup("click", clicklist); + stage.mouse.lookup("mousecancel", cancellist); + } + function handleMove(event) { + event.preventDefault(); + stage.mouse.locate(event); + stage.mouse.publish(event.type, event); + } + function handleEnd(event) { + event.preventDefault(); + stage.mouse.publish(event.type, event); + if (clicklist.length) { + stage.mouse.publish("click", event, clicklist); + } + cancellist.length = 0; + } + function handleCancel(event) { + if (cancellist.length) { + stage.mouse.publish("mousecancel", event, cancellist); + } + clicklist.length = 0; + } +}; +function Mouse(stage, elem) { + if (!(this instanceof Mouse)) { + return; + } + var ratio = stage.viewport().ratio || 1; + stage.on("viewport", function(size) { + ratio = size.ratio || ratio; + }); + this.x = 0; + this.y = 0; + this.toString = function() { + return (this.x | 0) + "x" + (this.y | 0); + }; + this.locate = function(event) { + locateElevent(elem, event, this); + this.x *= ratio; + this.y *= ratio; + }; + this.lookup = function(type, collect) { + this.type = type; + this.root = stage; + this.event = null; + collect.length = 0; + this.collect = collect; + this.root.visit(this.visitor, this); + }; + this.publish = function(type, event, targets) { + this.type = type; + this.root = stage; + this.event = event; + this.collect = false; + this.timeStamp = Date.now(); + if (type !== "mousemove" && type !== "touchmove") { + console.log(this.type + " " + this); + } + if (targets) { + while (targets.length) + if (this.visitor.end(targets.shift(), this)) + break; + targets.length = 0; + } else { + this.root.visit(this.visitor, this); + } + }; + this.visitor = { + reverse: true, + visible: true, + start: function(node, mouse) { + return !node._flag(mouse.type); + }, + end: function(node, mouse) { + rel.raw = mouse.event; + rel.type = mouse.type; + rel.timeStamp = mouse.timeStamp; + rel.abs.x = mouse.x; + rel.abs.y = mouse.y; + var listeners = node.listeners(mouse.type); + if (!listeners) { + return; + } + node.matrix().inverse().map(mouse, rel); + if (!(node === mouse.root || node.attr("spy") || node.hitTest(rel))) { + return; + } + if (mouse.collect) { + mouse.collect.push(node); + } + if (mouse.event) { + var cancel = false; + for (var l = 0; l < listeners.length; l++) { + cancel = listeners[l].call(node, rel) ? true : cancel; + } + return cancel; + } + } + }; +} +var rel = {}, abs = {}; +defineValue(rel, "clone", function(obj) { + obj = obj || {}, obj.x = this.x, obj.y = this.y; + return obj; +}); +defineValue(rel, "toString", function() { + return (this.x | 0) + "x" + (this.y | 0) + " (" + this.abs + ")"; +}); +defineValue(rel, "abs", abs); +defineValue(abs, "clone", function(obj) { + obj = obj || {}, obj.x = this.x, obj.y = this.y; + return obj; +}); +defineValue(abs, "toString", function() { + return (this.x | 0) + "x" + (this.y | 0); +}); +function defineValue(obj, name, value) { + Object.defineProperty(obj, name, { + value + }); +} +function locateElevent(el, ev, loc) { + if (ev.touches && ev.touches.length) { + loc.x = ev.touches[0].clientX; + loc.y = ev.touches[0].clientY; + } else { + loc.x = ev.clientX; + loc.y = ev.clientY; + } + var rect = el.getBoundingClientRect(); + loc.x -= rect.left; + loc.y -= rect.top; + loc.x -= el.clientLeft | 0; + loc.y -= el.clientTop | 0; + return loc; +} +window.addEventListener("load", function() { + console.log("On load."); + Stage.start(); +}, false); +Stage.config({ + "app-loader": AppLoader, + "image-loader": ImageLoader +}); +function AppLoader(app, configs) { + configs = configs || {}; + var canvas = configs.canvas, context = null, full = false; + var width = 0, height = 0, ratio = 1; + if (typeof canvas === "string") { + canvas = document.getElementById(canvas); + } + if (!canvas) { + canvas = document.getElementById("cutjs") || document.getElementById("stage"); + } + if (!canvas) { + full = true; + console.log("Creating Canvas..."); + canvas = document.createElement("canvas"); + canvas.style.position = "absolute"; + canvas.style.top = "0"; + canvas.style.left = "0"; + var body = document.body; + body.insertBefore(canvas, body.firstChild); + } + context = canvas.getContext("2d"); + var devicePixelRatio = window.devicePixelRatio || 1; + var backingStoreRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1; + ratio = devicePixelRatio / backingStoreRatio; + var requestAnimationFrame = window.requestAnimationFrame || window.msRequestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.oRequestAnimationFrame || function(callback) { + return window.setTimeout(callback, 1e3 / 60); + }; + console.log("Creating stage..."); + var root = Stage.root(requestAnimationFrame, render); + function render() { + if (width > 0 && height > 0) { + context.setTransform(1, 0, 0, 1, 0, 0); + context.clearRect(0, 0, width, height); + root.render(context); + } + } + root.background = function(color) { + canvas.style.backgroundColor = color; + return this; + }; + app(root, canvas); + var lastWidth = -1; + var lastHeight = -1; + (function resizeLoop() { + var width2, height2; + if (full) { + width2 = window.innerWidth > 0 ? window.innerWidth : screen.width; + height2 = window.innerHeight > 0 ? window.innerHeight : screen.height; + } else { + width2 = canvas.clientWidth; + height2 = canvas.clientHeight; + } + if (lastWidth !== width2 || lastHeight !== height2) { + lastWidth = width2; + lastHeight = height2; + resize(); + } + requestAnimationFrame(resizeLoop); + })(); + function resize() { + if (full) { + width = window.innerWidth > 0 ? window.innerWidth : screen.width; + height = window.innerHeight > 0 ? window.innerHeight : screen.height; + canvas.style.width = width + "px"; + canvas.style.height = height + "px"; + } else { + width = canvas.clientWidth; + height = canvas.clientHeight; + } + width *= ratio; + height *= ratio; + if (canvas.width === width && canvas.height === height) { + return; + } + canvas.width = width; + canvas.height = height; + console.log("Resize: " + width + " x " + height + " / " + ratio); + root.viewport(width, height, ratio); + render(); + } +} +function ImageLoader(src, success, error) { + console.log("Loading image: " + src); + var image = new Image(); + image.onload = function() { + success(image); + }; + image.onerror = error; + image.src = src; +} +listenable(Stage.prototype, function(obj, name, on) { + obj._flag(name, on); +}); +Stage.Matrix = Matrix; +Stage.Texture = Texture; +Stage.Mouse = Mouse; +Stage.Math = math; +Stage.Image = Image$1; +module.exports = Stage; diff --git a/dist/stage.js b/dist/stage.js index b0b3cb8..b0065ce 100644 --- a/dist/stage.js +++ b/dist/stage.js @@ -1,5 +1,5 @@ -function wt(t) { - return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, "default") ? t.default : t; +function getDefaultExportFromCjs(x) { + return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x; } /**! * is @@ -8,1718 +8,2945 @@ function wt(t) { * @copyright 2013-2014 Enrico Marino / Jordan Harband * @license MIT */ -var ct = Object.prototype, _t = ct.hasOwnProperty, Y = ct.toString, pt; -typeof Symbol == "function" && (pt = Symbol.prototype.valueOf); -var lt; -typeof BigInt == "function" && (lt = BigInt.prototype.valueOf); -var x = function(t) { - return t !== t; -}, bt = { - boolean: 1, +var objProto = Object.prototype; +var owns = objProto.hasOwnProperty; +var toStr = objProto.toString; +var symbolValueOf; +if (typeof Symbol === "function") { + symbolValueOf = Symbol.prototype.valueOf; +} +var bigIntValueOf; +if (typeof BigInt === "function") { + bigIntValueOf = BigInt.prototype.valueOf; +} +var isActualNaN = function(value) { + return value !== value; +}; +var NON_HOST_TYPES = { + "boolean": 1, number: 1, string: 1, undefined: 1 -}, xt = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/, kt = /^[A-Fa-f0-9]+$/, h = {}; -h.a = h.type = function(t, i) { - return typeof t === i; -}; -h.defined = function(t) { - return typeof t < "u"; }; -h.empty = function(t) { - var i = Y.call(t), e; - if (i === "[object Array]" || i === "[object Arguments]" || i === "[object String]") - return t.length === 0; - if (i === "[object Object]") { - for (e in t) - if (_t.call(t, e)) - return !1; - return !0; +var base64Regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/; +var hexRegex = /^[A-Fa-f0-9]+$/; +var is = {}; +is.a = is.type = function(value, type) { + return typeof value === type; +}; +is.defined = function(value) { + return typeof value !== "undefined"; +}; +is.empty = function(value) { + var type = toStr.call(value); + var key; + if (type === "[object Array]" || type === "[object Arguments]" || type === "[object String]") { + return value.length === 0; + } + if (type === "[object Object]") { + for (key in value) { + if (owns.call(value, key)) { + return false; + } + } + return true; } - return !t; + return !value; }; -h.equal = function(i, e) { - if (i === e) - return !0; - var n = Y.call(i), r; - if (n !== Y.call(e)) - return !1; - if (n === "[object Object]") { - for (r in i) - if (!h.equal(i[r], e[r]) || !(r in e)) - return !1; - for (r in e) - if (!h.equal(i[r], e[r]) || !(r in i)) - return !1; - return !0; +is.equal = function equal(value, other) { + if (value === other) { + return true; + } + var type = toStr.call(value); + var key; + if (type !== toStr.call(other)) { + return false; + } + if (type === "[object Object]") { + for (key in value) { + if (!is.equal(value[key], other[key]) || !(key in other)) { + return false; + } + } + for (key in other) { + if (!is.equal(value[key], other[key]) || !(key in value)) { + return false; + } + } + return true; } - if (n === "[object Array]") { - if (r = i.length, r !== e.length) - return !1; - for (; r--; ) - if (!h.equal(i[r], e[r])) - return !1; - return !0; + if (type === "[object Array]") { + key = value.length; + if (key !== other.length) { + return false; + } + while (key--) { + if (!is.equal(value[key], other[key])) { + return false; + } + } + return true; + } + if (type === "[object Function]") { + return value.prototype === other.prototype; } - return n === "[object Function]" ? i.prototype === e.prototype : n === "[object Date]" ? i.getTime() === e.getTime() : !1; + if (type === "[object Date]") { + return value.getTime() === other.getTime(); + } + return false; }; -h.hosted = function(t, i) { - var e = typeof i[t]; - return e === "object" ? !!i[t] : !bt[e]; +is.hosted = function(value, host) { + var type = typeof host[value]; + return type === "object" ? !!host[value] : !NON_HOST_TYPES[type]; }; -h.instance = h.instanceof = function(t, i) { - return t instanceof i; +is.instance = is["instanceof"] = function(value, constructor) { + return value instanceof constructor; }; -h.nil = h.null = function(t) { - return t === null; +is.nil = is["null"] = function(value) { + return value === null; }; -h.undef = h.undefined = function(t) { - return typeof t > "u"; +is.undef = is.undefined = function(value) { + return typeof value === "undefined"; }; -h.args = h.arguments = function(t) { - var i = Y.call(t) === "[object Arguments]", e = !h.array(t) && h.arraylike(t) && h.object(t) && h.fn(t.callee); - return i || e; +is.args = is.arguments = function(value) { + var isStandardArguments = toStr.call(value) === "[object Arguments]"; + var isOldArguments = !is.array(value) && is.arraylike(value) && is.object(value) && is.fn(value.callee); + return isStandardArguments || isOldArguments; }; -h.array = Array.isArray || function(t) { - return Y.call(t) === "[object Array]"; +is.array = Array.isArray || function(value) { + return toStr.call(value) === "[object Array]"; }; -h.args.empty = function(t) { - return h.args(t) && t.length === 0; +is.args.empty = function(value) { + return is.args(value) && value.length === 0; }; -h.array.empty = function(t) { - return h.array(t) && t.length === 0; +is.array.empty = function(value) { + return is.array(value) && value.length === 0; }; -h.arraylike = function(t) { - return !!t && !h.bool(t) && _t.call(t, "length") && isFinite(t.length) && h.number(t.length) && t.length >= 0; +is.arraylike = function(value) { + return !!value && !is.bool(value) && owns.call(value, "length") && isFinite(value.length) && is.number(value.length) && value.length >= 0; }; -h.bool = h.boolean = function(t) { - return Y.call(t) === "[object Boolean]"; +is.bool = is["boolean"] = function(value) { + return toStr.call(value) === "[object Boolean]"; }; -h.false = function(t) { - return h.bool(t) && !Number(t); +is["false"] = function(value) { + return is.bool(value) && Boolean(Number(value)) === false; }; -h.true = function(t) { - return h.bool(t) && !!Number(t); +is["true"] = function(value) { + return is.bool(value) && Boolean(Number(value)) === true; }; -h.date = function(t) { - return Y.call(t) === "[object Date]"; +is.date = function(value) { + return toStr.call(value) === "[object Date]"; }; -h.date.valid = function(t) { - return h.date(t) && !isNaN(Number(t)); +is.date.valid = function(value) { + return is.date(value) && !isNaN(Number(value)); }; -h.element = function(t) { - return t !== void 0 && typeof HTMLElement < "u" && t instanceof HTMLElement && t.nodeType === 1; +is.element = function(value) { + return value !== void 0 && typeof HTMLElement !== "undefined" && value instanceof HTMLElement && value.nodeType === 1; }; -h.error = function(t) { - return Y.call(t) === "[object Error]"; +is.error = function(value) { + return toStr.call(value) === "[object Error]"; }; -h.fn = h.function = function(t) { - var i = typeof window < "u" && t === window.alert; - if (i) - return !0; - var e = Y.call(t); - return e === "[object Function]" || e === "[object GeneratorFunction]" || e === "[object AsyncFunction]"; +is.fn = is["function"] = function(value) { + var isAlert = typeof window !== "undefined" && value === window.alert; + if (isAlert) { + return true; + } + var str = toStr.call(value); + return str === "[object Function]" || str === "[object GeneratorFunction]" || str === "[object AsyncFunction]"; }; -h.number = function(t) { - return Y.call(t) === "[object Number]"; +is.number = function(value) { + return toStr.call(value) === "[object Number]"; }; -h.infinite = function(t) { - return t === 1 / 0 || t === -1 / 0; +is.infinite = function(value) { + return value === Infinity || value === -Infinity; }; -h.decimal = function(t) { - return h.number(t) && !x(t) && !h.infinite(t) && t % 1 !== 0; +is.decimal = function(value) { + return is.number(value) && !isActualNaN(value) && !is.infinite(value) && value % 1 !== 0; }; -h.divisibleBy = function(t, i) { - var e = h.infinite(t), n = h.infinite(i), r = h.number(t) && !x(t) && h.number(i) && !x(i) && i !== 0; - return e || n || r && t % i === 0; +is.divisibleBy = function(value, n) { + var isDividendInfinite = is.infinite(value); + var isDivisorInfinite = is.infinite(n); + var isNonZeroNumber = is.number(value) && !isActualNaN(value) && is.number(n) && !isActualNaN(n) && n !== 0; + return isDividendInfinite || isDivisorInfinite || isNonZeroNumber && value % n === 0; }; -h.integer = h.int = function(t) { - return h.number(t) && !x(t) && t % 1 === 0; +is.integer = is["int"] = function(value) { + return is.number(value) && !isActualNaN(value) && value % 1 === 0; }; -h.maximum = function(t, i) { - if (x(t)) +is.maximum = function(value, others) { + if (isActualNaN(value)) { throw new TypeError("NaN is not a valid value"); - if (!h.arraylike(i)) + } else if (!is.arraylike(others)) { throw new TypeError("second argument must be array-like"); - for (var e = i.length; --e >= 0; ) - if (t < i[e]) - return !1; - return !0; + } + var len = others.length; + while (--len >= 0) { + if (value < others[len]) { + return false; + } + } + return true; }; -h.minimum = function(t, i) { - if (x(t)) +is.minimum = function(value, others) { + if (isActualNaN(value)) { throw new TypeError("NaN is not a valid value"); - if (!h.arraylike(i)) + } else if (!is.arraylike(others)) { throw new TypeError("second argument must be array-like"); - for (var e = i.length; --e >= 0; ) - if (t > i[e]) - return !1; - return !0; + } + var len = others.length; + while (--len >= 0) { + if (value > others[len]) { + return false; + } + } + return true; }; -h.nan = function(t) { - return !h.number(t) || t !== t; +is.nan = function(value) { + return !is.number(value) || value !== value; }; -h.even = function(t) { - return h.infinite(t) || h.number(t) && t === t && t % 2 === 0; +is.even = function(value) { + return is.infinite(value) || is.number(value) && value === value && value % 2 === 0; }; -h.odd = function(t) { - return h.infinite(t) || h.number(t) && t === t && t % 2 !== 0; +is.odd = function(value) { + return is.infinite(value) || is.number(value) && value === value && value % 2 !== 0; }; -h.ge = function(t, i) { - if (x(t) || x(i)) +is.ge = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { throw new TypeError("NaN is not a valid value"); - return !h.infinite(t) && !h.infinite(i) && t >= i; + } + return !is.infinite(value) && !is.infinite(other) && value >= other; }; -h.gt = function(t, i) { - if (x(t) || x(i)) +is.gt = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { throw new TypeError("NaN is not a valid value"); - return !h.infinite(t) && !h.infinite(i) && t > i; + } + return !is.infinite(value) && !is.infinite(other) && value > other; }; -h.le = function(t, i) { - if (x(t) || x(i)) +is.le = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { throw new TypeError("NaN is not a valid value"); - return !h.infinite(t) && !h.infinite(i) && t <= i; + } + return !is.infinite(value) && !is.infinite(other) && value <= other; }; -h.lt = function(t, i) { - if (x(t) || x(i)) +is.lt = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { throw new TypeError("NaN is not a valid value"); - return !h.infinite(t) && !h.infinite(i) && t < i; + } + return !is.infinite(value) && !is.infinite(other) && value < other; }; -h.within = function(t, i, e) { - if (x(t) || x(i) || x(e)) +is.within = function(value, start, finish) { + if (isActualNaN(value) || isActualNaN(start) || isActualNaN(finish)) { throw new TypeError("NaN is not a valid value"); - if (!h.number(t) || !h.number(i) || !h.number(e)) + } else if (!is.number(value) || !is.number(start) || !is.number(finish)) { throw new TypeError("all arguments must be numbers"); - var n = h.infinite(t) || h.infinite(i) || h.infinite(e); - return n || t >= i && t <= e; + } + var isAnyInfinite = is.infinite(value) || is.infinite(start) || is.infinite(finish); + return isAnyInfinite || value >= start && value <= finish; }; -h.object = function(t) { - return Y.call(t) === "[object Object]"; +is.object = function(value) { + return toStr.call(value) === "[object Object]"; }; -h.primitive = function(i) { - return i ? !(typeof i == "object" || h.object(i) || h.fn(i) || h.array(i)) : !0; +is.primitive = function isPrimitive(value) { + if (!value) { + return true; + } + if (typeof value === "object" || is.object(value) || is.fn(value) || is.array(value)) { + return false; + } + return true; }; -h.hash = function(t) { - return h.object(t) && t.constructor === Object && !t.nodeType && !t.setInterval; +is.hash = function(value) { + return is.object(value) && value.constructor === Object && !value.nodeType && !value.setInterval; }; -h.regexp = function(t) { - return Y.call(t) === "[object RegExp]"; +is.regexp = function(value) { + return toStr.call(value) === "[object RegExp]"; }; -h.string = function(t) { - return Y.call(t) === "[object String]"; +is.string = function(value) { + return toStr.call(value) === "[object String]"; }; -h.base64 = function(t) { - return h.string(t) && (!t.length || xt.test(t)); +is.base64 = function(value) { + return is.string(value) && (!value.length || base64Regex.test(value)); }; -h.hex = function(t) { - return h.string(t) && (!t.length || kt.test(t)); +is.hex = function(value) { + return is.string(value) && (!value.length || hexRegex.test(value)); }; -h.symbol = function(t) { - return typeof Symbol == "function" && Y.call(t) === "[object Symbol]" && typeof pt.call(t) == "symbol"; +is.symbol = function(value) { + return typeof Symbol === "function" && toStr.call(value) === "[object Symbol]" && typeof symbolValueOf.call(value) === "symbol"; }; -h.bigint = function(t) { - return typeof BigInt == "function" && Y.call(t) === "[object BigInt]" && typeof lt.call(t) == "bigint"; +is.bigint = function(value) { + return typeof BigInt === "function" && toStr.call(value) === "[object BigInt]" && typeof bigIntValueOf.call(value) === "bigint"; }; -var Xt = h; -const d = /* @__PURE__ */ wt(Xt), I = {}; -function Yt() { - var t = 0; - function i(r, s) { - return t += s = typeof s == "number" && s >= 1 ? s : 1, function() { - r && r.apply(this, arguments), s > 0 && (s--, t--, n()); +var is_1 = is; +const is$1 = /* @__PURE__ */ getDefaultExportFromCjs(is_1); +const stats = {}; +function _await() { + var count = 0; + function fork(fn, n) { + count += n = typeof n === "number" && n >= 1 ? n : 1; + return function() { + fn && fn.apply(this, arguments); + if (n > 0) { + n--, count--, call(); + } }; } - var e = []; - function n() { - if (t === 0) - for (; e.length; ) - setTimeout(e.shift(), 0); + var then = []; + function call() { + if (count === 0) { + while (then.length) { + setTimeout(then.shift(), 0); + } + } } - return i.then = function(r) { - t === 0 ? setTimeout(r, 0) : e.push(r); - }, i; + fork.then = function(fn) { + if (count === 0) { + setTimeout(fn, 0); + } else { + then.push(fn); + } + }; + return fork; } -I.create = 0; -function o(t) { - if (!(this instanceof o)) - return d.fn(t) ? o.app.apply(o, arguments) : d.object(t) ? o.atlas.apply(o, arguments) : t; - I.create++; - for (var i = 0; i < et.length; i++) - et[i].call(this); +stats.create = 0; +function Stage(arg) { + if (!(this instanceof Stage)) { + if (is$1.fn(arg)) { + return Stage.app.apply(Stage, arguments); + } else if (is$1.object(arg)) { + return Stage.atlas.apply(Stage, arguments); + } else { + return arg; + } + } + stats.create++; + for (var i = 0; i < _init.length; i++) { + _init[i].call(this); + } } -var et = []; -o._init = function(t) { - et.push(t); -}; -var nt = []; -o._load = function(t) { - nt.push(t); -}; -var tt = {}; -o.config = function() { - if (arguments.length === 1 && d.string(arguments[0])) - return tt[arguments[0]]; - arguments.length === 1 && d.object(arguments[0]) && Object.assign(tt, arguments[0]), arguments.length === 2 && d.string(arguments[0]) && tt[arguments[1]]; -}; -var rt = [], D = [], dt = !1, Q = !1; -o.app = function(t, i) { - if (!dt) { - rt.push(arguments); +var _init = []; +Stage._init = function(fn) { + _init.push(fn); +}; +var _load = []; +Stage._load = function(fn) { + _load.push(fn); +}; +var _config = {}; +Stage.config = function() { + if (arguments.length === 1 && is$1.string(arguments[0])) { + return _config[arguments[0]]; + } + if (arguments.length === 1 && is$1.object(arguments[0])) { + Object.assign(_config, arguments[0]); + } + if (arguments.length === 2 && is$1.string(arguments[0])) { + _config[arguments[1]]; + } +}; +var _app_queue = []; +var _stages = []; +var _loaded = false; +var _paused = false; +Stage.app = function(app, opts) { + if (!_loaded) { + _app_queue.push(arguments); return; } console.log("Creating app..."); - var e = o.config("app-loader"); - e(function(n, r) { + var loader = Stage.config("app-loader"); + loader(function(stage, canvas) { console.log("Initing app..."); - for (var s = 0; s < nt.length; s++) - nt[s].call(this, n, r); - t(n, r), D.push(n), console.log("Starting app..."), n.start(); - }, i); -}; -var gt = Yt(); -o.preload = function(t) { - typeof t == "function" && t(gt()); -}; -o.start = function(t) { - console.log("Starting..."), o.config(t), gt.then(function() { - for (console.log("Loading apps..."), dt = !0; rt.length; ) { - var i = rt.shift(); - o.app.apply(o, i); + for (var i = 0; i < _load.length; i++) { + _load[i].call(this, stage, canvas); + } + app(stage, canvas); + _stages.push(stage); + console.log("Starting app..."); + stage.start(); + }, opts); +}; +var loading = _await(); +Stage.preload = function(load) { + if (typeof load !== "function") { + return; + } + load(loading()); +}; +Stage.start = function(config) { + console.log("Starting..."); + Stage.config(config); + loading.then(function() { + console.log("Loading apps..."); + _loaded = true; + while (_app_queue.length) { + var args = _app_queue.shift(); + Stage.app.apply(Stage, args); } }); }; -o.pause = function() { - if (!Q) { - Q = !0; - for (var t = D.length - 1; t >= 0; t--) - D[t].pause(); +Stage.pause = function() { + if (!_paused) { + _paused = true; + for (var i = _stages.length - 1; i >= 0; i--) { + _stages[i].pause(); + } } }; -o.resume = function() { - if (Q) { - Q = !1; - for (var t = D.length - 1; t >= 0; t--) - D[t].resume(); +Stage.resume = function() { + if (_paused) { + _paused = false; + for (var i = _stages.length - 1; i >= 0; i--) { + _stages[i].resume(); + } } }; -o.create = function() { - return new o(); +Stage.create = function() { + return new Stage(); }; -function m(t, i, e, n, r, s) { - this.reset(t, i, e, n, r, s); +function Matrix(a, b, c, d, e, f) { + this.reset(a, b, c, d, e, f); } -m.prototype.toString = function() { +Matrix.prototype.toString = function() { return "[" + this.a + ", " + this.b + ", " + this.c + ", " + this.d + ", " + this.e + ", " + this.f + "]"; }; -m.prototype.clone = function() { - return new m(this.a, this.b, this.c, this.d, this.e, this.f); -}; -m.prototype.reset = function(t, i, e, n, r, s) { - return this._dirty = !0, typeof t == "object" ? (this.a = t.a, this.d = t.d, this.b = t.b, this.c = t.c, this.e = t.e, this.f = t.f) : (this.a = t || 1, this.d = n || 1, this.b = i || 0, this.c = e || 0, this.e = r || 0, this.f = s || 0), this; +Matrix.prototype.clone = function() { + return new Matrix(this.a, this.b, this.c, this.d, this.e, this.f); +}; +Matrix.prototype.reset = function(a, b, c, d, e, f) { + this._dirty = true; + if (typeof a === "object") { + this.a = a.a, this.d = a.d; + this.b = a.b, this.c = a.c; + this.e = a.e, this.f = a.f; + } else { + this.a = a || 1, this.d = d || 1; + this.b = b || 0, this.c = c || 0; + this.e = e || 0, this.f = f || 0; + } + return this; }; -m.prototype.identity = function() { - return this._dirty = !0, this.a = 1, this.b = 0, this.c = 0, this.d = 1, this.e = 0, this.f = 0, this; +Matrix.prototype.identity = function() { + this._dirty = true; + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.e = 0; + this.f = 0; + return this; }; -m.prototype.rotate = function(t) { - if (!t) +Matrix.prototype.rotate = function(angle) { + if (!angle) { return this; - this._dirty = !0; - var i = t ? Math.cos(t) : 1, e = t ? Math.sin(t) : 0, n = i * this.a - e * this.b, r = i * this.b + e * this.a, s = i * this.c - e * this.d, a = i * this.d + e * this.c, c = i * this.e - e * this.f, u = i * this.f + e * this.e; - return this.a = n, this.b = r, this.c = s, this.d = a, this.e = c, this.f = u, this; + } + this._dirty = true; + var u = angle ? Math.cos(angle) : 1; + var v = angle ? Math.sin(angle) : 0; + var a = u * this.a - v * this.b; + var b = u * this.b + v * this.a; + var c = u * this.c - v * this.d; + var d = u * this.d + v * this.c; + var e = u * this.e - v * this.f; + var f = u * this.f + v * this.e; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + return this; }; -m.prototype.translate = function(t, i) { - return !t && !i ? this : (this._dirty = !0, this.e += t, this.f += i, this); +Matrix.prototype.translate = function(x, y) { + if (!x && !y) { + return this; + } + this._dirty = true; + this.e += x; + this.f += y; + return this; }; -m.prototype.scale = function(t, i) { - return !(t - 1) && !(i - 1) ? this : (this._dirty = !0, this.a *= t, this.b *= i, this.c *= t, this.d *= i, this.e *= t, this.f *= i, this); +Matrix.prototype.scale = function(x, y) { + if (!(x - 1) && !(y - 1)) { + return this; + } + this._dirty = true; + this.a *= x; + this.b *= y; + this.c *= x; + this.d *= y; + this.e *= x; + this.f *= y; + return this; }; -m.prototype.skew = function(t, i) { - if (!t && !i) +Matrix.prototype.skew = function(x, y) { + if (!x && !y) { return this; - this._dirty = !0; - var e = this.a + this.b * t, n = this.b + this.a * i, r = this.c + this.d * t, s = this.d + this.c * i, a = this.e + this.f * t, c = this.f + this.e * i; - return this.a = e, this.b = n, this.c = r, this.d = s, this.e = a, this.f = c, this; + } + this._dirty = true; + var a = this.a + this.b * x; + var b = this.b + this.a * y; + var c = this.c + this.d * x; + var d = this.d + this.c * y; + var e = this.e + this.f * x; + var f = this.f + this.e * y; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + return this; }; -m.prototype.concat = function(t) { - this._dirty = !0; - var i = this, e = i.a * t.a + i.b * t.c, n = i.b * t.d + i.a * t.b, r = i.c * t.a + i.d * t.c, s = i.d * t.d + i.c * t.b, a = i.e * t.a + t.e + i.f * t.c, c = i.f * t.d + t.f + i.e * t.b; - return this.a = e, this.b = n, this.c = r, this.d = s, this.e = a, this.f = c, this; +Matrix.prototype.concat = function(m) { + this._dirty = true; + var n = this; + var a = n.a * m.a + n.b * m.c; + var b = n.b * m.d + n.a * m.b; + var c = n.c * m.a + n.d * m.c; + var d = n.d * m.d + n.c * m.b; + var e = n.e * m.a + m.e + n.f * m.c; + var f = n.f * m.d + m.f + n.e * m.b; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + return this; }; -m.prototype.inverse = m.prototype.reverse = function() { +Matrix.prototype.inverse = Matrix.prototype.reverse = function() { if (this._dirty) { - this._dirty = !1, this.inversed = this.inversed || new m(); - var t = this.a * this.d - this.b * this.c; - this.inversed.a = this.d / t, this.inversed.b = -this.b / t, this.inversed.c = -this.c / t, this.inversed.d = this.a / t, this.inversed.e = (this.c * this.f - this.e * this.d) / t, this.inversed.f = (this.e * this.b - this.a * this.f) / t; + this._dirty = false; + this.inversed = this.inversed || new Matrix(); + var z = this.a * this.d - this.b * this.c; + this.inversed.a = this.d / z; + this.inversed.b = -this.b / z; + this.inversed.c = -this.c / z; + this.inversed.d = this.a / z; + this.inversed.e = (this.c * this.f - this.e * this.d) / z; + this.inversed.f = (this.e * this.b - this.a * this.f) / z; } return this.inversed; }; -m.prototype.map = function(t, i) { - return i = i || {}, i.x = this.a * t.x + this.c * t.y + this.e, i.y = this.b * t.x + this.d * t.y + this.f, i; -}; -m.prototype.mapX = function(t, i) { - return typeof t == "object" && (i = t.y, t = t.x), this.a * t + this.c * i + this.e; -}; -m.prototype.mapY = function(t, i) { - return typeof t == "object" && (i = t.y, t = t.x), this.b * t + this.d * i + this.f; -}; -var yt = Math; -const N = Object.create(Math); -N.random = function(t, i) { - return typeof t > "u" ? (i = 1, t = 0) : typeof i > "u" && (i = t, t = 0), t == i ? t : yt.random() * (i - t) + t; +Matrix.prototype.map = function(p, q) { + q = q || {}; + q.x = this.a * p.x + this.c * p.y + this.e; + q.y = this.b * p.x + this.d * p.y + this.f; + return q; +}; +Matrix.prototype.mapX = function(x, y) { + if (typeof x === "object") + y = x.y, x = x.x; + return this.a * x + this.c * y + this.e; +}; +Matrix.prototype.mapY = function(x, y) { + if (typeof x === "object") + y = x.y, x = x.x; + return this.b * x + this.d * y + this.f; +}; +var native = Math; +const math = Object.create(Math); +math.random = function(min, max) { + if (typeof min === "undefined") { + max = 1, min = 0; + } else if (typeof max === "undefined") { + max = min, min = 0; + } + return min == max ? min : native.random() * (max - min) + min; }; -N.rotate = function(t, i, e) { - return typeof i > "u" ? (e = 1, i = 0) : typeof e > "u" && (e = i, i = 0), e > i ? (t = (t - i) % (e - i), t + (t < 0 ? e : i)) : (t = (t - e) % (i - e), t + (t <= 0 ? i : e)); +math.rotate = function(num, min, max) { + if (typeof min === "undefined") { + max = 1, min = 0; + } else if (typeof max === "undefined") { + max = min, min = 0; + } + if (max > min) { + num = (num - min) % (max - min); + return num + (num < 0 ? max : min); + } else { + num = (num - max) % (min - max); + return num + (num <= 0 ? min : max); + } }; -N.limit = function(t, i, e) { - return t < i ? i : t > e ? e : t; +math.limit = function(num, min, max) { + if (num < min) { + return min; + } else if (num > max) { + return max; + } else { + return num; + } }; -N.length = function(t, i) { - return yt.sqrt(t * t + i * i); +math.length = function(x, y) { + return native.sqrt(x * x + y * y); }; -function R(t, i) { - typeof t == "object" && this.src(t, i); +function Texture(image, ratio) { + if (typeof image === "object") { + this.src(image, ratio); + } } -R.prototype.pipe = function() { - return new R(this); -}; -R.prototype.src = function(t, i, e, n) { - if (typeof t == "object") { - var r = t, s = i || 1; - this._image = r, this._sx = this._dx = 0, this._sy = this._dy = 0, this._sw = this._dw = r.width / s, this._sh = this._dh = r.height / s, this.width = r.width / s, this.height = r.height / s, this.ratio = s; - } else - typeof e > "u" ? (e = t, n = i) : (this._sx = t, this._sy = i), this._sw = this._dw = e, this._sh = this._dh = n, this.width = e, this.height = n; +Texture.prototype.pipe = function() { + return new Texture(this); +}; +Texture.prototype.src = function(x, y, w, h) { + if (typeof x === "object") { + var image = x, ratio = y || 1; + this._image = image; + this._sx = this._dx = 0; + this._sy = this._dy = 0; + this._sw = this._dw = image.width / ratio; + this._sh = this._dh = image.height / ratio; + this.width = image.width / ratio; + this.height = image.height / ratio; + this.ratio = ratio; + } else { + if (typeof w === "undefined") { + w = x, h = y; + } else { + this._sx = x, this._sy = y; + } + this._sw = this._dw = w; + this._sh = this._dh = h; + this.width = w; + this.height = h; + } return this; }; -R.prototype.dest = function(t, i, e, n) { - return this._dx = t, this._dy = i, this._dx = t, this._dy = i, typeof e < "u" && (this._dw = e, this._dh = n, this.width = e, this.height = n), this; -}; -R.prototype.draw = function(t, i, e, n, r, s, a, c, u) { - var p = this._image; - if (!(p === null || typeof p != "object")) { - var f = this._sx, _ = this._sy, l = this._sw, y = this._sh, k = this._dx, b = this._dy, T = this._dw, X = this._dh; - typeof s < "u" ? (i = N.limit(i, 0, this._sw), n = N.limit(n, 0, this._sw - i), e = N.limit(e, 0, this._sh), r = N.limit(r, 0, this._sh - e), f += i, _ += e, l = n, y = r, k = s, b = a, T = c, X = u) : typeof n < "u" ? (k = i, b = e, T = n, X = r) : typeof i < "u" && (T = i, X = e); - var v = this.ratio || 1; - f *= v, _ *= v, l *= v, y *= v; - try { - typeof p.draw == "function" ? p.draw(t, f, _, l, y, k, b, T, X) : (I.draw++, t.drawImage(p, f, _, l, y, k, b, T, X)); - } catch (C) { - p._draw_failed || (console.log("Unable to draw: ", p), console.log(C), p._draw_failed = !0); - } - } -}; -var st = {}, ot = []; -o.atlas = function(t) { - var i = d.fn(t.draw) ? t : new $(t); - t.name && (st[t.name] = i), ot.push(i), z(t, "imagePath"), z(t, "imageRatio"); - var e = t.imagePath, n = t.imageRatio || 1; - return d.string(t.image) ? e = t.image : d.hash(t.image) && (e = t.image.src || t.image.url, n = t.image.ratio || n), e && o.preload(function(r) { - console.log("Loading atlas: " + e); - var s = o.config("image-loader"); - s(e, function(a) { - console.log("Image loaded: " + e), i.src(a, n), r(); - }, function(a) { - console.log("Error loading atlas: " + e, a), r(); +Texture.prototype.dest = function(x, y, w, h) { + this._dx = x, this._dy = y; + this._dx = x, this._dy = y; + if (typeof w !== "undefined") { + this._dw = w, this._dh = h; + this.width = w, this.height = h; + } + return this; +}; +Texture.prototype.draw = function(context, x1, y1, x2, y2, x3, y3, x4, y4) { + var image = this._image; + if (image === null || typeof image !== "object") { + return; + } + var sx = this._sx, sy = this._sy; + var sw = this._sw, sh = this._sh; + var dx = this._dx, dy = this._dy; + var dw = this._dw, dh = this._dh; + if (typeof x3 !== "undefined") { + x1 = math.limit(x1, 0, this._sw), x2 = math.limit(x2, 0, this._sw - x1); + y1 = math.limit(y1, 0, this._sh), y2 = math.limit(y2, 0, this._sh - y1); + sx += x1, sy += y1, sw = x2, sh = y2; + dx = x3, dy = y3, dw = x4, dh = y4; + } else if (typeof x2 !== "undefined") { + dx = x1, dy = y1, dw = x2, dh = y2; + } else if (typeof x1 !== "undefined") { + dw = x1, dh = y1; + } + var ratio = this.ratio || 1; + sx *= ratio, sy *= ratio, sw *= ratio, sh *= ratio; + try { + if (typeof image.draw === "function") { + image.draw(context, sx, sy, sw, sh, dx, dy, dw, dh); + } else { + stats.draw++; + context.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh); + } + } catch (ex) { + if (!image._draw_failed) { + console.log("Unable to draw: ", image); + console.log(ex); + image._draw_failed = true; + } + } +}; +var _atlases_map = {}; +var _atlases_arr = []; +Stage.atlas = function(def) { + var atlas = is$1.fn(def.draw) ? def : new Atlas(def); + if (def.name) { + _atlases_map[def.name] = atlas; + } + _atlases_arr.push(atlas); + deprecated(def, "imagePath"); + deprecated(def, "imageRatio"); + var url = def.imagePath; + var ratio = def.imageRatio || 1; + if (is$1.string(def.image)) { + url = def.image; + } else if (is$1.hash(def.image)) { + url = def.image.src || def.image.url; + ratio = def.image.ratio || ratio; + } + url && Stage.preload(function(done) { + console.log("Loading atlas: " + url); + var imageloader = Stage.config("image-loader"); + imageloader(url, function(image) { + console.log("Image loaded: " + url); + atlas.src(image, ratio); + done(); + }, function(err) { + console.log("Error loading atlas: " + url, err); + done(); }); - }), i; -}; -$._super = R; -$.prototype = Object.create($._super.prototype); -function $(t) { - $._super.call(this); - var i = this; - z(t, "filter"), z(t, "cutouts"), z(t, "sprites"), z(t, "factory"); - var e = t.map || t.filter, n = t.ppu || t.ratio || 1, r = t.trim || 0, s = t.textures, a = t.factory, c = t.cutouts || t.sprites; - function u(f) { - if (!f || d.fn(f.draw)) - return f; - f = Object.assign({}, f), d.fn(e) && (f = e(f)), n != 1 && (f.x *= n, f.y *= n, f.width *= n, f.height *= n, f.top *= n, f.bottom *= n, f.left *= n, f.right *= n), r != 0 && (f.x += r, f.y += r, f.width -= 2 * r, f.height -= 2 * r, f.top -= r, f.bottom -= r, f.left -= r, f.right -= r); - var _ = i.pipe(); - return _.top = f.top, _.bottom = f.bottom, _.left = f.left, _.right = f.right, _.src(f.x, f.y, f.width, f.height), _; - } - function p(f) { - if (s) { - if (d.fn(s)) - return s(f); - if (d.hash(s)) - return s[f]; - } - if (c) { - for (var _ = null, l = 0, y = 0; y < c.length; y++) - string.startsWith(c[y].name, f) && (l === 0 ? _ = c[y] : l === 1 ? _ = [_, c[y]] : _.push(c[y]), l++); - return l === 0 && d.fn(a) && (_ = function(k) { - return a(f + (k || "")); - }), _; - } - } - this.select = function(f) { - if (!f) - return new q(this.pipe()); - var _ = p(f); - if (_) - return new q(_, p, u); + }); + return atlas; +}; +Atlas._super = Texture; +Atlas.prototype = Object.create(Atlas._super.prototype); +function Atlas(def) { + Atlas._super.call(this); + var atlas = this; + deprecated(def, "filter"); + deprecated(def, "cutouts"); + deprecated(def, "sprites"); + deprecated(def, "factory"); + var map = def.map || def.filter; + var ppu = def.ppu || def.ratio || 1; + var trim = def.trim || 0; + var textures = def.textures; + var factory = def.factory; + var cutouts = def.cutouts || def.sprites; + function make(def2) { + if (!def2 || is$1.fn(def2.draw)) { + return def2; + } + def2 = Object.assign({}, def2); + if (is$1.fn(map)) { + def2 = map(def2); + } + if (ppu != 1) { + def2.x *= ppu, def2.y *= ppu; + def2.width *= ppu, def2.height *= ppu; + def2.top *= ppu, def2.bottom *= ppu; + def2.left *= ppu, def2.right *= ppu; + } + if (trim != 0) { + def2.x += trim, def2.y += trim; + def2.width -= 2 * trim, def2.height -= 2 * trim; + def2.top -= trim, def2.bottom -= trim; + def2.left -= trim, def2.right -= trim; + } + var texture = atlas.pipe(); + texture.top = def2.top, texture.bottom = def2.bottom; + texture.left = def2.left, texture.right = def2.right; + texture.src(def2.x, def2.y, def2.width, def2.height); + return texture; + } + function find(query) { + if (textures) { + if (is$1.fn(textures)) { + return textures(query); + } else if (is$1.hash(textures)) { + return textures[query]; + } + } + if (cutouts) { + var result = null, n = 0; + for (var i = 0; i < cutouts.length; i++) { + if (string.startsWith(cutouts[i].name, query)) { + if (n === 0) { + result = cutouts[i]; + } else if (n === 1) { + result = [result, cutouts[i]]; + } else { + result.push(cutouts[i]); + } + n++; + } + } + if (n === 0 && is$1.fn(factory)) { + result = function(subquery) { + return factory(query + (subquery ? subquery : "")); + }; + } + return result; + } + } + this.select = function(query) { + if (!query) { + return new Selection(this.pipe()); + } + var found = find(query); + if (found) { + return new Selection(found, find, make); + } }; } -var P = new R(); -P.x = P.y = P.width = P.height = 0; -P.pipe = P.src = P.dest = function() { +var nfTexture = new Texture(); +nfTexture.x = nfTexture.y = nfTexture.width = nfTexture.height = 0; +nfTexture.pipe = nfTexture.src = nfTexture.dest = function() { return this; }; -P.draw = function() { -}; -var At = new q(P); -function q(t, i, e) { - function n(r, s) { - if (r) { - if (d.fn(r.draw)) - return r; - if (d.hash(r) && d.number(r.width) && d.number(r.height) && d.fn(e)) - return e(r); - if (d.hash(r) && d.defined(s)) - return n(r[s]); - if (d.fn(r)) - return n(r(s)); - if (d.array(r)) - return n(r[0]); - if (d.string(r) && d.fn(i)) - return n(i(r)); - } else - return P; - } - this.one = function(r) { - return n(t, r); - }, this.array = function(r) { - var s = d.array(r) ? r : []; - if (d.array(t)) - for (var a = 0; a < t.length; a++) - s[a] = n(t[a]); - else - s[0] = n(t); - return s; +nfTexture.draw = function() { +}; +var nfSelection = new Selection(nfTexture); +function Selection(result, find, make) { + function link(result2, subquery) { + if (!result2) { + return nfTexture; + } else if (is$1.fn(result2.draw)) { + return result2; + } else if (is$1.hash(result2) && is$1.number(result2.width) && is$1.number(result2.height) && is$1.fn(make)) { + return make(result2); + } else if (is$1.hash(result2) && is$1.defined(subquery)) { + return link(result2[subquery]); + } else if (is$1.fn(result2)) { + return link(result2(subquery)); + } else if (is$1.array(result2)) { + return link(result2[0]); + } else if (is$1.string(result2) && is$1.fn(find)) { + return link(find(result2)); + } + } + this.one = function(subquery) { + return link(result, subquery); + }; + this.array = function(arr) { + var array = is$1.array(arr) ? arr : []; + if (is$1.array(result)) { + for (var i = 0; i < result.length; i++) { + array[i] = link(result[i]); + } + } else { + array[0] = link(result); + } + return array; }; } -o.texture = function(t) { - if (!d.string(t)) - return new q(t); - var i = null, e, n; - for ((n = t.indexOf(":")) > 0 && t.length > n + 1 && (e = st[t.slice(0, n)], i = e && e.select(t.slice(n + 1))), !i && (e = st[t]) && (i = e.select()), n = 0; !i && n < ot.length; n++) - i = ot[n].select(t); - return i || (console.error("Texture not found: " + t), i = At), i; -}; -function z(t, i, e) { - i in t && console.log(e ? e.replace("%name", i) : "'" + i + "' field of texture atlas is deprecated."); +Stage.texture = function(query) { + if (!is$1.string(query)) { + return new Selection(query); + } + var result = null, atlas, i; + if ((i = query.indexOf(":")) > 0 && query.length > i + 1) { + atlas = _atlases_map[query.slice(0, i)]; + result = atlas && atlas.select(query.slice(i + 1)); + } + if (!result && (atlas = _atlases_map[query])) { + result = atlas.select(); + } + for (i = 0; !result && i < _atlases_arr.length; i++) { + result = _atlases_arr[i].select(query); + } + if (!result) { + console.error("Texture not found: " + query); + result = nfSelection; + } + return result; +}; +function deprecated(hash, name, msg) { + if (name in hash) + console.log(msg ? msg.replace("%name", name) : "'" + name + "' field of texture atlas is deprecated."); } -var j = 0; -o.prototype._label = ""; -o.prototype._visible = !0; -o.prototype._parent = null; -o.prototype._next = null; -o.prototype._prev = null; -o.prototype._first = null; -o.prototype._last = null; -o.prototype._attrs = null; -o.prototype._flags = null; -o.prototype.toString = function() { +var iid$1 = 0; +Stage.prototype._label = ""; +Stage.prototype._visible = true; +Stage.prototype._parent = null; +Stage.prototype._next = null; +Stage.prototype._prev = null; +Stage.prototype._first = null; +Stage.prototype._last = null; +Stage.prototype._attrs = null; +Stage.prototype._flags = null; +Stage.prototype.toString = function() { return "[" + this._label + "]"; }; -o.prototype.id = function(t) { - return this.label(t); +Stage.prototype.id = function(id) { + return this.label(id); }; -o.prototype.label = function(t) { - return typeof t > "u" ? this._label : (this._label = t, this); +Stage.prototype.label = function(label) { + if (typeof label === "undefined") { + return this._label; + } + this._label = label; + return this; }; -o.prototype.attr = function(t, i) { - return typeof i > "u" ? this._attrs !== null ? this._attrs[t] : void 0 : ((this._attrs !== null ? this._attrs : this._attrs = {})[t] = i, this); +Stage.prototype.attr = function(name, value) { + if (typeof value === "undefined") { + return this._attrs !== null ? this._attrs[name] : void 0; + } + (this._attrs !== null ? this._attrs : this._attrs = {})[name] = value; + return this; }; -o.prototype.visible = function(t) { - return typeof t > "u" ? this._visible : (this._visible = t, this._parent && (this._parent._ts_children = ++j), this._ts_pin = ++j, this.touch(), this); +Stage.prototype.visible = function(visible) { + if (typeof visible === "undefined") { + return this._visible; + } + this._visible = visible; + this._parent && (this._parent._ts_children = ++iid$1); + this._ts_pin = ++iid$1; + this.touch(); + return this; }; -o.prototype.hide = function() { - return this.visible(!1); +Stage.prototype.hide = function() { + return this.visible(false); }; -o.prototype.show = function() { - return this.visible(!0); +Stage.prototype.show = function() { + return this.visible(true); }; -o.prototype.parent = function() { +Stage.prototype.parent = function() { return this._parent; }; -o.prototype.next = function(t) { - for (var i = this._next; i && t && !i._visible; ) - i = i._next; - return i; +Stage.prototype.next = function(visible) { + var next = this._next; + while (next && visible && !next._visible) { + next = next._next; + } + return next; }; -o.prototype.prev = function(t) { - for (var i = this._prev; i && t && !i._visible; ) - i = i._prev; - return i; +Stage.prototype.prev = function(visible) { + var prev = this._prev; + while (prev && visible && !prev._visible) { + prev = prev._prev; + } + return prev; }; -o.prototype.first = function(t) { - for (var i = this._first; i && t && !i._visible; ) - i = i._next; - return i; +Stage.prototype.first = function(visible) { + var next = this._first; + while (next && visible && !next._visible) { + next = next._next; + } + return next; }; -o.prototype.last = function(t) { - for (var i = this._last; i && t && !i._visible; ) - i = i._prev; - return i; +Stage.prototype.last = function(visible) { + var prev = this._last; + while (prev && visible && !prev._visible) { + prev = prev._prev; + } + return prev; +}; +Stage.prototype.visit = function(visitor, data) { + var reverse = visitor.reverse; + var visible = visitor.visible; + if (visitor.start && visitor.start(this, data)) { + return; + } + var child, next = reverse ? this.last(visible) : this.first(visible); + while (child = next) { + next = reverse ? child.prev(visible) : child.next(visible); + if (child.visit(visitor, data)) { + return true; + } + } + return visitor.end && visitor.end(this, data); +}; +Stage.prototype.append = function(child, more) { + if (is$1.array(child)) + for (var i = 0; i < child.length; i++) + append(this, child[i]); + else if (typeof more !== "undefined") + for (var i = 0; i < arguments.length; i++) + append(this, arguments[i]); + else if (typeof child !== "undefined") + append(this, child); + return this; }; -o.prototype.visit = function(t, i) { - var e = t.reverse, n = t.visible; - if (!(t.start && t.start(this, i))) { - for (var r, s = e ? this.last(n) : this.first(n); r = s; ) - if (s = e ? r.prev(n) : r.next(n), r.visit(t, i)) - return !0; - return t.end && t.end(this, i); - } -}; -o.prototype.append = function(t, i) { - if (d.array(t)) - for (var e = 0; e < t.length; e++) - G(this, t[e]); - else if (typeof i < "u") - for (var e = 0; e < arguments.length; e++) - G(this, arguments[e]); - else - typeof t < "u" && G(this, t); +Stage.prototype.prepend = function(child, more) { + if (is$1.array(child)) + for (var i = child.length - 1; i >= 0; i--) + prepend(this, child[i]); + else if (typeof more !== "undefined") + for (var i = arguments.length - 1; i >= 0; i--) + prepend(this, arguments[i]); + else if (typeof child !== "undefined") + prepend(this, child); return this; }; -o.prototype.prepend = function(t, i) { - if (d.array(t)) - for (var e = t.length - 1; e >= 0; e--) - K(this, t[e]); - else if (typeof i < "u") - for (var e = arguments.length - 1; e >= 0; e--) - K(this, arguments[e]); - else - typeof t < "u" && K(this, t); +Stage.prototype.appendTo = function(parent) { + append(parent, this); return this; }; -o.prototype.appendTo = function(t) { - return G(t, this), this; -}; -o.prototype.prependTo = function(t) { - return K(t, this), this; -}; -o.prototype.insertNext = function(t, i) { - if (d.array(t)) - for (var e = 0; e < t.length; e++) - J(t[e], this); - else if (typeof i < "u") - for (var e = 0; e < arguments.length; e++) - J(arguments[e], this); - else - typeof t < "u" && J(t, this); +Stage.prototype.prependTo = function(parent) { + prepend(parent, this); return this; }; -o.prototype.insertPrev = function(t, i) { - if (d.array(t)) - for (var e = t.length - 1; e >= 0; e--) - U(t[e], this); - else if (typeof i < "u") - for (var e = arguments.length - 1; e >= 0; e--) - U(arguments[e], this); - else - typeof t < "u" && U(t, this); +Stage.prototype.insertNext = function(sibling, more) { + if (is$1.array(sibling)) + for (var i = 0; i < sibling.length; i++) + insertAfter(sibling[i], this); + else if (typeof more !== "undefined") + for (var i = 0; i < arguments.length; i++) + insertAfter(arguments[i], this); + else if (typeof sibling !== "undefined") + insertAfter(sibling, this); return this; }; -o.prototype.insertAfter = function(t) { - return J(this, t), this; +Stage.prototype.insertPrev = function(sibling, more) { + if (is$1.array(sibling)) + for (var i = sibling.length - 1; i >= 0; i--) + insertBefore(sibling[i], this); + else if (typeof more !== "undefined") + for (var i = arguments.length - 1; i >= 0; i--) + insertBefore(arguments[i], this); + else if (typeof sibling !== "undefined") + insertBefore(sibling, this); + return this; }; -o.prototype.insertBefore = function(t) { - return U(this, t), this; +Stage.prototype.insertAfter = function(prev) { + insertAfter(this, prev); + return this; +}; +Stage.prototype.insertBefore = function(next) { + insertBefore(this, next); + return this; }; -function G(t, i) { - S(i), S(t), i.remove(), t._last && (t._last._next = i, i._prev = t._last), i._parent = t, t._last = i, t._first || (t._first = i), i._parent._flag(i, !0), i._ts_parent = ++j, t._ts_children = ++j, t.touch(); +function append(parent, child) { + _ensure(child); + _ensure(parent); + child.remove(); + if (parent._last) { + parent._last._next = child; + child._prev = parent._last; + } + child._parent = parent; + parent._last = child; + if (!parent._first) { + parent._first = child; + } + child._parent._flag(child, true); + child._ts_parent = ++iid$1; + parent._ts_children = ++iid$1; + parent.touch(); } -function K(t, i) { - S(i), S(t), i.remove(), t._first && (t._first._prev = i, i._next = t._first), i._parent = t, t._first = i, t._last || (t._last = i), i._parent._flag(i, !0), i._ts_parent = ++j, t._ts_children = ++j, t.touch(); +function prepend(parent, child) { + _ensure(child); + _ensure(parent); + child.remove(); + if (parent._first) { + parent._first._prev = child; + child._next = parent._first; + } + child._parent = parent; + parent._first = child; + if (!parent._last) { + parent._last = child; + } + child._parent._flag(child, true); + child._ts_parent = ++iid$1; + parent._ts_children = ++iid$1; + parent.touch(); } -function U(t, i) { - S(t), S(i), t.remove(); - var e = i._parent, n = i._prev; - i._prev = t, n && (n._next = t) || e && (e._first = t), t._parent = e, t._prev = n, t._next = i, t._parent._flag(t, !0), t._ts_parent = ++j, t.touch(); +function insertBefore(self, next) { + _ensure(self); + _ensure(next); + self.remove(); + var parent = next._parent; + var prev = next._prev; + next._prev = self; + prev && (prev._next = self) || parent && (parent._first = self); + self._parent = parent; + self._prev = prev; + self._next = next; + self._parent._flag(self, true); + self._ts_parent = ++iid$1; + self.touch(); } -function J(t, i) { - S(t), S(i), t.remove(); - var e = i._parent, n = i._next; - i._next = t, n && (n._prev = t) || e && (e._last = t), t._parent = e, t._prev = i, t._next = n, t._parent._flag(t, !0), t._ts_parent = ++j, t.touch(); +function insertAfter(self, prev) { + _ensure(self); + _ensure(prev); + self.remove(); + var parent = prev._parent; + var next = prev._next; + prev._next = self; + next && (next._prev = self) || parent && (parent._last = self); + self._parent = parent; + self._prev = prev; + self._next = next; + self._parent._flag(self, true); + self._ts_parent = ++iid$1; + self.touch(); } -o.prototype.remove = function(t, i) { - if (typeof t < "u") { - if (d.array(t)) - for (var e = 0; e < t.length; e++) - S(t[e]).remove(); - else if (typeof i < "u") - for (var e = 0; e < arguments.length; e++) - S(arguments[e]).remove(); - else - S(t).remove(); +Stage.prototype.remove = function(child, more) { + if (typeof child !== "undefined") { + if (is$1.array(child)) { + for (var i = 0; i < child.length; i++) + _ensure(child[i]).remove(); + } else if (typeof more !== "undefined") { + for (var i = 0; i < arguments.length; i++) + _ensure(arguments[i]).remove(); + } else { + _ensure(child).remove(); + } return this; } - return this._prev && (this._prev._next = this._next), this._next && (this._next._prev = this._prev), this._parent && (this._parent._first === this && (this._parent._first = this._next), this._parent._last === this && (this._parent._last = this._prev), this._parent._flag(this, !1), this._parent._ts_children = ++j, this._parent.touch()), this._prev = this._next = this._parent = null, this._ts_parent = ++j, this; + if (this._prev) { + this._prev._next = this._next; + } + if (this._next) { + this._next._prev = this._prev; + } + if (this._parent) { + if (this._parent._first === this) { + this._parent._first = this._next; + } + if (this._parent._last === this) { + this._parent._last = this._prev; + } + this._parent._flag(this, false); + this._parent._ts_children = ++iid$1; + this._parent.touch(); + } + this._prev = this._next = this._parent = null; + this._ts_parent = ++iid$1; + return this; }; -o.prototype.empty = function() { - for (var t, i = this._first; t = i; ) - i = t._next, t._prev = t._next = t._parent = null, this._flag(t, !1); - return this._first = this._last = null, this._ts_children = ++j, this.touch(), this; +Stage.prototype.empty = function() { + var child, next = this._first; + while (child = next) { + next = child._next; + child._prev = child._next = child._parent = null; + this._flag(child, false); + } + this._first = this._last = null; + this._ts_children = ++iid$1; + this.touch(); + return this; }; -o.prototype.touch = function() { - return this._ts_touch = ++j, this._parent && this._parent.touch(), this; +Stage.prototype.touch = function() { + this._ts_touch = ++iid$1; + this._parent && this._parent.touch(); + return this; }; -o.prototype._flag = function(t, i) { - if (typeof i > "u") - return this._flags !== null && this._flags[t] || 0; - if (typeof t == "string" && (i ? (this._flags = this._flags || {}, !this._flags[t] && this._parent && this._parent._flag(t, !0), this._flags[t] = (this._flags[t] || 0) + 1) : this._flags && this._flags[t] > 0 && (this._flags[t] == 1 && this._parent && this._parent._flag(t, !1), this._flags[t] = this._flags[t] - 1)), typeof t == "object" && t._flags) - for (var e in t._flags) - t._flags[e] > 0 && this._flag(e, i); +Stage.prototype._flag = function(obj, name) { + if (typeof name === "undefined") { + return this._flags !== null && this._flags[obj] || 0; + } + if (typeof obj === "string") { + if (name) { + this._flags = this._flags || {}; + if (!this._flags[obj] && this._parent) { + this._parent._flag(obj, true); + } + this._flags[obj] = (this._flags[obj] || 0) + 1; + } else if (this._flags && this._flags[obj] > 0) { + if (this._flags[obj] == 1 && this._parent) { + this._parent._flag(obj, false); + } + this._flags[obj] = this._flags[obj] - 1; + } + } + if (typeof obj === "object") { + if (obj._flags) { + for (var type in obj._flags) { + if (obj._flags[type] > 0) { + this._flag(type, name); + } + } + } + } return this; }; -o.prototype.hitTest = function(t) { - var i = this._pin._width, e = this._pin._height; - return t.x >= 0 && t.x <= i && t.y >= 0 && t.y <= e; +Stage.prototype.hitTest = function(hit) { + var width = this._pin._width; + var height = this._pin._height; + return hit.x >= 0 && hit.x <= width && hit.y >= 0 && hit.y <= height; }; -function S(t) { - if (t && t instanceof o) - return t; - throw "Invalid node: " + t; +function _ensure(obj) { + if (obj && obj instanceof Stage) { + return obj; + } + throw "Invalid node: " + obj; } -function Mt(t, i) { - t._listeners = null, t.on = t.listen = function(e, n) { - if (!e || !e.length || typeof n != "function") +function listenable(prototype, callback) { + prototype._listeners = null; + prototype.on = prototype.listen = function(types, listener) { + if (!types || !types.length || typeof listener !== "function") { return this; - this._listeners === null && (this._listeners = {}); - var r = typeof e != "string" && typeof e.join == "function"; - if (e = (r ? e.join(" ") : e).match(/\S+/g)) - for (var s = 0; s < e.length; s++) { - var a = e[s]; - this._listeners[a] = this._listeners[a] || [], this._listeners[a].push(n), typeof i == "function" && i(this, a, !0); + } + if (this._listeners === null) { + this._listeners = {}; + } + var isarray = typeof types !== "string" && typeof types.join === "function"; + if (types = (isarray ? types.join(" ") : types).match(/\S+/g)) { + for (var i = 0; i < types.length; i++) { + var type = types[i]; + this._listeners[type] = this._listeners[type] || []; + this._listeners[type].push(listener); + if (typeof callback === "function") { + callback(this, type, true); + } } + } return this; - }, t.off = function(e, n) { - if (!e || !e.length || typeof n != "function") + }; + prototype.off = function(types, listener) { + if (!types || !types.length || typeof listener !== "function") { return this; - if (this._listeners === null) + } + if (this._listeners === null) { return this; - var r = typeof e != "string" && typeof e.join == "function"; - if (e = (r ? e.join(" ") : e).match(/\S+/g)) - for (var s = 0; s < e.length; s++) { - var a = e[s], c = this._listeners[a], u; - c && (u = c.indexOf(n)) >= 0 && (c.splice(u, 1), c.length || delete this._listeners[a], typeof i == "function" && i(this, a, !1)); + } + var isarray = typeof types !== "string" && typeof types.join === "function"; + if (types = (isarray ? types.join(" ") : types).match(/\S+/g)) { + for (var i = 0; i < types.length; i++) { + var type = types[i], all = this._listeners[type], index; + if (all && (index = all.indexOf(listener)) >= 0) { + all.splice(index, 1); + if (!all.length) { + delete this._listeners[type]; + } + if (typeof callback === "function") { + callback(this, type, false); + } + } } + } return this; - }, t.listeners = function(e) { - return this._listeners && this._listeners[e]; - }, t.publish = function(e, n) { - var r = this.listeners(e); - if (!r || !r.length) + }; + prototype.listeners = function(type) { + return this._listeners && this._listeners[type]; + }; + prototype.publish = function(name, args) { + var listeners = this.listeners(name); + if (!listeners || !listeners.length) { return 0; - for (var s = 0; s < r.length; s++) - r[s].apply(this, n); - return r.length; - }, t.trigger = function(e, n) { - return this.publish(e, n), this; + } + for (var l = 0; l < listeners.length; l++) { + listeners[l].apply(this, args); + } + return listeners.length; + }; + prototype.trigger = function(name, args) { + this.publish(name, args); + return this; }; } -var g = 0; -o._init(function() { - this._pin = new O(this); +var iid = 0; +Stage._init(function() { + this._pin = new Pin(this); }); -o.prototype.matrix = function(t) { - return t === !0 ? this._pin.relativeMatrix() : this._pin.absoluteMatrix(); -}; -o.prototype.pin = function(t, i) { - if (typeof t == "object") - return this._pin.set(t), this; - if (typeof t == "string") - return typeof i > "u" ? this._pin.get(t) : (this._pin.set(t, i), this); - if (typeof t > "u") +Stage.prototype.matrix = function(relative) { + if (relative === true) { + return this._pin.relativeMatrix(); + } + return this._pin.absoluteMatrix(); +}; +Stage.prototype.pin = function(a, b) { + if (typeof a === "object") { + this._pin.set(a); + return this; + } else if (typeof a === "string") { + if (typeof b === "undefined") { + return this._pin.get(a); + } else { + this._pin.set(a, b); + return this; + } + } else if (typeof a === "undefined") { return this._pin; + } }; -function O(t) { - this._owner = t, this._parent = null, this._relativeMatrix = new m(), this._absoluteMatrix = new m(), this.reset(); +function Pin(owner) { + this._owner = owner; + this._parent = null; + this._relativeMatrix = new Matrix(); + this._absoluteMatrix = new Matrix(); + this.reset(); } -O.prototype.reset = function() { - this._textureAlpha = 1, this._alpha = 1, this._width = 0, this._height = 0, this._scaleX = 1, this._scaleY = 1, this._skewX = 0, this._skewY = 0, this._rotation = 0, this._pivoted = !1, this._pivotX = null, this._pivotY = null, this._handled = !1, this._handleX = 0, this._handleY = 0, this._aligned = !1, this._alignX = 0, this._alignY = 0, this._offsetX = 0, this._offsetY = 0, this._boxX = 0, this._boxY = 0, this._boxWidth = this._width, this._boxHeight = this._height, this._ts_translate = ++g, this._ts_transform = ++g, this._ts_matrix = ++g; -}; -O.prototype._update = function() { - return this._parent = this._owner._parent && this._owner._parent._pin, this._handled && this._mo_handle != this._ts_transform && (this._mo_handle = this._ts_transform, this._ts_translate = ++g), this._aligned && this._parent && this._mo_align != this._parent._ts_transform && (this._mo_align = this._parent._ts_transform, this._ts_translate = ++g), this; +Pin.prototype.reset = function() { + this._textureAlpha = 1; + this._alpha = 1; + this._width = 0; + this._height = 0; + this._scaleX = 1; + this._scaleY = 1; + this._skewX = 0; + this._skewY = 0; + this._rotation = 0; + this._pivoted = false; + this._pivotX = null; + this._pivotY = null; + this._handled = false; + this._handleX = 0; + this._handleY = 0; + this._aligned = false; + this._alignX = 0; + this._alignY = 0; + this._offsetX = 0; + this._offsetY = 0; + this._boxX = 0; + this._boxY = 0; + this._boxWidth = this._width; + this._boxHeight = this._height; + this._ts_translate = ++iid; + this._ts_transform = ++iid; + this._ts_matrix = ++iid; +}; +Pin.prototype._update = function() { + this._parent = this._owner._parent && this._owner._parent._pin; + if (this._handled && this._mo_handle != this._ts_transform) { + this._mo_handle = this._ts_transform; + this._ts_translate = ++iid; + } + if (this._aligned && this._parent && this._mo_align != this._parent._ts_transform) { + this._mo_align = this._parent._ts_transform; + this._ts_translate = ++iid; + } + return this; }; -O.prototype.toString = function() { +Pin.prototype.toString = function() { return this._owner + " (" + (this._parent ? this._parent._owner : null) + ")"; }; -O.prototype.absoluteMatrix = function() { +Pin.prototype.absoluteMatrix = function() { this._update(); - var t = Math.max( + var ts = Math.max( this._ts_transform, this._ts_translate, this._parent ? this._parent._ts_matrix : 0 ); - if (this._mo_abs == t) + if (this._mo_abs == ts) { return this._absoluteMatrix; - this._mo_abs = t; - var i = this._absoluteMatrix; - return i.reset(this.relativeMatrix()), this._parent && i.concat(this._parent._absoluteMatrix), this._ts_matrix = ++g, i; -}; -O.prototype.relativeMatrix = function() { + } + this._mo_abs = ts; + var abs2 = this._absoluteMatrix; + abs2.reset(this.relativeMatrix()); + this._parent && abs2.concat(this._parent._absoluteMatrix); + this._ts_matrix = ++iid; + return abs2; +}; +Pin.prototype.relativeMatrix = function() { this._update(); - var t = Math.max( + var ts = Math.max( this._ts_transform, this._ts_translate, this._parent ? this._parent._ts_transform : 0 ); - if (this._mo_rel == t) + if (this._mo_rel == ts) { return this._relativeMatrix; - this._mo_rel = t; - var i = this._relativeMatrix; - if (i.identity(), this._pivoted && i.translate(-this._pivotX * this._width, -this._pivotY * this._height), i.scale(this._scaleX, this._scaleY), i.skew(this._skewX, this._skewY), i.rotate(this._rotation), this._pivoted && i.translate(this._pivotX * this._width, this._pivotY * this._height), this._pivoted) - this._boxX = 0, this._boxY = 0, this._boxWidth = this._width, this._boxHeight = this._height; - else { - var e, n; - i.a > 0 && i.c > 0 || i.a < 0 && i.c < 0 ? (e = 0, n = i.a * this._width + i.c * this._height) : (e = i.a * this._width, n = i.c * this._height), e > n ? (this._boxX = n, this._boxWidth = e - n) : (this._boxX = e, this._boxWidth = n - e), i.b > 0 && i.d > 0 || i.b < 0 && i.d < 0 ? (e = 0, n = i.b * this._width + i.d * this._height) : (e = i.b * this._width, n = i.d * this._height), e > n ? (this._boxY = n, this._boxHeight = e - n) : (this._boxY = e, this._boxHeight = n - e); - } - return this._x = this._offsetX, this._y = this._offsetY, this._x -= this._boxX + this._handleX * this._boxWidth, this._y -= this._boxY + this._handleY * this._boxHeight, this._aligned && this._parent && (this._parent.relativeMatrix(), this._x += this._alignX * this._parent._width, this._y += this._alignY * this._parent._height), i.translate(this._x, this._y), this._relativeMatrix; -}; -O.prototype.get = function(t) { - if (typeof ft[t] == "function") - return ft[t](this); -}; -O.prototype.set = function(t, i) { - if (typeof t == "string") - typeof Z[t] == "function" && typeof i < "u" && Z[t](this, i); - else if (typeof t == "object") - for (i in t) - typeof Z[i] == "function" && typeof t[i] < "u" && Z[i](this, t[i], t); - return this._owner && (this._owner._ts_pin = ++g, this._owner.touch()), this; -}; -var ft = { - alpha: function(t) { - return t._alpha; + } + this._mo_rel = ts; + var rel2 = this._relativeMatrix; + rel2.identity(); + if (this._pivoted) { + rel2.translate(-this._pivotX * this._width, -this._pivotY * this._height); + } + rel2.scale(this._scaleX, this._scaleY); + rel2.skew(this._skewX, this._skewY); + rel2.rotate(this._rotation); + if (this._pivoted) { + rel2.translate(this._pivotX * this._width, this._pivotY * this._height); + } + if (this._pivoted) { + this._boxX = 0; + this._boxY = 0; + this._boxWidth = this._width; + this._boxHeight = this._height; + } else { + var p, q; + if (rel2.a > 0 && rel2.c > 0 || rel2.a < 0 && rel2.c < 0) { + p = 0, q = rel2.a * this._width + rel2.c * this._height; + } else { + p = rel2.a * this._width, q = rel2.c * this._height; + } + if (p > q) { + this._boxX = q; + this._boxWidth = p - q; + } else { + this._boxX = p; + this._boxWidth = q - p; + } + if (rel2.b > 0 && rel2.d > 0 || rel2.b < 0 && rel2.d < 0) { + p = 0, q = rel2.b * this._width + rel2.d * this._height; + } else { + p = rel2.b * this._width, q = rel2.d * this._height; + } + if (p > q) { + this._boxY = q; + this._boxHeight = p - q; + } else { + this._boxY = p; + this._boxHeight = q - p; + } + } + this._x = this._offsetX; + this._y = this._offsetY; + this._x -= this._boxX + this._handleX * this._boxWidth; + this._y -= this._boxY + this._handleY * this._boxHeight; + if (this._aligned && this._parent) { + this._parent.relativeMatrix(); + this._x += this._alignX * this._parent._width; + this._y += this._alignY * this._parent._height; + } + rel2.translate(this._x, this._y); + return this._relativeMatrix; +}; +Pin.prototype.get = function(key) { + if (typeof getters[key] === "function") { + return getters[key](this); + } +}; +Pin.prototype.set = function(a, b) { + if (typeof a === "string") { + if (typeof setters[a] === "function" && typeof b !== "undefined") { + setters[a](this, b); + } + } else if (typeof a === "object") { + for (b in a) { + if (typeof setters[b] === "function" && typeof a[b] !== "undefined") { + setters[b](this, a[b], a); + } + } + } + if (this._owner) { + this._owner._ts_pin = ++iid; + this._owner.touch(); + } + return this; +}; +var getters = { + alpha: function(pin) { + return pin._alpha; }, - textureAlpha: function(t) { - return t._textureAlpha; + textureAlpha: function(pin) { + return pin._textureAlpha; }, - width: function(t) { - return t._width; + width: function(pin) { + return pin._width; }, - height: function(t) { - return t._height; + height: function(pin) { + return pin._height; }, - boxWidth: function(t) { - return t._boxWidth; + boxWidth: function(pin) { + return pin._boxWidth; }, - boxHeight: function(t) { - return t._boxHeight; + boxHeight: function(pin) { + return pin._boxHeight; }, // scale : function(pin) { // }, - scaleX: function(t) { - return t._scaleX; + scaleX: function(pin) { + return pin._scaleX; }, - scaleY: function(t) { - return t._scaleY; + scaleY: function(pin) { + return pin._scaleY; }, // skew : function(pin) { // }, - skewX: function(t) { - return t._skewX; + skewX: function(pin) { + return pin._skewX; }, - skewY: function(t) { - return t._skewY; + skewY: function(pin) { + return pin._skewY; }, - rotation: function(t) { - return t._rotation; + rotation: function(pin) { + return pin._rotation; }, // pivot : function(pin) { // }, - pivotX: function(t) { - return t._pivotX; + pivotX: function(pin) { + return pin._pivotX; }, - pivotY: function(t) { - return t._pivotY; + pivotY: function(pin) { + return pin._pivotY; }, // offset : function(pin) { // }, - offsetX: function(t) { - return t._offsetX; + offsetX: function(pin) { + return pin._offsetX; }, - offsetY: function(t) { - return t._offsetY; + offsetY: function(pin) { + return pin._offsetY; }, // align : function(pin) { // }, - alignX: function(t) { - return t._alignX; + alignX: function(pin) { + return pin._alignX; }, - alignY: function(t) { - return t._alignY; + alignY: function(pin) { + return pin._alignY; }, // handle : function(pin) { // }, - handleX: function(t) { - return t._handleX; + handleX: function(pin) { + return pin._handleX; }, - handleY: function(t) { - return t._handleY; + handleY: function(pin) { + return pin._handleY; } -}, Z = { - alpha: function(t, i) { - t._alpha = i; +}; +var setters = { + alpha: function(pin, value) { + pin._alpha = value; }, - textureAlpha: function(t, i) { - t._textureAlpha = i; + textureAlpha: function(pin, value) { + pin._textureAlpha = value; }, - width: function(t, i) { - t._width_ = i, t._width = i, t._ts_transform = ++g; + width: function(pin, value) { + pin._width_ = value; + pin._width = value; + pin._ts_transform = ++iid; }, - height: function(t, i) { - t._height_ = i, t._height = i, t._ts_transform = ++g; + height: function(pin, value) { + pin._height_ = value; + pin._height = value; + pin._ts_transform = ++iid; }, - scale: function(t, i) { - t._scaleX = i, t._scaleY = i, t._ts_transform = ++g; + scale: function(pin, value) { + pin._scaleX = value; + pin._scaleY = value; + pin._ts_transform = ++iid; }, - scaleX: function(t, i) { - t._scaleX = i, t._ts_transform = ++g; + scaleX: function(pin, value) { + pin._scaleX = value; + pin._ts_transform = ++iid; }, - scaleY: function(t, i) { - t._scaleY = i, t._ts_transform = ++g; + scaleY: function(pin, value) { + pin._scaleY = value; + pin._ts_transform = ++iid; }, - skew: function(t, i) { - t._skewX = i, t._skewY = i, t._ts_transform = ++g; + skew: function(pin, value) { + pin._skewX = value; + pin._skewY = value; + pin._ts_transform = ++iid; }, - skewX: function(t, i) { - t._skewX = i, t._ts_transform = ++g; + skewX: function(pin, value) { + pin._skewX = value; + pin._ts_transform = ++iid; }, - skewY: function(t, i) { - t._skewY = i, t._ts_transform = ++g; + skewY: function(pin, value) { + pin._skewY = value; + pin._ts_transform = ++iid; }, - rotation: function(t, i) { - t._rotation = i, t._ts_transform = ++g; + rotation: function(pin, value) { + pin._rotation = value; + pin._ts_transform = ++iid; }, - pivot: function(t, i) { - t._pivotX = i, t._pivotY = i, t._pivoted = !0, t._ts_transform = ++g; + pivot: function(pin, value) { + pin._pivotX = value; + pin._pivotY = value; + pin._pivoted = true; + pin._ts_transform = ++iid; }, - pivotX: function(t, i) { - t._pivotX = i, t._pivoted = !0, t._ts_transform = ++g; + pivotX: function(pin, value) { + pin._pivotX = value; + pin._pivoted = true; + pin._ts_transform = ++iid; }, - pivotY: function(t, i) { - t._pivotY = i, t._pivoted = !0, t._ts_transform = ++g; + pivotY: function(pin, value) { + pin._pivotY = value; + pin._pivoted = true; + pin._ts_transform = ++iid; }, - offset: function(t, i) { - t._offsetX = i, t._offsetY = i, t._ts_translate = ++g; + offset: function(pin, value) { + pin._offsetX = value; + pin._offsetY = value; + pin._ts_translate = ++iid; }, - offsetX: function(t, i) { - t._offsetX = i, t._ts_translate = ++g; + offsetX: function(pin, value) { + pin._offsetX = value; + pin._ts_translate = ++iid; }, - offsetY: function(t, i) { - t._offsetY = i, t._ts_translate = ++g; + offsetY: function(pin, value) { + pin._offsetY = value; + pin._ts_translate = ++iid; }, - align: function(t, i) { - this.alignX(t, i), this.alignY(t, i); + align: function(pin, value) { + this.alignX(pin, value); + this.alignY(pin, value); }, - alignX: function(t, i) { - t._alignX = i, t._aligned = !0, t._ts_translate = ++g, this.handleX(t, i); + alignX: function(pin, value) { + pin._alignX = value; + pin._aligned = true; + pin._ts_translate = ++iid; + this.handleX(pin, value); }, - alignY: function(t, i) { - t._alignY = i, t._aligned = !0, t._ts_translate = ++g, this.handleY(t, i); + alignY: function(pin, value) { + pin._alignY = value; + pin._aligned = true; + pin._ts_translate = ++iid; + this.handleY(pin, value); }, - handle: function(t, i) { - this.handleX(t, i), this.handleY(t, i); + handle: function(pin, value) { + this.handleX(pin, value); + this.handleY(pin, value); }, - handleX: function(t, i) { - t._handleX = i, t._handled = !0, t._ts_translate = ++g; + handleX: function(pin, value) { + pin._handleX = value; + pin._handled = true; + pin._ts_translate = ++iid; }, - handleY: function(t, i) { - t._handleY = i, t._handled = !0, t._ts_translate = ++g; + handleY: function(pin, value) { + pin._handleY = value; + pin._handled = true; + pin._ts_translate = ++iid; }, - resizeMode: function(t, i, e) { - e && (i == "in" ? i = "in-pad" : i == "out" && (i = "out-crop"), W(t, e.resizeWidth, e.resizeHeight, i)); + resizeMode: function(pin, value, all) { + if (all) { + if (value == "in") { + value = "in-pad"; + } else if (value == "out") { + value = "out-crop"; + } + scaleTo(pin, all.resizeWidth, all.resizeHeight, value); + } }, - resizeWidth: function(t, i, e) { - (!e || !e.resizeMode) && W(t, i, null); + resizeWidth: function(pin, value, all) { + if (!all || !all.resizeMode) { + scaleTo(pin, value, null); + } }, - resizeHeight: function(t, i, e) { - (!e || !e.resizeMode) && W(t, null, i); + resizeHeight: function(pin, value, all) { + if (!all || !all.resizeMode) { + scaleTo(pin, null, value); + } }, - scaleMode: function(t, i, e) { - e && W(t, e.scaleWidth, e.scaleHeight, i); + scaleMode: function(pin, value, all) { + if (all) { + scaleTo(pin, all.scaleWidth, all.scaleHeight, value); + } }, - scaleWidth: function(t, i, e) { - (!e || !e.scaleMode) && W(t, i, null); + scaleWidth: function(pin, value, all) { + if (!all || !all.scaleMode) { + scaleTo(pin, value, null); + } }, - scaleHeight: function(t, i, e) { - (!e || !e.scaleMode) && W(t, null, i); + scaleHeight: function(pin, value, all) { + if (!all || !all.scaleMode) { + scaleTo(pin, null, value); + } }, - matrix: function(t, i) { - this.scaleX(t, i.a), this.skewX(t, i.c / i.d), this.skewY(t, i.b / i.a), this.scaleY(t, i.d), this.offsetX(t, i.e), this.offsetY(t, i.f), this.rotation(t, 0); + matrix: function(pin, value) { + this.scaleX(pin, value.a); + this.skewX(pin, value.c / value.d); + this.skewY(pin, value.b / value.a); + this.scaleY(pin, value.d); + this.offsetX(pin, value.e); + this.offsetY(pin, value.f); + this.rotation(pin, 0); } }; -function W(t, i, e, n) { - var r = typeof i == "number", s = typeof e == "number", a = typeof n == "string"; - t._ts_transform = ++g, r && (t._scaleX = i / t._width_, t._width = t._width_), s && (t._scaleY = e / t._height_, t._height = t._height_), r && s && a && (n == "out" || n == "out-crop" ? t._scaleX = t._scaleY = Math.max(t._scaleX, t._scaleY) : (n == "in" || n == "in-pad") && (t._scaleX = t._scaleY = Math.min(t._scaleX, t._scaleY)), (n == "out-crop" || n == "in-pad") && (t._width = i / t._scaleX, t._height = e / t._scaleY)); +function scaleTo(pin, width, height, mode) { + var w = typeof width === "number"; + var h = typeof height === "number"; + var m = typeof mode === "string"; + pin._ts_transform = ++iid; + if (w) { + pin._scaleX = width / pin._width_; + pin._width = pin._width_; + } + if (h) { + pin._scaleY = height / pin._height_; + pin._height = pin._height_; + } + if (w && h && m) { + if (mode == "out" || mode == "out-crop") { + pin._scaleX = pin._scaleY = Math.max(pin._scaleX, pin._scaleY); + } else if (mode == "in" || mode == "in-pad") { + pin._scaleX = pin._scaleY = Math.min(pin._scaleX, pin._scaleY); + } + if (mode == "out-crop" || mode == "in-pad") { + pin._width = width / pin._scaleX; + pin._height = height / pin._scaleY; + } + } } -o.prototype.scaleTo = function(t, i, e) { - return typeof t == "object" && (e = i, i = t.y, t = t.x), W(this._pin, t, i, e), this; -}; -O._add_shortcuts = function(t) { - t.prototype.size = function(i, e) { - return this.pin("width", i), this.pin("height", e), this; - }, t.prototype.width = function(i) { - return typeof i > "u" ? this.pin("width") : (this.pin("width", i), this); - }, t.prototype.height = function(i) { - return typeof i > "u" ? this.pin("height") : (this.pin("height", i), this); - }, t.prototype.offset = function(i, e) { - return typeof i == "object" && (e = i.y, i = i.x), this.pin("offsetX", i), this.pin("offsetY", e), this; - }, t.prototype.rotate = function(i) { - return this.pin("rotation", i), this; - }, t.prototype.skew = function(i, e) { - return typeof i == "object" ? (e = i.y, i = i.x) : typeof e > "u" && (e = i), this.pin("skewX", i), this.pin("skewY", e), this; - }, t.prototype.scale = function(i, e) { - return typeof i == "object" ? (e = i.y, i = i.x) : typeof e > "u" && (e = i), this.pin("scaleX", i), this.pin("scaleY", e), this; - }, t.prototype.alpha = function(i, e) { - return this.pin("alpha", i), typeof e < "u" && this.pin("textureAlpha", e), this; +Stage.prototype.scaleTo = function(a, b, c) { + if (typeof a === "object") + c = b, b = a.y, a = a.x; + scaleTo(this._pin, a, b, c); + return this; +}; +Pin._add_shortcuts = function(Stage2) { + Stage2.prototype.size = function(w, h) { + this.pin("width", w); + this.pin("height", h); + return this; + }; + Stage2.prototype.width = function(w) { + if (typeof w === "undefined") { + return this.pin("width"); + } + this.pin("width", w); + return this; + }; + Stage2.prototype.height = function(h) { + if (typeof h === "undefined") { + return this.pin("height"); + } + this.pin("height", h); + return this; + }; + Stage2.prototype.offset = function(a, b) { + if (typeof a === "object") + b = a.y, a = a.x; + this.pin("offsetX", a); + this.pin("offsetY", b); + return this; + }; + Stage2.prototype.rotate = function(a) { + this.pin("rotation", a); + return this; + }; + Stage2.prototype.skew = function(a, b) { + if (typeof a === "object") + b = a.y, a = a.x; + else if (typeof b === "undefined") + b = a; + this.pin("skewX", a); + this.pin("skewY", b); + return this; + }; + Stage2.prototype.scale = function(a, b) { + if (typeof a === "object") + b = a.y, a = a.x; + else if (typeof b === "undefined") + b = a; + this.pin("scaleX", a); + this.pin("scaleY", b); + return this; + }; + Stage2.prototype.alpha = function(a, ta) { + this.pin("alpha", a); + if (typeof ta !== "undefined") { + this.pin("textureAlpha", ta); + } + return this; }; }; -O._add_shortcuts(o); -o.prototype._textures = null; -o.prototype._alpha = 1; -o.prototype.render = function(t) { - if (this._visible) { - I.node++; - var i = this.matrix(); - t.setTransform(i.a, i.b, i.c, i.d, i.e, i.f), this._alpha = this._pin._alpha * (this._parent ? this._parent._alpha : 1); - var e = this._pin._textureAlpha * this._alpha; - if (t.globalAlpha != e && (t.globalAlpha = e), this._textures !== null) - for (var n = 0, r = this._textures.length; n < r; n++) - this._textures[n].draw(t); - t.globalAlpha != this._alpha && (t.globalAlpha = this._alpha); - for (var s, a = this._first; s = a; ) - a = s._next, s.render(t); - } -}; -o.prototype._tickBefore = null; -o.prototype._tickAfter = null; -o.prototype.MAX_ELAPSE = 1 / 0; -o.prototype._tick = function(t, i, e) { - if (this._visible) { - t > this.MAX_ELAPSE && (t = this.MAX_ELAPSE); - var n = !1; - if (this._tickBefore !== null) - for (var r = 0; r < this._tickBefore.length; r++) { - I.tick++; - var s = this._tickBefore[r]; - n = s.call(this, t, i, e) === !0 || n; - } - for (var a, c = this._first; a = c; ) - c = a._next, a._flag("_tick") && (n = a._tick(t, i, e) === !0 ? !0 : n); - if (this._tickAfter !== null) - for (var r = 0; r < this._tickAfter.length; r++) { - I.tick++; - var s = this._tickAfter[r]; - n = s.call(this, t, i, e) === !0 || n; - } - return n; +Pin._add_shortcuts(Stage); +Stage.prototype._textures = null; +Stage.prototype._alpha = 1; +Stage.prototype.render = function(context) { + if (!this._visible) { + return; } -}; -o.prototype.tick = function(t, i) { - typeof t == "function" && (i ? (this._tickBefore === null && (this._tickBefore = []), this._tickBefore.push(t)) : (this._tickAfter === null && (this._tickAfter = []), this._tickAfter.push(t)), this._flag("_tick", this._tickAfter !== null && this._tickAfter.length > 0 || this._tickBefore !== null && this._tickBefore.length > 0)); -}; -o.prototype.untick = function(t) { - if (typeof t == "function") { - var i; - this._tickBefore !== null && (i = this._tickBefore.indexOf(t)) >= 0 && this._tickBefore.splice(i, 1), this._tickAfter !== null && (i = this._tickAfter.indexOf(t)) >= 0 && this._tickAfter.splice(i, 1); + stats.node++; + var m = this.matrix(); + context.setTransform(m.a, m.b, m.c, m.d, m.e, m.f); + this._alpha = this._pin._alpha * (this._parent ? this._parent._alpha : 1); + var alpha = this._pin._textureAlpha * this._alpha; + if (context.globalAlpha != alpha) { + context.globalAlpha = alpha; + } + if (this._textures !== null) { + for (var i = 0, n = this._textures.length; i < n; i++) { + this._textures[i].draw(context); + } + } + if (context.globalAlpha != this._alpha) { + context.globalAlpha = this._alpha; + } + var child, next = this._first; + while (child = next) { + next = child._next; + child.render(context); } }; -o.prototype.timeout = function(t, i) { - this.setTimeout(t, i); +Stage.prototype._tickBefore = null; +Stage.prototype._tickAfter = null; +Stage.prototype.MAX_ELAPSE = Infinity; +Stage.prototype._tick = function(elapsed, now, last) { + if (!this._visible) { + return; + } + if (elapsed > this.MAX_ELAPSE) { + elapsed = this.MAX_ELAPSE; + } + var ticked = false; + if (this._tickBefore !== null) { + for (var i = 0; i < this._tickBefore.length; i++) { + stats.tick++; + var tickFn = this._tickBefore[i]; + ticked = tickFn.call(this, elapsed, now, last) === true || ticked; + } + } + var child, next = this._first; + while (child = next) { + next = child._next; + if (child._flag("_tick")) { + ticked = child._tick(elapsed, now, last) === true ? true : ticked; + } + } + if (this._tickAfter !== null) { + for (var i = 0; i < this._tickAfter.length; i++) { + stats.tick++; + var tickFn = this._tickAfter[i]; + ticked = tickFn.call(this, elapsed, now, last) === true || ticked; + } + } + return ticked; }; -o.prototype.setTimeout = function(t, i) { - function e(n) { - if ((i -= n) < 0) - this.untick(e), t.call(this); - else - return !0; +Stage.prototype.tick = function(ticker, before) { + if (typeof ticker !== "function") { + return; + } + if (before) { + if (this._tickBefore === null) { + this._tickBefore = []; + } + this._tickBefore.push(ticker); + } else { + if (this._tickAfter === null) { + this._tickAfter = []; + } + this._tickAfter.push(ticker); } - return this.tick(e), e; + this._flag("_tick", this._tickAfter !== null && this._tickAfter.length > 0 || this._tickBefore !== null && this._tickBefore.length > 0); }; -o.prototype.clearTimeout = function(t) { - this.untick(t); +Stage.prototype.untick = function(ticker) { + if (typeof ticker !== "function") { + return; + } + var i; + if (this._tickBefore !== null && (i = this._tickBefore.indexOf(ticker)) >= 0) { + this._tickBefore.splice(i, 1); + } + if (this._tickAfter !== null && (i = this._tickAfter.indexOf(ticker)) >= 0) { + this._tickAfter.splice(i, 1); + } }; -F._super = o; -F.prototype = Object.create(F._super.prototype); -o.root = function(t, i) { - return new F(t, i); +Stage.prototype.timeout = function(fn, time) { + this.setTimeout(fn, time); }; -function F(t, i) { - F._super.call(this), this.label("Root"); - var e = !0, n = !0, r = this, s = 0, a = function(c) { - if (!(e === !0 || n === !0)) { - I.tick = I.node = I.draw = 0; - var u = s || c, p = c - u; - s = c; - var f = r._tick(p, c, u); - r._mo_touch != r._ts_touch ? (r._mo_touch = r._ts_touch, i(r), t(a)) : f ? t(a) : e = !0, I.fps = p ? 1e3 / p : 0; +Stage.prototype.setTimeout = function(fn, time) { + function timer(t) { + if ((time -= t) < 0) { + this.untick(timer); + fn.call(this); + } else { + return true; + } + } + this.tick(timer); + return timer; +}; +Stage.prototype.clearTimeout = function(timer) { + this.untick(timer); +}; +Root._super = Stage; +Root.prototype = Object.create(Root._super.prototype); +Stage.root = function(request, render) { + return new Root(request, render); +}; +function Root(request, render) { + Root._super.call(this); + this.label("Root"); + var paused = true; + var stopped = true; + var self = this; + var lastTime = 0; + var loop = function(now) { + if (paused === true || stopped === true) { + return; } + stats.tick = stats.node = stats.draw = 0; + var last = lastTime || now; + var elapsed = now - last; + lastTime = now; + var ticked = self._tick(elapsed, now, last); + if (self._mo_touch != self._ts_touch) { + self._mo_touch = self._ts_touch; + render(self); + request(loop); + } else if (ticked) { + request(loop); + } else { + paused = true; + } + stats.fps = elapsed ? 1e3 / elapsed : 0; }; this.start = function() { - return n = !1, this.resume(); - }, this.resume = function() { - return e && (this.publish("resume"), e = !1, t(a)), this; - }, this.pause = function() { - return e || this.publish("pause"), e = !0, this; - }, this.touch_root = this.touch, this.touch = function() { - return this.resume(), this.touch_root(); - }, this.stop = function() { - return n = !0, this; + stopped = false; + return this.resume(); + }; + this.resume = function() { + if (paused) { + this.publish("resume"); + paused = false; + request(loop); + } + return this; + }; + this.pause = function() { + if (!paused) { + this.publish("pause"); + } + paused = true; + return this; + }; + this.touch_root = this.touch; + this.touch = function() { + this.resume(); + return this.touch_root(); + }; + this.stop = function() { + stopped = true; + return this; }; } -F.prototype.background = function(t) { +Root.prototype.background = function(color) { return this; }; -F.prototype.viewport = function(t, i, e) { - if (typeof t > "u") +Root.prototype.viewport = function(width, height, ratio) { + if (typeof width === "undefined") { return Object.assign({}, this._viewport); + } this._viewport = { - width: t, - height: i, - ratio: e || 1 - }, this.viewbox(); - var n = Object.assign({}, this._viewport); - return this.visit({ - start: function(r) { - if (!r._flag("viewport")) - return !0; - r.publish("viewport", [n]); - } - }), this; -}; -F.prototype.viewbox = function(t, i, e) { - typeof t == "number" && typeof i == "number" && (this._viewbox = { - width: t, - height: i, - mode: /^(in|out|in-pad|out-crop)$/.test(e) ? e : "in-pad" + width, + height, + ratio: ratio || 1 + }; + this.viewbox(); + var data = Object.assign({}, this._viewport); + this.visit({ + start: function(node) { + if (!node._flag("viewport")) { + return true; + } + node.publish("viewport", [data]); + } }); - var n = this._viewbox, r = this._viewport; - return r && n ? (this.pin({ - width: n.width, - height: n.height - }), this.scaleTo(r.width, r.height, n.mode)) : r && this.pin({ - width: r.width, - height: r.height - }), this; -}; -o.canvas = function(t, i, e) { - typeof t == "string" ? typeof i == "object" || (typeof i == "function" && (e = i), i = {}) : (typeof t == "function" && (e = t), i = {}, t = "2d"); - var n = document.createElement("canvas"), r = n.getContext(t, i), s = new R(n); - return s.context = function() { - return r; - }, s.size = function(a, c, u) { - return u = u || 1, n.width = a * u, n.height = c * u, this.src(n, u), this; - }, s.canvas = function(a) { - return typeof a == "function" ? a.call(this, r) : typeof a > "u" && typeof e == "function" && e.call(this, r), this; - }, typeof e == "function" && e.call(s, r), s; -}; -function Tt(t, i, e, n, r, s) { - var a = t.width, c = t.height, u = t.left, p = t.right, f = t.top, _ = t.bottom; - u = typeof u == "number" && u === u ? u : 0, p = typeof p == "number" && p === p ? p : 0, f = typeof f == "number" && f === f ? f : 0, _ = typeof _ == "number" && _ === _ ? _ : 0, a = a - u - p, c = c - f - _, r || (i = Math.max(i - u - p, 0), e = Math.max(e - f - _, 0)); - var l = 0; - if (f > 0 && u > 0 && s(l++, 0, 0, u, f, 0, 0, u, f), _ > 0 && u > 0 && s(l++, 0, c + f, u, _, 0, e + f, u, _), f > 0 && p > 0 && s(l++, a + u, 0, p, f, i + u, 0, p, f), _ > 0 && p > 0 && s( - l++, - a + u, - c + f, - p, - _, - i + u, - e + f, - p, - _ - ), n) - f > 0 && s(l++, u, 0, a, f, u, 0, i, f), _ > 0 && s( - l++, - u, - c + f, - a, - _, - u, - e + f, - i, - _ - ), u > 0 && s(l++, 0, f, u, c, 0, f, u, e), p > 0 && s( - l++, - a + u, - f, - p, - c, - i + u, - f, - p, - e - ), s(l++, u, f, a, c, u, f, i, e); - else - for (var y = u, k = i, b; k > 0; ) { - b = Math.min(a, k), k -= a; - for (var T = f, X = e, v; X > 0; ) - v = Math.min(c, X), X -= c, s(l++, u, f, b, v, y, T, b, v), k <= 0 && (u && s(l++, 0, f, u, v, 0, T, u, v), p && s(l++, a + u, f, p, v, y + b, T, p, v)), T += v; - f && s(l++, u, 0, b, f, y, 0, b, f), _ && s(l++, u, c + f, b, _, y, T, b, _), y += b; - } - return l; + return this; +}; +Root.prototype.viewbox = function(width, height, mode) { + if (typeof width === "number" && typeof height === "number") { + this._viewbox = { + width, + height, + mode: /^(in|out|in-pad|out-crop)$/.test(mode) ? mode : "in-pad" + }; + } + var box = this._viewbox; + var size = this._viewport; + if (size && box) { + this.pin({ + width: box.width, + height: box.height + }); + this.scaleTo(size.width, size.height, box.mode); + } else if (size) { + this.pin({ + width: size.width, + height: size.height + }); + } + return this; +}; +Stage.canvas = function(type, attributes, drawFn) { + if (typeof type === "string") { + if (typeof attributes === "object") + ; + else { + if (typeof attributes === "function") { + drawFn = attributes; + } + attributes = {}; + } + } else { + if (typeof type === "function") { + drawFn = type; + } + attributes = {}; + type = "2d"; + } + var canvas = document.createElement("canvas"); + var context = canvas.getContext(type, attributes); + var texture = new Texture(canvas); + texture.context = function() { + return context; + }; + texture.size = function(width, height, ratio) { + ratio = ratio || 1; + canvas.width = width * ratio; + canvas.height = height * ratio; + this.src(canvas, ratio); + return this; + }; + texture.canvas = function(fn) { + if (typeof fn === "function") { + fn.call(this, context); + } else if (typeof fn === "undefined" && typeof drawFn === "function") { + drawFn.call(this, context); + } + return this; + }; + if (typeof drawFn === "function") { + drawFn.call(texture, context); + } + return texture; +}; +function repeat(img, owidth, oheight, stretch, inner, insert) { + var width = img.width; + var height = img.height; + var left = img.left; + var right = img.right; + var top = img.top; + var bottom = img.bottom; + left = typeof left === "number" && left === left ? left : 0; + right = typeof right === "number" && right === right ? right : 0; + top = typeof top === "number" && top === top ? top : 0; + bottom = typeof bottom === "number" && bottom === bottom ? bottom : 0; + width = width - left - right; + height = height - top - bottom; + if (!inner) { + owidth = Math.max(owidth - left - right, 0); + oheight = Math.max(oheight - top - bottom, 0); + } + var i = 0; + if (top > 0 && left > 0) + insert(i++, 0, 0, left, top, 0, 0, left, top); + if (bottom > 0 && left > 0) + insert(i++, 0, height + top, left, bottom, 0, oheight + top, left, bottom); + if (top > 0 && right > 0) + insert(i++, width + left, 0, right, top, owidth + left, 0, right, top); + if (bottom > 0 && right > 0) + insert( + i++, + width + left, + height + top, + right, + bottom, + owidth + left, + oheight + top, + right, + bottom + ); + if (stretch) { + if (top > 0) + insert(i++, left, 0, width, top, left, 0, owidth, top); + if (bottom > 0) + insert( + i++, + left, + height + top, + width, + bottom, + left, + oheight + top, + owidth, + bottom + ); + if (left > 0) + insert(i++, 0, top, left, height, 0, top, left, oheight); + if (right > 0) + insert( + i++, + width + left, + top, + right, + height, + owidth + left, + top, + right, + oheight + ); + insert(i++, left, top, width, height, left, top, owidth, oheight); + } else { + var l = left, r = owidth, w; + while (r > 0) { + w = Math.min(width, r), r -= width; + var t = top, b = oheight, h; + while (b > 0) { + h = Math.min(height, b), b -= height; + insert(i++, left, top, w, h, l, t, w, h); + if (r <= 0) { + if (left) + insert(i++, 0, top, left, h, 0, t, left, h); + if (right) + insert(i++, width + left, top, right, h, l + w, t, right, h); + } + t += h; + } + if (top) + insert(i++, left, 0, w, top, l, 0, w, top); + if (bottom) + insert(i++, left, height + top, w, bottom, l, t, w, bottom); + l += w; + } + } + return i; } -o.image = function(t) { - var i = new B(); - return t && i.image(t), i; -}; -B._super = o; -B.prototype = Object.create(B._super.prototype); -function B() { - B._super.call(this), this.label("Image"), this._textures = [], this._image = null; +Stage.image = function(image) { + var img = new Image$1(); + image && img.image(image); + return img; +}; +Image$1._super = Stage; +Image$1.prototype = Object.create(Image$1._super.prototype); +function Image$1() { + Image$1._super.call(this); + this.label("Image"); + this._textures = []; + this._image = null; } -B.prototype.setImage = function(t, i, e) { - return this.image(t, i, e); -}; -B.prototype.image = function(t) { - return this._image = o.texture(t).one(), this.pin("width", this._image ? this._image.width : 0), this.pin("height", this._image ? this._image.height : 0), this._textures[0] = this._image.pipe(), this._textures.length = 1, this; -}; -B.prototype.tile = function(t) { - return this._repeat(!1, t), this; -}; -B.prototype.stretch = function(t) { - return this._repeat(!0, t), this; -}; -B.prototype._repeat = function(t, i) { - var e = this; - this.untick(this._repeatTicker), this.tick(this._repeatTicker = function() { - if (this._mo_stretch != this._pin._ts_transform) { - this._mo_stretch = this._pin._ts_transform; - var r = this.pin("width"), s = this.pin("height"); - this._textures.length = Tt( - this._image, - r, - s, - t, - i, - n - ); +Image$1.prototype.image = function(image) { + this._image = Stage.texture(image).one(); + this.pin("width", this._image ? this._image.width : 0); + this.pin("height", this._image ? this._image.height : 0); + this._textures[0] = this._image.pipe(); + this._textures.length = 1; + return this; +}; +Image$1.prototype.tile = function(inner) { + this._repeat(false, inner); + return this; +}; +Image$1.prototype.stretch = function(inner) { + this._repeat(true, inner); + return this; +}; +Image$1.prototype._repeat = function(stretch, inner) { + var self = this; + this.untick(this._repeatTicker); + this.tick(this._repeatTicker = function() { + if (this._mo_stretch == this._pin._ts_transform) { + return; } + this._mo_stretch = this._pin._ts_transform; + var width = this.pin("width"); + var height = this.pin("height"); + this._textures.length = repeat(this._image, width, height, stretch, inner, insert); }); - function n(r, s, a, c, u, p, f, _, l) { - var y = e._textures.length > r ? e._textures[r] : e._textures[r] = e._image.pipe(); - y.src(s, a, c, u), y.dest(p, f, _, l); + function insert(i, sx, sy, sw, sh, dx, dy, dw, dh) { + var repeat2 = self._textures.length > i ? self._textures[i] : self._textures[i] = self._image.pipe(); + repeat2.src(sx, sy, sw, sh); + repeat2.dest(dx, dy, dw, dh); } }; -o.anim = function(t, i) { - var e = new A(); - return e.frames(t).gotoFrame(0), i && e.fps(i), e; +Stage.anim = function(frames, fps) { + var anim = new Anim(); + anim.frames(frames).gotoFrame(0); + fps && anim.fps(fps); + return anim; }; -A._super = o; -A.prototype = Object.create(A._super.prototype); -o.Anim = { +Anim._super = Stage; +Anim.prototype = Object.create(Anim._super.prototype); +Stage.Anim = { FPS: 15 }; -function A() { - A._super.call(this), this.label("Anim"), this._textures = [], this._fps = o.Anim.FPS, this._ft = 1e3 / this._fps, this._time = -1, this._repeat = 0, this._index = 0, this._frames = []; - var t = 0; - this.tick(function(i, e, n) { - if (!(this._time < 0 || this._frames.length <= 1)) { - var r = t != n; - if (t = e, r || (this._time += i, this._time < this._ft)) - return !0; - var s = this._time / this._ft | 0; - return this._time -= s * this._ft, this.moveFrame(s), this._repeat > 0 && (this._repeat -= s) <= 0 ? (this.stop(), this._callback && this._callback(), !1) : !0; - } - }, !1); +function Anim() { + Anim._super.call(this); + this.label("Anim"); + this._textures = []; + this._fps = Stage.Anim.FPS; + this._ft = 1e3 / this._fps; + this._time = -1; + this._repeat = 0; + this._index = 0; + this._frames = []; + var lastTime = 0; + this.tick(function(t, now, last) { + if (this._time < 0 || this._frames.length <= 1) { + return; + } + var ignore = lastTime != last; + lastTime = now; + if (ignore) { + return true; + } + this._time += t; + if (this._time < this._ft) { + return true; + } + var n = this._time / this._ft | 0; + this._time -= n * this._ft; + this.moveFrame(n); + if (this._repeat > 0 && (this._repeat -= n) <= 0) { + this.stop(); + this._callback && this._callback(); + return false; + } + return true; + }, false); } -A.prototype.fps = function(t) { - return typeof t > "u" ? this._fps : (this._fps = t > 0 ? t : o.Anim.FPS, this._ft = 1e3 / this._fps, this); +Anim.prototype.fps = function(fps) { + if (typeof fps === "undefined") { + return this._fps; + } + this._fps = fps > 0 ? fps : Stage.Anim.FPS; + this._ft = 1e3 / this._fps; + return this; }; -A.prototype.setFrames = function(t, i, e) { - return this.frames(t, i, e); +Anim.prototype.setFrames = function(a, b, c) { + return this.frames(a, b, c); }; -A.prototype.frames = function(t) { - return this._index = 0, this._frames = o.texture(t).array(), this.touch(), this; +Anim.prototype.frames = function(frames) { + this._index = 0; + this._frames = Stage.texture(frames).array(); + this.touch(); + return this; }; -A.prototype.length = function() { +Anim.prototype.length = function() { return this._frames ? this._frames.length : 0; }; -A.prototype.gotoFrame = function(t, i) { - return this._index = N.rotate(t, this._frames.length) | 0, i = i || !this._textures[0], this._textures[0] = this._frames[this._index], i && (this.pin("width", this._textures[0].width), this.pin("height", this._textures[0].height)), this.touch(), this; +Anim.prototype.gotoFrame = function(frame, resize) { + this._index = math.rotate(frame, this._frames.length) | 0; + resize = resize || !this._textures[0]; + this._textures[0] = this._frames[this._index]; + if (resize) { + this.pin("width", this._textures[0].width); + this.pin("height", this._textures[0].height); + } + this.touch(); + return this; }; -A.prototype.moveFrame = function(t) { - return this.gotoFrame(this._index + t); +Anim.prototype.moveFrame = function(move) { + return this.gotoFrame(this._index + move); }; -A.prototype.repeat = function(t, i) { - return this._repeat = t * this._frames.length - 1, this._callback = i, this.play(), this; +Anim.prototype.repeat = function(repeat2, callback) { + this._repeat = repeat2 * this._frames.length - 1; + this._callback = callback; + this.play(); + return this; }; -A.prototype.play = function(t) { - return typeof t < "u" ? (this.gotoFrame(t), this._time = 0) : this._time < 0 && (this._time = 0), this.touch(), this; +Anim.prototype.play = function(frame) { + if (typeof frame !== "undefined") { + this.gotoFrame(frame); + this._time = 0; + } else if (this._time < 0) { + this._time = 0; + } + this.touch(); + return this; }; -A.prototype.stop = function(t) { - return this._time = -1, typeof t < "u" && this.gotoFrame(t), this; +Anim.prototype.stop = function(frame) { + this._time = -1; + if (typeof frame !== "undefined") { + this.gotoFrame(frame); + } + return this; }; -o.string = function(t) { - return new H().frames(t); +Stage.string = function(frames) { + return new Str().frames(frames); }; -H._super = o; -H.prototype = Object.create(H._super.prototype); -function H() { - H._super.call(this), this.label("String"), this._textures = []; +Str._super = Stage; +Str.prototype = Object.create(Str._super.prototype); +function Str() { + Str._super.call(this); + this.label("String"); + this._textures = []; } -H.prototype.setFont = function(t, i, e) { - return this.frames(t, i, e); -}; -H.prototype.frames = function(t) { - return this._textures = [], typeof t == "string" ? (t = o.texture(t), this._item = function(i) { - return t.one(i); - }) : typeof t == "object" ? this._item = function(i) { - return t[i]; - } : typeof t == "function" && (this._item = t), this; +Str.prototype.setFont = function(a, b, c) { + return this.frames(a, b, c); +}; +Str.prototype.frames = function(frames) { + this._textures = []; + if (typeof frames == "string") { + frames = Stage.texture(frames); + this._item = function(value) { + return frames.one(value); + }; + } else if (typeof frames === "object") { + this._item = function(value) { + return frames[value]; + }; + } else if (typeof frames === "function") { + this._item = frames; + } + return this; }; -H.prototype.setValue = function(t, i, e) { - return this.value(t, i, e); +Str.prototype.setValue = function(a, b, c) { + return this.value(a, b, c); }; -H.prototype.value = function(t) { - if (typeof t > "u") +Str.prototype.value = function(value) { + if (typeof value === "undefined") { return this._value; - if (this._value === t) + } + if (this._value === value) { return this; - this._value = t, t === null ? t = "" : typeof t != "string" && !d.array(t) && (t = t.toString()), this._spacing = this._spacing || 0; - for (var i = 0, e = 0, n = 0; n < t.length; n++) { - var r = this._textures[n] = this._item(t[n]); - i += n > 0 ? this._spacing : 0, r.dest(i, 0), i = i + r.width, e = Math.max(e, r.height); - } - return this.pin("width", i), this.pin("height", e), this._textures.length = t.length, this; -}; -o.row = function(t) { - return o.create().row(t).label("Row"); -}; -o.prototype.row = function(t) { - return this.sequence("row", t), this; -}; -o.column = function(t) { - return o.create().column(t).label("Row"); -}; -o.prototype.column = function(t) { - return this.sequence("column", t), this; -}; -o.sequence = function(t, i) { - return o.create().sequence(t, i).label("Sequence"); -}; -o.prototype.sequence = function(t, i) { - return this._padding = this._padding || 0, this._spacing = this._spacing || 0, this.untick(this._layoutTiker), this.tick(this._layoutTiker = function() { - if (this._mo_seq != this._ts_touch) { - this._mo_seq = this._ts_touch; - var e = this._mo_seqAlign != this._ts_children; - this._mo_seqAlign = this._ts_children; - for (var n = 0, r = 0, s, a = this.first(!0), c = !0; s = a; ) { - a = s.next(!0), s.matrix(!0); - var u = s.pin("boxWidth"), p = s.pin("boxHeight"); - t == "column" ? (!c && (r += this._spacing), s.pin("offsetY") != r && s.pin("offsetY", r), n = Math.max(n, u), r = r + p, e && s.pin("alignX", i)) : t == "row" && (!c && (n += this._spacing), s.pin("offsetX") != n && s.pin("offsetX", n), n = n + u, r = Math.max(r, p), e && s.pin("alignY", i)), c = !1; - } - n += 2 * this._padding, r += 2 * this._padding, this.pin("width") != n && this.pin("width", n), this.pin("height") != r && this.pin("height", r); - } - }), this; + } + this._value = value; + if (value === null) { + value = ""; + } else if (typeof value !== "string" && !is$1.array(value)) { + value = value.toString(); + } + this._spacing = this._spacing || 0; + var width = 0, height = 0; + for (var i = 0; i < value.length; i++) { + var image = this._textures[i] = this._item(value[i]); + width += i > 0 ? this._spacing : 0; + image.dest(width, 0); + width = width + image.width; + height = Math.max(height, image.height); + } + this.pin("width", width); + this.pin("height", height); + this._textures.length = value.length; + return this; +}; +Stage.row = function(align) { + return Stage.create().row(align).label("Row"); }; -o.box = function() { - return o.create().box().label("Box"); +Stage.prototype.row = function(align) { + this.sequence("row", align); + return this; +}; +Stage.column = function(align) { + return Stage.create().column(align).label("Row"); +}; +Stage.prototype.column = function(align) { + this.sequence("column", align); + return this; }; -o.prototype.box = function() { - return this._padding = this._padding || 0, this.untick(this._layoutTiker), this.tick(this._layoutTiker = function() { - if (this._mo_box != this._ts_touch) { - this._mo_box = this._ts_touch; - for (var t = 0, i = 0, e, n = this.first(!0); e = n; ) { - n = e.next(!0), e.matrix(!0); - var r = e.pin("boxWidth"), s = e.pin("boxHeight"); - t = Math.max(t, r), i = Math.max(i, s); +Stage.sequence = function(type, align) { + return Stage.create().sequence(type, align).label("Sequence"); +}; +Stage.prototype.sequence = function(type, align) { + this._padding = this._padding || 0; + this._spacing = this._spacing || 0; + this.untick(this._layoutTiker); + this.tick(this._layoutTiker = function() { + if (this._mo_seq == this._ts_touch) { + return; + } + this._mo_seq = this._ts_touch; + var alignChildren = this._mo_seqAlign != this._ts_children; + this._mo_seqAlign = this._ts_children; + var width = 0, height = 0; + var child, next = this.first(true); + var first = true; + while (child = next) { + next = child.next(true); + child.matrix(true); + var w = child.pin("boxWidth"); + var h = child.pin("boxHeight"); + if (type == "column") { + !first && (height += this._spacing); + child.pin("offsetY") != height && child.pin("offsetY", height); + width = Math.max(width, w); + height = height + h; + alignChildren && child.pin("alignX", align); + } else if (type == "row") { + !first && (width += this._spacing); + child.pin("offsetX") != width && child.pin("offsetX", width); + width = width + w; + height = Math.max(height, h); + alignChildren && child.pin("alignY", align); } - t += 2 * this._padding, i += 2 * this._padding, this.pin("width") != t && this.pin("width", t), this.pin("height") != i && this.pin("height", i); + first = false; } - }), this; + width += 2 * this._padding; + height += 2 * this._padding; + this.pin("width") != width && this.pin("width", width); + this.pin("height") != height && this.pin("height", height); + }); + return this; }; -o.layer = function() { - return o.create().layer().label("Layer"); +Stage.box = function() { + return Stage.create().box().label("Box"); }; -o.prototype.layer = function() { - return this.untick(this._layoutTiker), this.tick(this._layoutTiker = function() { - var t = this.parent(); - if (t) { - var i = t.pin("width"); - this.pin("width") != i && this.pin("width", i); - var e = t.pin("height"); - this.pin("height") != e && this.pin("height", e); +Stage.prototype.box = function() { + this._padding = this._padding || 0; + this.untick(this._layoutTiker); + this.tick(this._layoutTiker = function() { + if (this._mo_box == this._ts_touch) { + return; + } + this._mo_box = this._ts_touch; + var width = 0, height = 0; + var child, next = this.first(true); + while (child = next) { + next = child.next(true); + child.matrix(true); + var w = child.pin("boxWidth"); + var h = child.pin("boxHeight"); + width = Math.max(width, w); + height = Math.max(height, h); } - }, !0), this; + width += 2 * this._padding; + height += 2 * this._padding; + this.pin("width") != width && this.pin("width", width); + this.pin("height") != height && this.pin("height", height); + }); + return this; }; -o.prototype.padding = function(t) { - return this._padding = t, this; +Stage.layer = function() { + return Stage.create().layer().label("Layer"); +}; +Stage.prototype.layer = function() { + this.untick(this._layoutTiker); + this.tick(this._layoutTiker = function() { + var parent = this.parent(); + if (parent) { + var width = parent.pin("width"); + if (this.pin("width") != width) { + this.pin("width", width); + } + var height = parent.pin("height"); + if (this.pin("height") != height) { + this.pin("height", height); + } + } + }, true); + return this; +}; +Stage.prototype.padding = function(pad) { + this._padding = pad; + return this; }; -o.prototype.spacing = function(t) { - return this._spacing = t, this; +Stage.prototype.spacing = function(space) { + this._spacing = space; + return this; }; -function it(t) { - return t; +function _identity(x) { + return x; } -var at = {}, mt = {}, vt = {}; -function w(t) { - if (typeof t == "function") - return t; - if (typeof t != "string") - return it; - var i = at[t]; - if (i) - return i; - var e = /^(\w+)(-(in|out|in-out|out-in))?(\((.*)\))?$/i.exec(t); - if (!e || !e.length) - return it; - var n = vt[e[1]], r = mt[e[3]], s = e[5]; - return n && n.fn ? i = n.fn : n && n.fc ? i = n.fc.apply(n.fc, s && s.replace(/\s+/, "").split(",")) : i = it, r && (i = r.fn(i)), at[t] = i, i; +var _cache = {}; +var _modes = {}; +var _easings = {}; +function Easing(token) { + if (typeof token === "function") { + return token; + } + if (typeof token !== "string") { + return _identity; + } + var fn = _cache[token]; + if (fn) { + return fn; + } + var match = /^(\w+)(-(in|out|in-out|out-in))?(\((.*)\))?$/i.exec(token); + if (!match || !match.length) { + return _identity; + } + var easing = _easings[match[1]]; + var mode = _modes[match[3]]; + var params = match[5]; + if (easing && easing.fn) { + fn = easing.fn; + } else if (easing && easing.fc) { + fn = easing.fc.apply(easing.fc, params && params.replace(/\s+/, "").split(",")); + } else { + fn = _identity; + } + if (mode) { + fn = mode.fn(fn); + } + _cache[token] = fn; + return fn; } -w.add = function(t) { - for (var i = (t.name || t.mode).split(/\s+/), e = 0; e < i.length; e++) { - var n = i[e]; - n && ((t.name ? vt : mt)[n] = t); +Easing.add = function(data) { + var names = (data.name || data.mode).split(/\s+/); + for (var i = 0; i < names.length; i++) { + var name = names[i]; + if (name) { + (data.name ? _easings : _modes)[name] = data; + } } }; -w.add({ +Easing.add({ mode: "in", - fn: function(t) { - return t; + fn: function(f) { + return f; } }); -w.add({ +Easing.add({ mode: "out", - fn: function(t) { - return function(i) { - return 1 - t(1 - i); + fn: function(f) { + return function(t) { + return 1 - f(1 - t); }; } }); -w.add({ +Easing.add({ mode: "in-out", - fn: function(t) { - return function(i) { - return i < 0.5 ? t(2 * i) / 2 : 1 - t(2 * (1 - i)) / 2; + fn: function(f) { + return function(t) { + return t < 0.5 ? f(2 * t) / 2 : 1 - f(2 * (1 - t)) / 2; }; } }); -w.add({ +Easing.add({ mode: "out-in", - fn: function(t) { - return function(i) { - return i < 0.5 ? 1 - t(2 * (1 - i)) / 2 : t(2 * i) / 2; + fn: function(f) { + return function(t) { + return t < 0.5 ? 1 - f(2 * (1 - t)) / 2 : f(2 * t) / 2; }; } }); -w.add({ +Easing.add({ name: "linear", fn: function(t) { return t; } }); -w.add({ +Easing.add({ name: "quad", fn: function(t) { return t * t; } }); -w.add({ +Easing.add({ name: "cubic", fn: function(t) { return t * t * t; } }); -w.add({ +Easing.add({ name: "quart", fn: function(t) { return t * t * t * t; } }); -w.add({ +Easing.add({ name: "quint", fn: function(t) { return t * t * t * t * t; } }); -w.add({ +Easing.add({ name: "sin sine", fn: function(t) { return 1 - Math.cos(t * Math.PI / 2); } }); -w.add({ +Easing.add({ name: "exp expo", fn: function(t) { return t == 0 ? 0 : Math.pow(2, 10 * (t - 1)); } }); -w.add({ +Easing.add({ name: "circle circ", fn: function(t) { return 1 - Math.sqrt(1 - t * t); } }); -w.add({ +Easing.add({ name: "bounce", fn: function(t) { return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + 0.75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + 0.9375 : 7.5625 * (t -= 2.625 / 2.75) * t + 0.984375; } }); -w.add({ +Easing.add({ name: "poly", - fc: function(t) { - return function(i) { - return Math.pow(i, t); + fc: function(e) { + return function(t) { + return Math.pow(t, e); }; } }); -w.add({ +Easing.add({ name: "elastic", - fc: function(t, i) { - i = i || 0.45, t = t || 1; - var e = i / (2 * Math.PI) * Math.asin(1 / t); - return function(n) { - return 1 + t * Math.pow(2, -10 * n) * Math.sin((n - e) * (2 * Math.PI) / i); + fc: function(a, p) { + p = p || 0.45; + a = a || 1; + var s = p / (2 * Math.PI) * Math.asin(1 / a); + return function(t) { + return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / p); }; } }); -w.add({ +Easing.add({ name: "back", - fc: function(t) { - return t = typeof t < "u" ? t : 1.70158, function(i) { - return i * i * ((t + 1) * i - t); + fc: function(s) { + s = typeof s !== "undefined" ? s : 1.70158; + return function(t) { + return t * t * ((s + 1) * t - s); }; } }); -o.prototype.tween = function(t, i, e) { - if (typeof t != "number" ? (e = t, i = 0, t = 0) : typeof i != "number" && (e = i, i = 0), !this._tweens) { +Stage.prototype.tween = function(duration, delay, append2) { + if (typeof duration !== "number") { + append2 = duration, delay = 0, duration = 0; + } else if (typeof delay !== "number") { + append2 = delay, delay = 0; + } + if (!this._tweens) { this._tweens = []; - var n = 0; - this.tick(function(s, a, c) { - if (this._tweens.length) { - var u = n != c; - if (n = a, u) - return !0; - var p = this._tweens[0], f = p.tick(this, s, a, c); - if (f && p === this._tweens[0] && this._tweens.shift(), Array.isArray(f)) - for (var _ = 0; _ < f.length; _++) - try { - f[_].call(this); - } catch (l) { - console.log(l); - } - else - typeof f == "object" && this._tweens.unshift(f); - return !0; + var ticktime = 0; + this.tick(function(elapsed, now, last) { + if (!this._tweens.length) { + return; } - }, !0); + var ignore = ticktime != last; + ticktime = now; + if (ignore) { + return true; + } + var head = this._tweens[0]; + var next = head.tick(this, elapsed, now, last); + if (next && head === this._tweens[0]) { + this._tweens.shift(); + } + if (Array.isArray(next)) { + for (var i = 0; i < next.length; i++) { + try { + next[i].call(this); + } catch (e) { + console.log(e); + } + } + } else if (typeof next === "object") { + this._tweens.unshift(next); + } + return true; + }, true); } - this.touch(), e || (this._tweens.length = 0); - var r = new M(this, t, i); - return this._tweens.push(r), r; -}; -function M(t, i, e) { - this._end = {}, this._duration = i || 400, this._delay = e || 0, this._owner = t, this._time = 0; + this.touch(); + if (!append2) { + this._tweens.length = 0; + } + var tween = new Tween(this, duration, delay); + this._tweens.push(tween); + return tween; +}; +function Tween(owner, duration, delay) { + this._end = {}; + this._duration = duration || 400; + this._delay = delay || 0; + this._owner = owner; + this._time = 0; } -M.prototype.tick = function(t, i, e, n) { - if (this._time += i, !(this._time < this._delay)) { - var r = this._time - this._delay; - if (!this._start) { - this._start = {}; - for (var s in this._end) - this._start[s] = this._owner.pin(s); - } - var a, c; - r < this._duration ? (a = r / this._duration, c = !1) : (a = 1, c = !0), typeof this._easing == "function" && (a = this._easing(a)); - var u = 1 - a; - for (var s in this._end) - this._owner.pin(s, this._start[s] * u + this._end[s] * a); - if (c) { - var p = [this._hide, this._remove, this._done]; - return p = p.filter(function(f) { - return typeof f == "function"; - }), this._next || p; +Tween.prototype.tick = function(node, elapsed, now, last) { + this._time += elapsed; + if (this._time < this._delay) { + return; + } + var time = this._time - this._delay; + if (!this._start) { + this._start = {}; + for (var key in this._end) { + this._start[key] = this._owner.pin(key); } } + var p, over; + if (time < this._duration) { + p = time / this._duration; + over = false; + } else { + p = 1; + over = true; + } + if (typeof this._easing == "function") { + p = this._easing(p); + } + var q = 1 - p; + for (var key in this._end) { + this._owner.pin(key, this._start[key] * q + this._end[key] * p); + } + if (over) { + var actions = [this._hide, this._remove, this._done]; + actions = actions.filter(function(element) { + return typeof element === "function"; + }); + return this._next || actions; + } }; -M.prototype.tween = function(t, i) { - return this._next = new M(this._owner, t, i); +Tween.prototype.tween = function(duration, delay) { + return this._next = new Tween(this._owner, duration, delay); }; -M.prototype.duration = function(t) { - return this._duration = t, this; +Tween.prototype.duration = function(duration) { + this._duration = duration; + return this; }; -M.prototype.delay = function(t) { - return this._delay = t, this; +Tween.prototype.delay = function(delay) { + this._delay = delay; + return this; }; -M.prototype.ease = function(t) { - return this._easing = w(t), this; +Tween.prototype.ease = function(easing) { + this._easing = Easing(easing); + return this; }; -M.prototype.done = function(t) { - return this._done = t, this; +Tween.prototype.done = function(fn) { + this._done = fn; + return this; }; -M.prototype.hide = function() { - return this._hide = function() { +Tween.prototype.hide = function() { + this._hide = function() { this.hide(); - }, this; + }; + return this; }; -M.prototype.remove = function() { - return this._remove = function() { +Tween.prototype.remove = function() { + this._remove = function() { this.remove(); - }, this; -}; -M.prototype.pin = function(t, i) { - if (typeof t == "object") - for (var e in t) - ut(this._owner, this._end, e, t[e]); - else - typeof i < "u" && ut(this._owner, this._end, t, i); + }; + return this; +}; +Tween.prototype.pin = function(a, b) { + if (typeof a === "object") { + for (var attr in a) { + pinning(this._owner, this._end, attr, a[attr]); + } + } else if (typeof b !== "undefined") { + pinning(this._owner, this._end, a, b); + } return this; }; -function ut(t, i, e, n) { - typeof t.pin(e) == "number" ? i[e] = n : typeof t.pin(e + "X") == "number" && typeof t.pin(e + "Y") == "number" && (i[e + "X"] = n, i[e + "Y"] = n); +function pinning(node, map, key, value) { + if (typeof node.pin(key) === "number") { + map[key] = value; + } else if (typeof node.pin(key + "X") === "number" && typeof node.pin(key + "Y") === "number") { + map[key + "X"] = value; + map[key + "Y"] = value; + } } -O._add_shortcuts(M); -M.prototype.then = function(t) { - return this.done(t), this; +Pin._add_shortcuts(Tween); +Tween.prototype.then = function(fn) { + this.done(fn); + return this; }; -M.prototype.clear = function(t) { +Tween.prototype.clear = function(forward) { return this; }; -o._load(function(t, i) { - L.subscribe(t, i); +Stage._load(function(stage, elem) { + Mouse.subscribe(stage, elem); }); -L.CLICK = "click"; -L.START = "touchstart mousedown"; -L.MOVE = "touchmove mousemove"; -L.END = "touchend mouseup"; -L.CANCEL = "touchcancel mousecancel"; -L.subscribe = function(t, i) { - if (t.mouse) +Mouse.CLICK = "click"; +Mouse.START = "touchstart mousedown"; +Mouse.MOVE = "touchmove mousemove"; +Mouse.END = "touchend mouseup"; +Mouse.CANCEL = "touchcancel mousecancel"; +Mouse.subscribe = function(stage, elem) { + if (stage.mouse) { + return; + } + stage.mouse = new Mouse(stage, elem); + elem.addEventListener("touchstart", handleStart); + elem.addEventListener("touchend", handleEnd); + elem.addEventListener("touchmove", handleMove); + elem.addEventListener("touchcancel", handleCancel); + elem.addEventListener("mousedown", handleStart); + elem.addEventListener("mouseup", handleEnd); + elem.addEventListener("mousemove", handleMove); + document.addEventListener("mouseup", handleCancel); + window.addEventListener("blur", handleCancel); + var clicklist = [], cancellist = []; + function handleStart(event) { + event.preventDefault(); + stage.mouse.locate(event); + stage.mouse.publish(event.type, event); + stage.mouse.lookup("click", clicklist); + stage.mouse.lookup("mousecancel", cancellist); + } + function handleMove(event) { + event.preventDefault(); + stage.mouse.locate(event); + stage.mouse.publish(event.type, event); + } + function handleEnd(event) { + event.preventDefault(); + stage.mouse.publish(event.type, event); + if (clicklist.length) { + stage.mouse.publish("click", event, clicklist); + } + cancellist.length = 0; + } + function handleCancel(event) { + if (cancellist.length) { + stage.mouse.publish("mousecancel", event, cancellist); + } + clicklist.length = 0; + } +}; +function Mouse(stage, elem) { + if (!(this instanceof Mouse)) { return; - t.mouse = new L(t, i), i.addEventListener("touchstart", r), i.addEventListener("touchend", a), i.addEventListener("touchmove", s), i.addEventListener("touchcancel", c), i.addEventListener("mousedown", r), i.addEventListener("mouseup", a), i.addEventListener("mousemove", s), document.addEventListener("mouseup", c), window.addEventListener("blur", c); - var e = [], n = []; - function r(u) { - u.preventDefault(), t.mouse.locate(u), t.mouse.publish(u.type, u), t.mouse.lookup("click", e), t.mouse.lookup("mousecancel", n); - } - function s(u) { - u.preventDefault(), t.mouse.locate(u), t.mouse.publish(u.type, u); - } - function a(u) { - u.preventDefault(), t.mouse.publish(u.type, u), e.length && t.mouse.publish("click", u, e), n.length = 0; - } - function c(u) { - n.length && t.mouse.publish("mousecancel", u, n), e.length = 0; - } -}; -function L(t, i) { - if (this instanceof L) { - var e = t.viewport().ratio || 1; - t.on("viewport", function(n) { - e = n.ratio || e; - }), this.x = 0, this.y = 0, this.toString = function() { - return (this.x | 0) + "x" + (this.y | 0); - }, this.locate = function(n) { - jt(i, n, this), this.x *= e, this.y *= e; - }, this.lookup = function(n, r) { - this.type = n, this.root = t, this.event = null, r.length = 0, this.collect = r, this.root.visit(this.visitor, this); - }, this.publish = function(n, r, s) { - if (this.type = n, this.root = t, this.event = r, this.collect = !1, this.timeStamp = Date.now(), n !== "mousemove" && n !== "touchmove" && console.log(this.type + " " + this), s) { - for (; s.length && !this.visitor.end(s.shift(), this); ) - ; - s.length = 0; - } else - this.root.visit(this.visitor, this); - }, this.visitor = { - reverse: !0, - visible: !0, - start: function(n, r) { - return !n._flag(r.type); - }, - end: function(n, r) { - E.raw = r.event, E.type = r.type, E.timeStamp = r.timeStamp, E.abs.x = r.x, E.abs.y = r.y; - var s = n.listeners(r.type); - if (s && (n.matrix().inverse().map(r, E), !!(n === r.root || n.attr("spy") || n.hitTest(E)) && (r.collect && r.collect.push(n), r.event))) { - for (var a = !1, c = 0; c < s.length; c++) - a = s[c].call(n, E) ? !0 : a; - return a; + } + var ratio = stage.viewport().ratio || 1; + stage.on("viewport", function(size) { + ratio = size.ratio || ratio; + }); + this.x = 0; + this.y = 0; + this.toString = function() { + return (this.x | 0) + "x" + (this.y | 0); + }; + this.locate = function(event) { + locateElevent(elem, event, this); + this.x *= ratio; + this.y *= ratio; + }; + this.lookup = function(type, collect) { + this.type = type; + this.root = stage; + this.event = null; + collect.length = 0; + this.collect = collect; + this.root.visit(this.visitor, this); + }; + this.publish = function(type, event, targets) { + this.type = type; + this.root = stage; + this.event = event; + this.collect = false; + this.timeStamp = Date.now(); + if (type !== "mousemove" && type !== "touchmove") { + console.log(this.type + " " + this); + } + if (targets) { + while (targets.length) + if (this.visitor.end(targets.shift(), this)) + break; + targets.length = 0; + } else { + this.root.visit(this.visitor, this); + } + }; + this.visitor = { + reverse: true, + visible: true, + start: function(node, mouse) { + return !node._flag(mouse.type); + }, + end: function(node, mouse) { + rel.raw = mouse.event; + rel.type = mouse.type; + rel.timeStamp = mouse.timeStamp; + rel.abs.x = mouse.x; + rel.abs.y = mouse.y; + var listeners = node.listeners(mouse.type); + if (!listeners) { + return; + } + node.matrix().inverse().map(mouse, rel); + if (!(node === mouse.root || node.attr("spy") || node.hitTest(rel))) { + return; + } + if (mouse.collect) { + mouse.collect.push(node); + } + if (mouse.event) { + var cancel = false; + for (var l = 0; l < listeners.length; l++) { + cancel = listeners[l].call(node, rel) ? true : cancel; } + return cancel; } - }; - } + } + }; } -var E = {}, ht = {}; -V(E, "clone", function(t) { - return t = t || {}, t.x = this.x, t.y = this.y, t; +var rel = {}, abs = {}; +defineValue(rel, "clone", function(obj) { + obj = obj || {}, obj.x = this.x, obj.y = this.y; + return obj; }); -V(E, "toString", function() { +defineValue(rel, "toString", function() { return (this.x | 0) + "x" + (this.y | 0) + " (" + this.abs + ")"; }); -V(E, "abs", ht); -V(ht, "clone", function(t) { - return t = t || {}, t.x = this.x, t.y = this.y, t; +defineValue(rel, "abs", abs); +defineValue(abs, "clone", function(obj) { + obj = obj || {}, obj.x = this.x, obj.y = this.y; + return obj; }); -V(ht, "toString", function() { +defineValue(abs, "toString", function() { return (this.x | 0) + "x" + (this.y | 0); }); -function V(t, i, e) { - Object.defineProperty(t, i, { - value: e +function defineValue(obj, name, value) { + Object.defineProperty(obj, name, { + value }); } -function jt(t, i, e) { - i.touches && i.touches.length ? (e.x = i.touches[0].clientX, e.y = i.touches[0].clientY) : (e.x = i.clientX, e.y = i.clientY); - var n = t.getBoundingClientRect(); - return e.x -= n.left, e.y -= n.top, e.x -= t.clientLeft | 0, e.y -= t.clientTop | 0, e; +function locateElevent(el, ev, loc) { + if (ev.touches && ev.touches.length) { + loc.x = ev.touches[0].clientX; + loc.y = ev.touches[0].clientY; + } else { + loc.x = ev.clientX; + loc.y = ev.clientY; + } + var rect = el.getBoundingClientRect(); + loc.x -= rect.left; + loc.y -= rect.top; + loc.x -= el.clientLeft | 0; + loc.y -= el.clientTop | 0; + return loc; } window.addEventListener("load", function() { - console.log("On load."), o.start(); -}, !1); -o.config({ - "app-loader": Et, - "image-loader": St + console.log("On load."); + Stage.start(); +}, false); +Stage.config({ + "app-loader": AppLoader, + "image-loader": ImageLoader }); -function Et(t, i) { - i = i || {}; - var e = i.canvas, n = null, r = !1, s = 0, a = 0, c = 1; - if (typeof e == "string" && (e = document.getElementById(e)), e || (e = document.getElementById("cutjs") || document.getElementById("stage")), !e) { - r = !0, console.log("Creating Canvas..."), e = document.createElement("canvas"), e.style.position = "absolute", e.style.top = "0", e.style.left = "0"; - var u = document.body; - u.insertBefore(e, u.firstChild); - } - n = e.getContext("2d"); - var p = window.devicePixelRatio || 1, f = n.webkitBackingStorePixelRatio || n.mozBackingStorePixelRatio || n.msBackingStorePixelRatio || n.oBackingStorePixelRatio || n.backingStorePixelRatio || 1; - c = p / f; - var _ = window.requestAnimationFrame || window.msRequestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.oRequestAnimationFrame || function(X) { - return window.setTimeout(X, 1e3 / 60); +function AppLoader(app, configs) { + configs = configs || {}; + var canvas = configs.canvas, context = null, full = false; + var width = 0, height = 0, ratio = 1; + if (typeof canvas === "string") { + canvas = document.getElementById(canvas); + } + if (!canvas) { + canvas = document.getElementById("cutjs") || document.getElementById("stage"); + } + if (!canvas) { + full = true; + console.log("Creating Canvas..."); + canvas = document.createElement("canvas"); + canvas.style.position = "absolute"; + canvas.style.top = "0"; + canvas.style.left = "0"; + var body = document.body; + body.insertBefore(canvas, body.firstChild); + } + context = canvas.getContext("2d"); + var devicePixelRatio = window.devicePixelRatio || 1; + var backingStoreRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1; + ratio = devicePixelRatio / backingStoreRatio; + var requestAnimationFrame = window.requestAnimationFrame || window.msRequestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.oRequestAnimationFrame || function(callback) { + return window.setTimeout(callback, 1e3 / 60); }; console.log("Creating stage..."); - var l = o.root(_, y); - function y() { - s > 0 && a > 0 && (n.setTransform(1, 0, 0, 1, 0, 0), n.clearRect(0, 0, s, a), l.render(n)); - } - l.background = function(X) { - return e.style.backgroundColor = X, this; - }, t(l, e); - var k = -1, b = -1; - (function X() { - var v, C; - r ? (v = window.innerWidth > 0 ? window.innerWidth : screen.width, C = window.innerHeight > 0 ? window.innerHeight : screen.height) : (v = e.clientWidth, C = e.clientHeight), (k !== v || b !== C) && (k = v, b = C, T()), _(X); + var root = Stage.root(requestAnimationFrame, render); + function render() { + if (width > 0 && height > 0) { + context.setTransform(1, 0, 0, 1, 0, 0); + context.clearRect(0, 0, width, height); + root.render(context); + } + } + root.background = function(color) { + canvas.style.backgroundColor = color; + return this; + }; + app(root, canvas); + var lastWidth = -1; + var lastHeight = -1; + (function resizeLoop() { + var width2, height2; + if (full) { + width2 = window.innerWidth > 0 ? window.innerWidth : screen.width; + height2 = window.innerHeight > 0 ? window.innerHeight : screen.height; + } else { + width2 = canvas.clientWidth; + height2 = canvas.clientHeight; + } + if (lastWidth !== width2 || lastHeight !== height2) { + lastWidth = width2; + lastHeight = height2; + resize(); + } + requestAnimationFrame(resizeLoop); })(); - function T() { - r ? (s = window.innerWidth > 0 ? window.innerWidth : screen.width, a = window.innerHeight > 0 ? window.innerHeight : screen.height, e.style.width = s + "px", e.style.height = a + "px") : (s = e.clientWidth, a = e.clientHeight), s *= c, a *= c, !(e.width === s && e.height === a) && (e.width = s, e.height = a, console.log("Resize: " + s + " x " + a + " / " + c), l.viewport(s, a, c), y()); + function resize() { + if (full) { + width = window.innerWidth > 0 ? window.innerWidth : screen.width; + height = window.innerHeight > 0 ? window.innerHeight : screen.height; + canvas.style.width = width + "px"; + canvas.style.height = height + "px"; + } else { + width = canvas.clientWidth; + height = canvas.clientHeight; + } + width *= ratio; + height *= ratio; + if (canvas.width === width && canvas.height === height) { + return; + } + canvas.width = width; + canvas.height = height; + console.log("Resize: " + width + " x " + height + " / " + ratio); + root.viewport(width, height, ratio); + render(); } } -function St(t, i, e) { - console.log("Loading image: " + t); - var n = new Image(); - n.onload = function() { - i(n); - }, n.onerror = e, n.src = t; +function ImageLoader(src, success, error) { + console.log("Loading image: " + src); + var image = new Image(); + image.onload = function() { + success(image); + }; + image.onerror = error; + image.src = src; } -Mt(o.prototype, function(t, i, e) { - t._flag(i, e); +listenable(Stage.prototype, function(obj, name, on) { + obj._flag(name, on); }); -o.Matrix = m; -o.Texture = R; -o.Mouse = L; -o.Math = N; -o.internal = {}; -o.internal.Image = B; +Stage.Matrix = Matrix; +Stage.Texture = Texture; +Stage.Mouse = Mouse; +Stage.Math = math; +Stage.Image = Image$1; export { - o as default + Stage as default }; diff --git a/dist/stage.umd.cjs b/dist/stage.umd.cjs index be2bc76..c50a1a9 100644 --- a/dist/stage.umd.cjs +++ b/dist/stage.umd.cjs @@ -1,7 +1,2955 @@ -(function($,C){typeof exports=="object"&&typeof module<"u"?module.exports=C():typeof define=="function"&&define.amd?define(C):($=typeof globalThis<"u"?globalThis:$||self,$.Stage=C())})(this,function(){"use strict";function $(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}/**! - * is - * the definitive JavaScript type testing library - * - * @copyright 2013-2014 Enrico Marino / Jordan Harband - * @license MIT - */var C=Object.prototype,ut=C.hasOwnProperty,k=C.toString,ct;typeof Symbol=="function"&&(ct=Symbol.prototype.valueOf);var _t;typeof BigInt=="function"&&(_t=BigInt.prototype.valueOf);var b=function(t){return t!==t},bt={boolean:1,number:1,string:1,undefined:1},xt=/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/,kt=/^[A-Fa-f0-9]+$/,h={};h.a=h.type=function(t,i){return typeof t===i},h.defined=function(t){return typeof t<"u"},h.empty=function(t){var i=k.call(t),e;if(i==="[object Array]"||i==="[object Arguments]"||i==="[object String]")return t.length===0;if(i==="[object Object]"){for(e in t)if(ut.call(t,e))return!1;return!0}return!t},h.equal=function(i,e){if(i===e)return!0;var n=k.call(i),r;if(n!==k.call(e))return!1;if(n==="[object Object]"){for(r in i)if(!h.equal(i[r],e[r])||!(r in e))return!1;for(r in e)if(!h.equal(i[r],e[r])||!(r in i))return!1;return!0}if(n==="[object Array]"){if(r=i.length,r!==e.length)return!1;for(;r--;)if(!h.equal(i[r],e[r]))return!1;return!0}return n==="[object Function]"?i.prototype===e.prototype:n==="[object Date]"?i.getTime()===e.getTime():!1},h.hosted=function(t,i){var e=typeof i[t];return e==="object"?!!i[t]:!bt[e]},h.instance=h.instanceof=function(t,i){return t instanceof i},h.nil=h.null=function(t){return t===null},h.undef=h.undefined=function(t){return typeof t>"u"},h.args=h.arguments=function(t){var i=k.call(t)==="[object Arguments]",e=!h.array(t)&&h.arraylike(t)&&h.object(t)&&h.fn(t.callee);return i||e},h.array=Array.isArray||function(t){return k.call(t)==="[object Array]"},h.args.empty=function(t){return h.args(t)&&t.length===0},h.array.empty=function(t){return h.array(t)&&t.length===0},h.arraylike=function(t){return!!t&&!h.bool(t)&&ut.call(t,"length")&&isFinite(t.length)&&h.number(t.length)&&t.length>=0},h.bool=h.boolean=function(t){return k.call(t)==="[object Boolean]"},h.false=function(t){return h.bool(t)&&!Number(t)},h.true=function(t){return h.bool(t)&&!!Number(t)},h.date=function(t){return k.call(t)==="[object Date]"},h.date.valid=function(t){return h.date(t)&&!isNaN(Number(t))},h.element=function(t){return t!==void 0&&typeof HTMLElement<"u"&&t instanceof HTMLElement&&t.nodeType===1},h.error=function(t){return k.call(t)==="[object Error]"},h.fn=h.function=function(t){var i=typeof window<"u"&&t===window.alert;if(i)return!0;var e=k.call(t);return e==="[object Function]"||e==="[object GeneratorFunction]"||e==="[object AsyncFunction]"},h.number=function(t){return k.call(t)==="[object Number]"},h.infinite=function(t){return t===1/0||t===-1/0},h.decimal=function(t){return h.number(t)&&!b(t)&&!h.infinite(t)&&t%1!==0},h.divisibleBy=function(t,i){var e=h.infinite(t),n=h.infinite(i),r=h.number(t)&&!b(t)&&h.number(i)&&!b(i)&&i!==0;return e||n||r&&t%i===0},h.integer=h.int=function(t){return h.number(t)&&!b(t)&&t%1===0},h.maximum=function(t,i){if(b(t))throw new TypeError("NaN is not a valid value");if(!h.arraylike(i))throw new TypeError("second argument must be array-like");for(var e=i.length;--e>=0;)if(t=0;)if(t>i[e])return!1;return!0},h.nan=function(t){return!h.number(t)||t!==t},h.even=function(t){return h.infinite(t)||h.number(t)&&t===t&&t%2===0},h.odd=function(t){return h.infinite(t)||h.number(t)&&t===t&&t%2!==0},h.ge=function(t,i){if(b(t)||b(i))throw new TypeError("NaN is not a valid value");return!h.infinite(t)&&!h.infinite(i)&&t>=i},h.gt=function(t,i){if(b(t)||b(i))throw new TypeError("NaN is not a valid value");return!h.infinite(t)&&!h.infinite(i)&&t>i},h.le=function(t,i){if(b(t)||b(i))throw new TypeError("NaN is not a valid value");return!h.infinite(t)&&!h.infinite(i)&&t<=i},h.lt=function(t,i){if(b(t)||b(i))throw new TypeError("NaN is not a valid value");return!h.infinite(t)&&!h.infinite(i)&&t=i&&t<=e},h.object=function(t){return k.call(t)==="[object Object]"},h.primitive=function(i){return i?!(typeof i=="object"||h.object(i)||h.fn(i)||h.array(i)):!0},h.hash=function(t){return h.object(t)&&t.constructor===Object&&!t.nodeType&&!t.setInterval},h.regexp=function(t){return k.call(t)==="[object RegExp]"},h.string=function(t){return k.call(t)==="[object String]"},h.base64=function(t){return h.string(t)&&(!t.length||xt.test(t))},h.hex=function(t){return h.string(t)&&(!t.length||kt.test(t))},h.symbol=function(t){return typeof Symbol=="function"&&k.call(t)==="[object Symbol]"&&typeof ct.call(t)=="symbol"},h.bigint=function(t){return typeof BigInt=="function"&&k.call(t)==="[object BigInt]"&&typeof _t.call(t)=="bigint"};var Xt=h;const d=$(Xt),I={};function Yt(){var t=0;function i(r,s){return t+=s=typeof s=="number"&&s>=1?s:1,function(){r&&r.apply(this,arguments),s>0&&(s--,t--,n())}}var e=[];function n(){if(t===0)for(;e.length;)setTimeout(e.shift(),0)}return i.then=function(r){t===0?setTimeout(r,0):e.push(r)},i}I.create=0;function o(t){if(!(this instanceof o))return d.fn(t)?o.app.apply(o,arguments):d.object(t)?o.atlas.apply(o,arguments):t;I.create++;for(var i=0;i=0;t--)V[t].pause()}},o.resume=function(){if(K){K=!1;for(var t=V.length-1;t>=0;t--)V[t].resume()}},o.create=function(){return new o};function m(t,i,e,n,r,s){this.reset(t,i,e,n,r,s)}m.prototype.toString=function(){return"["+this.a+", "+this.b+", "+this.c+", "+this.d+", "+this.e+", "+this.f+"]"},m.prototype.clone=function(){return new m(this.a,this.b,this.c,this.d,this.e,this.f)},m.prototype.reset=function(t,i,e,n,r,s){return this._dirty=!0,typeof t=="object"?(this.a=t.a,this.d=t.d,this.b=t.b,this.c=t.c,this.e=t.e,this.f=t.f):(this.a=t||1,this.d=n||1,this.b=i||0,this.c=e||0,this.e=r||0,this.f=s||0),this},m.prototype.identity=function(){return this._dirty=!0,this.a=1,this.b=0,this.c=0,this.d=1,this.e=0,this.f=0,this},m.prototype.rotate=function(t){if(!t)return this;this._dirty=!0;var i=t?Math.cos(t):1,e=t?Math.sin(t):0,n=i*this.a-e*this.b,r=i*this.b+e*this.a,s=i*this.c-e*this.d,a=i*this.d+e*this.c,c=i*this.e-e*this.f,u=i*this.f+e*this.e;return this.a=n,this.b=r,this.c=s,this.d=a,this.e=c,this.f=u,this},m.prototype.translate=function(t,i){return!t&&!i?this:(this._dirty=!0,this.e+=t,this.f+=i,this)},m.prototype.scale=function(t,i){return!(t-1)&&!(i-1)?this:(this._dirty=!0,this.a*=t,this.b*=i,this.c*=t,this.d*=i,this.e*=t,this.f*=i,this)},m.prototype.skew=function(t,i){if(!t&&!i)return this;this._dirty=!0;var e=this.a+this.b*t,n=this.b+this.a*i,r=this.c+this.d*t,s=this.d+this.c*i,a=this.e+this.f*t,c=this.f+this.e*i;return this.a=e,this.b=n,this.c=r,this.d=s,this.e=a,this.f=c,this},m.prototype.concat=function(t){this._dirty=!0;var i=this,e=i.a*t.a+i.b*t.c,n=i.b*t.d+i.a*t.b,r=i.c*t.a+i.d*t.c,s=i.d*t.d+i.c*t.b,a=i.e*t.a+t.e+i.f*t.c,c=i.f*t.d+t.f+i.e*t.b;return this.a=e,this.b=n,this.c=r,this.d=s,this.e=a,this.f=c,this},m.prototype.inverse=m.prototype.reverse=function(){if(this._dirty){this._dirty=!1,this.inversed=this.inversed||new m;var t=this.a*this.d-this.b*this.c;this.inversed.a=this.d/t,this.inversed.b=-this.b/t,this.inversed.c=-this.c/t,this.inversed.d=this.a/t,this.inversed.e=(this.c*this.f-this.e*this.d)/t,this.inversed.f=(this.e*this.b-this.a*this.f)/t}return this.inversed},m.prototype.map=function(t,i){return i=i||{},i.x=this.a*t.x+this.c*t.y+this.e,i.y=this.b*t.x+this.d*t.y+this.f,i},m.prototype.mapX=function(t,i){return typeof t=="object"&&(i=t.y,t=t.x),this.a*t+this.c*i+this.e},m.prototype.mapY=function(t,i){return typeof t=="object"&&(i=t.y,t=t.x),this.b*t+this.d*i+this.f};var dt=Math;const N=Object.create(Math);N.random=function(t,i){return typeof t>"u"?(i=1,t=0):typeof i>"u"&&(i=t,t=0),t==i?t:dt.random()*(i-t)+t},N.rotate=function(t,i,e){return typeof i>"u"?(e=1,i=0):typeof e>"u"&&(e=i,i=0),e>i?(t=(t-i)%(e-i),t+(t<0?e:i)):(t=(t-e)%(i-e),t+(t<=0?i:e))},N.limit=function(t,i,e){return te?e:t},N.length=function(t,i){return dt.sqrt(t*t+i*i)};function R(t,i){typeof t=="object"&&this.src(t,i)}R.prototype.pipe=function(){return new R(this)},R.prototype.src=function(t,i,e,n){if(typeof t=="object"){var r=t,s=i||1;this._image=r,this._sx=this._dx=0,this._sy=this._dy=0,this._sw=this._dw=r.width/s,this._sh=this._dh=r.height/s,this.width=r.width/s,this.height=r.height/s,this.ratio=s}else typeof e>"u"?(e=t,n=i):(this._sx=t,this._sy=i),this._sw=this._dw=e,this._sh=this._dh=n,this.width=e,this.height=n;return this},R.prototype.dest=function(t,i,e,n){return this._dx=t,this._dy=i,this._dx=t,this._dy=i,typeof e<"u"&&(this._dw=e,this._dh=n,this.width=e,this.height=n),this},R.prototype.draw=function(t,i,e,n,r,s,a,c,u){var p=this._image;if(!(p===null||typeof p!="object")){var f=this._sx,_=this._sy,l=this._sw,y=this._sh,X=this._dx,x=this._dy,T=this._dw,Y=this._dh;typeof s<"u"?(i=N.limit(i,0,this._sw),n=N.limit(n,0,this._sw-i),e=N.limit(e,0,this._sh),r=N.limit(r,0,this._sh-e),f+=i,_+=e,l=n,y=r,X=s,x=a,T=c,Y=u):typeof n<"u"?(X=i,x=e,T=n,Y=r):typeof i<"u"&&(T=i,Y=e);var w=this.ratio||1;f*=w,_*=w,l*=w,y*=w;try{typeof p.draw=="function"?p.draw(t,f,_,l,y,X,x,T,Y):(I.draw++,t.drawImage(p,f,_,l,y,X,x,T,Y))}catch(D){p._draw_failed||(console.log("Unable to draw: ",p),console.log(D),p._draw_failed=!0)}}};var ot={},ht=[];o.atlas=function(t){var i=d.fn(t.draw)?t:new Z(t);t.name&&(ot[t.name]=i),ht.push(i),z(t,"imagePath"),z(t,"imageRatio");var e=t.imagePath,n=t.imageRatio||1;return d.string(t.image)?e=t.image:d.hash(t.image)&&(e=t.image.src||t.image.url,n=t.image.ratio||n),e&&o.preload(function(r){console.log("Loading atlas: "+e);var s=o.config("image-loader");s(e,function(a){console.log("Image loaded: "+e),i.src(a,n),r()},function(a){console.log("Error loading atlas: "+e,a),r()})}),i},Z._super=R,Z.prototype=Object.create(Z._super.prototype);function Z(t){Z._super.call(this);var i=this;z(t,"filter"),z(t,"cutouts"),z(t,"sprites"),z(t,"factory");var e=t.map||t.filter,n=t.ppu||t.ratio||1,r=t.trim||0,s=t.textures,a=t.factory,c=t.cutouts||t.sprites;function u(f){if(!f||d.fn(f.draw))return f;f=Object.assign({},f),d.fn(e)&&(f=e(f)),n!=1&&(f.x*=n,f.y*=n,f.width*=n,f.height*=n,f.top*=n,f.bottom*=n,f.left*=n,f.right*=n),r!=0&&(f.x+=r,f.y+=r,f.width-=2*r,f.height-=2*r,f.top-=r,f.bottom-=r,f.left-=r,f.right-=r);var _=i.pipe();return _.top=f.top,_.bottom=f.bottom,_.left=f.left,_.right=f.right,_.src(f.x,f.y,f.width,f.height),_}function p(f){if(s){if(d.fn(s))return s(f);if(d.hash(s))return s[f]}if(c){for(var _=null,l=0,y=0;y0&&t.length>n+1&&(e=ot[t.slice(0,n)],i=e&&e.select(t.slice(n+1))),!i&&(e=ot[t])&&(i=e.select()),n=0;!i&&n"u"?this._label:(this._label=t,this)},o.prototype.attr=function(t,i){return typeof i>"u"?this._attrs!==null?this._attrs[t]:void 0:((this._attrs!==null?this._attrs:this._attrs={})[t]=i,this)},o.prototype.visible=function(t){return typeof t>"u"?this._visible:(this._visible=t,this._parent&&(this._parent._ts_children=++j),this._ts_pin=++j,this.touch(),this)},o.prototype.hide=function(){return this.visible(!1)},o.prototype.show=function(){return this.visible(!0)},o.prototype.parent=function(){return this._parent},o.prototype.next=function(t){for(var i=this._next;i&&t&&!i._visible;)i=i._next;return i},o.prototype.prev=function(t){for(var i=this._prev;i&&t&&!i._visible;)i=i._prev;return i},o.prototype.first=function(t){for(var i=this._first;i&&t&&!i._visible;)i=i._next;return i},o.prototype.last=function(t){for(var i=this._last;i&&t&&!i._visible;)i=i._prev;return i},o.prototype.visit=function(t,i){var e=t.reverse,n=t.visible;if(!(t.start&&t.start(this,i))){for(var r,s=e?this.last(n):this.first(n);r=s;)if(s=e?r.prev(n):r.next(n),r.visit(t,i))return!0;return t.end&&t.end(this,i)}},o.prototype.append=function(t,i){if(d.array(t))for(var e=0;e=0;e--)Q(this,t[e]);else if(typeof i<"u")for(var e=arguments.length-1;e>=0;e--)Q(this,arguments[e]);else typeof t<"u"&&Q(this,t);return this},o.prototype.appendTo=function(t){return J(t,this),this},o.prototype.prependTo=function(t){return Q(t,this),this},o.prototype.insertNext=function(t,i){if(d.array(t))for(var e=0;e=0;e--)q(t[e],this);else if(typeof i<"u")for(var e=arguments.length-1;e>=0;e--)q(arguments[e],this);else typeof t<"u"&&q(t,this);return this},o.prototype.insertAfter=function(t){return tt(this,t),this},o.prototype.insertBefore=function(t){return q(this,t),this};function J(t,i){E(i),E(t),i.remove(),t._last&&(t._last._next=i,i._prev=t._last),i._parent=t,t._last=i,t._first||(t._first=i),i._parent._flag(i,!0),i._ts_parent=++j,t._ts_children=++j,t.touch()}function Q(t,i){E(i),E(t),i.remove(),t._first&&(t._first._prev=i,i._next=t._first),i._parent=t,t._first=i,t._last||(t._last=i),i._parent._flag(i,!0),i._ts_parent=++j,t._ts_children=++j,t.touch()}function q(t,i){E(t),E(i),t.remove();var e=i._parent,n=i._prev;i._prev=t,n&&(n._next=t)||e&&(e._first=t),t._parent=e,t._prev=n,t._next=i,t._parent._flag(t,!0),t._ts_parent=++j,t.touch()}function tt(t,i){E(t),E(i),t.remove();var e=i._parent,n=i._next;i._next=t,n&&(n._prev=t)||e&&(e._last=t),t._parent=e,t._prev=i,t._next=n,t._parent._flag(t,!0),t._ts_parent=++j,t.touch()}o.prototype.remove=function(t,i){if(typeof t<"u"){if(d.array(t))for(var e=0;e"u")return this._flags!==null&&this._flags[t]||0;if(typeof t=="string"&&(i?(this._flags=this._flags||{},!this._flags[t]&&this._parent&&this._parent._flag(t,!0),this._flags[t]=(this._flags[t]||0)+1):this._flags&&this._flags[t]>0&&(this._flags[t]==1&&this._parent&&this._parent._flag(t,!1),this._flags[t]=this._flags[t]-1)),typeof t=="object"&&t._flags)for(var e in t._flags)t._flags[e]>0&&this._flag(e,i);return this},o.prototype.hitTest=function(t){var i=this._pin._width,e=this._pin._height;return t.x>=0&&t.x<=i&&t.y>=0&&t.y<=e};function E(t){if(t&&t instanceof o)return t;throw"Invalid node: "+t}function Mt(t,i){t._listeners=null,t.on=t.listen=function(e,n){if(!e||!e.length||typeof n!="function")return this;this._listeners===null&&(this._listeners={});var r=typeof e!="string"&&typeof e.join=="function";if(e=(r?e.join(" "):e).match(/\S+/g))for(var s=0;s=0&&(c.splice(u,1),c.length||delete this._listeners[a],typeof i=="function"&&i(this,a,!1))}return this},t.listeners=function(e){return this._listeners&&this._listeners[e]},t.publish=function(e,n){var r=this.listeners(e);if(!r||!r.length)return 0;for(var s=0;s"u"?this._pin.get(t):(this._pin.set(t,i),this);if(typeof t>"u")return this._pin};function S(t){this._owner=t,this._parent=null,this._relativeMatrix=new m,this._absoluteMatrix=new m,this.reset()}S.prototype.reset=function(){this._textureAlpha=1,this._alpha=1,this._width=0,this._height=0,this._scaleX=1,this._scaleY=1,this._skewX=0,this._skewY=0,this._rotation=0,this._pivoted=!1,this._pivotX=null,this._pivotY=null,this._handled=!1,this._handleX=0,this._handleY=0,this._aligned=!1,this._alignX=0,this._alignY=0,this._offsetX=0,this._offsetY=0,this._boxX=0,this._boxY=0,this._boxWidth=this._width,this._boxHeight=this._height,this._ts_translate=++g,this._ts_transform=++g,this._ts_matrix=++g},S.prototype._update=function(){return this._parent=this._owner._parent&&this._owner._parent._pin,this._handled&&this._mo_handle!=this._ts_transform&&(this._mo_handle=this._ts_transform,this._ts_translate=++g),this._aligned&&this._parent&&this._mo_align!=this._parent._ts_transform&&(this._mo_align=this._parent._ts_transform,this._ts_translate=++g),this},S.prototype.toString=function(){return this._owner+" ("+(this._parent?this._parent._owner:null)+")"},S.prototype.absoluteMatrix=function(){this._update();var t=Math.max(this._ts_transform,this._ts_translate,this._parent?this._parent._ts_matrix:0);if(this._mo_abs==t)return this._absoluteMatrix;this._mo_abs=t;var i=this._absoluteMatrix;return i.reset(this.relativeMatrix()),this._parent&&i.concat(this._parent._absoluteMatrix),this._ts_matrix=++g,i},S.prototype.relativeMatrix=function(){this._update();var t=Math.max(this._ts_transform,this._ts_translate,this._parent?this._parent._ts_transform:0);if(this._mo_rel==t)return this._relativeMatrix;this._mo_rel=t;var i=this._relativeMatrix;if(i.identity(),this._pivoted&&i.translate(-this._pivotX*this._width,-this._pivotY*this._height),i.scale(this._scaleX,this._scaleY),i.skew(this._skewX,this._skewY),i.rotate(this._rotation),this._pivoted&&i.translate(this._pivotX*this._width,this._pivotY*this._height),this._pivoted)this._boxX=0,this._boxY=0,this._boxWidth=this._width,this._boxHeight=this._height;else{var e,n;i.a>0&&i.c>0||i.a<0&&i.c<0?(e=0,n=i.a*this._width+i.c*this._height):(e=i.a*this._width,n=i.c*this._height),e>n?(this._boxX=n,this._boxWidth=e-n):(this._boxX=e,this._boxWidth=n-e),i.b>0&&i.d>0||i.b<0&&i.d<0?(e=0,n=i.b*this._width+i.d*this._height):(e=i.b*this._width,n=i.d*this._height),e>n?(this._boxY=n,this._boxHeight=e-n):(this._boxY=e,this._boxHeight=n-e)}return this._x=this._offsetX,this._y=this._offsetY,this._x-=this._boxX+this._handleX*this._boxWidth,this._y-=this._boxY+this._handleY*this._boxHeight,this._aligned&&this._parent&&(this._parent.relativeMatrix(),this._x+=this._alignX*this._parent._width,this._y+=this._alignY*this._parent._height),i.translate(this._x,this._y),this._relativeMatrix},S.prototype.get=function(t){if(typeof gt[t]=="function")return gt[t](this)},S.prototype.set=function(t,i){if(typeof t=="string")typeof it[t]=="function"&&typeof i<"u"&&it[t](this,i);else if(typeof t=="object")for(i in t)typeof it[i]=="function"&&typeof t[i]<"u"&&it[i](this,t[i],t);return this._owner&&(this._owner._ts_pin=++g,this._owner.touch()),this};var gt={alpha:function(t){return t._alpha},textureAlpha:function(t){return t._textureAlpha},width:function(t){return t._width},height:function(t){return t._height},boxWidth:function(t){return t._boxWidth},boxHeight:function(t){return t._boxHeight},scaleX:function(t){return t._scaleX},scaleY:function(t){return t._scaleY},skewX:function(t){return t._skewX},skewY:function(t){return t._skewY},rotation:function(t){return t._rotation},pivotX:function(t){return t._pivotX},pivotY:function(t){return t._pivotY},offsetX:function(t){return t._offsetX},offsetY:function(t){return t._offsetY},alignX:function(t){return t._alignX},alignY:function(t){return t._alignY},handleX:function(t){return t._handleX},handleY:function(t){return t._handleY}},it={alpha:function(t,i){t._alpha=i},textureAlpha:function(t,i){t._textureAlpha=i},width:function(t,i){t._width_=i,t._width=i,t._ts_transform=++g},height:function(t,i){t._height_=i,t._height=i,t._ts_transform=++g},scale:function(t,i){t._scaleX=i,t._scaleY=i,t._ts_transform=++g},scaleX:function(t,i){t._scaleX=i,t._ts_transform=++g},scaleY:function(t,i){t._scaleY=i,t._ts_transform=++g},skew:function(t,i){t._skewX=i,t._skewY=i,t._ts_transform=++g},skewX:function(t,i){t._skewX=i,t._ts_transform=++g},skewY:function(t,i){t._skewY=i,t._ts_transform=++g},rotation:function(t,i){t._rotation=i,t._ts_transform=++g},pivot:function(t,i){t._pivotX=i,t._pivotY=i,t._pivoted=!0,t._ts_transform=++g},pivotX:function(t,i){t._pivotX=i,t._pivoted=!0,t._ts_transform=++g},pivotY:function(t,i){t._pivotY=i,t._pivoted=!0,t._ts_transform=++g},offset:function(t,i){t._offsetX=i,t._offsetY=i,t._ts_translate=++g},offsetX:function(t,i){t._offsetX=i,t._ts_translate=++g},offsetY:function(t,i){t._offsetY=i,t._ts_translate=++g},align:function(t,i){this.alignX(t,i),this.alignY(t,i)},alignX:function(t,i){t._alignX=i,t._aligned=!0,t._ts_translate=++g,this.handleX(t,i)},alignY:function(t,i){t._alignY=i,t._aligned=!0,t._ts_translate=++g,this.handleY(t,i)},handle:function(t,i){this.handleX(t,i),this.handleY(t,i)},handleX:function(t,i){t._handleX=i,t._handled=!0,t._ts_translate=++g},handleY:function(t,i){t._handleY=i,t._handled=!0,t._ts_translate=++g},resizeMode:function(t,i,e){e&&(i=="in"?i="in-pad":i=="out"&&(i="out-crop"),W(t,e.resizeWidth,e.resizeHeight,i))},resizeWidth:function(t,i,e){(!e||!e.resizeMode)&&W(t,i,null)},resizeHeight:function(t,i,e){(!e||!e.resizeMode)&&W(t,null,i)},scaleMode:function(t,i,e){e&&W(t,e.scaleWidth,e.scaleHeight,i)},scaleWidth:function(t,i,e){(!e||!e.scaleMode)&&W(t,i,null)},scaleHeight:function(t,i,e){(!e||!e.scaleMode)&&W(t,null,i)},matrix:function(t,i){this.scaleX(t,i.a),this.skewX(t,i.c/i.d),this.skewY(t,i.b/i.a),this.scaleY(t,i.d),this.offsetX(t,i.e),this.offsetY(t,i.f),this.rotation(t,0)}};function W(t,i,e,n){var r=typeof i=="number",s=typeof e=="number",a=typeof n=="string";t._ts_transform=++g,r&&(t._scaleX=i/t._width_,t._width=t._width_),s&&(t._scaleY=e/t._height_,t._height=t._height_),r&&s&&a&&(n=="out"||n=="out-crop"?t._scaleX=t._scaleY=Math.max(t._scaleX,t._scaleY):(n=="in"||n=="in-pad")&&(t._scaleX=t._scaleY=Math.min(t._scaleX,t._scaleY)),(n=="out-crop"||n=="in-pad")&&(t._width=i/t._scaleX,t._height=e/t._scaleY))}o.prototype.scaleTo=function(t,i,e){return typeof t=="object"&&(e=i,i=t.y,t=t.x),W(this._pin,t,i,e),this},S._add_shortcuts=function(t){t.prototype.size=function(i,e){return this.pin("width",i),this.pin("height",e),this},t.prototype.width=function(i){return typeof i>"u"?this.pin("width"):(this.pin("width",i),this)},t.prototype.height=function(i){return typeof i>"u"?this.pin("height"):(this.pin("height",i),this)},t.prototype.offset=function(i,e){return typeof i=="object"&&(e=i.y,i=i.x),this.pin("offsetX",i),this.pin("offsetY",e),this},t.prototype.rotate=function(i){return this.pin("rotation",i),this},t.prototype.skew=function(i,e){return typeof i=="object"?(e=i.y,i=i.x):typeof e>"u"&&(e=i),this.pin("skewX",i),this.pin("skewY",e),this},t.prototype.scale=function(i,e){return typeof i=="object"?(e=i.y,i=i.x):typeof e>"u"&&(e=i),this.pin("scaleX",i),this.pin("scaleY",e),this},t.prototype.alpha=function(i,e){return this.pin("alpha",i),typeof e<"u"&&this.pin("textureAlpha",e),this}},S._add_shortcuts(o),o.prototype._textures=null,o.prototype._alpha=1,o.prototype.render=function(t){if(this._visible){I.node++;var i=this.matrix();t.setTransform(i.a,i.b,i.c,i.d,i.e,i.f),this._alpha=this._pin._alpha*(this._parent?this._parent._alpha:1);var e=this._pin._textureAlpha*this._alpha;if(t.globalAlpha!=e&&(t.globalAlpha=e),this._textures!==null)for(var n=0,r=this._textures.length;nthis.MAX_ELAPSE&&(t=this.MAX_ELAPSE);var n=!1;if(this._tickBefore!==null)for(var r=0;r0||this._tickBefore!==null&&this._tickBefore.length>0))},o.prototype.untick=function(t){if(typeof t=="function"){var i;this._tickBefore!==null&&(i=this._tickBefore.indexOf(t))>=0&&this._tickBefore.splice(i,1),this._tickAfter!==null&&(i=this._tickAfter.indexOf(t))>=0&&this._tickAfter.splice(i,1)}},o.prototype.timeout=function(t,i){this.setTimeout(t,i)},o.prototype.setTimeout=function(t,i){function e(n){if((i-=n)<0)this.untick(e),t.call(this);else return!0}return this.tick(e),e},o.prototype.clearTimeout=function(t){this.untick(t)},F._super=o,F.prototype=Object.create(F._super.prototype),o.root=function(t,i){return new F(t,i)};function F(t,i){F._super.call(this),this.label("Root");var e=!0,n=!0,r=this,s=0,a=function(c){if(!(e===!0||n===!0)){I.tick=I.node=I.draw=0;var u=s||c,p=c-u;s=c;var f=r._tick(p,c,u);r._mo_touch!=r._ts_touch?(r._mo_touch=r._ts_touch,i(r),t(a)):f?t(a):e=!0,I.fps=p?1e3/p:0}};this.start=function(){return n=!1,this.resume()},this.resume=function(){return e&&(this.publish("resume"),e=!1,t(a)),this},this.pause=function(){return e||this.publish("pause"),e=!0,this},this.touch_root=this.touch,this.touch=function(){return this.resume(),this.touch_root()},this.stop=function(){return n=!0,this}}F.prototype.background=function(t){return this},F.prototype.viewport=function(t,i,e){if(typeof t>"u")return Object.assign({},this._viewport);this._viewport={width:t,height:i,ratio:e||1},this.viewbox();var n=Object.assign({},this._viewport);return this.visit({start:function(r){if(!r._flag("viewport"))return!0;r.publish("viewport",[n])}}),this},F.prototype.viewbox=function(t,i,e){typeof t=="number"&&typeof i=="number"&&(this._viewbox={width:t,height:i,mode:/^(in|out|in-pad|out-crop)$/.test(e)?e:"in-pad"});var n=this._viewbox,r=this._viewport;return r&&n?(this.pin({width:n.width,height:n.height}),this.scaleTo(r.width,r.height,n.mode)):r&&this.pin({width:r.width,height:r.height}),this},o.canvas=function(t,i,e){typeof t=="string"?typeof i=="object"||(typeof i=="function"&&(e=i),i={}):(typeof t=="function"&&(e=t),i={},t="2d");var n=document.createElement("canvas"),r=n.getContext(t,i),s=new R(n);return s.context=function(){return r},s.size=function(a,c,u){return u=u||1,n.width=a*u,n.height=c*u,this.src(n,u),this},s.canvas=function(a){return typeof a=="function"?a.call(this,r):typeof a>"u"&&typeof e=="function"&&e.call(this,r),this},typeof e=="function"&&e.call(s,r),s};function Tt(t,i,e,n,r,s){var a=t.width,c=t.height,u=t.left,p=t.right,f=t.top,_=t.bottom;u=typeof u=="number"&&u===u?u:0,p=typeof p=="number"&&p===p?p:0,f=typeof f=="number"&&f===f?f:0,_=typeof _=="number"&&_===_?_:0,a=a-u-p,c=c-f-_,r||(i=Math.max(i-u-p,0),e=Math.max(e-f-_,0));var l=0;if(f>0&&u>0&&s(l++,0,0,u,f,0,0,u,f),_>0&&u>0&&s(l++,0,c+f,u,_,0,e+f,u,_),f>0&&p>0&&s(l++,a+u,0,p,f,i+u,0,p,f),_>0&&p>0&&s(l++,a+u,c+f,p,_,i+u,e+f,p,_),n)f>0&&s(l++,u,0,a,f,u,0,i,f),_>0&&s(l++,u,c+f,a,_,u,e+f,i,_),u>0&&s(l++,0,f,u,c,0,f,u,e),p>0&&s(l++,a+u,f,p,c,i+u,f,p,e),s(l++,u,f,a,c,u,f,i,e);else for(var y=u,X=i,x;X>0;){x=Math.min(a,X),X-=a;for(var T=f,Y=e,w;Y>0;)w=Math.min(c,Y),Y-=c,s(l++,u,f,x,w,y,T,x,w),X<=0&&(u&&s(l++,0,f,u,w,0,T,u,w),p&&s(l++,a+u,f,p,w,y+x,T,p,w)),T+=w;f&&s(l++,u,0,x,f,y,0,x,f),_&&s(l++,u,c+f,x,_,y,T,x,_),y+=x}return l}o.image=function(t){var i=new B;return t&&i.image(t),i},B._super=o,B.prototype=Object.create(B._super.prototype);function B(){B._super.call(this),this.label("Image"),this._textures=[],this._image=null}B.prototype.setImage=function(t,i,e){return this.image(t,i,e)},B.prototype.image=function(t){return this._image=o.texture(t).one(),this.pin("width",this._image?this._image.width:0),this.pin("height",this._image?this._image.height:0),this._textures[0]=this._image.pipe(),this._textures.length=1,this},B.prototype.tile=function(t){return this._repeat(!1,t),this},B.prototype.stretch=function(t){return this._repeat(!0,t),this},B.prototype._repeat=function(t,i){var e=this;this.untick(this._repeatTicker),this.tick(this._repeatTicker=function(){if(this._mo_stretch!=this._pin._ts_transform){this._mo_stretch=this._pin._ts_transform;var r=this.pin("width"),s=this.pin("height");this._textures.length=Tt(this._image,r,s,t,i,n)}});function n(r,s,a,c,u,p,f,_,l){var y=e._textures.length>r?e._textures[r]:e._textures[r]=e._image.pipe();y.src(s,a,c,u),y.dest(p,f,_,l)}},o.anim=function(t,i){var e=new A;return e.frames(t).gotoFrame(0),i&&e.fps(i),e},A._super=o,A.prototype=Object.create(A._super.prototype),o.Anim={FPS:15};function A(){A._super.call(this),this.label("Anim"),this._textures=[],this._fps=o.Anim.FPS,this._ft=1e3/this._fps,this._time=-1,this._repeat=0,this._index=0,this._frames=[];var t=0;this.tick(function(i,e,n){if(!(this._time<0||this._frames.length<=1)){var r=t!=n;if(t=e,r||(this._time+=i,this._time0&&(this._repeat-=s)<=0?(this.stop(),this._callback&&this._callback(),!1):!0}},!1)}A.prototype.fps=function(t){return typeof t>"u"?this._fps:(this._fps=t>0?t:o.Anim.FPS,this._ft=1e3/this._fps,this)},A.prototype.setFrames=function(t,i,e){return this.frames(t,i,e)},A.prototype.frames=function(t){return this._index=0,this._frames=o.texture(t).array(),this.touch(),this},A.prototype.length=function(){return this._frames?this._frames.length:0},A.prototype.gotoFrame=function(t,i){return this._index=N.rotate(t,this._frames.length)|0,i=i||!this._textures[0],this._textures[0]=this._frames[this._index],i&&(this.pin("width",this._textures[0].width),this.pin("height",this._textures[0].height)),this.touch(),this},A.prototype.moveFrame=function(t){return this.gotoFrame(this._index+t)},A.prototype.repeat=function(t,i){return this._repeat=t*this._frames.length-1,this._callback=i,this.play(),this},A.prototype.play=function(t){return typeof t<"u"?(this.gotoFrame(t),this._time=0):this._time<0&&(this._time=0),this.touch(),this},A.prototype.stop=function(t){return this._time=-1,typeof t<"u"&&this.gotoFrame(t),this},o.string=function(t){return new H().frames(t)},H._super=o,H.prototype=Object.create(H._super.prototype);function H(){H._super.call(this),this.label("String"),this._textures=[]}H.prototype.setFont=function(t,i,e){return this.frames(t,i,e)},H.prototype.frames=function(t){return this._textures=[],typeof t=="string"?(t=o.texture(t),this._item=function(i){return t.one(i)}):typeof t=="object"?this._item=function(i){return t[i]}:typeof t=="function"&&(this._item=t),this},H.prototype.setValue=function(t,i,e){return this.value(t,i,e)},H.prototype.value=function(t){if(typeof t>"u")return this._value;if(this._value===t)return this;this._value=t,t===null?t="":typeof t!="string"&&!d.array(t)&&(t=t.toString()),this._spacing=this._spacing||0;for(var i=0,e=0,n=0;n0?this._spacing:0,r.dest(i,0),i=i+r.width,e=Math.max(e,r.height)}return this.pin("width",i),this.pin("height",e),this._textures.length=t.length,this},o.row=function(t){return o.create().row(t).label("Row")},o.prototype.row=function(t){return this.sequence("row",t),this},o.column=function(t){return o.create().column(t).label("Row")},o.prototype.column=function(t){return this.sequence("column",t),this},o.sequence=function(t,i){return o.create().sequence(t,i).label("Sequence")},o.prototype.sequence=function(t,i){return this._padding=this._padding||0,this._spacing=this._spacing||0,this.untick(this._layoutTiker),this.tick(this._layoutTiker=function(){if(this._mo_seq!=this._ts_touch){this._mo_seq=this._ts_touch;var e=this._mo_seqAlign!=this._ts_children;this._mo_seqAlign=this._ts_children;for(var n=0,r=0,s,a=this.first(!0),c=!0;s=a;){a=s.next(!0),s.matrix(!0);var u=s.pin("boxWidth"),p=s.pin("boxHeight");t=="column"?(!c&&(r+=this._spacing),s.pin("offsetY")!=r&&s.pin("offsetY",r),n=Math.max(n,u),r=r+p,e&&s.pin("alignX",i)):t=="row"&&(!c&&(n+=this._spacing),s.pin("offsetX")!=n&&s.pin("offsetX",n),n=n+u,r=Math.max(r,p),e&&s.pin("alignY",i)),c=!1}n+=2*this._padding,r+=2*this._padding,this.pin("width")!=n&&this.pin("width",n),this.pin("height")!=r&&this.pin("height",r)}}),this},o.box=function(){return o.create().box().label("Box")},o.prototype.box=function(){return this._padding=this._padding||0,this.untick(this._layoutTiker),this.tick(this._layoutTiker=function(){if(this._mo_box!=this._ts_touch){this._mo_box=this._ts_touch;for(var t=0,i=0,e,n=this.first(!0);e=n;){n=e.next(!0),e.matrix(!0);var r=e.pin("boxWidth"),s=e.pin("boxHeight");t=Math.max(t,r),i=Math.max(i,s)}t+=2*this._padding,i+=2*this._padding,this.pin("width")!=t&&this.pin("width",t),this.pin("height")!=i&&this.pin("height",i)}}),this},o.layer=function(){return o.create().layer().label("Layer")},o.prototype.layer=function(){return this.untick(this._layoutTiker),this.tick(this._layoutTiker=function(){var t=this.parent();if(t){var i=t.pin("width");this.pin("width")!=i&&this.pin("width",i);var e=t.pin("height");this.pin("height")!=e&&this.pin("height",e)}},!0),this},o.prototype.padding=function(t){return this._padding=t,this},o.prototype.spacing=function(t){return this._spacing=t,this};function ft(t){return t}var yt={},mt={},vt={};function v(t){if(typeof t=="function")return t;if(typeof t!="string")return ft;var i=yt[t];if(i)return i;var e=/^(\w+)(-(in|out|in-out|out-in))?(\((.*)\))?$/i.exec(t);if(!e||!e.length)return ft;var n=vt[e[1]],r=mt[e[3]],s=e[5];return n&&n.fn?i=n.fn:n&&n.fc?i=n.fc.apply(n.fc,s&&s.replace(/\s+/,"").split(",")):i=ft,r&&(i=r.fn(i)),yt[t]=i,i}v.add=function(t){for(var i=(t.name||t.mode).split(/\s+/),e=0;e0&&a>0&&(n.setTransform(1,0,0,1,0,0),n.clearRect(0,0,s,a),l.render(n))}l.background=function(Y){return e.style.backgroundColor=Y,this},t(l,e);var X=-1,x=-1;(function Y(){var w,D;r?(w=window.innerWidth>0?window.innerWidth:screen.width,D=window.innerHeight>0?window.innerHeight:screen.height):(w=e.clientWidth,D=e.clientHeight),(X!==w||x!==D)&&(X=w,x=D,T()),_(Y)})();function T(){r?(s=window.innerWidth>0?window.innerWidth:screen.width,a=window.innerHeight>0?window.innerHeight:screen.height,e.style.width=s+"px",e.style.height=a+"px"):(s=e.clientWidth,a=e.clientHeight),s*=c,a*=c,!(e.width===s&&e.height===a)&&(e.width=s,e.height=a,console.log("Resize: "+s+" x "+a+" / "+c),l.viewport(s,a,c),y())}}function St(t,i,e){console.log("Loading image: "+t);var n=new Image;n.onload=function(){i(n)},n.onerror=e,n.src=t}return Mt(o.prototype,function(t,i,e){t._flag(i,e)}),o.Matrix=m,o.Texture=R,o.Mouse=L,o.Math=N,o.internal={},o.internal.Image=B,o}); +(function(global, factory) { + typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, global.Stage = factory()); +})(this, function() { + "use strict"; + function getDefaultExportFromCjs(x) { + return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x; + } + /**! + * is + * the definitive JavaScript type testing library + * + * @copyright 2013-2014 Enrico Marino / Jordan Harband + * @license MIT + */ + var objProto = Object.prototype; + var owns = objProto.hasOwnProperty; + var toStr = objProto.toString; + var symbolValueOf; + if (typeof Symbol === "function") { + symbolValueOf = Symbol.prototype.valueOf; + } + var bigIntValueOf; + if (typeof BigInt === "function") { + bigIntValueOf = BigInt.prototype.valueOf; + } + var isActualNaN = function(value) { + return value !== value; + }; + var NON_HOST_TYPES = { + "boolean": 1, + number: 1, + string: 1, + undefined: 1 + }; + var base64Regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/; + var hexRegex = /^[A-Fa-f0-9]+$/; + var is = {}; + is.a = is.type = function(value, type) { + return typeof value === type; + }; + is.defined = function(value) { + return typeof value !== "undefined"; + }; + is.empty = function(value) { + var type = toStr.call(value); + var key; + if (type === "[object Array]" || type === "[object Arguments]" || type === "[object String]") { + return value.length === 0; + } + if (type === "[object Object]") { + for (key in value) { + if (owns.call(value, key)) { + return false; + } + } + return true; + } + return !value; + }; + is.equal = function equal(value, other) { + if (value === other) { + return true; + } + var type = toStr.call(value); + var key; + if (type !== toStr.call(other)) { + return false; + } + if (type === "[object Object]") { + for (key in value) { + if (!is.equal(value[key], other[key]) || !(key in other)) { + return false; + } + } + for (key in other) { + if (!is.equal(value[key], other[key]) || !(key in value)) { + return false; + } + } + return true; + } + if (type === "[object Array]") { + key = value.length; + if (key !== other.length) { + return false; + } + while (key--) { + if (!is.equal(value[key], other[key])) { + return false; + } + } + return true; + } + if (type === "[object Function]") { + return value.prototype === other.prototype; + } + if (type === "[object Date]") { + return value.getTime() === other.getTime(); + } + return false; + }; + is.hosted = function(value, host) { + var type = typeof host[value]; + return type === "object" ? !!host[value] : !NON_HOST_TYPES[type]; + }; + is.instance = is["instanceof"] = function(value, constructor) { + return value instanceof constructor; + }; + is.nil = is["null"] = function(value) { + return value === null; + }; + is.undef = is.undefined = function(value) { + return typeof value === "undefined"; + }; + is.args = is.arguments = function(value) { + var isStandardArguments = toStr.call(value) === "[object Arguments]"; + var isOldArguments = !is.array(value) && is.arraylike(value) && is.object(value) && is.fn(value.callee); + return isStandardArguments || isOldArguments; + }; + is.array = Array.isArray || function(value) { + return toStr.call(value) === "[object Array]"; + }; + is.args.empty = function(value) { + return is.args(value) && value.length === 0; + }; + is.array.empty = function(value) { + return is.array(value) && value.length === 0; + }; + is.arraylike = function(value) { + return !!value && !is.bool(value) && owns.call(value, "length") && isFinite(value.length) && is.number(value.length) && value.length >= 0; + }; + is.bool = is["boolean"] = function(value) { + return toStr.call(value) === "[object Boolean]"; + }; + is["false"] = function(value) { + return is.bool(value) && Boolean(Number(value)) === false; + }; + is["true"] = function(value) { + return is.bool(value) && Boolean(Number(value)) === true; + }; + is.date = function(value) { + return toStr.call(value) === "[object Date]"; + }; + is.date.valid = function(value) { + return is.date(value) && !isNaN(Number(value)); + }; + is.element = function(value) { + return value !== void 0 && typeof HTMLElement !== "undefined" && value instanceof HTMLElement && value.nodeType === 1; + }; + is.error = function(value) { + return toStr.call(value) === "[object Error]"; + }; + is.fn = is["function"] = function(value) { + var isAlert = typeof window !== "undefined" && value === window.alert; + if (isAlert) { + return true; + } + var str = toStr.call(value); + return str === "[object Function]" || str === "[object GeneratorFunction]" || str === "[object AsyncFunction]"; + }; + is.number = function(value) { + return toStr.call(value) === "[object Number]"; + }; + is.infinite = function(value) { + return value === Infinity || value === -Infinity; + }; + is.decimal = function(value) { + return is.number(value) && !isActualNaN(value) && !is.infinite(value) && value % 1 !== 0; + }; + is.divisibleBy = function(value, n) { + var isDividendInfinite = is.infinite(value); + var isDivisorInfinite = is.infinite(n); + var isNonZeroNumber = is.number(value) && !isActualNaN(value) && is.number(n) && !isActualNaN(n) && n !== 0; + return isDividendInfinite || isDivisorInfinite || isNonZeroNumber && value % n === 0; + }; + is.integer = is["int"] = function(value) { + return is.number(value) && !isActualNaN(value) && value % 1 === 0; + }; + is.maximum = function(value, others) { + if (isActualNaN(value)) { + throw new TypeError("NaN is not a valid value"); + } else if (!is.arraylike(others)) { + throw new TypeError("second argument must be array-like"); + } + var len = others.length; + while (--len >= 0) { + if (value < others[len]) { + return false; + } + } + return true; + }; + is.minimum = function(value, others) { + if (isActualNaN(value)) { + throw new TypeError("NaN is not a valid value"); + } else if (!is.arraylike(others)) { + throw new TypeError("second argument must be array-like"); + } + var len = others.length; + while (--len >= 0) { + if (value > others[len]) { + return false; + } + } + return true; + }; + is.nan = function(value) { + return !is.number(value) || value !== value; + }; + is.even = function(value) { + return is.infinite(value) || is.number(value) && value === value && value % 2 === 0; + }; + is.odd = function(value) { + return is.infinite(value) || is.number(value) && value === value && value % 2 !== 0; + }; + is.ge = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { + throw new TypeError("NaN is not a valid value"); + } + return !is.infinite(value) && !is.infinite(other) && value >= other; + }; + is.gt = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { + throw new TypeError("NaN is not a valid value"); + } + return !is.infinite(value) && !is.infinite(other) && value > other; + }; + is.le = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { + throw new TypeError("NaN is not a valid value"); + } + return !is.infinite(value) && !is.infinite(other) && value <= other; + }; + is.lt = function(value, other) { + if (isActualNaN(value) || isActualNaN(other)) { + throw new TypeError("NaN is not a valid value"); + } + return !is.infinite(value) && !is.infinite(other) && value < other; + }; + is.within = function(value, start, finish) { + if (isActualNaN(value) || isActualNaN(start) || isActualNaN(finish)) { + throw new TypeError("NaN is not a valid value"); + } else if (!is.number(value) || !is.number(start) || !is.number(finish)) { + throw new TypeError("all arguments must be numbers"); + } + var isAnyInfinite = is.infinite(value) || is.infinite(start) || is.infinite(finish); + return isAnyInfinite || value >= start && value <= finish; + }; + is.object = function(value) { + return toStr.call(value) === "[object Object]"; + }; + is.primitive = function isPrimitive(value) { + if (!value) { + return true; + } + if (typeof value === "object" || is.object(value) || is.fn(value) || is.array(value)) { + return false; + } + return true; + }; + is.hash = function(value) { + return is.object(value) && value.constructor === Object && !value.nodeType && !value.setInterval; + }; + is.regexp = function(value) { + return toStr.call(value) === "[object RegExp]"; + }; + is.string = function(value) { + return toStr.call(value) === "[object String]"; + }; + is.base64 = function(value) { + return is.string(value) && (!value.length || base64Regex.test(value)); + }; + is.hex = function(value) { + return is.string(value) && (!value.length || hexRegex.test(value)); + }; + is.symbol = function(value) { + return typeof Symbol === "function" && toStr.call(value) === "[object Symbol]" && typeof symbolValueOf.call(value) === "symbol"; + }; + is.bigint = function(value) { + return typeof BigInt === "function" && toStr.call(value) === "[object BigInt]" && typeof bigIntValueOf.call(value) === "bigint"; + }; + var is_1 = is; + const is$1 = /* @__PURE__ */ getDefaultExportFromCjs(is_1); + const stats = {}; + function _await() { + var count = 0; + function fork(fn, n) { + count += n = typeof n === "number" && n >= 1 ? n : 1; + return function() { + fn && fn.apply(this, arguments); + if (n > 0) { + n--, count--, call(); + } + }; + } + var then = []; + function call() { + if (count === 0) { + while (then.length) { + setTimeout(then.shift(), 0); + } + } + } + fork.then = function(fn) { + if (count === 0) { + setTimeout(fn, 0); + } else { + then.push(fn); + } + }; + return fork; + } + stats.create = 0; + function Stage(arg) { + if (!(this instanceof Stage)) { + if (is$1.fn(arg)) { + return Stage.app.apply(Stage, arguments); + } else if (is$1.object(arg)) { + return Stage.atlas.apply(Stage, arguments); + } else { + return arg; + } + } + stats.create++; + for (var i = 0; i < _init.length; i++) { + _init[i].call(this); + } + } + var _init = []; + Stage._init = function(fn) { + _init.push(fn); + }; + var _load = []; + Stage._load = function(fn) { + _load.push(fn); + }; + var _config = {}; + Stage.config = function() { + if (arguments.length === 1 && is$1.string(arguments[0])) { + return _config[arguments[0]]; + } + if (arguments.length === 1 && is$1.object(arguments[0])) { + Object.assign(_config, arguments[0]); + } + if (arguments.length === 2 && is$1.string(arguments[0])) { + _config[arguments[1]]; + } + }; + var _app_queue = []; + var _stages = []; + var _loaded = false; + var _paused = false; + Stage.app = function(app, opts) { + if (!_loaded) { + _app_queue.push(arguments); + return; + } + console.log("Creating app..."); + var loader = Stage.config("app-loader"); + loader(function(stage, canvas) { + console.log("Initing app..."); + for (var i = 0; i < _load.length; i++) { + _load[i].call(this, stage, canvas); + } + app(stage, canvas); + _stages.push(stage); + console.log("Starting app..."); + stage.start(); + }, opts); + }; + var loading = _await(); + Stage.preload = function(load) { + if (typeof load !== "function") { + return; + } + load(loading()); + }; + Stage.start = function(config) { + console.log("Starting..."); + Stage.config(config); + loading.then(function() { + console.log("Loading apps..."); + _loaded = true; + while (_app_queue.length) { + var args = _app_queue.shift(); + Stage.app.apply(Stage, args); + } + }); + }; + Stage.pause = function() { + if (!_paused) { + _paused = true; + for (var i = _stages.length - 1; i >= 0; i--) { + _stages[i].pause(); + } + } + }; + Stage.resume = function() { + if (_paused) { + _paused = false; + for (var i = _stages.length - 1; i >= 0; i--) { + _stages[i].resume(); + } + } + }; + Stage.create = function() { + return new Stage(); + }; + function Matrix(a, b, c, d, e, f) { + this.reset(a, b, c, d, e, f); + } + Matrix.prototype.toString = function() { + return "[" + this.a + ", " + this.b + ", " + this.c + ", " + this.d + ", " + this.e + ", " + this.f + "]"; + }; + Matrix.prototype.clone = function() { + return new Matrix(this.a, this.b, this.c, this.d, this.e, this.f); + }; + Matrix.prototype.reset = function(a, b, c, d, e, f) { + this._dirty = true; + if (typeof a === "object") { + this.a = a.a, this.d = a.d; + this.b = a.b, this.c = a.c; + this.e = a.e, this.f = a.f; + } else { + this.a = a || 1, this.d = d || 1; + this.b = b || 0, this.c = c || 0; + this.e = e || 0, this.f = f || 0; + } + return this; + }; + Matrix.prototype.identity = function() { + this._dirty = true; + this.a = 1; + this.b = 0; + this.c = 0; + this.d = 1; + this.e = 0; + this.f = 0; + return this; + }; + Matrix.prototype.rotate = function(angle) { + if (!angle) { + return this; + } + this._dirty = true; + var u = angle ? Math.cos(angle) : 1; + var v = angle ? Math.sin(angle) : 0; + var a = u * this.a - v * this.b; + var b = u * this.b + v * this.a; + var c = u * this.c - v * this.d; + var d = u * this.d + v * this.c; + var e = u * this.e - v * this.f; + var f = u * this.f + v * this.e; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + return this; + }; + Matrix.prototype.translate = function(x, y) { + if (!x && !y) { + return this; + } + this._dirty = true; + this.e += x; + this.f += y; + return this; + }; + Matrix.prototype.scale = function(x, y) { + if (!(x - 1) && !(y - 1)) { + return this; + } + this._dirty = true; + this.a *= x; + this.b *= y; + this.c *= x; + this.d *= y; + this.e *= x; + this.f *= y; + return this; + }; + Matrix.prototype.skew = function(x, y) { + if (!x && !y) { + return this; + } + this._dirty = true; + var a = this.a + this.b * x; + var b = this.b + this.a * y; + var c = this.c + this.d * x; + var d = this.d + this.c * y; + var e = this.e + this.f * x; + var f = this.f + this.e * y; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + return this; + }; + Matrix.prototype.concat = function(m) { + this._dirty = true; + var n = this; + var a = n.a * m.a + n.b * m.c; + var b = n.b * m.d + n.a * m.b; + var c = n.c * m.a + n.d * m.c; + var d = n.d * m.d + n.c * m.b; + var e = n.e * m.a + m.e + n.f * m.c; + var f = n.f * m.d + m.f + n.e * m.b; + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + return this; + }; + Matrix.prototype.inverse = Matrix.prototype.reverse = function() { + if (this._dirty) { + this._dirty = false; + this.inversed = this.inversed || new Matrix(); + var z = this.a * this.d - this.b * this.c; + this.inversed.a = this.d / z; + this.inversed.b = -this.b / z; + this.inversed.c = -this.c / z; + this.inversed.d = this.a / z; + this.inversed.e = (this.c * this.f - this.e * this.d) / z; + this.inversed.f = (this.e * this.b - this.a * this.f) / z; + } + return this.inversed; + }; + Matrix.prototype.map = function(p, q) { + q = q || {}; + q.x = this.a * p.x + this.c * p.y + this.e; + q.y = this.b * p.x + this.d * p.y + this.f; + return q; + }; + Matrix.prototype.mapX = function(x, y) { + if (typeof x === "object") + y = x.y, x = x.x; + return this.a * x + this.c * y + this.e; + }; + Matrix.prototype.mapY = function(x, y) { + if (typeof x === "object") + y = x.y, x = x.x; + return this.b * x + this.d * y + this.f; + }; + var native = Math; + const math = Object.create(Math); + math.random = function(min, max) { + if (typeof min === "undefined") { + max = 1, min = 0; + } else if (typeof max === "undefined") { + max = min, min = 0; + } + return min == max ? min : native.random() * (max - min) + min; + }; + math.rotate = function(num, min, max) { + if (typeof min === "undefined") { + max = 1, min = 0; + } else if (typeof max === "undefined") { + max = min, min = 0; + } + if (max > min) { + num = (num - min) % (max - min); + return num + (num < 0 ? max : min); + } else { + num = (num - max) % (min - max); + return num + (num <= 0 ? min : max); + } + }; + math.limit = function(num, min, max) { + if (num < min) { + return min; + } else if (num > max) { + return max; + } else { + return num; + } + }; + math.length = function(x, y) { + return native.sqrt(x * x + y * y); + }; + function Texture(image, ratio) { + if (typeof image === "object") { + this.src(image, ratio); + } + } + Texture.prototype.pipe = function() { + return new Texture(this); + }; + Texture.prototype.src = function(x, y, w, h) { + if (typeof x === "object") { + var image = x, ratio = y || 1; + this._image = image; + this._sx = this._dx = 0; + this._sy = this._dy = 0; + this._sw = this._dw = image.width / ratio; + this._sh = this._dh = image.height / ratio; + this.width = image.width / ratio; + this.height = image.height / ratio; + this.ratio = ratio; + } else { + if (typeof w === "undefined") { + w = x, h = y; + } else { + this._sx = x, this._sy = y; + } + this._sw = this._dw = w; + this._sh = this._dh = h; + this.width = w; + this.height = h; + } + return this; + }; + Texture.prototype.dest = function(x, y, w, h) { + this._dx = x, this._dy = y; + this._dx = x, this._dy = y; + if (typeof w !== "undefined") { + this._dw = w, this._dh = h; + this.width = w, this.height = h; + } + return this; + }; + Texture.prototype.draw = function(context, x1, y1, x2, y2, x3, y3, x4, y4) { + var image = this._image; + if (image === null || typeof image !== "object") { + return; + } + var sx = this._sx, sy = this._sy; + var sw = this._sw, sh = this._sh; + var dx = this._dx, dy = this._dy; + var dw = this._dw, dh = this._dh; + if (typeof x3 !== "undefined") { + x1 = math.limit(x1, 0, this._sw), x2 = math.limit(x2, 0, this._sw - x1); + y1 = math.limit(y1, 0, this._sh), y2 = math.limit(y2, 0, this._sh - y1); + sx += x1, sy += y1, sw = x2, sh = y2; + dx = x3, dy = y3, dw = x4, dh = y4; + } else if (typeof x2 !== "undefined") { + dx = x1, dy = y1, dw = x2, dh = y2; + } else if (typeof x1 !== "undefined") { + dw = x1, dh = y1; + } + var ratio = this.ratio || 1; + sx *= ratio, sy *= ratio, sw *= ratio, sh *= ratio; + try { + if (typeof image.draw === "function") { + image.draw(context, sx, sy, sw, sh, dx, dy, dw, dh); + } else { + stats.draw++; + context.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh); + } + } catch (ex) { + if (!image._draw_failed) { + console.log("Unable to draw: ", image); + console.log(ex); + image._draw_failed = true; + } + } + }; + var _atlases_map = {}; + var _atlases_arr = []; + Stage.atlas = function(def) { + var atlas = is$1.fn(def.draw) ? def : new Atlas(def); + if (def.name) { + _atlases_map[def.name] = atlas; + } + _atlases_arr.push(atlas); + deprecated(def, "imagePath"); + deprecated(def, "imageRatio"); + var url = def.imagePath; + var ratio = def.imageRatio || 1; + if (is$1.string(def.image)) { + url = def.image; + } else if (is$1.hash(def.image)) { + url = def.image.src || def.image.url; + ratio = def.image.ratio || ratio; + } + url && Stage.preload(function(done) { + console.log("Loading atlas: " + url); + var imageloader = Stage.config("image-loader"); + imageloader(url, function(image) { + console.log("Image loaded: " + url); + atlas.src(image, ratio); + done(); + }, function(err) { + console.log("Error loading atlas: " + url, err); + done(); + }); + }); + return atlas; + }; + Atlas._super = Texture; + Atlas.prototype = Object.create(Atlas._super.prototype); + function Atlas(def) { + Atlas._super.call(this); + var atlas = this; + deprecated(def, "filter"); + deprecated(def, "cutouts"); + deprecated(def, "sprites"); + deprecated(def, "factory"); + var map = def.map || def.filter; + var ppu = def.ppu || def.ratio || 1; + var trim = def.trim || 0; + var textures = def.textures; + var factory = def.factory; + var cutouts = def.cutouts || def.sprites; + function make(def2) { + if (!def2 || is$1.fn(def2.draw)) { + return def2; + } + def2 = Object.assign({}, def2); + if (is$1.fn(map)) { + def2 = map(def2); + } + if (ppu != 1) { + def2.x *= ppu, def2.y *= ppu; + def2.width *= ppu, def2.height *= ppu; + def2.top *= ppu, def2.bottom *= ppu; + def2.left *= ppu, def2.right *= ppu; + } + if (trim != 0) { + def2.x += trim, def2.y += trim; + def2.width -= 2 * trim, def2.height -= 2 * trim; + def2.top -= trim, def2.bottom -= trim; + def2.left -= trim, def2.right -= trim; + } + var texture = atlas.pipe(); + texture.top = def2.top, texture.bottom = def2.bottom; + texture.left = def2.left, texture.right = def2.right; + texture.src(def2.x, def2.y, def2.width, def2.height); + return texture; + } + function find(query) { + if (textures) { + if (is$1.fn(textures)) { + return textures(query); + } else if (is$1.hash(textures)) { + return textures[query]; + } + } + if (cutouts) { + var result = null, n = 0; + for (var i = 0; i < cutouts.length; i++) { + if (string.startsWith(cutouts[i].name, query)) { + if (n === 0) { + result = cutouts[i]; + } else if (n === 1) { + result = [result, cutouts[i]]; + } else { + result.push(cutouts[i]); + } + n++; + } + } + if (n === 0 && is$1.fn(factory)) { + result = function(subquery) { + return factory(query + (subquery ? subquery : "")); + }; + } + return result; + } + } + this.select = function(query) { + if (!query) { + return new Selection(this.pipe()); + } + var found = find(query); + if (found) { + return new Selection(found, find, make); + } + }; + } + var nfTexture = new Texture(); + nfTexture.x = nfTexture.y = nfTexture.width = nfTexture.height = 0; + nfTexture.pipe = nfTexture.src = nfTexture.dest = function() { + return this; + }; + nfTexture.draw = function() { + }; + var nfSelection = new Selection(nfTexture); + function Selection(result, find, make) { + function link(result2, subquery) { + if (!result2) { + return nfTexture; + } else if (is$1.fn(result2.draw)) { + return result2; + } else if (is$1.hash(result2) && is$1.number(result2.width) && is$1.number(result2.height) && is$1.fn(make)) { + return make(result2); + } else if (is$1.hash(result2) && is$1.defined(subquery)) { + return link(result2[subquery]); + } else if (is$1.fn(result2)) { + return link(result2(subquery)); + } else if (is$1.array(result2)) { + return link(result2[0]); + } else if (is$1.string(result2) && is$1.fn(find)) { + return link(find(result2)); + } + } + this.one = function(subquery) { + return link(result, subquery); + }; + this.array = function(arr) { + var array = is$1.array(arr) ? arr : []; + if (is$1.array(result)) { + for (var i = 0; i < result.length; i++) { + array[i] = link(result[i]); + } + } else { + array[0] = link(result); + } + return array; + }; + } + Stage.texture = function(query) { + if (!is$1.string(query)) { + return new Selection(query); + } + var result = null, atlas, i; + if ((i = query.indexOf(":")) > 0 && query.length > i + 1) { + atlas = _atlases_map[query.slice(0, i)]; + result = atlas && atlas.select(query.slice(i + 1)); + } + if (!result && (atlas = _atlases_map[query])) { + result = atlas.select(); + } + for (i = 0; !result && i < _atlases_arr.length; i++) { + result = _atlases_arr[i].select(query); + } + if (!result) { + console.error("Texture not found: " + query); + result = nfSelection; + } + return result; + }; + function deprecated(hash, name, msg) { + if (name in hash) + console.log(msg ? msg.replace("%name", name) : "'" + name + "' field of texture atlas is deprecated."); + } + var iid$1 = 0; + Stage.prototype._label = ""; + Stage.prototype._visible = true; + Stage.prototype._parent = null; + Stage.prototype._next = null; + Stage.prototype._prev = null; + Stage.prototype._first = null; + Stage.prototype._last = null; + Stage.prototype._attrs = null; + Stage.prototype._flags = null; + Stage.prototype.toString = function() { + return "[" + this._label + "]"; + }; + Stage.prototype.id = function(id) { + return this.label(id); + }; + Stage.prototype.label = function(label) { + if (typeof label === "undefined") { + return this._label; + } + this._label = label; + return this; + }; + Stage.prototype.attr = function(name, value) { + if (typeof value === "undefined") { + return this._attrs !== null ? this._attrs[name] : void 0; + } + (this._attrs !== null ? this._attrs : this._attrs = {})[name] = value; + return this; + }; + Stage.prototype.visible = function(visible) { + if (typeof visible === "undefined") { + return this._visible; + } + this._visible = visible; + this._parent && (this._parent._ts_children = ++iid$1); + this._ts_pin = ++iid$1; + this.touch(); + return this; + }; + Stage.prototype.hide = function() { + return this.visible(false); + }; + Stage.prototype.show = function() { + return this.visible(true); + }; + Stage.prototype.parent = function() { + return this._parent; + }; + Stage.prototype.next = function(visible) { + var next = this._next; + while (next && visible && !next._visible) { + next = next._next; + } + return next; + }; + Stage.prototype.prev = function(visible) { + var prev = this._prev; + while (prev && visible && !prev._visible) { + prev = prev._prev; + } + return prev; + }; + Stage.prototype.first = function(visible) { + var next = this._first; + while (next && visible && !next._visible) { + next = next._next; + } + return next; + }; + Stage.prototype.last = function(visible) { + var prev = this._last; + while (prev && visible && !prev._visible) { + prev = prev._prev; + } + return prev; + }; + Stage.prototype.visit = function(visitor, data) { + var reverse = visitor.reverse; + var visible = visitor.visible; + if (visitor.start && visitor.start(this, data)) { + return; + } + var child, next = reverse ? this.last(visible) : this.first(visible); + while (child = next) { + next = reverse ? child.prev(visible) : child.next(visible); + if (child.visit(visitor, data)) { + return true; + } + } + return visitor.end && visitor.end(this, data); + }; + Stage.prototype.append = function(child, more) { + if (is$1.array(child)) + for (var i = 0; i < child.length; i++) + append(this, child[i]); + else if (typeof more !== "undefined") + for (var i = 0; i < arguments.length; i++) + append(this, arguments[i]); + else if (typeof child !== "undefined") + append(this, child); + return this; + }; + Stage.prototype.prepend = function(child, more) { + if (is$1.array(child)) + for (var i = child.length - 1; i >= 0; i--) + prepend(this, child[i]); + else if (typeof more !== "undefined") + for (var i = arguments.length - 1; i >= 0; i--) + prepend(this, arguments[i]); + else if (typeof child !== "undefined") + prepend(this, child); + return this; + }; + Stage.prototype.appendTo = function(parent) { + append(parent, this); + return this; + }; + Stage.prototype.prependTo = function(parent) { + prepend(parent, this); + return this; + }; + Stage.prototype.insertNext = function(sibling, more) { + if (is$1.array(sibling)) + for (var i = 0; i < sibling.length; i++) + insertAfter(sibling[i], this); + else if (typeof more !== "undefined") + for (var i = 0; i < arguments.length; i++) + insertAfter(arguments[i], this); + else if (typeof sibling !== "undefined") + insertAfter(sibling, this); + return this; + }; + Stage.prototype.insertPrev = function(sibling, more) { + if (is$1.array(sibling)) + for (var i = sibling.length - 1; i >= 0; i--) + insertBefore(sibling[i], this); + else if (typeof more !== "undefined") + for (var i = arguments.length - 1; i >= 0; i--) + insertBefore(arguments[i], this); + else if (typeof sibling !== "undefined") + insertBefore(sibling, this); + return this; + }; + Stage.prototype.insertAfter = function(prev) { + insertAfter(this, prev); + return this; + }; + Stage.prototype.insertBefore = function(next) { + insertBefore(this, next); + return this; + }; + function append(parent, child) { + _ensure(child); + _ensure(parent); + child.remove(); + if (parent._last) { + parent._last._next = child; + child._prev = parent._last; + } + child._parent = parent; + parent._last = child; + if (!parent._first) { + parent._first = child; + } + child._parent._flag(child, true); + child._ts_parent = ++iid$1; + parent._ts_children = ++iid$1; + parent.touch(); + } + function prepend(parent, child) { + _ensure(child); + _ensure(parent); + child.remove(); + if (parent._first) { + parent._first._prev = child; + child._next = parent._first; + } + child._parent = parent; + parent._first = child; + if (!parent._last) { + parent._last = child; + } + child._parent._flag(child, true); + child._ts_parent = ++iid$1; + parent._ts_children = ++iid$1; + parent.touch(); + } + function insertBefore(self2, next) { + _ensure(self2); + _ensure(next); + self2.remove(); + var parent = next._parent; + var prev = next._prev; + next._prev = self2; + prev && (prev._next = self2) || parent && (parent._first = self2); + self2._parent = parent; + self2._prev = prev; + self2._next = next; + self2._parent._flag(self2, true); + self2._ts_parent = ++iid$1; + self2.touch(); + } + function insertAfter(self2, prev) { + _ensure(self2); + _ensure(prev); + self2.remove(); + var parent = prev._parent; + var next = prev._next; + prev._next = self2; + next && (next._prev = self2) || parent && (parent._last = self2); + self2._parent = parent; + self2._prev = prev; + self2._next = next; + self2._parent._flag(self2, true); + self2._ts_parent = ++iid$1; + self2.touch(); + } + Stage.prototype.remove = function(child, more) { + if (typeof child !== "undefined") { + if (is$1.array(child)) { + for (var i = 0; i < child.length; i++) + _ensure(child[i]).remove(); + } else if (typeof more !== "undefined") { + for (var i = 0; i < arguments.length; i++) + _ensure(arguments[i]).remove(); + } else { + _ensure(child).remove(); + } + return this; + } + if (this._prev) { + this._prev._next = this._next; + } + if (this._next) { + this._next._prev = this._prev; + } + if (this._parent) { + if (this._parent._first === this) { + this._parent._first = this._next; + } + if (this._parent._last === this) { + this._parent._last = this._prev; + } + this._parent._flag(this, false); + this._parent._ts_children = ++iid$1; + this._parent.touch(); + } + this._prev = this._next = this._parent = null; + this._ts_parent = ++iid$1; + return this; + }; + Stage.prototype.empty = function() { + var child, next = this._first; + while (child = next) { + next = child._next; + child._prev = child._next = child._parent = null; + this._flag(child, false); + } + this._first = this._last = null; + this._ts_children = ++iid$1; + this.touch(); + return this; + }; + Stage.prototype.touch = function() { + this._ts_touch = ++iid$1; + this._parent && this._parent.touch(); + return this; + }; + Stage.prototype._flag = function(obj, name) { + if (typeof name === "undefined") { + return this._flags !== null && this._flags[obj] || 0; + } + if (typeof obj === "string") { + if (name) { + this._flags = this._flags || {}; + if (!this._flags[obj] && this._parent) { + this._parent._flag(obj, true); + } + this._flags[obj] = (this._flags[obj] || 0) + 1; + } else if (this._flags && this._flags[obj] > 0) { + if (this._flags[obj] == 1 && this._parent) { + this._parent._flag(obj, false); + } + this._flags[obj] = this._flags[obj] - 1; + } + } + if (typeof obj === "object") { + if (obj._flags) { + for (var type in obj._flags) { + if (obj._flags[type] > 0) { + this._flag(type, name); + } + } + } + } + return this; + }; + Stage.prototype.hitTest = function(hit) { + var width = this._pin._width; + var height = this._pin._height; + return hit.x >= 0 && hit.x <= width && hit.y >= 0 && hit.y <= height; + }; + function _ensure(obj) { + if (obj && obj instanceof Stage) { + return obj; + } + throw "Invalid node: " + obj; + } + function listenable(prototype, callback) { + prototype._listeners = null; + prototype.on = prototype.listen = function(types, listener) { + if (!types || !types.length || typeof listener !== "function") { + return this; + } + if (this._listeners === null) { + this._listeners = {}; + } + var isarray = typeof types !== "string" && typeof types.join === "function"; + if (types = (isarray ? types.join(" ") : types).match(/\S+/g)) { + for (var i = 0; i < types.length; i++) { + var type = types[i]; + this._listeners[type] = this._listeners[type] || []; + this._listeners[type].push(listener); + if (typeof callback === "function") { + callback(this, type, true); + } + } + } + return this; + }; + prototype.off = function(types, listener) { + if (!types || !types.length || typeof listener !== "function") { + return this; + } + if (this._listeners === null) { + return this; + } + var isarray = typeof types !== "string" && typeof types.join === "function"; + if (types = (isarray ? types.join(" ") : types).match(/\S+/g)) { + for (var i = 0; i < types.length; i++) { + var type = types[i], all = this._listeners[type], index; + if (all && (index = all.indexOf(listener)) >= 0) { + all.splice(index, 1); + if (!all.length) { + delete this._listeners[type]; + } + if (typeof callback === "function") { + callback(this, type, false); + } + } + } + } + return this; + }; + prototype.listeners = function(type) { + return this._listeners && this._listeners[type]; + }; + prototype.publish = function(name, args) { + var listeners = this.listeners(name); + if (!listeners || !listeners.length) { + return 0; + } + for (var l = 0; l < listeners.length; l++) { + listeners[l].apply(this, args); + } + return listeners.length; + }; + prototype.trigger = function(name, args) { + this.publish(name, args); + return this; + }; + } + var iid = 0; + Stage._init(function() { + this._pin = new Pin(this); + }); + Stage.prototype.matrix = function(relative) { + if (relative === true) { + return this._pin.relativeMatrix(); + } + return this._pin.absoluteMatrix(); + }; + Stage.prototype.pin = function(a, b) { + if (typeof a === "object") { + this._pin.set(a); + return this; + } else if (typeof a === "string") { + if (typeof b === "undefined") { + return this._pin.get(a); + } else { + this._pin.set(a, b); + return this; + } + } else if (typeof a === "undefined") { + return this._pin; + } + }; + function Pin(owner) { + this._owner = owner; + this._parent = null; + this._relativeMatrix = new Matrix(); + this._absoluteMatrix = new Matrix(); + this.reset(); + } + Pin.prototype.reset = function() { + this._textureAlpha = 1; + this._alpha = 1; + this._width = 0; + this._height = 0; + this._scaleX = 1; + this._scaleY = 1; + this._skewX = 0; + this._skewY = 0; + this._rotation = 0; + this._pivoted = false; + this._pivotX = null; + this._pivotY = null; + this._handled = false; + this._handleX = 0; + this._handleY = 0; + this._aligned = false; + this._alignX = 0; + this._alignY = 0; + this._offsetX = 0; + this._offsetY = 0; + this._boxX = 0; + this._boxY = 0; + this._boxWidth = this._width; + this._boxHeight = this._height; + this._ts_translate = ++iid; + this._ts_transform = ++iid; + this._ts_matrix = ++iid; + }; + Pin.prototype._update = function() { + this._parent = this._owner._parent && this._owner._parent._pin; + if (this._handled && this._mo_handle != this._ts_transform) { + this._mo_handle = this._ts_transform; + this._ts_translate = ++iid; + } + if (this._aligned && this._parent && this._mo_align != this._parent._ts_transform) { + this._mo_align = this._parent._ts_transform; + this._ts_translate = ++iid; + } + return this; + }; + Pin.prototype.toString = function() { + return this._owner + " (" + (this._parent ? this._parent._owner : null) + ")"; + }; + Pin.prototype.absoluteMatrix = function() { + this._update(); + var ts = Math.max( + this._ts_transform, + this._ts_translate, + this._parent ? this._parent._ts_matrix : 0 + ); + if (this._mo_abs == ts) { + return this._absoluteMatrix; + } + this._mo_abs = ts; + var abs2 = this._absoluteMatrix; + abs2.reset(this.relativeMatrix()); + this._parent && abs2.concat(this._parent._absoluteMatrix); + this._ts_matrix = ++iid; + return abs2; + }; + Pin.prototype.relativeMatrix = function() { + this._update(); + var ts = Math.max( + this._ts_transform, + this._ts_translate, + this._parent ? this._parent._ts_transform : 0 + ); + if (this._mo_rel == ts) { + return this._relativeMatrix; + } + this._mo_rel = ts; + var rel2 = this._relativeMatrix; + rel2.identity(); + if (this._pivoted) { + rel2.translate(-this._pivotX * this._width, -this._pivotY * this._height); + } + rel2.scale(this._scaleX, this._scaleY); + rel2.skew(this._skewX, this._skewY); + rel2.rotate(this._rotation); + if (this._pivoted) { + rel2.translate(this._pivotX * this._width, this._pivotY * this._height); + } + if (this._pivoted) { + this._boxX = 0; + this._boxY = 0; + this._boxWidth = this._width; + this._boxHeight = this._height; + } else { + var p, q; + if (rel2.a > 0 && rel2.c > 0 || rel2.a < 0 && rel2.c < 0) { + p = 0, q = rel2.a * this._width + rel2.c * this._height; + } else { + p = rel2.a * this._width, q = rel2.c * this._height; + } + if (p > q) { + this._boxX = q; + this._boxWidth = p - q; + } else { + this._boxX = p; + this._boxWidth = q - p; + } + if (rel2.b > 0 && rel2.d > 0 || rel2.b < 0 && rel2.d < 0) { + p = 0, q = rel2.b * this._width + rel2.d * this._height; + } else { + p = rel2.b * this._width, q = rel2.d * this._height; + } + if (p > q) { + this._boxY = q; + this._boxHeight = p - q; + } else { + this._boxY = p; + this._boxHeight = q - p; + } + } + this._x = this._offsetX; + this._y = this._offsetY; + this._x -= this._boxX + this._handleX * this._boxWidth; + this._y -= this._boxY + this._handleY * this._boxHeight; + if (this._aligned && this._parent) { + this._parent.relativeMatrix(); + this._x += this._alignX * this._parent._width; + this._y += this._alignY * this._parent._height; + } + rel2.translate(this._x, this._y); + return this._relativeMatrix; + }; + Pin.prototype.get = function(key) { + if (typeof getters[key] === "function") { + return getters[key](this); + } + }; + Pin.prototype.set = function(a, b) { + if (typeof a === "string") { + if (typeof setters[a] === "function" && typeof b !== "undefined") { + setters[a](this, b); + } + } else if (typeof a === "object") { + for (b in a) { + if (typeof setters[b] === "function" && typeof a[b] !== "undefined") { + setters[b](this, a[b], a); + } + } + } + if (this._owner) { + this._owner._ts_pin = ++iid; + this._owner.touch(); + } + return this; + }; + var getters = { + alpha: function(pin) { + return pin._alpha; + }, + textureAlpha: function(pin) { + return pin._textureAlpha; + }, + width: function(pin) { + return pin._width; + }, + height: function(pin) { + return pin._height; + }, + boxWidth: function(pin) { + return pin._boxWidth; + }, + boxHeight: function(pin) { + return pin._boxHeight; + }, + // scale : function(pin) { + // }, + scaleX: function(pin) { + return pin._scaleX; + }, + scaleY: function(pin) { + return pin._scaleY; + }, + // skew : function(pin) { + // }, + skewX: function(pin) { + return pin._skewX; + }, + skewY: function(pin) { + return pin._skewY; + }, + rotation: function(pin) { + return pin._rotation; + }, + // pivot : function(pin) { + // }, + pivotX: function(pin) { + return pin._pivotX; + }, + pivotY: function(pin) { + return pin._pivotY; + }, + // offset : function(pin) { + // }, + offsetX: function(pin) { + return pin._offsetX; + }, + offsetY: function(pin) { + return pin._offsetY; + }, + // align : function(pin) { + // }, + alignX: function(pin) { + return pin._alignX; + }, + alignY: function(pin) { + return pin._alignY; + }, + // handle : function(pin) { + // }, + handleX: function(pin) { + return pin._handleX; + }, + handleY: function(pin) { + return pin._handleY; + } + }; + var setters = { + alpha: function(pin, value) { + pin._alpha = value; + }, + textureAlpha: function(pin, value) { + pin._textureAlpha = value; + }, + width: function(pin, value) { + pin._width_ = value; + pin._width = value; + pin._ts_transform = ++iid; + }, + height: function(pin, value) { + pin._height_ = value; + pin._height = value; + pin._ts_transform = ++iid; + }, + scale: function(pin, value) { + pin._scaleX = value; + pin._scaleY = value; + pin._ts_transform = ++iid; + }, + scaleX: function(pin, value) { + pin._scaleX = value; + pin._ts_transform = ++iid; + }, + scaleY: function(pin, value) { + pin._scaleY = value; + pin._ts_transform = ++iid; + }, + skew: function(pin, value) { + pin._skewX = value; + pin._skewY = value; + pin._ts_transform = ++iid; + }, + skewX: function(pin, value) { + pin._skewX = value; + pin._ts_transform = ++iid; + }, + skewY: function(pin, value) { + pin._skewY = value; + pin._ts_transform = ++iid; + }, + rotation: function(pin, value) { + pin._rotation = value; + pin._ts_transform = ++iid; + }, + pivot: function(pin, value) { + pin._pivotX = value; + pin._pivotY = value; + pin._pivoted = true; + pin._ts_transform = ++iid; + }, + pivotX: function(pin, value) { + pin._pivotX = value; + pin._pivoted = true; + pin._ts_transform = ++iid; + }, + pivotY: function(pin, value) { + pin._pivotY = value; + pin._pivoted = true; + pin._ts_transform = ++iid; + }, + offset: function(pin, value) { + pin._offsetX = value; + pin._offsetY = value; + pin._ts_translate = ++iid; + }, + offsetX: function(pin, value) { + pin._offsetX = value; + pin._ts_translate = ++iid; + }, + offsetY: function(pin, value) { + pin._offsetY = value; + pin._ts_translate = ++iid; + }, + align: function(pin, value) { + this.alignX(pin, value); + this.alignY(pin, value); + }, + alignX: function(pin, value) { + pin._alignX = value; + pin._aligned = true; + pin._ts_translate = ++iid; + this.handleX(pin, value); + }, + alignY: function(pin, value) { + pin._alignY = value; + pin._aligned = true; + pin._ts_translate = ++iid; + this.handleY(pin, value); + }, + handle: function(pin, value) { + this.handleX(pin, value); + this.handleY(pin, value); + }, + handleX: function(pin, value) { + pin._handleX = value; + pin._handled = true; + pin._ts_translate = ++iid; + }, + handleY: function(pin, value) { + pin._handleY = value; + pin._handled = true; + pin._ts_translate = ++iid; + }, + resizeMode: function(pin, value, all) { + if (all) { + if (value == "in") { + value = "in-pad"; + } else if (value == "out") { + value = "out-crop"; + } + scaleTo(pin, all.resizeWidth, all.resizeHeight, value); + } + }, + resizeWidth: function(pin, value, all) { + if (!all || !all.resizeMode) { + scaleTo(pin, value, null); + } + }, + resizeHeight: function(pin, value, all) { + if (!all || !all.resizeMode) { + scaleTo(pin, null, value); + } + }, + scaleMode: function(pin, value, all) { + if (all) { + scaleTo(pin, all.scaleWidth, all.scaleHeight, value); + } + }, + scaleWidth: function(pin, value, all) { + if (!all || !all.scaleMode) { + scaleTo(pin, value, null); + } + }, + scaleHeight: function(pin, value, all) { + if (!all || !all.scaleMode) { + scaleTo(pin, null, value); + } + }, + matrix: function(pin, value) { + this.scaleX(pin, value.a); + this.skewX(pin, value.c / value.d); + this.skewY(pin, value.b / value.a); + this.scaleY(pin, value.d); + this.offsetX(pin, value.e); + this.offsetY(pin, value.f); + this.rotation(pin, 0); + } + }; + function scaleTo(pin, width, height, mode) { + var w = typeof width === "number"; + var h = typeof height === "number"; + var m = typeof mode === "string"; + pin._ts_transform = ++iid; + if (w) { + pin._scaleX = width / pin._width_; + pin._width = pin._width_; + } + if (h) { + pin._scaleY = height / pin._height_; + pin._height = pin._height_; + } + if (w && h && m) { + if (mode == "out" || mode == "out-crop") { + pin._scaleX = pin._scaleY = Math.max(pin._scaleX, pin._scaleY); + } else if (mode == "in" || mode == "in-pad") { + pin._scaleX = pin._scaleY = Math.min(pin._scaleX, pin._scaleY); + } + if (mode == "out-crop" || mode == "in-pad") { + pin._width = width / pin._scaleX; + pin._height = height / pin._scaleY; + } + } + } + Stage.prototype.scaleTo = function(a, b, c) { + if (typeof a === "object") + c = b, b = a.y, a = a.x; + scaleTo(this._pin, a, b, c); + return this; + }; + Pin._add_shortcuts = function(Stage2) { + Stage2.prototype.size = function(w, h) { + this.pin("width", w); + this.pin("height", h); + return this; + }; + Stage2.prototype.width = function(w) { + if (typeof w === "undefined") { + return this.pin("width"); + } + this.pin("width", w); + return this; + }; + Stage2.prototype.height = function(h) { + if (typeof h === "undefined") { + return this.pin("height"); + } + this.pin("height", h); + return this; + }; + Stage2.prototype.offset = function(a, b) { + if (typeof a === "object") + b = a.y, a = a.x; + this.pin("offsetX", a); + this.pin("offsetY", b); + return this; + }; + Stage2.prototype.rotate = function(a) { + this.pin("rotation", a); + return this; + }; + Stage2.prototype.skew = function(a, b) { + if (typeof a === "object") + b = a.y, a = a.x; + else if (typeof b === "undefined") + b = a; + this.pin("skewX", a); + this.pin("skewY", b); + return this; + }; + Stage2.prototype.scale = function(a, b) { + if (typeof a === "object") + b = a.y, a = a.x; + else if (typeof b === "undefined") + b = a; + this.pin("scaleX", a); + this.pin("scaleY", b); + return this; + }; + Stage2.prototype.alpha = function(a, ta) { + this.pin("alpha", a); + if (typeof ta !== "undefined") { + this.pin("textureAlpha", ta); + } + return this; + }; + }; + Pin._add_shortcuts(Stage); + Stage.prototype._textures = null; + Stage.prototype._alpha = 1; + Stage.prototype.render = function(context) { + if (!this._visible) { + return; + } + stats.node++; + var m = this.matrix(); + context.setTransform(m.a, m.b, m.c, m.d, m.e, m.f); + this._alpha = this._pin._alpha * (this._parent ? this._parent._alpha : 1); + var alpha = this._pin._textureAlpha * this._alpha; + if (context.globalAlpha != alpha) { + context.globalAlpha = alpha; + } + if (this._textures !== null) { + for (var i = 0, n = this._textures.length; i < n; i++) { + this._textures[i].draw(context); + } + } + if (context.globalAlpha != this._alpha) { + context.globalAlpha = this._alpha; + } + var child, next = this._first; + while (child = next) { + next = child._next; + child.render(context); + } + }; + Stage.prototype._tickBefore = null; + Stage.prototype._tickAfter = null; + Stage.prototype.MAX_ELAPSE = Infinity; + Stage.prototype._tick = function(elapsed, now, last) { + if (!this._visible) { + return; + } + if (elapsed > this.MAX_ELAPSE) { + elapsed = this.MAX_ELAPSE; + } + var ticked = false; + if (this._tickBefore !== null) { + for (var i = 0; i < this._tickBefore.length; i++) { + stats.tick++; + var tickFn = this._tickBefore[i]; + ticked = tickFn.call(this, elapsed, now, last) === true || ticked; + } + } + var child, next = this._first; + while (child = next) { + next = child._next; + if (child._flag("_tick")) { + ticked = child._tick(elapsed, now, last) === true ? true : ticked; + } + } + if (this._tickAfter !== null) { + for (var i = 0; i < this._tickAfter.length; i++) { + stats.tick++; + var tickFn = this._tickAfter[i]; + ticked = tickFn.call(this, elapsed, now, last) === true || ticked; + } + } + return ticked; + }; + Stage.prototype.tick = function(ticker, before) { + if (typeof ticker !== "function") { + return; + } + if (before) { + if (this._tickBefore === null) { + this._tickBefore = []; + } + this._tickBefore.push(ticker); + } else { + if (this._tickAfter === null) { + this._tickAfter = []; + } + this._tickAfter.push(ticker); + } + this._flag("_tick", this._tickAfter !== null && this._tickAfter.length > 0 || this._tickBefore !== null && this._tickBefore.length > 0); + }; + Stage.prototype.untick = function(ticker) { + if (typeof ticker !== "function") { + return; + } + var i; + if (this._tickBefore !== null && (i = this._tickBefore.indexOf(ticker)) >= 0) { + this._tickBefore.splice(i, 1); + } + if (this._tickAfter !== null && (i = this._tickAfter.indexOf(ticker)) >= 0) { + this._tickAfter.splice(i, 1); + } + }; + Stage.prototype.timeout = function(fn, time) { + this.setTimeout(fn, time); + }; + Stage.prototype.setTimeout = function(fn, time) { + function timer(t) { + if ((time -= t) < 0) { + this.untick(timer); + fn.call(this); + } else { + return true; + } + } + this.tick(timer); + return timer; + }; + Stage.prototype.clearTimeout = function(timer) { + this.untick(timer); + }; + Root._super = Stage; + Root.prototype = Object.create(Root._super.prototype); + Stage.root = function(request, render) { + return new Root(request, render); + }; + function Root(request, render) { + Root._super.call(this); + this.label("Root"); + var paused = true; + var stopped = true; + var self2 = this; + var lastTime = 0; + var loop = function(now) { + if (paused === true || stopped === true) { + return; + } + stats.tick = stats.node = stats.draw = 0; + var last = lastTime || now; + var elapsed = now - last; + lastTime = now; + var ticked = self2._tick(elapsed, now, last); + if (self2._mo_touch != self2._ts_touch) { + self2._mo_touch = self2._ts_touch; + render(self2); + request(loop); + } else if (ticked) { + request(loop); + } else { + paused = true; + } + stats.fps = elapsed ? 1e3 / elapsed : 0; + }; + this.start = function() { + stopped = false; + return this.resume(); + }; + this.resume = function() { + if (paused) { + this.publish("resume"); + paused = false; + request(loop); + } + return this; + }; + this.pause = function() { + if (!paused) { + this.publish("pause"); + } + paused = true; + return this; + }; + this.touch_root = this.touch; + this.touch = function() { + this.resume(); + return this.touch_root(); + }; + this.stop = function() { + stopped = true; + return this; + }; + } + Root.prototype.background = function(color) { + return this; + }; + Root.prototype.viewport = function(width, height, ratio) { + if (typeof width === "undefined") { + return Object.assign({}, this._viewport); + } + this._viewport = { + width, + height, + ratio: ratio || 1 + }; + this.viewbox(); + var data = Object.assign({}, this._viewport); + this.visit({ + start: function(node) { + if (!node._flag("viewport")) { + return true; + } + node.publish("viewport", [data]); + } + }); + return this; + }; + Root.prototype.viewbox = function(width, height, mode) { + if (typeof width === "number" && typeof height === "number") { + this._viewbox = { + width, + height, + mode: /^(in|out|in-pad|out-crop)$/.test(mode) ? mode : "in-pad" + }; + } + var box = this._viewbox; + var size = this._viewport; + if (size && box) { + this.pin({ + width: box.width, + height: box.height + }); + this.scaleTo(size.width, size.height, box.mode); + } else if (size) { + this.pin({ + width: size.width, + height: size.height + }); + } + return this; + }; + Stage.canvas = function(type, attributes, drawFn) { + if (typeof type === "string") { + if (typeof attributes === "object") + ; + else { + if (typeof attributes === "function") { + drawFn = attributes; + } + attributes = {}; + } + } else { + if (typeof type === "function") { + drawFn = type; + } + attributes = {}; + type = "2d"; + } + var canvas = document.createElement("canvas"); + var context = canvas.getContext(type, attributes); + var texture = new Texture(canvas); + texture.context = function() { + return context; + }; + texture.size = function(width, height, ratio) { + ratio = ratio || 1; + canvas.width = width * ratio; + canvas.height = height * ratio; + this.src(canvas, ratio); + return this; + }; + texture.canvas = function(fn) { + if (typeof fn === "function") { + fn.call(this, context); + } else if (typeof fn === "undefined" && typeof drawFn === "function") { + drawFn.call(this, context); + } + return this; + }; + if (typeof drawFn === "function") { + drawFn.call(texture, context); + } + return texture; + }; + function repeat(img, owidth, oheight, stretch, inner, insert) { + var width = img.width; + var height = img.height; + var left = img.left; + var right = img.right; + var top = img.top; + var bottom = img.bottom; + left = typeof left === "number" && left === left ? left : 0; + right = typeof right === "number" && right === right ? right : 0; + top = typeof top === "number" && top === top ? top : 0; + bottom = typeof bottom === "number" && bottom === bottom ? bottom : 0; + width = width - left - right; + height = height - top - bottom; + if (!inner) { + owidth = Math.max(owidth - left - right, 0); + oheight = Math.max(oheight - top - bottom, 0); + } + var i = 0; + if (top > 0 && left > 0) + insert(i++, 0, 0, left, top, 0, 0, left, top); + if (bottom > 0 && left > 0) + insert(i++, 0, height + top, left, bottom, 0, oheight + top, left, bottom); + if (top > 0 && right > 0) + insert(i++, width + left, 0, right, top, owidth + left, 0, right, top); + if (bottom > 0 && right > 0) + insert( + i++, + width + left, + height + top, + right, + bottom, + owidth + left, + oheight + top, + right, + bottom + ); + if (stretch) { + if (top > 0) + insert(i++, left, 0, width, top, left, 0, owidth, top); + if (bottom > 0) + insert( + i++, + left, + height + top, + width, + bottom, + left, + oheight + top, + owidth, + bottom + ); + if (left > 0) + insert(i++, 0, top, left, height, 0, top, left, oheight); + if (right > 0) + insert( + i++, + width + left, + top, + right, + height, + owidth + left, + top, + right, + oheight + ); + insert(i++, left, top, width, height, left, top, owidth, oheight); + } else { + var l = left, r = owidth, w; + while (r > 0) { + w = Math.min(width, r), r -= width; + var t = top, b = oheight, h; + while (b > 0) { + h = Math.min(height, b), b -= height; + insert(i++, left, top, w, h, l, t, w, h); + if (r <= 0) { + if (left) + insert(i++, 0, top, left, h, 0, t, left, h); + if (right) + insert(i++, width + left, top, right, h, l + w, t, right, h); + } + t += h; + } + if (top) + insert(i++, left, 0, w, top, l, 0, w, top); + if (bottom) + insert(i++, left, height + top, w, bottom, l, t, w, bottom); + l += w; + } + } + return i; + } + Stage.image = function(image) { + var img = new Image$1(); + image && img.image(image); + return img; + }; + Image$1._super = Stage; + Image$1.prototype = Object.create(Image$1._super.prototype); + function Image$1() { + Image$1._super.call(this); + this.label("Image"); + this._textures = []; + this._image = null; + } + Image$1.prototype.image = function(image) { + this._image = Stage.texture(image).one(); + this.pin("width", this._image ? this._image.width : 0); + this.pin("height", this._image ? this._image.height : 0); + this._textures[0] = this._image.pipe(); + this._textures.length = 1; + return this; + }; + Image$1.prototype.tile = function(inner) { + this._repeat(false, inner); + return this; + }; + Image$1.prototype.stretch = function(inner) { + this._repeat(true, inner); + return this; + }; + Image$1.prototype._repeat = function(stretch, inner) { + var self2 = this; + this.untick(this._repeatTicker); + this.tick(this._repeatTicker = function() { + if (this._mo_stretch == this._pin._ts_transform) { + return; + } + this._mo_stretch = this._pin._ts_transform; + var width = this.pin("width"); + var height = this.pin("height"); + this._textures.length = repeat(this._image, width, height, stretch, inner, insert); + }); + function insert(i, sx, sy, sw, sh, dx, dy, dw, dh) { + var repeat2 = self2._textures.length > i ? self2._textures[i] : self2._textures[i] = self2._image.pipe(); + repeat2.src(sx, sy, sw, sh); + repeat2.dest(dx, dy, dw, dh); + } + }; + Stage.anim = function(frames, fps) { + var anim = new Anim(); + anim.frames(frames).gotoFrame(0); + fps && anim.fps(fps); + return anim; + }; + Anim._super = Stage; + Anim.prototype = Object.create(Anim._super.prototype); + Stage.Anim = { + FPS: 15 + }; + function Anim() { + Anim._super.call(this); + this.label("Anim"); + this._textures = []; + this._fps = Stage.Anim.FPS; + this._ft = 1e3 / this._fps; + this._time = -1; + this._repeat = 0; + this._index = 0; + this._frames = []; + var lastTime = 0; + this.tick(function(t, now, last) { + if (this._time < 0 || this._frames.length <= 1) { + return; + } + var ignore = lastTime != last; + lastTime = now; + if (ignore) { + return true; + } + this._time += t; + if (this._time < this._ft) { + return true; + } + var n = this._time / this._ft | 0; + this._time -= n * this._ft; + this.moveFrame(n); + if (this._repeat > 0 && (this._repeat -= n) <= 0) { + this.stop(); + this._callback && this._callback(); + return false; + } + return true; + }, false); + } + Anim.prototype.fps = function(fps) { + if (typeof fps === "undefined") { + return this._fps; + } + this._fps = fps > 0 ? fps : Stage.Anim.FPS; + this._ft = 1e3 / this._fps; + return this; + }; + Anim.prototype.setFrames = function(a, b, c) { + return this.frames(a, b, c); + }; + Anim.prototype.frames = function(frames) { + this._index = 0; + this._frames = Stage.texture(frames).array(); + this.touch(); + return this; + }; + Anim.prototype.length = function() { + return this._frames ? this._frames.length : 0; + }; + Anim.prototype.gotoFrame = function(frame, resize) { + this._index = math.rotate(frame, this._frames.length) | 0; + resize = resize || !this._textures[0]; + this._textures[0] = this._frames[this._index]; + if (resize) { + this.pin("width", this._textures[0].width); + this.pin("height", this._textures[0].height); + } + this.touch(); + return this; + }; + Anim.prototype.moveFrame = function(move) { + return this.gotoFrame(this._index + move); + }; + Anim.prototype.repeat = function(repeat2, callback) { + this._repeat = repeat2 * this._frames.length - 1; + this._callback = callback; + this.play(); + return this; + }; + Anim.prototype.play = function(frame) { + if (typeof frame !== "undefined") { + this.gotoFrame(frame); + this._time = 0; + } else if (this._time < 0) { + this._time = 0; + } + this.touch(); + return this; + }; + Anim.prototype.stop = function(frame) { + this._time = -1; + if (typeof frame !== "undefined") { + this.gotoFrame(frame); + } + return this; + }; + Stage.string = function(frames) { + return new Str().frames(frames); + }; + Str._super = Stage; + Str.prototype = Object.create(Str._super.prototype); + function Str() { + Str._super.call(this); + this.label("String"); + this._textures = []; + } + Str.prototype.setFont = function(a, b, c) { + return this.frames(a, b, c); + }; + Str.prototype.frames = function(frames) { + this._textures = []; + if (typeof frames == "string") { + frames = Stage.texture(frames); + this._item = function(value) { + return frames.one(value); + }; + } else if (typeof frames === "object") { + this._item = function(value) { + return frames[value]; + }; + } else if (typeof frames === "function") { + this._item = frames; + } + return this; + }; + Str.prototype.setValue = function(a, b, c) { + return this.value(a, b, c); + }; + Str.prototype.value = function(value) { + if (typeof value === "undefined") { + return this._value; + } + if (this._value === value) { + return this; + } + this._value = value; + if (value === null) { + value = ""; + } else if (typeof value !== "string" && !is$1.array(value)) { + value = value.toString(); + } + this._spacing = this._spacing || 0; + var width = 0, height = 0; + for (var i = 0; i < value.length; i++) { + var image = this._textures[i] = this._item(value[i]); + width += i > 0 ? this._spacing : 0; + image.dest(width, 0); + width = width + image.width; + height = Math.max(height, image.height); + } + this.pin("width", width); + this.pin("height", height); + this._textures.length = value.length; + return this; + }; + Stage.row = function(align) { + return Stage.create().row(align).label("Row"); + }; + Stage.prototype.row = function(align) { + this.sequence("row", align); + return this; + }; + Stage.column = function(align) { + return Stage.create().column(align).label("Row"); + }; + Stage.prototype.column = function(align) { + this.sequence("column", align); + return this; + }; + Stage.sequence = function(type, align) { + return Stage.create().sequence(type, align).label("Sequence"); + }; + Stage.prototype.sequence = function(type, align) { + this._padding = this._padding || 0; + this._spacing = this._spacing || 0; + this.untick(this._layoutTiker); + this.tick(this._layoutTiker = function() { + if (this._mo_seq == this._ts_touch) { + return; + } + this._mo_seq = this._ts_touch; + var alignChildren = this._mo_seqAlign != this._ts_children; + this._mo_seqAlign = this._ts_children; + var width = 0, height = 0; + var child, next = this.first(true); + var first = true; + while (child = next) { + next = child.next(true); + child.matrix(true); + var w = child.pin("boxWidth"); + var h = child.pin("boxHeight"); + if (type == "column") { + !first && (height += this._spacing); + child.pin("offsetY") != height && child.pin("offsetY", height); + width = Math.max(width, w); + height = height + h; + alignChildren && child.pin("alignX", align); + } else if (type == "row") { + !first && (width += this._spacing); + child.pin("offsetX") != width && child.pin("offsetX", width); + width = width + w; + height = Math.max(height, h); + alignChildren && child.pin("alignY", align); + } + first = false; + } + width += 2 * this._padding; + height += 2 * this._padding; + this.pin("width") != width && this.pin("width", width); + this.pin("height") != height && this.pin("height", height); + }); + return this; + }; + Stage.box = function() { + return Stage.create().box().label("Box"); + }; + Stage.prototype.box = function() { + this._padding = this._padding || 0; + this.untick(this._layoutTiker); + this.tick(this._layoutTiker = function() { + if (this._mo_box == this._ts_touch) { + return; + } + this._mo_box = this._ts_touch; + var width = 0, height = 0; + var child, next = this.first(true); + while (child = next) { + next = child.next(true); + child.matrix(true); + var w = child.pin("boxWidth"); + var h = child.pin("boxHeight"); + width = Math.max(width, w); + height = Math.max(height, h); + } + width += 2 * this._padding; + height += 2 * this._padding; + this.pin("width") != width && this.pin("width", width); + this.pin("height") != height && this.pin("height", height); + }); + return this; + }; + Stage.layer = function() { + return Stage.create().layer().label("Layer"); + }; + Stage.prototype.layer = function() { + this.untick(this._layoutTiker); + this.tick(this._layoutTiker = function() { + var parent = this.parent(); + if (parent) { + var width = parent.pin("width"); + if (this.pin("width") != width) { + this.pin("width", width); + } + var height = parent.pin("height"); + if (this.pin("height") != height) { + this.pin("height", height); + } + } + }, true); + return this; + }; + Stage.prototype.padding = function(pad) { + this._padding = pad; + return this; + }; + Stage.prototype.spacing = function(space) { + this._spacing = space; + return this; + }; + function _identity(x) { + return x; + } + var _cache = {}; + var _modes = {}; + var _easings = {}; + function Easing(token) { + if (typeof token === "function") { + return token; + } + if (typeof token !== "string") { + return _identity; + } + var fn = _cache[token]; + if (fn) { + return fn; + } + var match = /^(\w+)(-(in|out|in-out|out-in))?(\((.*)\))?$/i.exec(token); + if (!match || !match.length) { + return _identity; + } + var easing = _easings[match[1]]; + var mode = _modes[match[3]]; + var params = match[5]; + if (easing && easing.fn) { + fn = easing.fn; + } else if (easing && easing.fc) { + fn = easing.fc.apply(easing.fc, params && params.replace(/\s+/, "").split(",")); + } else { + fn = _identity; + } + if (mode) { + fn = mode.fn(fn); + } + _cache[token] = fn; + return fn; + } + Easing.add = function(data) { + var names = (data.name || data.mode).split(/\s+/); + for (var i = 0; i < names.length; i++) { + var name = names[i]; + if (name) { + (data.name ? _easings : _modes)[name] = data; + } + } + }; + Easing.add({ + mode: "in", + fn: function(f) { + return f; + } + }); + Easing.add({ + mode: "out", + fn: function(f) { + return function(t) { + return 1 - f(1 - t); + }; + } + }); + Easing.add({ + mode: "in-out", + fn: function(f) { + return function(t) { + return t < 0.5 ? f(2 * t) / 2 : 1 - f(2 * (1 - t)) / 2; + }; + } + }); + Easing.add({ + mode: "out-in", + fn: function(f) { + return function(t) { + return t < 0.5 ? 1 - f(2 * (1 - t)) / 2 : f(2 * t) / 2; + }; + } + }); + Easing.add({ + name: "linear", + fn: function(t) { + return t; + } + }); + Easing.add({ + name: "quad", + fn: function(t) { + return t * t; + } + }); + Easing.add({ + name: "cubic", + fn: function(t) { + return t * t * t; + } + }); + Easing.add({ + name: "quart", + fn: function(t) { + return t * t * t * t; + } + }); + Easing.add({ + name: "quint", + fn: function(t) { + return t * t * t * t * t; + } + }); + Easing.add({ + name: "sin sine", + fn: function(t) { + return 1 - Math.cos(t * Math.PI / 2); + } + }); + Easing.add({ + name: "exp expo", + fn: function(t) { + return t == 0 ? 0 : Math.pow(2, 10 * (t - 1)); + } + }); + Easing.add({ + name: "circle circ", + fn: function(t) { + return 1 - Math.sqrt(1 - t * t); + } + }); + Easing.add({ + name: "bounce", + fn: function(t) { + return t < 1 / 2.75 ? 7.5625 * t * t : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + 0.75 : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + 0.9375 : 7.5625 * (t -= 2.625 / 2.75) * t + 0.984375; + } + }); + Easing.add({ + name: "poly", + fc: function(e) { + return function(t) { + return Math.pow(t, e); + }; + } + }); + Easing.add({ + name: "elastic", + fc: function(a, p) { + p = p || 0.45; + a = a || 1; + var s = p / (2 * Math.PI) * Math.asin(1 / a); + return function(t) { + return 1 + a * Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / p); + }; + } + }); + Easing.add({ + name: "back", + fc: function(s) { + s = typeof s !== "undefined" ? s : 1.70158; + return function(t) { + return t * t * ((s + 1) * t - s); + }; + } + }); + Stage.prototype.tween = function(duration, delay, append2) { + if (typeof duration !== "number") { + append2 = duration, delay = 0, duration = 0; + } else if (typeof delay !== "number") { + append2 = delay, delay = 0; + } + if (!this._tweens) { + this._tweens = []; + var ticktime = 0; + this.tick(function(elapsed, now, last) { + if (!this._tweens.length) { + return; + } + var ignore = ticktime != last; + ticktime = now; + if (ignore) { + return true; + } + var head = this._tweens[0]; + var next = head.tick(this, elapsed, now, last); + if (next && head === this._tweens[0]) { + this._tweens.shift(); + } + if (Array.isArray(next)) { + for (var i = 0; i < next.length; i++) { + try { + next[i].call(this); + } catch (e) { + console.log(e); + } + } + } else if (typeof next === "object") { + this._tweens.unshift(next); + } + return true; + }, true); + } + this.touch(); + if (!append2) { + this._tweens.length = 0; + } + var tween = new Tween(this, duration, delay); + this._tweens.push(tween); + return tween; + }; + function Tween(owner, duration, delay) { + this._end = {}; + this._duration = duration || 400; + this._delay = delay || 0; + this._owner = owner; + this._time = 0; + } + Tween.prototype.tick = function(node, elapsed, now, last) { + this._time += elapsed; + if (this._time < this._delay) { + return; + } + var time = this._time - this._delay; + if (!this._start) { + this._start = {}; + for (var key in this._end) { + this._start[key] = this._owner.pin(key); + } + } + var p, over; + if (time < this._duration) { + p = time / this._duration; + over = false; + } else { + p = 1; + over = true; + } + if (typeof this._easing == "function") { + p = this._easing(p); + } + var q = 1 - p; + for (var key in this._end) { + this._owner.pin(key, this._start[key] * q + this._end[key] * p); + } + if (over) { + var actions = [this._hide, this._remove, this._done]; + actions = actions.filter(function(element) { + return typeof element === "function"; + }); + return this._next || actions; + } + }; + Tween.prototype.tween = function(duration, delay) { + return this._next = new Tween(this._owner, duration, delay); + }; + Tween.prototype.duration = function(duration) { + this._duration = duration; + return this; + }; + Tween.prototype.delay = function(delay) { + this._delay = delay; + return this; + }; + Tween.prototype.ease = function(easing) { + this._easing = Easing(easing); + return this; + }; + Tween.prototype.done = function(fn) { + this._done = fn; + return this; + }; + Tween.prototype.hide = function() { + this._hide = function() { + this.hide(); + }; + return this; + }; + Tween.prototype.remove = function() { + this._remove = function() { + this.remove(); + }; + return this; + }; + Tween.prototype.pin = function(a, b) { + if (typeof a === "object") { + for (var attr in a) { + pinning(this._owner, this._end, attr, a[attr]); + } + } else if (typeof b !== "undefined") { + pinning(this._owner, this._end, a, b); + } + return this; + }; + function pinning(node, map, key, value) { + if (typeof node.pin(key) === "number") { + map[key] = value; + } else if (typeof node.pin(key + "X") === "number" && typeof node.pin(key + "Y") === "number") { + map[key + "X"] = value; + map[key + "Y"] = value; + } + } + Pin._add_shortcuts(Tween); + Tween.prototype.then = function(fn) { + this.done(fn); + return this; + }; + Tween.prototype.clear = function(forward) { + return this; + }; + Stage._load(function(stage, elem) { + Mouse.subscribe(stage, elem); + }); + Mouse.CLICK = "click"; + Mouse.START = "touchstart mousedown"; + Mouse.MOVE = "touchmove mousemove"; + Mouse.END = "touchend mouseup"; + Mouse.CANCEL = "touchcancel mousecancel"; + Mouse.subscribe = function(stage, elem) { + if (stage.mouse) { + return; + } + stage.mouse = new Mouse(stage, elem); + elem.addEventListener("touchstart", handleStart); + elem.addEventListener("touchend", handleEnd); + elem.addEventListener("touchmove", handleMove); + elem.addEventListener("touchcancel", handleCancel); + elem.addEventListener("mousedown", handleStart); + elem.addEventListener("mouseup", handleEnd); + elem.addEventListener("mousemove", handleMove); + document.addEventListener("mouseup", handleCancel); + window.addEventListener("blur", handleCancel); + var clicklist = [], cancellist = []; + function handleStart(event) { + event.preventDefault(); + stage.mouse.locate(event); + stage.mouse.publish(event.type, event); + stage.mouse.lookup("click", clicklist); + stage.mouse.lookup("mousecancel", cancellist); + } + function handleMove(event) { + event.preventDefault(); + stage.mouse.locate(event); + stage.mouse.publish(event.type, event); + } + function handleEnd(event) { + event.preventDefault(); + stage.mouse.publish(event.type, event); + if (clicklist.length) { + stage.mouse.publish("click", event, clicklist); + } + cancellist.length = 0; + } + function handleCancel(event) { + if (cancellist.length) { + stage.mouse.publish("mousecancel", event, cancellist); + } + clicklist.length = 0; + } + }; + function Mouse(stage, elem) { + if (!(this instanceof Mouse)) { + return; + } + var ratio = stage.viewport().ratio || 1; + stage.on("viewport", function(size) { + ratio = size.ratio || ratio; + }); + this.x = 0; + this.y = 0; + this.toString = function() { + return (this.x | 0) + "x" + (this.y | 0); + }; + this.locate = function(event) { + locateElevent(elem, event, this); + this.x *= ratio; + this.y *= ratio; + }; + this.lookup = function(type, collect) { + this.type = type; + this.root = stage; + this.event = null; + collect.length = 0; + this.collect = collect; + this.root.visit(this.visitor, this); + }; + this.publish = function(type, event, targets) { + this.type = type; + this.root = stage; + this.event = event; + this.collect = false; + this.timeStamp = Date.now(); + if (type !== "mousemove" && type !== "touchmove") { + console.log(this.type + " " + this); + } + if (targets) { + while (targets.length) + if (this.visitor.end(targets.shift(), this)) + break; + targets.length = 0; + } else { + this.root.visit(this.visitor, this); + } + }; + this.visitor = { + reverse: true, + visible: true, + start: function(node, mouse) { + return !node._flag(mouse.type); + }, + end: function(node, mouse) { + rel.raw = mouse.event; + rel.type = mouse.type; + rel.timeStamp = mouse.timeStamp; + rel.abs.x = mouse.x; + rel.abs.y = mouse.y; + var listeners = node.listeners(mouse.type); + if (!listeners) { + return; + } + node.matrix().inverse().map(mouse, rel); + if (!(node === mouse.root || node.attr("spy") || node.hitTest(rel))) { + return; + } + if (mouse.collect) { + mouse.collect.push(node); + } + if (mouse.event) { + var cancel = false; + for (var l = 0; l < listeners.length; l++) { + cancel = listeners[l].call(node, rel) ? true : cancel; + } + return cancel; + } + } + }; + } + var rel = {}, abs = {}; + defineValue(rel, "clone", function(obj) { + obj = obj || {}, obj.x = this.x, obj.y = this.y; + return obj; + }); + defineValue(rel, "toString", function() { + return (this.x | 0) + "x" + (this.y | 0) + " (" + this.abs + ")"; + }); + defineValue(rel, "abs", abs); + defineValue(abs, "clone", function(obj) { + obj = obj || {}, obj.x = this.x, obj.y = this.y; + return obj; + }); + defineValue(abs, "toString", function() { + return (this.x | 0) + "x" + (this.y | 0); + }); + function defineValue(obj, name, value) { + Object.defineProperty(obj, name, { + value + }); + } + function locateElevent(el, ev, loc) { + if (ev.touches && ev.touches.length) { + loc.x = ev.touches[0].clientX; + loc.y = ev.touches[0].clientY; + } else { + loc.x = ev.clientX; + loc.y = ev.clientY; + } + var rect = el.getBoundingClientRect(); + loc.x -= rect.left; + loc.y -= rect.top; + loc.x -= el.clientLeft | 0; + loc.y -= el.clientTop | 0; + return loc; + } + window.addEventListener("load", function() { + console.log("On load."); + Stage.start(); + }, false); + Stage.config({ + "app-loader": AppLoader, + "image-loader": ImageLoader + }); + function AppLoader(app, configs) { + configs = configs || {}; + var canvas = configs.canvas, context = null, full = false; + var width = 0, height = 0, ratio = 1; + if (typeof canvas === "string") { + canvas = document.getElementById(canvas); + } + if (!canvas) { + canvas = document.getElementById("cutjs") || document.getElementById("stage"); + } + if (!canvas) { + full = true; + console.log("Creating Canvas..."); + canvas = document.createElement("canvas"); + canvas.style.position = "absolute"; + canvas.style.top = "0"; + canvas.style.left = "0"; + var body = document.body; + body.insertBefore(canvas, body.firstChild); + } + context = canvas.getContext("2d"); + var devicePixelRatio = window.devicePixelRatio || 1; + var backingStoreRatio = context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || context.backingStorePixelRatio || 1; + ratio = devicePixelRatio / backingStoreRatio; + var requestAnimationFrame = window.requestAnimationFrame || window.msRequestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.oRequestAnimationFrame || function(callback) { + return window.setTimeout(callback, 1e3 / 60); + }; + console.log("Creating stage..."); + var root = Stage.root(requestAnimationFrame, render); + function render() { + if (width > 0 && height > 0) { + context.setTransform(1, 0, 0, 1, 0, 0); + context.clearRect(0, 0, width, height); + root.render(context); + } + } + root.background = function(color) { + canvas.style.backgroundColor = color; + return this; + }; + app(root, canvas); + var lastWidth = -1; + var lastHeight = -1; + (function resizeLoop() { + var width2, height2; + if (full) { + width2 = window.innerWidth > 0 ? window.innerWidth : screen.width; + height2 = window.innerHeight > 0 ? window.innerHeight : screen.height; + } else { + width2 = canvas.clientWidth; + height2 = canvas.clientHeight; + } + if (lastWidth !== width2 || lastHeight !== height2) { + lastWidth = width2; + lastHeight = height2; + resize(); + } + requestAnimationFrame(resizeLoop); + })(); + function resize() { + if (full) { + width = window.innerWidth > 0 ? window.innerWidth : screen.width; + height = window.innerHeight > 0 ? window.innerHeight : screen.height; + canvas.style.width = width + "px"; + canvas.style.height = height + "px"; + } else { + width = canvas.clientWidth; + height = canvas.clientHeight; + } + width *= ratio; + height *= ratio; + if (canvas.width === width && canvas.height === height) { + return; + } + canvas.width = width; + canvas.height = height; + console.log("Resize: " + width + " x " + height + " / " + ratio); + root.viewport(width, height, ratio); + render(); + } + } + function ImageLoader(src, success, error) { + console.log("Loading image: " + src); + var image = new Image(); + image.onload = function() { + success(image); + }; + image.onerror = error; + image.src = src; + } + listenable(Stage.prototype, function(obj, name, on) { + obj._flag(name, on); + }); + Stage.Matrix = Matrix; + Stage.Texture = Texture; + Stage.Mouse = Mouse; + Stage.Math = math; + Stage.Image = Image$1; + return Stage; +}); diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..82cdac2 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,230 @@ +export default Stage; + +type AppDefinition = (root: Stage, canvas: Element) => void; + +interface TextureDefinition { +} + +type ImageType = HTMLImageElement | SVGImageElement | HTMLVideoElement | HTMLCanvasElement | ImageBitmap | OffscreenCanvas | VideoFrame; + +type Ticker = (t: number, dt: number) => boolean | void + +type Plotter = (context: CanvasRenderingContext2D) => void; + +interface Visitor { + start(child: Stage, data: D): boolean | void; + end(child: Stage, data: D): boolean | void; + reverse: boolean; + visible: boolean; +} + +export class Stage { + constructor(); + constructor(app: AppDefinition); + constructor(texture: TextureDefinition); + + static app(app: (root: Stage) => void, opts: object): void; + static atlas(def: TextureDefinition): Texture; + + static config(key: string, value: any): void; + static config(map: Record): void; + static config(key: string): any; + + static preload(load: (done: () => void) => void): void; + static start(config: Record): void; + static pause(): void; + static resume(): void; + + static root( + request: (loop: (time: number) => void) => void, + render: (root: Stage) => void, + ): Stage; + + render(context: CanvasRenderingContext2D): void; + touch(): Stage; + toString(): string; + + appendTo(parent: Stage): Stage; + prependTo(parent: Stage): Stage; + + append(child: Stage | Stage[]): Stage; + prepend(child: Stage | Stage[]): Stage; + + insertNext(sibling: Stage, child: Stage | Stage[]): Stage; + insertPrev(sibling: Stage, child: Stage | Stage[]): Stage; + + insertAfter(sibling: Stage): Stage; + insertBefore(sibling: Stage): Stage; + + remove(child: Stage | Stage[]): Stage; + remove(): Stage; + empty(): Stage; + + parent(): Stage; + + first(visible?: boolean): Stage; + last(visible?: boolean): Stage; + + next(visible?: boolean): Stage; + prev(visible?: boolean): Stage; + + visible(): boolean; + visible(visible: boolean): Stage; + hide(): Stage; + show(): Stage; + visit(visitor: Visitor, data: D): boolean | void; + + tick(ticker: Ticker, before?: boolean): void; + untick(ticker: Ticker): void; + + pin(a: string, b: any): Stage; + pin(a: string): any; + + pin(a: Record): Stage; + pin(): Record; + + size(w: number, h: number): Stage; + width(w: number): Stage; + height(h: number): Stage; + scale(a: number, b: number): Stage; + skew(a: number, b: number): Stage; + rotate(a: number): Stage; + offset(a: number, b: number): Stage; + + scaleTo(a: number, b: number, mode: string): Stage; + + padding(pad: number): Stage; + spacing(space: number): Stage; + + alpha(a: number, ta: number): Stage; + + matrix(relative?: boolean): Stage.Matrix; + + tween(duration: number, delay: number, append: boolean): Stage.Tween; + tween(duration: number, append: boolean): Stage.Tween; + tween(append: boolean): Stage.Tween; + + on(type: string, listener: (...args: unknown[]) => void): Stage; + off(type: string, listener: (...args: unknown[]) => void): Stage; + publish(name: string, ...args: unknown[]): number; + listeners(type: string): unknown[]; + + id(): string; + id(id: string): Stage; + + label(): string; + label(label: string): Stage; + + attr(name: string): unknown; + attr(name: string, value: unknown): Stage; + + static create(): Stage; + + static box(): Stage; + box(): Stage; + + static column(align: number): Stage; + column(align: number): Stage; + + static layer(): Stage; + layer(): Stage; + + static row(align: number): Stage; + row(align: number): Stage; + + // static sequence(type: 'row' | 'column', align: number): Stage; + // sequence(type: 'row' | 'column', align: number): Stage; + + static anim(frames: string | Stage.Texture[], fps: number): Stage; + static string(frames: string | Stage.Texture[]): Stage; + + static canvas(): Stage; + static canvas(plotter: Plotter): Stage; + static canvas(type: string, attributes: Record, plotter: Plotter): Stage; + + static texture(query: string): Stage.Texture | Stage.Texture[]; + + static image(image?: ImageType): Stage.Image; + + timeout(fn: () => unknown, time: number): void; + setTimeout(fn: () => unknown, time: number): any; + clearTimeout(timer: any): void; + + hitTest(hit: { x: number, y: number }): boolean; + + static Anim: { + FPS: number; + }; +} + +export namespace Stage { + class Tween { + } + + class Matrix { + a: number; + b: number; + c: number; + d: number; + e: number; + f: number; + constructor(a: number, b: number, c: number, d: number, e: number, f: number); + clone(): Matrix; + concat(m: Matrix): Matrix; + identity(): Matrix; + inverse(): Matrix; + map(p: number, q: number): Matrix; + mapX(x: number, y: number): Matrix; + mapY(x: number, y: number): Matrix; + reset(a: number, b: number, c: number, d: number, e: number, f: number): Matrix; + reverse(): Matrix; + rotate(angle: number): Matrix; + scale(x: number, y: number): Matrix; + skew(x: number, y: number): Matrix; + toString(): string; + translate(x: number, y: number): Matrix; + } + + class Texture { + constructor(); + constructor(image: ImageType, ratio: number); + + src(image: ImageType, ratio: number): Texture; + + src(x: number, y: number): Texture; + src(x: number, y: number, w: number, h: number): Texture; + + dest(x: number, y: number): Texture; + dest(x: number, y: number, w: number, h: number): Texture; + + pipe(): Texture; + + draw(context: CanvasRenderingContext2D): void; + draw(context: CanvasRenderingContext2D, dw: number, dh: number): void; + draw(context: CanvasRenderingContext2D, dx: number, dy: number, dw: number, dh: number): void; + draw(context: CanvasRenderingContext2D, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void; + } + + interface Mouse { + CANCEL: string; + CLICK: string; + END: string; + MOVE: string; + START: string; + subscribe(stage: Stage, elem: Element): void; + } + + class Image extends Stage { + constructor(texture: Texture); + image(texture: Texture): Image; + stretch(inner: any): Image; + tile(inner: any): Image; + } + + interface Math { + random(min: number, max: number): number; + rotate(num: number, min: number, max: number): number; + length(x: number, y: number): number; + limit(num: number, min: number, max: number): number; + } +} diff --git a/package.json b/package.json index cbded86..cedf3de 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "require": "./dist/stage.umd.cjs" } }, + "types": "./index.d.ts", "files": [ "src/", "dist/", diff --git a/src/image.js b/src/image.js index 4aee2cb..ffbc85b 100644 --- a/src/image.js +++ b/src/image.js @@ -22,13 +22,6 @@ function Image() { this._image = null; }; -/** - * @deprecated Use image - */ -Image.prototype.setImage = function(a, b, c) { - return this.image(a, b, c); -}; - Image.prototype.image = function(image) { this._image = Stage.texture(image).one(); this.pin('width', this._image ? this._image.width : 0); @@ -58,8 +51,7 @@ Image.prototype._repeat = function(stretch, inner) { this._mo_stretch = this._pin._ts_transform; var width = this.pin('width'); var height = this.pin('height'); - this._textures.length = repeat(this._image, width, height, stretch, inner, - insert); + this._textures.length = repeat(this._image, width, height, stretch, inner, insert); }); function insert(i, sx, sy, sw, sh, dx, dy, dw, dh) { diff --git a/src/index.js b/src/index.js index ca360bb..a4d9974 100644 --- a/src/index.js +++ b/src/index.js @@ -26,9 +26,7 @@ Stage.Matrix = Matrix; Stage.Texture = Texture; Stage.Mouse = Mouse; Stage.Math = Math; - -Stage.internal = {}; -Stage.internal.Image = Image; +Stage.Image = Image; import './loader'; diff --git a/vite.config.js b/vite.config.js index 1abc4d3..006296f 100644 --- a/vite.config.js +++ b/vite.config.js @@ -11,10 +11,12 @@ export default { }), ], build: { + minify: false, lib: { entry: path.resolve(__dirname, 'src/index.js'), name: 'Stage', fileName: 'stage', + formats: ['es', 'umd', 'cjs'], } } }