| var AnimationItem = function () { |
| this._cbs = []; |
| this.name = ''; |
| this.path = ''; |
| this.isLoaded = false; |
| this.currentFrame = 0; |
| this.currentRawFrame = 0; |
| this.totalFrames = 0; |
| this.frameRate = 0; |
| this.frameMult = 0; |
| this.playSpeed = 1; |
| this.playDirection = 1; |
| this.pendingElements = 0; |
| this.playCount = 0; |
| this.prerenderFramesFlag = true; |
| this.repeat = 'indefinite'; |
| this.animationData = {}; |
| this.layers = []; |
| this.assets = []; |
| this.isPaused = true; |
| this.isScrolling = false; |
| this.autoplay = false; |
| this.loop = true; |
| this.renderer = null; |
| this.animationID = randomString(10); |
| this.renderedFrameCount = 0; |
| this.scaleMode = 'fit'; |
| this.math = Math; |
| this.removed = false; |
| this.timeCompleted = 0; |
| this.segmentPos = 0; |
| this.segments = []; |
| }; |
| |
| AnimationItem.prototype.setParams = function(params) { |
| var self = this; |
| if(params.context){ |
| this.context = params.context; |
| } |
| if(params.wrapper || params.container){ |
| this.wrapper = params.wrapper || params.container; |
| } |
| var animType = params.animType ? params.animType : params.renderer ? params.renderer : 'canvas'; |
| switch(animType){ |
| case 'canvas': |
| this.renderer = new CanvasRenderer(this, params.renderer); |
| break; |
| case 'svg': |
| this.renderer = new SVGRenderer(this, params.renderer); |
| } |
| this.animType = animType; |
| |
| if(params.loop === '' || params.loop === null){ |
| }else if(params.loop === false){ |
| this.loop = false; |
| }else if(params.loop === true){ |
| this.loop = true; |
| }else{ |
| this.loop = parseInt(params.loop); |
| } |
| this.autoplay = 'autoplay' in params ? params.autoplay : true; |
| this.name = params.name ? params.name : ''; |
| this.prerenderFramesFlag = 'prerender' in params ? params.prerender : true; |
| if(params.animationData){ |
| self.configAnimation(params.animationData); |
| }else if(params.path){ |
| if(params.path.substr(-4) != 'json'){ |
| if (params.path.substr(-1, 1) != '/') { |
| params.path += '/'; |
| } |
| params.path += 'data.json'; |
| } |
| |
| var xhr = new XMLHttpRequest(); |
| if(params.path.lastIndexOf('\\') != -1){ |
| this.path = params.path.substr(0,params.path.lastIndexOf('\\')+1); |
| }else{ |
| this.path = params.path.substr(0,params.path.lastIndexOf('/')+1); |
| } |
| this.fileName = params.path.substr(params.path.lastIndexOf('/')+1); |
| this.fileName = this.fileName.substr(0,this.fileName.lastIndexOf('.json')); |
| xhr.open('GET', params.path, true); |
| xhr.send(); |
| xhr.onreadystatechange = function () { |
| if (xhr.readyState == 4) { |
| if(xhr.status == 200){ |
| self.configAnimation(JSON.parse(xhr.responseText)); |
| }else{ |
| try{ |
| var response = JSON.parse(xhr.responseText); |
| self.configAnimation(response); |
| }catch(err){ |
| } |
| } |
| } |
| }; |
| } |
| }; |
| |
| AnimationItem.prototype.setData = function (wrapper) { |
| var params = { |
| wrapper: wrapper |
| }; |
| var wrapperAttributes = wrapper.attributes; |
| |
| params.path = wrapperAttributes.getNamedItem('data-animation-path') ? wrapperAttributes.getNamedItem('data-animation-path').value : wrapperAttributes.getNamedItem('data-bm-path') ? wrapperAttributes.getNamedItem('data-bm-path').value : wrapperAttributes.getNamedItem('bm-path') ? wrapperAttributes.getNamedItem('bm-path').value : ''; |
| params.animType = wrapperAttributes.getNamedItem('data-anim-type') ? wrapperAttributes.getNamedItem('data-anim-type').value : wrapperAttributes.getNamedItem('data-bm-type') ? wrapperAttributes.getNamedItem('data-bm-type').value : wrapperAttributes.getNamedItem('bm-type') ? wrapperAttributes.getNamedItem('bm-type').value : 'canvas'; |
| |
| var loop = wrapperAttributes.getNamedItem('data-anim-loop') ? wrapperAttributes.getNamedItem('data-anim-loop').value : wrapperAttributes.getNamedItem('data-bm-loop') ? wrapperAttributes.getNamedItem('data-bm-loop').value : wrapperAttributes.getNamedItem('bm-loop') ? wrapperAttributes.getNamedItem('bm-loop').value : ''; |
| if(loop === ''){ |
| }else if(loop === 'false'){ |
| params.loop = false; |
| }else if(loop === 'true'){ |
| params.loop = true; |
| }else{ |
| params.loop = parseInt(loop); |
| } |
| |
| params.name = wrapperAttributes.getNamedItem('data-name') ? wrapperAttributes.getNamedItem('data-name').value : wrapperAttributes.getNamedItem('data-bm-name') ? wrapperAttributes.getNamedItem('data-bm-name').value : wrapperAttributes.getNamedItem('bm-name') ? wrapperAttributes.getNamedItem('bm-name').value : ''; |
| var prerender = wrapperAttributes.getNamedItem('data-anim-prerender') ? wrapperAttributes.getNamedItem('data-anim-prerender').value : wrapperAttributes.getNamedItem('data-bm-prerender') ? wrapperAttributes.getNamedItem('data-bm-prerender').value : wrapperAttributes.getNamedItem('bm-prerender') ? wrapperAttributes.getNamedItem('bm-prerender').value : ''; |
| |
| if(prerender === 'false'){ |
| params.prerender = false; |
| } |
| this.setParams(params); |
| }; |
| |
| AnimationItem.prototype.includeLayers = function(data) { |
| var layers = this.animationData.layers; |
| var i, len = layers.length; |
| var newLayers = data.layers; |
| var j, jLen = newLayers.length; |
| for(j=0;j<jLen;j+=1){ |
| i = 0; |
| while(i<len){ |
| if(layers[i].id == newLayers[j].id){ |
| layers[i] = newLayers[j]; |
| break; |
| } |
| i += 1; |
| } |
| } |
| if(data.assets){ |
| len = data.assets.length; |
| for(i = 0; i < len; i += 1){ |
| this.animationData.assets.push(data.assets[i]); |
| } |
| } |
| dataManager.completeData(this.animationData); |
| this.renderer.includeLayers(data.layers); |
| this.renderer.buildStage(this.container, this.layers); |
| this.renderer.renderFrame(null); |
| this.loadNextSegment(); |
| }; |
| |
| AnimationItem.prototype.loadNextSegment = function() { |
| var segments = this.animationData.segments; |
| if(!segments || segments.length === 0){ |
| this.trigger('data_ready'); |
| this.timeCompleted = this.animationData.tf; |
| return; |
| } |
| var segment = segments.shift(); |
| this.timeCompleted = segment.time * this.frameRate; |
| var xhr = new XMLHttpRequest(); |
| var self = this; |
| var segmentPath = this.path+this.fileName+'_' + this.segmentPos + '.json'; |
| this.segmentPos += 1; |
| xhr.open('GET', segmentPath, true); |
| xhr.send(); |
| xhr.onreadystatechange = function () { |
| if (xhr.readyState == 4) { |
| if(xhr.status == 200){ |
| self.includeLayers(JSON.parse(xhr.responseText)); |
| }else{ |
| try{ |
| var response = JSON.parse(xhr.responseText); |
| self.includeLayers(response); |
| }catch(err){ |
| } |
| } |
| } |
| }; |
| }; |
| |
| AnimationItem.prototype.loadSegments = function() { |
| var segments = this.animationData.segments; |
| if(!segments) { |
| this.timeCompleted = this.animationData.tf; |
| } |
| this.loadNextSegment(); |
| }; |
| |
| AnimationItem.prototype.configAnimation = function (animData) { |
| this.animationData = animData; |
| this.totalFrames = Math.floor(this.animationData.op - this.animationData.ip); |
| this.animationData.tf = this.totalFrames; |
| this.renderer.configAnimation(animData); |
| if(!animData.assets){ |
| animData.assets = []; |
| } |
| if(animData.comps) { |
| animData.assets = animData.assets.concat(animData.comps); |
| animData.comps = null; |
| } |
| |
| this.animationData._id = this.animationID; |
| this.animationData._animType = this.animType; |
| this.layers = this.animationData.layers; |
| this.assets = this.animationData.assets; |
| this.frameRate = this.animationData.fr; |
| this.firstFrame = Math.round(this.animationData.ip); |
| this.frameMult = this.animationData.fr / 1000; |
| /* |
| this.firstFrame = 187; |
| this.totalFrames = 2; |
| this.animationData.tf = 2; |
| //this.frameMult = 10000/1000; |
| //*///// |
| this.trigger('config_ready'); |
| this.loadSegments(); |
| dataManager.completeData(this.animationData); |
| this.renderer.buildItems(this.animationData.layers); |
| this.updaFrameModifier(); |
| this.checkLoaded(); |
| }; |
| |
| AnimationItem.prototype.elementLoaded = function () { |
| this.pendingElements--; |
| this.checkLoaded(); |
| }; |
| |
| AnimationItem.prototype.checkLoaded = function () { |
| if (this.pendingElements === 0) { |
| this.renderer.buildStage(this.container, this.layers); |
| this.isLoaded = true; |
| this.gotoFrame(); |
| if(this.autoplay){ |
| this.play(); |
| } |
| } |
| }; |
| |
| AnimationItem.prototype.resize = function () { |
| this.renderer.updateContainerSize(); |
| }; |
| |
| AnimationItem.prototype.gotoFrame = function () { |
| if(subframeEnabled){ |
| this.currentFrame = this.currentRawFrame; |
| }else{ |
| this.currentFrame = this.math.floor(this.currentRawFrame); |
| } |
| |
| if(this.timeCompleted !== this.totalFrames && this.currentFrame > this.timeCompleted){ |
| this.currentFrame = this.timeCompleted; |
| } |
| this.trigger('enterFrame'); |
| this.renderFrame(); |
| }; |
| |
| AnimationItem.prototype.renderFrame = function () { |
| if(this.isLoaded === false){ |
| return; |
| } |
| this.renderer.renderFrame(this.currentFrame + this.firstFrame); |
| }; |
| |
| AnimationItem.prototype.play = function (name) { |
| if(name && this.name != name){ |
| return; |
| } |
| if(this.isPaused === true){ |
| this.isPaused = false; |
| } |
| }; |
| |
| AnimationItem.prototype.pause = function (name) { |
| if(name && this.name != name){ |
| return; |
| } |
| if(this.isPaused === false){ |
| this.isPaused = true; |
| } |
| }; |
| |
| AnimationItem.prototype.togglePause = function (name) { |
| if(name && this.name != name){ |
| return; |
| } |
| if(this.isPaused === true){ |
| this.isPaused = false; |
| this.play(); |
| }else{ |
| this.isPaused = true; |
| this.pause(); |
| } |
| }; |
| |
| AnimationItem.prototype.stop = function (name) { |
| if(name && this.name != name){ |
| return; |
| } |
| this.isPaused = true; |
| this.currentFrame = this.currentRawFrame = 0; |
| this.playCount = 0; |
| this.gotoFrame(); |
| }; |
| |
| AnimationItem.prototype.goToAndStop = function (value, isFrame, name) { |
| if(name && this.name != name){ |
| return; |
| } |
| if(isFrame){ |
| this.setCurrentRawFrameValue(value); |
| }else{ |
| this.setCurrentRawFrameValue(value * this.frameModifier); |
| } |
| this.isPaused = true; |
| }; |
| |
| AnimationItem.prototype.advanceTime = function (value) { |
| if (this.isPaused === true || this.isScrolling === true || this.isLoaded === false) { |
| return; |
| } |
| this.setCurrentRawFrameValue(this.currentRawFrame + value * this.frameModifier); |
| }; |
| |
| AnimationItem.prototype.updateAnimation = function (perc) { |
| this.setCurrentRawFrameValue(this.totalFrames * perc); |
| }; |
| |
| AnimationItem.prototype.moveFrame = function (value, name) { |
| if(name && this.name != name){ |
| return; |
| } |
| this.setCurrentRawFrameValue(this.currentRawFrame+value); |
| }; |
| |
| AnimationItem.prototype.adjustSegment = function(arr){ |
| this.totalFrames = arr[1] - arr[0]; |
| this.firstFrame = arr[0]; |
| this.trigger('segmentStart'); |
| }; |
| |
| AnimationItem.prototype.playSegments = function (arr,forceFlag) { |
| if(typeof arr[0] === 'object'){ |
| var i, len = arr.length; |
| for(i=0;i<len;i+=1){ |
| this.segments.push(arr[i]); |
| } |
| }else{ |
| this.segments.push(arr); |
| } |
| if(forceFlag){ |
| this.adjustSegment(this.segments.shift()); |
| this.setCurrentRawFrameValue(0); |
| } |
| }; |
| |
| AnimationItem.prototype.resetSegments = function (forceFlag) { |
| this.segments.length = 0; |
| this.segments.push([this.animationData.ip*this.frameRate,Math.floor(this.animationData.op - this.animationData.ip+this.animationData.ip*this.frameRate)]); |
| if(forceFlag){ |
| this.adjustSegment(this.segments.shift()); |
| } |
| }; |
| |
| AnimationItem.prototype.remove = function (name) { |
| if(name && this.name != name){ |
| return; |
| } |
| this.renderer.destroy(); |
| }; |
| |
| AnimationItem.prototype.destroy = function (name) { |
| if((name && this.name != name) || (this.renderer && this.renderer.destroyed)){ |
| return; |
| } |
| this.renderer.destroy(); |
| }; |
| |
| AnimationItem.prototype.setCurrentRawFrameValue = function(value){ |
| this.currentRawFrame = value; |
| var newSegment = false; |
| if (this.currentRawFrame >= this.totalFrames) { |
| if(this.segments.length){ |
| newSegment = true; |
| } |
| if(this.loop === false){ |
| this.currentRawFrame = this.totalFrames - 0.01; |
| this.gotoFrame(); |
| this.pause(); |
| this.trigger('complete'); |
| return; |
| }else{ |
| this.trigger('loopComplete'); |
| this.playCount += 1; |
| if(this.loop !== true){ |
| if(this.playCount == this.loop){ |
| this.currentRawFrame = this.totalFrames - 0.01; |
| this.gotoFrame(); |
| this.pause(); |
| this.trigger('complete'); |
| return; |
| } |
| } |
| } |
| } else if (this.currentRawFrame < 0) { |
| this.playCount -= 1; |
| if(this.playCount < 0){ |
| this.playCount = 0; |
| } |
| if(this.loop === false){ |
| this.currentRawFrame = 0; |
| this.gotoFrame(); |
| this.pause(); |
| this.trigger('complete'); |
| return; |
| }else{ |
| this.trigger('loopComplete'); |
| this.currentRawFrame = this.totalFrames + this.currentRawFrame; |
| this.gotoFrame(); |
| return; |
| } |
| } |
| |
| |
| if(newSegment){ |
| var offset = this.currentRawFrame % this.totalFrames; |
| this.adjustSegment(this.segments.shift()); |
| this.currentRawFrame = offset; |
| }else{ |
| this.currentRawFrame = this.currentRawFrame % this.totalFrames; |
| } |
| this.gotoFrame(); |
| }; |
| |
| AnimationItem.prototype.setSpeed = function (val) { |
| this.playSpeed = val; |
| this.updaFrameModifier(); |
| }; |
| |
| AnimationItem.prototype.setDirection = function (val) { |
| this.playDirection = val < 0 ? -1 : 1; |
| this.updaFrameModifier(); |
| }; |
| |
| AnimationItem.prototype.updaFrameModifier = function () { |
| this.frameModifier = this.frameMult * this.playSpeed * this.playDirection; |
| }; |
| |
| AnimationItem.prototype.getPath = function () { |
| return this.path; |
| }; |
| |
| AnimationItem.prototype.getAssetData = function (id) { |
| var i = 0, len = this.assets.length; |
| while (i < len) { |
| if(id == this.assets[i].id){ |
| return this.assets[i]; |
| } |
| i += 1; |
| } |
| }; |
| |
| AnimationItem.prototype.getAssets = function () { |
| return this.assets; |
| }; |
| |
| AnimationItem.prototype.trigger = function(name){ |
| if(this._cbs[name]){ |
| switch(name){ |
| case 'enterFrame': |
| this.triggerEvent(name,new BMEnterFrameEvent(name,this.currentFrame,this.totalFrames,this.frameMult)); |
| break; |
| case 'loopComplete': |
| this.triggerEvent(name,new BMCompleteLoopEvent(name,this.loop,this.playCount,this.frameMult)); |
| break; |
| case 'complete': |
| this.triggerEvent(name,new BMCompleteEvent(name,this.frameMult)); |
| break; |
| case 'segmentStart': |
| this.triggerEvent(name,new BMSegmentStartEvent(name,this.firstFrame,this.totalFrames)); |
| break; |
| default: |
| this.triggerEvent(name); |
| } |
| } |
| if(name === 'enterFrame' && this.onEnterFrame){ |
| this.onEnterFrame.call(this,new BMEnterFrameEvent(name,this.currentFrame,this.totalFrames,this.frameMult)); |
| } |
| if(name === 'loopComplete' && this.onLoopComplete){ |
| this.onLoopComplete.call(this,new BMCompleteLoopEvent(name,this.loop,this.playCount,this.frameMult)); |
| } |
| if(name === 'complete' && this.onComplete){ |
| this.onComplete.call(this,new BMCompleteEvent(name,this.frameMult)); |
| } |
| if(name === 'segmentStart' && this.onSegmentStart){ |
| this.onSegmentStart.call(this,new BMSegmentStartEvent(name,this.firstFrame,this.totalFrames)); |
| } |
| }; |
| |
| AnimationItem.prototype.addEventListener = addEventListener; |
| AnimationItem.prototype.removeEventListener = removeEventListener; |
| AnimationItem.prototype.triggerEvent = triggerEvent; |