sprite.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*!
  2. * @pixi/sprite - v6.5.10
  3. * Compiled Thu, 06 Jul 2023 15:25:11 UTC
  4. *
  5. * @pixi/sprite is licensed under the MIT License.
  6. * http://www.opensource.org/licenses/mit-license
  7. */
  8. this.PIXI = this.PIXI || {};
  9. var _pixi_sprite = (function (exports, constants, core, display, math, settings, utils) {
  10. 'use strict';
  11. /*! *****************************************************************************
  12. Copyright (c) Microsoft Corporation.
  13. Permission to use, copy, modify, and/or distribute this software for any
  14. purpose with or without fee is hereby granted.
  15. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  16. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  17. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  18. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  19. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  20. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  21. PERFORMANCE OF THIS SOFTWARE.
  22. ***************************************************************************** */
  23. /* global Reflect, Promise */
  24. var extendStatics = function(d, b) {
  25. extendStatics = Object.setPrototypeOf ||
  26. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  27. function (d, b) { for (var p in b) { if (b.hasOwnProperty(p)) { d[p] = b[p]; } } };
  28. return extendStatics(d, b);
  29. };
  30. function __extends(d, b) {
  31. extendStatics(d, b);
  32. function __() { this.constructor = d; }
  33. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  34. }
  35. var __assign = function() {
  36. __assign = Object.assign || function __assign(t) {
  37. var arguments$1 = arguments;
  38. for (var s, i = 1, n = arguments.length; i < n; i++) {
  39. s = arguments$1[i];
  40. for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p)) { t[p] = s[p]; } }
  41. }
  42. return t;
  43. };
  44. return __assign.apply(this, arguments);
  45. };
  46. function __rest(s, e) {
  47. var t = {};
  48. for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  49. { t[p] = s[p]; } }
  50. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  51. { for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  52. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  53. { t[p[i]] = s[p[i]]; }
  54. } }
  55. return t;
  56. }
  57. function __decorate(decorators, target, key, desc) {
  58. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  59. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") { r = Reflect.decorate(decorators, target, key, desc); }
  60. else { for (var i = decorators.length - 1; i >= 0; i--) { if (d = decorators[i]) { r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; } } }
  61. return c > 3 && r && Object.defineProperty(target, key, r), r;
  62. }
  63. function __param(paramIndex, decorator) {
  64. return function (target, key) { decorator(target, key, paramIndex); }
  65. }
  66. function __metadata(metadataKey, metadataValue) {
  67. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") { return Reflect.metadata(metadataKey, metadataValue); }
  68. }
  69. function __awaiter(thisArg, _arguments, P, generator) {
  70. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  71. return new (P || (P = Promise))(function (resolve, reject) {
  72. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  73. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  74. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  75. step((generator = generator.apply(thisArg, _arguments || [])).next());
  76. });
  77. }
  78. function __generator(thisArg, body) {
  79. var _ = { label: 0, sent: function() { if (t[0] & 1) { throw t[1]; } return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  80. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  81. function verb(n) { return function (v) { return step([n, v]); }; }
  82. function step(op) {
  83. if (f) { throw new TypeError("Generator is already executing."); }
  84. while (_) { try {
  85. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) { return t; }
  86. if (y = 0, t) { op = [op[0] & 2, t.value]; }
  87. switch (op[0]) {
  88. case 0: case 1: t = op; break;
  89. case 4: _.label++; return { value: op[1], done: false };
  90. case 5: _.label++; y = op[1]; op = [0]; continue;
  91. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  92. default:
  93. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  94. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  95. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  96. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  97. if (t[2]) { _.ops.pop(); }
  98. _.trys.pop(); continue;
  99. }
  100. op = body.call(thisArg, _);
  101. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } }
  102. if (op[0] & 5) { throw op[1]; } return { value: op[0] ? op[1] : void 0, done: true };
  103. }
  104. }
  105. function __createBinding(o, m, k, k2) {
  106. if (k2 === undefined) { k2 = k; }
  107. o[k2] = m[k];
  108. }
  109. function __exportStar(m, exports) {
  110. for (var p in m) { if (p !== "default" && !exports.hasOwnProperty(p)) { exports[p] = m[p]; } }
  111. }
  112. function __values(o) {
  113. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  114. if (m) { return m.call(o); }
  115. if (o && typeof o.length === "number") { return {
  116. next: function () {
  117. if (o && i >= o.length) { o = void 0; }
  118. return { value: o && o[i++], done: !o };
  119. }
  120. }; }
  121. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  122. }
  123. function __read(o, n) {
  124. var m = typeof Symbol === "function" && o[Symbol.iterator];
  125. if (!m) { return o; }
  126. var i = m.call(o), r, ar = [], e;
  127. try {
  128. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) { ar.push(r.value); }
  129. }
  130. catch (error) { e = { error: error }; }
  131. finally {
  132. try {
  133. if (r && !r.done && (m = i["return"])) { m.call(i); }
  134. }
  135. finally { if (e) { throw e.error; } }
  136. }
  137. return ar;
  138. }
  139. function __spread() {
  140. var arguments$1 = arguments;
  141. for (var ar = [], i = 0; i < arguments.length; i++)
  142. { ar = ar.concat(__read(arguments$1[i])); }
  143. return ar;
  144. }
  145. function __spreadArrays() {
  146. var arguments$1 = arguments;
  147. for (var s = 0, i = 0, il = arguments.length; i < il; i++) { s += arguments$1[i].length; }
  148. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  149. { for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  150. { r[k] = a[j]; } }
  151. return r;
  152. };
  153. function __await(v) {
  154. return this instanceof __await ? (this.v = v, this) : new __await(v);
  155. }
  156. function __asyncGenerator(thisArg, _arguments, generator) {
  157. if (!Symbol.asyncIterator) { throw new TypeError("Symbol.asyncIterator is not defined."); }
  158. var g = generator.apply(thisArg, _arguments || []), i, q = [];
  159. return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
  160. function verb(n) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } }
  161. function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
  162. function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
  163. function fulfill(value) { resume("next", value); }
  164. function reject(value) { resume("throw", value); }
  165. function settle(f, v) { if (f(v), q.shift(), q.length) { resume(q[0][0], q[0][1]); } }
  166. }
  167. function __asyncDelegator(o) {
  168. var i, p;
  169. return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
  170. function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
  171. }
  172. function __asyncValues(o) {
  173. if (!Symbol.asyncIterator) { throw new TypeError("Symbol.asyncIterator is not defined."); }
  174. var m = o[Symbol.asyncIterator], i;
  175. return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
  176. function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
  177. function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
  178. }
  179. function __makeTemplateObject(cooked, raw) {
  180. if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
  181. return cooked;
  182. };
  183. function __importStar(mod) {
  184. if (mod && mod.__esModule) { return mod; }
  185. var result = {};
  186. if (mod != null) { for (var k in mod) { if (Object.hasOwnProperty.call(mod, k)) { result[k] = mod[k]; } } }
  187. result.default = mod;
  188. return result;
  189. }
  190. function __importDefault(mod) {
  191. return (mod && mod.__esModule) ? mod : { default: mod };
  192. }
  193. function __classPrivateFieldGet(receiver, privateMap) {
  194. if (!privateMap.has(receiver)) {
  195. throw new TypeError("attempted to get private field on non-instance");
  196. }
  197. return privateMap.get(receiver);
  198. }
  199. function __classPrivateFieldSet(receiver, privateMap, value) {
  200. if (!privateMap.has(receiver)) {
  201. throw new TypeError("attempted to set private field on non-instance");
  202. }
  203. privateMap.set(receiver, value);
  204. return value;
  205. }
  206. var tempPoint = new math.Point();
  207. var indices = new Uint16Array([0, 1, 2, 0, 2, 3]);
  208. /**
  209. * The Sprite object is the base for all textured objects that are rendered to the screen
  210. *
  211. * A sprite can be created directly from an image like this:
  212. *
  213. * ```js
  214. * let sprite = PIXI.Sprite.from('assets/image.png');
  215. * ```
  216. *
  217. * The more efficient way to create sprites is using a {@link PIXI.Spritesheet},
  218. * as swapping base textures when rendering to the screen is inefficient.
  219. *
  220. * ```js
  221. * PIXI.Loader.shared.add("assets/spritesheet.json").load(setup);
  222. *
  223. * function setup() {
  224. * let sheet = PIXI.Loader.shared.resources["assets/spritesheet.json"].spritesheet;
  225. * let sprite = new PIXI.Sprite(sheet.textures["image.png"]);
  226. * ...
  227. * }
  228. * ```
  229. * @memberof PIXI
  230. */
  231. var Sprite = /** @class */ (function (_super) {
  232. __extends(Sprite, _super);
  233. /** @param texture - The texture for this sprite. */
  234. function Sprite(texture) {
  235. var _this = _super.call(this) || this;
  236. _this._anchor = new math.ObservablePoint(_this._onAnchorUpdate, _this, (texture ? texture.defaultAnchor.x : 0), (texture ? texture.defaultAnchor.y : 0));
  237. _this._texture = null;
  238. _this._width = 0;
  239. _this._height = 0;
  240. _this._tint = null;
  241. _this._tintRGB = null;
  242. _this.tint = 0xFFFFFF;
  243. _this.blendMode = constants.BLEND_MODES.NORMAL;
  244. _this._cachedTint = 0xFFFFFF;
  245. _this.uvs = null;
  246. // call texture setter
  247. _this.texture = texture || core.Texture.EMPTY;
  248. _this.vertexData = new Float32Array(8);
  249. _this.vertexTrimmedData = null;
  250. _this._transformID = -1;
  251. _this._textureID = -1;
  252. _this._transformTrimmedID = -1;
  253. _this._textureTrimmedID = -1;
  254. // Batchable stuff..
  255. // TODO could make this a mixin?
  256. _this.indices = indices;
  257. _this.pluginName = 'batch';
  258. /**
  259. * Used to fast check if a sprite is.. a sprite!
  260. * @member {boolean}
  261. */
  262. _this.isSprite = true;
  263. _this._roundPixels = settings.settings.ROUND_PIXELS;
  264. return _this;
  265. }
  266. /** When the texture is updated, this event will fire to update the scale and frame. */
  267. Sprite.prototype._onTextureUpdate = function () {
  268. this._textureID = -1;
  269. this._textureTrimmedID = -1;
  270. this._cachedTint = 0xFFFFFF;
  271. // so if _width is 0 then width was not set..
  272. if (this._width) {
  273. this.scale.x = utils.sign(this.scale.x) * this._width / this._texture.orig.width;
  274. }
  275. if (this._height) {
  276. this.scale.y = utils.sign(this.scale.y) * this._height / this._texture.orig.height;
  277. }
  278. };
  279. /** Called when the anchor position updates. */
  280. Sprite.prototype._onAnchorUpdate = function () {
  281. this._transformID = -1;
  282. this._transformTrimmedID = -1;
  283. };
  284. /** Calculates worldTransform * vertices, store it in vertexData. */
  285. Sprite.prototype.calculateVertices = function () {
  286. var texture = this._texture;
  287. if (this._transformID === this.transform._worldID && this._textureID === texture._updateID) {
  288. return;
  289. }
  290. // update texture UV here, because base texture can be changed without calling `_onTextureUpdate`
  291. if (this._textureID !== texture._updateID) {
  292. this.uvs = this._texture._uvs.uvsFloat32;
  293. }
  294. this._transformID = this.transform._worldID;
  295. this._textureID = texture._updateID;
  296. // set the vertex data
  297. var wt = this.transform.worldTransform;
  298. var a = wt.a;
  299. var b = wt.b;
  300. var c = wt.c;
  301. var d = wt.d;
  302. var tx = wt.tx;
  303. var ty = wt.ty;
  304. var vertexData = this.vertexData;
  305. var trim = texture.trim;
  306. var orig = texture.orig;
  307. var anchor = this._anchor;
  308. var w0 = 0;
  309. var w1 = 0;
  310. var h0 = 0;
  311. var h1 = 0;
  312. if (trim) {
  313. // if the sprite is trimmed and is not a tilingsprite then we need to add the extra
  314. // space before transforming the sprite coords.
  315. w1 = trim.x - (anchor._x * orig.width);
  316. w0 = w1 + trim.width;
  317. h1 = trim.y - (anchor._y * orig.height);
  318. h0 = h1 + trim.height;
  319. }
  320. else {
  321. w1 = -anchor._x * orig.width;
  322. w0 = w1 + orig.width;
  323. h1 = -anchor._y * orig.height;
  324. h0 = h1 + orig.height;
  325. }
  326. // xy
  327. vertexData[0] = (a * w1) + (c * h1) + tx;
  328. vertexData[1] = (d * h1) + (b * w1) + ty;
  329. // xy
  330. vertexData[2] = (a * w0) + (c * h1) + tx;
  331. vertexData[3] = (d * h1) + (b * w0) + ty;
  332. // xy
  333. vertexData[4] = (a * w0) + (c * h0) + tx;
  334. vertexData[5] = (d * h0) + (b * w0) + ty;
  335. // xy
  336. vertexData[6] = (a * w1) + (c * h0) + tx;
  337. vertexData[7] = (d * h0) + (b * w1) + ty;
  338. if (this._roundPixels) {
  339. var resolution = settings.settings.RESOLUTION;
  340. for (var i = 0; i < vertexData.length; ++i) {
  341. vertexData[i] = Math.round((vertexData[i] * resolution | 0) / resolution);
  342. }
  343. }
  344. };
  345. /**
  346. * Calculates worldTransform * vertices for a non texture with a trim. store it in vertexTrimmedData.
  347. *
  348. * This is used to ensure that the true width and height of a trimmed texture is respected.
  349. */
  350. Sprite.prototype.calculateTrimmedVertices = function () {
  351. if (!this.vertexTrimmedData) {
  352. this.vertexTrimmedData = new Float32Array(8);
  353. }
  354. else if (this._transformTrimmedID === this.transform._worldID && this._textureTrimmedID === this._texture._updateID) {
  355. return;
  356. }
  357. this._transformTrimmedID = this.transform._worldID;
  358. this._textureTrimmedID = this._texture._updateID;
  359. // lets do some special trim code!
  360. var texture = this._texture;
  361. var vertexData = this.vertexTrimmedData;
  362. var orig = texture.orig;
  363. var anchor = this._anchor;
  364. // lets calculate the new untrimmed bounds..
  365. var wt = this.transform.worldTransform;
  366. var a = wt.a;
  367. var b = wt.b;
  368. var c = wt.c;
  369. var d = wt.d;
  370. var tx = wt.tx;
  371. var ty = wt.ty;
  372. var w1 = -anchor._x * orig.width;
  373. var w0 = w1 + orig.width;
  374. var h1 = -anchor._y * orig.height;
  375. var h0 = h1 + orig.height;
  376. // xy
  377. vertexData[0] = (a * w1) + (c * h1) + tx;
  378. vertexData[1] = (d * h1) + (b * w1) + ty;
  379. // xy
  380. vertexData[2] = (a * w0) + (c * h1) + tx;
  381. vertexData[3] = (d * h1) + (b * w0) + ty;
  382. // xy
  383. vertexData[4] = (a * w0) + (c * h0) + tx;
  384. vertexData[5] = (d * h0) + (b * w0) + ty;
  385. // xy
  386. vertexData[6] = (a * w1) + (c * h0) + tx;
  387. vertexData[7] = (d * h0) + (b * w1) + ty;
  388. };
  389. /**
  390. *
  391. * Renders the object using the WebGL renderer
  392. * @param renderer - The webgl renderer to use.
  393. */
  394. Sprite.prototype._render = function (renderer) {
  395. this.calculateVertices();
  396. renderer.batch.setObjectRenderer(renderer.plugins[this.pluginName]);
  397. renderer.plugins[this.pluginName].render(this);
  398. };
  399. /** Updates the bounds of the sprite. */
  400. Sprite.prototype._calculateBounds = function () {
  401. var trim = this._texture.trim;
  402. var orig = this._texture.orig;
  403. // First lets check to see if the current texture has a trim..
  404. if (!trim || (trim.width === orig.width && trim.height === orig.height)) {
  405. // no trim! lets use the usual calculations..
  406. this.calculateVertices();
  407. this._bounds.addQuad(this.vertexData);
  408. }
  409. else {
  410. // lets calculate a special trimmed bounds...
  411. this.calculateTrimmedVertices();
  412. this._bounds.addQuad(this.vertexTrimmedData);
  413. }
  414. };
  415. /**
  416. * Gets the local bounds of the sprite object.
  417. * @param rect - Optional output rectangle.
  418. * @returns The bounds.
  419. */
  420. Sprite.prototype.getLocalBounds = function (rect) {
  421. // we can do a fast local bounds if the sprite has no children!
  422. if (this.children.length === 0) {
  423. if (!this._localBounds) {
  424. this._localBounds = new display.Bounds();
  425. }
  426. this._localBounds.minX = this._texture.orig.width * -this._anchor._x;
  427. this._localBounds.minY = this._texture.orig.height * -this._anchor._y;
  428. this._localBounds.maxX = this._texture.orig.width * (1 - this._anchor._x);
  429. this._localBounds.maxY = this._texture.orig.height * (1 - this._anchor._y);
  430. if (!rect) {
  431. if (!this._localBoundsRect) {
  432. this._localBoundsRect = new math.Rectangle();
  433. }
  434. rect = this._localBoundsRect;
  435. }
  436. return this._localBounds.getRectangle(rect);
  437. }
  438. return _super.prototype.getLocalBounds.call(this, rect);
  439. };
  440. /**
  441. * Tests if a point is inside this sprite
  442. * @param point - the point to test
  443. * @returns The result of the test
  444. */
  445. Sprite.prototype.containsPoint = function (point) {
  446. this.worldTransform.applyInverse(point, tempPoint);
  447. var width = this._texture.orig.width;
  448. var height = this._texture.orig.height;
  449. var x1 = -width * this.anchor.x;
  450. var y1 = 0;
  451. if (tempPoint.x >= x1 && tempPoint.x < x1 + width) {
  452. y1 = -height * this.anchor.y;
  453. if (tempPoint.y >= y1 && tempPoint.y < y1 + height) {
  454. return true;
  455. }
  456. }
  457. return false;
  458. };
  459. /**
  460. * Destroys this sprite and optionally its texture and children.
  461. * @param options - Options parameter. A boolean will act as if all options
  462. * have been set to that value
  463. * @param [options.children=false] - if set to true, all the children will have their destroy
  464. * method called as well. 'options' will be passed on to those calls.
  465. * @param [options.texture=false] - Should it destroy the current texture of the sprite as well
  466. * @param [options.baseTexture=false] - Should it destroy the base texture of the sprite as well
  467. */
  468. Sprite.prototype.destroy = function (options) {
  469. _super.prototype.destroy.call(this, options);
  470. this._texture.off('update', this._onTextureUpdate, this);
  471. this._anchor = null;
  472. var destroyTexture = typeof options === 'boolean' ? options : options && options.texture;
  473. if (destroyTexture) {
  474. var destroyBaseTexture = typeof options === 'boolean' ? options : options && options.baseTexture;
  475. this._texture.destroy(!!destroyBaseTexture);
  476. }
  477. this._texture = null;
  478. };
  479. // some helper functions..
  480. /**
  481. * Helper function that creates a new sprite based on the source you provide.
  482. * The source can be - frame id, image url, video url, canvas element, video element, base texture
  483. * @param {string|PIXI.Texture|HTMLCanvasElement|HTMLVideoElement} source - Source to create texture from
  484. * @param {object} [options] - See {@link PIXI.BaseTexture}'s constructor for options.
  485. * @returns The newly created sprite
  486. */
  487. Sprite.from = function (source, options) {
  488. var texture = (source instanceof core.Texture)
  489. ? source
  490. : core.Texture.from(source, options);
  491. return new Sprite(texture);
  492. };
  493. Object.defineProperty(Sprite.prototype, "roundPixels", {
  494. get: function () {
  495. return this._roundPixels;
  496. },
  497. /**
  498. * If true PixiJS will Math.floor() x/y values when rendering, stopping pixel interpolation.
  499. *
  500. * Advantages can include sharper image quality (like text) and faster rendering on canvas.
  501. * The main disadvantage is movement of objects may appear less smooth.
  502. *
  503. * To set the global default, change {@link PIXI.settings.ROUND_PIXELS}.
  504. * @default false
  505. */
  506. set: function (value) {
  507. if (this._roundPixels !== value) {
  508. this._transformID = -1;
  509. }
  510. this._roundPixels = value;
  511. },
  512. enumerable: false,
  513. configurable: true
  514. });
  515. Object.defineProperty(Sprite.prototype, "width", {
  516. /** The width of the sprite, setting this will actually modify the scale to achieve the value set. */
  517. get: function () {
  518. return Math.abs(this.scale.x) * this._texture.orig.width;
  519. },
  520. set: function (value) {
  521. var s = utils.sign(this.scale.x) || 1;
  522. this.scale.x = s * value / this._texture.orig.width;
  523. this._width = value;
  524. },
  525. enumerable: false,
  526. configurable: true
  527. });
  528. Object.defineProperty(Sprite.prototype, "height", {
  529. /** The height of the sprite, setting this will actually modify the scale to achieve the value set. */
  530. get: function () {
  531. return Math.abs(this.scale.y) * this._texture.orig.height;
  532. },
  533. set: function (value) {
  534. var s = utils.sign(this.scale.y) || 1;
  535. this.scale.y = s * value / this._texture.orig.height;
  536. this._height = value;
  537. },
  538. enumerable: false,
  539. configurable: true
  540. });
  541. Object.defineProperty(Sprite.prototype, "anchor", {
  542. /**
  543. * The anchor sets the origin point of the sprite. The default value is taken from the {@link PIXI.Texture|Texture}
  544. * and passed to the constructor.
  545. *
  546. * The default is `(0,0)`, this means the sprite's origin is the top left.
  547. *
  548. * Setting the anchor to `(0.5,0.5)` means the sprite's origin is centered.
  549. *
  550. * Setting the anchor to `(1,1)` would mean the sprite's origin point will be the bottom right corner.
  551. *
  552. * If you pass only single parameter, it will set both x and y to the same value as shown in the example below.
  553. * @example
  554. * const sprite = new PIXI.Sprite(texture);
  555. * sprite.anchor.set(0.5); // This will set the origin to center. (0.5) is same as (0.5, 0.5).
  556. */
  557. get: function () {
  558. return this._anchor;
  559. },
  560. set: function (value) {
  561. this._anchor.copyFrom(value);
  562. },
  563. enumerable: false,
  564. configurable: true
  565. });
  566. Object.defineProperty(Sprite.prototype, "tint", {
  567. /**
  568. * The tint applied to the sprite. This is a hex value.
  569. *
  570. * A value of 0xFFFFFF will remove any tint effect.
  571. * @default 0xFFFFFF
  572. */
  573. get: function () {
  574. return this._tint;
  575. },
  576. set: function (value) {
  577. this._tint = value;
  578. this._tintRGB = (value >> 16) + (value & 0xff00) + ((value & 0xff) << 16);
  579. },
  580. enumerable: false,
  581. configurable: true
  582. });
  583. Object.defineProperty(Sprite.prototype, "texture", {
  584. /** The texture that the sprite is using. */
  585. get: function () {
  586. return this._texture;
  587. },
  588. set: function (value) {
  589. if (this._texture === value) {
  590. return;
  591. }
  592. if (this._texture) {
  593. this._texture.off('update', this._onTextureUpdate, this);
  594. }
  595. this._texture = value || core.Texture.EMPTY;
  596. this._cachedTint = 0xFFFFFF;
  597. this._textureID = -1;
  598. this._textureTrimmedID = -1;
  599. if (value) {
  600. // wait for the texture to load
  601. if (value.baseTexture.valid) {
  602. this._onTextureUpdate();
  603. }
  604. else {
  605. value.once('update', this._onTextureUpdate, this);
  606. }
  607. }
  608. },
  609. enumerable: false,
  610. configurable: true
  611. });
  612. return Sprite;
  613. }(display.Container));
  614. exports.Sprite = Sprite;
  615. Object.defineProperty(exports, '__esModule', { value: true });
  616. return exports;
  617. })({}, PIXI, PIXI, PIXI, PIXI, PIXI, PIXI.utils);
  618. Object.assign(this.PIXI, _pixi_sprite);
  619. //# sourceMappingURL=sprite.js.map