UNPKG

aseprite-atlas

Version:

Aseprite sprite atlas parser and animator for browser and Node.js.

2 lines 3.67 kB
!function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.AsepriteAtlas=n():e.AsepriteAtlas=n()}(this,(function(){return(()=>{"use strict";var e={504:(e,n,r)=>{var t,i,o;r.r(n),r.d(n,{Animator:()=>o,Aseprite:()=>t,Parser:()=>s}),function(e){e.Infinite=65535,e.AnimationDirection={Forward:"forward",Reverse:"reverse",PingPong:"pingpong"}}(t||(t={})),function(e){e.wrap=function(e,n,r){const t=r-n;return((e-n)%t+t)%t+n}}(i||(i={})),function(e){function n(e,n){return Math.abs(e%n.length)}e.animate=function(e,r,t){if(t.cels.length<1)return{period:e,exposure:r};for(r%=t.duration;r>=t.cels[n(e,t.cels)].duration;)r-=t.cels[n(e,t.cels)].duration,e=a[t.direction](e,t.cels.length);return{period:e,exposure:r}},e.index=n}(o||(o={}));const a=Object.freeze({[t.AnimationDirection.Forward]:e=>e%Number.MAX_SAFE_INTEGER+1,[t.AnimationDirection.Reverse]:(e,n)=>e%Number.MIN_SAFE_INTEGER-1+n,[t.AnimationDirection.PingPong]:(e,n)=>i.wrap(e-1,2-n,n)});var s;!function(e){function n({meta:e,frames:n}){const{frameTags:t,slices:i}=e;return Object.freeze(t.reduce(((e,t)=>{if(t.name in e)throw new Error(`Duplicate tag "${t.name}".`);return Object.assign(Object.assign({},e),{[t.name]:r(t,n,i)})}),{}))}function r(e,n,r){const o=function({name:e,from:n,to:r},t){const i=[];for(;n<=r;++n){const r=t[`${e} ${n}`];if(!r)throw new Error(`Missing frame "${e} ${n}".`);i.push(r)}return i}(e,n),s=o.map(((n,t)=>a(e,n,t,r)));let c=s.reduce(((e,{duration:n})=>e+n),0);if(e.direction===t.AnimationDirection.PingPong&&s.length>2&&(c+=c-(s[0].duration+s[s.length-1].duration)),!s.length)throw new Error(`"${e.name}" animation missing cels.`);if(c<=0)throw new Error(`Total duration for "${e.name}" animation is non-positive.`);if(s.slice(0,-1).some((({duration:e})=>e===Number.POSITIVE_INFINITY)))throw new Error(`Intermediate cel has infinite duration for "${e.name}" animation.`);const{w:u,h:f}=o[0].sourceSize;return{size:Object.freeze({w:u,h:f}),cels:Object.freeze(s),duration:c,direction:i(e)}}function i({direction:e}){if(o(e))return e;throw new Error(`"${e}" is not a Direction.`)}function o(e){return Object.values(t.AnimationDirection).includes(e)}function a(e,n,r,t){return Object.freeze({position:s(n),duration:u(n.duration),slices:f(e,r,t)})}function s(e){const n=c(e);return Object.freeze({x:e.frame.x+n.w/2,y:e.frame.y+n.h/2})}function c({frame:e,sourceSize:n}){return Object.freeze({w:e.w-n.w,h:e.h-n.h})}function u(e){if(!e)throw new Error("Expected positive cel duration.");return e===t.Infinite?Number.POSITIVE_INFINITY:e}function f({name:e},n,r){const t=[];for(const i of r){if(i.name!==e)continue;const r=i.keys.filter((e=>e.frame<=n)).slice(-1)[0];if(!r)throw new Error(`No Keys for Tag "${i.name}".`);t.push(r.bounds)}return Object.freeze(t)}e.parse=function(e){return Object.freeze({version:e.meta.version,filename:e.meta.image,format:e.meta.format,size:e.meta.size,animations:n(e)})},e.parseAnimationRecord=n,e.parseAnimation=r,e.parseAnimationDirection=i,e.isAnimationDirection=o,e.parseCel=a,e.parsePosition=s,e.parsePadding=c,e.parseDuration=u,e.parseSlices=f}(s||(s={}))}},n={};function r(t){if(n[t])return n[t].exports;var i=n[t]={exports:{}};return e[t](i,i.exports,r),i.exports}return r.d=(e,n)=>{for(var t in n)r.o(n,t)&&!r.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:n[t]})},r.o=(e,n)=>Object.prototype.hasOwnProperty.call(e,n),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r(504)})()})); //# sourceMappingURL=index.js.map