blob: 8d1772ed65c6fbff9a94daa337b97ea25c0c19f4 [file] [log] [blame]
var AnimationItem = function () {
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.loop = true;
this.renderer = null;
this.animationID = randomString(10);
this.renderedFrameCount = 0;
this.math = Math;
};
AnimationItem.prototype.setData = function (wrapper) {
this.wrapper = wrapper;
//this.wrapper.style.position = 'relative';
var self = this;
var wrapperAttributes = this.wrapper.attributes;
this.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 : '';
this.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';
switch(this.animType){
case 'canvas':
this.renderer = new CanvasRenderer(this);
break;
case 'svg':
this.renderer = new SVGRenderer(this);
}
this.repeat = wrapperAttributes.getNamedItem('data-anim-repeat') ? wrapperAttributes.getNamedItem('data-anim-repeat').value : this.repeat;
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'){
this.loop = false;
}else if(loop === 'true'){
this.loop = true;
}else{
this.loop = parseInt(loop);
}
this.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'){
this.prerenderFramesFlag = false;
}
if(this.path == ''){
return;
}
if (this.path.substr(-1, 1) != '/') {
this.path += '/';
}
var xhr = new XMLHttpRequest();
xhr.open('GET', this.path + 'data.json', 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.configAnimation = function (animData) {
this.renderer.configAnimation(animData);
this.animationData = animData;
this.animationData._id = this.animationID;
this.animationData._animType = this.animType;
this.layers = this.animationData.animation.layers;
this.assets = this.animationData.assets;
this.totalFrames = this.animationData.animation.totalFrames;
this.frameRate = this.animationData.animation.frameRate;
this.frameMult = this.animationData.animation.frameRate / 1000;
dataManager.completeData(this.animationData);
this.renderer.buildItems(this.animationData.animation.layers);
this.updaFrameModifier();
this.checkLoaded();
};
AnimationItem.prototype.elementLoaded = function () {
this.pendingElements--;
this.checkLoaded();
};
AnimationItem.prototype.checkLoaded = function () {
this.renderer.buildStage(this.container, this.layers);
if (this.pendingElements == 0) {
if(this.prerenderFramesFlag){
this.prerenderFrames(0);
dataManager.renderFrame(this.animationID,this.currentFrame);
this.renderer.renderFrame(this.currentFrame);
}else{
this.isLoaded = true;
this.gotoFrame();
}
}
};
AnimationItem.prototype.prerenderFrames = function(count){
if(!count){
count = 0;
}
if(this.renderedFrameCount === this.totalFrames){
//TODO Need polyfill for ios 5.1
this.isLoaded = true;
this.gotoFrame();
}else{
dataManager.renderFrame(this.animationID,this.renderedFrameCount);
this.renderedFrameCount+=1;
if(count > 5){
setTimeout(this.prerenderFrames.bind(this),0);
}else{
count += 1;
this.prerenderFrames(count);
}
}
};
AnimationItem.prototype.resize = function () {
this.renderer.updateContainerSize();
};
AnimationItem.prototype.gotoFrame = function () {
if(subframeEnabled){
this.currentFrame = this.math.round(this.currentRawFrame*100)/100;
}else{
this.currentFrame = this.math.floor(this.currentRawFrame);
}
this.renderFrame();
};
AnimationItem.prototype.renderFrame = function () {
if(this.isLoaded === false){
return;
}
dataManager.renderFrame(this.animationID,this.currentFrame);
this.renderer.renderFrame(this.currentFrame);
};
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 (pos) {
this.isPaused = true;
this.currentFrame = this.currentRawFrame = pos;
this.playCount = 0;
this.gotoFrame();
};
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.setCurrentRawFrameValue = function(value){
this.currentRawFrame = value;
if (this.currentRawFrame >= this.totalFrames) {
this.currentRawFrame = this.currentRawFrame % this.totalFrames;
if(this.loop === false){
this.goToAndStop(this.totalFrames - 1);
}else{
this.playCount += 1;
if(this.loop !== true){
if(this.playCount == this.loop){
this.goToAndStop(this.totalFrames - 1);
}
}
}
} else if (this.currentRawFrame < 0) {
this.playCount -= 1;
if(this.playCount < 0){
this.playCount = 0;
}
this.currentRawFrame = this.totalFrames + this.currentRawFrame;
}
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.getAssets = function () {
return this.assets;
};