@curiousmedia/preloadjs-animate-composition-loader
Version:
A PreloadJS Loader that handles the loading of Adobe Animate CreateJS Exports
171 lines (146 loc) • 4.14 kB
JavaScript
export const ANIMATE_COMPOSITION = "animate_composition";
export class AnimateCompositionLoader extends createjs.AbstractLoader
{
constructor( loadItem, preferXHR )
{
super( loadItem, preferXHR, ANIMATE_COMPOSITION );
this._manifestQueue = null;
this.compositionsIDMap = {};
}
static addPlaySoundOnWindow()
{
window.playSound = ( id, loop, offset ) => createjs.Sound.play( id, { "interrupt":createjs.Sound.INTERRUPT_EARLY, "loop": loop, "offset": offset } );
}
static get MANIFEST_PROGRESS()
{
return 0.25;
}
static canLoadItem( item )
{
return item.type === ANIMATE_COMPOSITION;
}
destroy()
{
super.destroy();
this._manifestQueue.close();
}
_createRequest()
{
this._request = new createjs.JavaScriptLoader( this._item );
}
handleEvent( event )
{
const AdobeAn = window.AdobeAn;
switch ( event.type )
{
case "complete":
{
this._rawResult = event.target.getResult( true );
if ( !AdobeAn.compositions )
{
throw new Error( "Using out of date flash export! " + this._item.src );
}
let compId;
if ( this.compositionsIDMap[this._item.src] )
{
compId = this.compositionsIDMap[this._item.src];
}
else
{
let compIds = Object.keys( AdobeAn.compositions );
for ( let i = 0, len = compIds.length; i < len; i++ )
{
if ( !this.compositionsIDMap[compIds[i]] )
{
compId = compIds[i];
break;
}
}
this.compositionsIDMap[this._item.src] = compId;
}
this._item.assetsPath = this._item.src.slice( 0, this._item.src.lastIndexOf( "/" ) + 1 );
let comp = this._item.comp = AdobeAn.compositions[compId];
let lib = this._result = this._item.lib = comp.getLibrary();
// Clean up. Keep lib references exclusively with this.compositionsIDMap
delete AdobeAn.compositions[compId];
if ( !lib )
{
throw new Error( "Something went wrong. Lib not found" );
}
this._sendProgress( AnimateCompositionLoader.MANIFEST_PROGRESS );
this._loadManifest( lib );
return;
}
case "progress":
event.loaded *= AnimateCompositionLoader.MANIFEST_PROGRESS;
this.progress = event.loaded / event.total;
if ( isNaN( this.progress ) || this.progress === Infinity )
{
this.progress = 0;
}
this._sendProgress( event );
return;
}
super._handleEvent( event );
}
_loadManifest( lib )
{
if ( lib.properties.manifest && lib.properties.manifest.length )
{
let queue = new createjs.LoadQueue( this._preferXHR, this._item.assetsPath, this._item.crossOrigin );
queue.installPlugin( createjs.Sound );
queue.on( "complete", this._handleManifestComplete, this, true );
queue.on( "fileload", this._handleManifestFileLoad, this );
queue.on( "progress", this._handleManifestProgress, this );
queue.on( "error", this._handleManifestError, this, true );
this._manifestQueue = queue;
let manifest = [];
for ( let i = 0, len = lib.properties.manifest.length; i < len; i++ )
{
manifest.push( lib.properties.manifest[i] );
}
queue.loadManifest( manifest );
}
else
{
this._sendComplete();
}
}
_handleManifestFileLoad( evt )
{
if ( evt && ( evt.item.type === "image" ) )
{
let comp = this._item.comp;
let images = comp.getImages();
images[evt.item.id] = evt.result;
}
}
_handleManifestComplete( evt )
{
let comp = this._item.comp;
let lib = comp.getLibrary();
let ss = comp.getSpriteSheet();
let queue = evt.target;
let ssMetadata = lib.ssMetadata;
for ( let i = 0; i < ssMetadata.length; i++ )
{
ss[ssMetadata[i].name] = new createjs.SpriteSheet(
{
"images": [queue.getResult( ssMetadata[i].name )],
"frames": ssMetadata[i].frames
} );
}
this._sendComplete();
}
_handleManifestProgress( event )
{
this.progress = event.progress * ( 1 - AnimateCompositionLoader.MANIFEST_PROGRESS ) + AnimateCompositionLoader.MANIFEST_PROGRESS;
this._sendProgress( this.progress );
}
_handleManifestError( event )
{
let newEvent = new createjs.Event( "fileerror" );
newEvent.item = event.data;
this.dispatchEvent( newEvent );
}
}