Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@pixi/spritesheet

Package Overview
Dependencies
Maintainers
3
Versions
122
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pixi/spritesheet - npm Package Compare versions

Comparing version 7.0.0-alpha to 7.0.0-alpha.2

155

dist/cjs/spritesheet.js
/*!
* @pixi/spritesheet - v7.0.0-alpha
* Compiled Fri, 09 Sep 2022 16:09:18 UTC
* @pixi/spritesheet - v7.0.0-alpha.2
* Compiled Sun, 18 Sep 2022 20:45:04 UTC
*

@@ -12,4 +12,4 @@ * @pixi/spritesheet is licensed under the MIT License.

var core = require('@pixi/core');
var assets = require('@pixi/assets');
var core = require('@pixi/core');

@@ -129,52 +129,3 @@ const _Spritesheet = class {

const loadSpritesheet = {
extension: {
type: core.ExtensionType.LoadParser,
priority: assets.LoaderParserPriority.Normal
},
async testParse(asset, options) {
return core.utils.path.extname(options.src).includes(".json") && !!asset.frames;
},
async parse(asset, options, loader) {
let basePath = core.utils.path.dirname(options.src);
if (basePath && basePath.lastIndexOf("/") !== basePath.length - 1) {
basePath += "/";
}
const imagePath = basePath + asset.meta.image;
const assets = await loader.load([imagePath]);
const texture = assets[imagePath];
const spritesheet = new Spritesheet(texture.baseTexture, asset, options.src);
await spritesheet.parse();
const multiPacks = asset?.meta?.related_multi_packs;
if (Array.isArray(multiPacks)) {
const promises = [];
for (const item of multiPacks) {
if (typeof item !== "string") {
continue;
}
const itemUrl = basePath + item;
if (options.data?.ignoreMultiPack) {
continue;
}
promises.push(loader.load({
src: itemUrl,
data: {
ignoreMultiPack: true
}
}));
}
const res = await Promise.all(promises);
spritesheet.linkedSheets = res;
res.forEach((item) => {
item.linkedSheets = [spritesheet].concat(spritesheet.linkedSheets.filter((sp) => sp !== item));
});
}
return spritesheet;
},
unload(spritesheet) {
spritesheet.destroy(true);
}
};
core.extensions.add(loadSpritesheet);
const validImages = ["jpg", "png", "jpeg", "avif", "webp"];
function getCacheableAssets(keys, asset, ignoreMultiPack) {

@@ -197,34 +148,78 @@ const out = {};

}
const cacheSpritesheet = {
extension: core.ExtensionType.CacheParser,
test: (asset) => asset instanceof Spritesheet,
getCacheableAssets: (keys, asset) => getCacheableAssets(keys, asset, false)
};
core.extensions.add(cacheSpritesheet);
const validImages = ["jpg", "png", "jpeg", "avif", "webp"];
const resolveSpriteSheetUrl = {
extension: core.ExtensionType.ResolveParser,
test: (value) => {
const tempURL = value.split("?")[0];
const split = tempURL.split(".");
const extension = split.pop();
const format = split.pop();
return extension === "json" && validImages.includes(format);
const spritesheetAsset = {
extension: core.ExtensionType.Asset,
cache: {
test: (asset) => asset instanceof Spritesheet,
getCacheableAssets: (keys, asset) => getCacheableAssets(keys, asset, false)
},
parse: (value) => {
const split = value.split(".");
return {
resolution: parseFloat(core.settings.RETINA_PREFIX.exec(value)?.[1] ?? "1"),
format: split[split.length - 2],
src: value
};
resolver: {
test: (value) => {
const tempURL = value.split("?")[0];
const split = tempURL.split(".");
const extension = split.pop();
const format = split.pop();
return extension === "json" && validImages.includes(format);
},
parse: (value) => {
const split = value.split(".");
return {
resolution: parseFloat(core.settings.RETINA_PREFIX.exec(value)?.[1] ?? "1"),
format: split[split.length - 2],
src: value
};
}
},
loader: {
extension: {
type: core.ExtensionType.LoadParser,
priority: assets.LoaderParserPriority.Normal
},
async testParse(asset, options) {
return core.utils.path.extname(options.src).includes(".json") && !!asset.frames;
},
async parse(asset, options, loader) {
let basePath = core.utils.path.dirname(options.src);
if (basePath && basePath.lastIndexOf("/") !== basePath.length - 1) {
basePath += "/";
}
const imagePath = basePath + asset.meta.image;
const assets = await loader.load([imagePath]);
const texture = assets[imagePath];
const spritesheet = new Spritesheet(texture.baseTexture, asset, options.src);
await spritesheet.parse();
const multiPacks = asset?.meta?.related_multi_packs;
if (Array.isArray(multiPacks)) {
const promises = [];
for (const item of multiPacks) {
if (typeof item !== "string") {
continue;
}
const itemUrl = basePath + item;
if (options.data?.ignoreMultiPack) {
continue;
}
promises.push(loader.load({
src: itemUrl,
data: {
ignoreMultiPack: true
}
}));
}
const res = await Promise.all(promises);
spritesheet.linkedSheets = res;
res.forEach((item) => {
item.linkedSheets = [spritesheet].concat(spritesheet.linkedSheets.filter((sp) => sp !== item));
});
}
return spritesheet;
},
unload(spritesheet) {
spritesheet.destroy(true);
}
}
};
core.extensions.add(resolveSpriteSheetUrl);
core.extensions.add(spritesheetAsset);
exports.Spritesheet = Spritesheet;
exports.cacheSpritesheet = cacheSpritesheet;
exports.loadSpritesheet = loadSpritesheet;
exports.resolveSpriteSheetUrl = resolveSpriteSheetUrl;
exports.spritesheetAsset = spritesheetAsset;
//# sourceMappingURL=spritesheet.js.map
"use strict";/*!
* @pixi/spritesheet - v7.0.0-alpha
* Compiled Fri, 09 Sep 2022 16:09:18 UTC
* @pixi/spritesheet - v7.0.0-alpha.2
* Compiled Sun, 18 Sep 2022 20:45:04 UTC
*
* @pixi/spritesheet is licensed under the MIT License.
* http://www.opensource.org/licenses/mit-license
*/Object.defineProperty(exports,"__esModule",{value:!0});var y=require("@pixi/assets"),o=require("@pixi/core");const u=class{constructor(t,e,r=null){this.linkedSheets=[],this._texture=t instanceof o.Texture?t:null,this.baseTexture=t instanceof o.BaseTexture?t:this._texture.baseTexture,this.textures={},this.animations={},this.data=e;const s=this.baseTexture.resource;this.resolution=this._updateResolution(r||(s?s.url:null)),this._frames=this.data.frames,this._frameKeys=Object.keys(this._frames),this._batchIndex=0,this._callback=null}_updateResolution(t=null){const{scale:e}=this.data.meta;let r=o.utils.getResolutionOfUrl(t,null);return r===null&&(r=parseFloat(e??"1")),r!==1&&this.baseTexture.setResolution(r),r}parse(){return new Promise(t=>{this._callback=t,this._batchIndex=0,this._frameKeys.length<=u.BATCH_SIZE?(this._processFrames(0),this._processAnimations(),this._parseComplete()):this._nextBatch()})}_processFrames(t){let e=t;const r=u.BATCH_SIZE;for(;e-t<r&&e<this._frameKeys.length;){const s=this._frameKeys[e],i=this._frames[s],a=i.frame;if(a){let n=null,l=null;const h=i.trimmed!==!1&&i.sourceSize?i.sourceSize:i.frame,p=new o.Rectangle(0,0,Math.floor(h.w)/this.resolution,Math.floor(h.h)/this.resolution);i.rotated?n=new o.Rectangle(Math.floor(a.x)/this.resolution,Math.floor(a.y)/this.resolution,Math.floor(a.h)/this.resolution,Math.floor(a.w)/this.resolution):n=new o.Rectangle(Math.floor(a.x)/this.resolution,Math.floor(a.y)/this.resolution,Math.floor(a.w)/this.resolution,Math.floor(a.h)/this.resolution),i.trimmed!==!1&&i.spriteSourceSize&&(l=new o.Rectangle(Math.floor(i.spriteSourceSize.x)/this.resolution,Math.floor(i.spriteSourceSize.y)/this.resolution,Math.floor(a.w)/this.resolution,Math.floor(a.h)/this.resolution)),this.textures[s]=new o.Texture(this.baseTexture,n,p,l,i.rotated?2:0,i.anchor),o.Texture.addToCache(this.textures[s],s)}e++}}_processAnimations(){const t=this.data.animations||{};for(const e in t){this.animations[e]=[];for(let r=0;r<t[e].length;r++){const s=t[e][r];this.animations[e].push(this.textures[s])}}}_parseComplete(){const t=this._callback;this._callback=null,this._batchIndex=0,t.call(this,this.textures)}_nextBatch(){this._processFrames(this._batchIndex*u.BATCH_SIZE),this._batchIndex++,setTimeout(()=>{this._batchIndex*u.BATCH_SIZE<this._frameKeys.length?this._nextBatch():(this._processAnimations(),this._parseComplete())},0)}destroy(t=!1){for(const e in this.textures)this.textures[e].destroy();this._frames=null,this._frameKeys=null,this.data=null,this.textures=null,t&&(this._texture?.destroy(),this.baseTexture.destroy()),this._texture=null,this.baseTexture=null,this.linkedSheets=[]}};let d=u;d.BATCH_SIZE=1e3;const x={extension:{type:o.ExtensionType.LoadParser,priority:y.LoaderParserPriority.Normal},async testParse(t,e){return o.utils.path.extname(e.src).includes(".json")&&!!t.frames},async parse(t,e,r){let s=o.utils.path.dirname(e.src);s&&s.lastIndexOf("/")!==s.length-1&&(s+="/");const i=s+t.meta.image,a=(await r.load([i]))[i],n=new d(a.baseTexture,t,e.src);await n.parse();const l=t?.meta?.related_multi_packs;if(Array.isArray(l)){const h=[];for(const c of l){if(typeof c!="string")continue;const f=s+c;e.data?.ignoreMultiPack||h.push(r.load({src:f,data:{ignoreMultiPack:!0}}))}const p=await Promise.all(h);n.linkedSheets=p,p.forEach(c=>{c.linkedSheets=[n].concat(n.linkedSheets.filter(f=>f!==c))})}return n},unload(t){t.destroy(!0)}};o.extensions.add(x);function _(t,e,r){const s={};if(t.forEach(i=>{s[i]=e}),Object.keys(e.textures).forEach(i=>{s[i]=e.textures[i]}),!r){const i=o.utils.path.dirname(t[0]);e.linkedSheets.forEach((a,n)=>{const l=_([`${i}/${e.data.meta.related_multi_packs[n]}`],a,!0);Object.assign(s,l)})}return s}const m={extension:o.ExtensionType.CacheParser,test:t=>t instanceof d,getCacheableAssets:(t,e)=>_(t,e,!1)};o.extensions.add(m);const b=["jpg","png","jpeg","avif","webp"],S={extension:o.ExtensionType.ResolveParser,test:t=>{const e=t.split("?")[0].split("."),r=e.pop(),s=e.pop();return r==="json"&&b.includes(s)},parse:t=>{const e=t.split(".");return{resolution:parseFloat(o.settings.RETINA_PREFIX.exec(t)?.[1]??"1"),format:e[e.length-2],src:t}}};o.extensions.add(S),exports.Spritesheet=d,exports.cacheSpritesheet=m,exports.loadSpritesheet=x,exports.resolveSpriteSheetUrl=S;
*/Object.defineProperty(exports,"__esModule",{value:!0});var o=require("@pixi/core"),x=require("@pixi/assets");const c=class{constructor(t,e,i=null){this.linkedSheets=[],this._texture=t instanceof o.Texture?t:null,this.baseTexture=t instanceof o.BaseTexture?t:this._texture.baseTexture,this.textures={},this.animations={},this.data=e;const s=this.baseTexture.resource;this.resolution=this._updateResolution(i||(s?s.url:null)),this._frames=this.data.frames,this._frameKeys=Object.keys(this._frames),this._batchIndex=0,this._callback=null}_updateResolution(t=null){const{scale:e}=this.data.meta;let i=o.utils.getResolutionOfUrl(t,null);return i===null&&(i=parseFloat(e??"1")),i!==1&&this.baseTexture.setResolution(i),i}parse(){return new Promise(t=>{this._callback=t,this._batchIndex=0,this._frameKeys.length<=c.BATCH_SIZE?(this._processFrames(0),this._processAnimations(),this._parseComplete()):this._nextBatch()})}_processFrames(t){let e=t;const i=c.BATCH_SIZE;for(;e-t<i&&e<this._frameKeys.length;){const s=this._frameKeys[e],r=this._frames[s],a=r.frame;if(a){let n=null,l=null;const h=r.trimmed!==!1&&r.sourceSize?r.sourceSize:r.frame,p=new o.Rectangle(0,0,Math.floor(h.w)/this.resolution,Math.floor(h.h)/this.resolution);r.rotated?n=new o.Rectangle(Math.floor(a.x)/this.resolution,Math.floor(a.y)/this.resolution,Math.floor(a.h)/this.resolution,Math.floor(a.w)/this.resolution):n=new o.Rectangle(Math.floor(a.x)/this.resolution,Math.floor(a.y)/this.resolution,Math.floor(a.w)/this.resolution,Math.floor(a.h)/this.resolution),r.trimmed!==!1&&r.spriteSourceSize&&(l=new o.Rectangle(Math.floor(r.spriteSourceSize.x)/this.resolution,Math.floor(r.spriteSourceSize.y)/this.resolution,Math.floor(a.w)/this.resolution,Math.floor(a.h)/this.resolution)),this.textures[s]=new o.Texture(this.baseTexture,n,p,l,r.rotated?2:0,r.anchor),o.Texture.addToCache(this.textures[s],s)}e++}}_processAnimations(){const t=this.data.animations||{};for(const e in t){this.animations[e]=[];for(let i=0;i<t[e].length;i++){const s=t[e][i];this.animations[e].push(this.textures[s])}}}_parseComplete(){const t=this._callback;this._callback=null,this._batchIndex=0,t.call(this,this.textures)}_nextBatch(){this._processFrames(this._batchIndex*c.BATCH_SIZE),this._batchIndex++,setTimeout(()=>{this._batchIndex*c.BATCH_SIZE<this._frameKeys.length?this._nextBatch():(this._processAnimations(),this._parseComplete())},0)}destroy(t=!1){for(const e in this.textures)this.textures[e].destroy();this._frames=null,this._frameKeys=null,this.data=null,this.textures=null,t&&(this._texture?.destroy(),this.baseTexture.destroy()),this._texture=null,this.baseTexture=null,this.linkedSheets=[]}};let f=c;f.BATCH_SIZE=1e3;const y=["jpg","png","jpeg","avif","webp"];function _(t,e,i){const s={};if(t.forEach(r=>{s[r]=e}),Object.keys(e.textures).forEach(r=>{s[r]=e.textures[r]}),!i){const r=o.utils.path.dirname(t[0]);e.linkedSheets.forEach((a,n)=>{const l=_([`${r}/${e.data.meta.related_multi_packs[n]}`],a,!0);Object.assign(s,l)})}return s}const m={extension:o.ExtensionType.Asset,cache:{test:t=>t instanceof f,getCacheableAssets:(t,e)=>_(t,e,!1)},resolver:{test:t=>{const e=t.split("?")[0].split("."),i=e.pop(),s=e.pop();return i==="json"&&y.includes(s)},parse:t=>{const e=t.split(".");return{resolution:parseFloat(o.settings.RETINA_PREFIX.exec(t)?.[1]??"1"),format:e[e.length-2],src:t}}},loader:{extension:{type:o.ExtensionType.LoadParser,priority:x.LoaderParserPriority.Normal},async testParse(t,e){return o.utils.path.extname(e.src).includes(".json")&&!!t.frames},async parse(t,e,i){let s=o.utils.path.dirname(e.src);s&&s.lastIndexOf("/")!==s.length-1&&(s+="/");const r=s+t.meta.image,a=(await i.load([r]))[r],n=new f(a.baseTexture,t,e.src);await n.parse();const l=t?.meta?.related_multi_packs;if(Array.isArray(l)){const h=[];for(const u of l){if(typeof u!="string")continue;const d=s+u;e.data?.ignoreMultiPack||h.push(i.load({src:d,data:{ignoreMultiPack:!0}}))}const p=await Promise.all(h);n.linkedSheets=p,p.forEach(u=>{u.linkedSheets=[n].concat(n.linkedSheets.filter(d=>d!==u))})}return n},unload(t){t.destroy(!0)}}};o.extensions.add(m),exports.Spritesheet=f,exports.spritesheetAsset=m;
//# sourceMappingURL=spritesheet.min.js.map
/// <reference path="./global.d.ts" />
import type { AssetExtension } from '@pixi/assets';
import { BaseTexture } from '@pixi/core';
import type { CacheParser } from '@pixi/assets';
import type { IPointData } from '@pixi/core';
import type { LoaderParser } from '@pixi/assets';
import type { ResolveURLParser } from '@pixi/assets';
import { Texture } from '@pixi/core';
import { utils } from '@pixi/core';
export declare const cacheSpritesheet: CacheParser<Spritesheet>;
/** Atlas format. */

@@ -45,12 +41,2 @@ export declare interface ISpritesheetData {

/**
* Loader plugin that parses sprite sheets!
* once the JSON has been loaded this checks to see if the JSON is spritesheet data.
* If it is, we load the spritesheets image and parse the data into PIXI.Spritesheet
* All textures in the sprite sheet are then added to the cache
*/
export declare const loadSpritesheet: LoaderParser<any, any>;
export declare const resolveSpriteSheetUrl: ResolveURLParser;
/**
* Utility class for maintaining reference to a collection

@@ -62,8 +48,5 @@ * of Textures on a single Spritesheet.

* ```js
* PIXI.Loader.shared.add("images/spritesheet.json").load(setup);
* import { Assets } from 'pixi.js';
*
* function setup() {
* let sheet = PIXI.Loader.shared.resources["images/spritesheet.json"].spritesheet;
* ...
* }
* const sheet = await Assets.load("images/spritesheet.json");
* ```

@@ -73,3 +56,5 @@ *

* ```js
* const sheet = new PIXI.Spritesheet(texture, spritesheetData);
* import { Spritesheet } from 'pixi.js';
*
* const sheet = new Spritesheet(texture, spritesheetData);
* await sheet.parse();

@@ -97,5 +82,6 @@ * console.log('Spritesheet ready to use!');

* Can be used to create a {@link PIXI.Sprite|Sprite}:
* ```js
* new PIXI.Sprite(sheet.textures["image.png"]);
* ```
* @example
* import { Sprite } from 'pixi.js';
*
* new Sprite(sheet.textures["image.png"]);
*/

@@ -106,5 +92,6 @@ textures: utils.Dict<Texture>;

* Can be used to create an {@link PIXI.AnimatedSprite|AnimatedSprite}:
* ```js
* new PIXI.AnimatedSprite(sheet.animations["anim_name"])
* ```
* @example
* import { AnimatedSprite } from 'pixi.js';
*
* new AnimatedSprite(sheet.animations["anim_name"])
*/

@@ -178,2 +165,16 @@ animations: utils.Dict<Texture[]>;

/**
* Asset extension for loading spritesheets.
* @memberof PIXI
*/
export declare const spritesheetAsset: AssetExtension<Spritesheet | SpriteSheetJson, any>;
declare interface SpriteSheetJson extends ISpritesheetData {
meta: {
image: string;
scale: string;
related_multi_packs?: string[];
};
}
export { }
{
"name": "@pixi/spritesheet",
"version": "7.0.0-alpha",
"version": "7.0.0-alpha.2",
"main": "dist/cjs/spritesheet.js",

@@ -42,3 +42,3 @@ "module": "dist/esm/spritesheet.mjs",

],
"gitHead": "da993226df64b804a9c00ed9ee4d011191467b8a"
"gitHead": "439f6c1606ba63b26a93173284d25c35a7682b17"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc