Update of /cvsroot/dynapi/dynapi2x/src/dynapi/fx
In directory usw-pr-cvs1:/tmp/cvs-serv15575/src/dynapi/fx

Added Files:
        circleanim.js hoveranim.js imganim.js pathanim.js 
        pathanim_glide.js pathanim_slide.js thread.js 
Log Message:
Initial Import == 2.9

--- NEW FILE ---
/*
        DynAPI Distribution
        CircleAnimation Class

        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.

        requires: PathAnimation
*/

// You must add this to the DynAPI library if you want to include it via 
dynapi.library.include('CircleAnimation');
// 
dynapi.library.add('dynapi.fx.CircleAnimation','circleanim.js',['Thread','dynapi.functions']);

function CircleAnimation(dlyr) {
        this.inherit('Thread',dlyr);

        this.offsetX = 0;
        this.offsetY = 0;
        this.playing = false;
        this.radius = 100;
        this.angle = 0;
        this.setAngleIncrement(10);
}
dynapi.setPrototype('CircleAnimation','Thread');
CircleAnimation.prototype.setRadius = function (r) {
        this.hradius = this.vradius = r;
};
CircleAnimation.prototype.setHRadius = function (r) {
        this.hradius = r;
};
CircleAnimation.prototype.setVRadius = function (r) {
        this.vradius = r;
};
CircleAnimation.prototype.setAngle = function (a) {
        this.angle = dynapi.functions.degreeToRadian(a);
};
CircleAnimation.prototype.setAngleIncrement = function (inc) {
        this.angleinc = dynapi.functions.degreeToRadian(inc);
};
CircleAnimation.prototype.playAnimation = function () {
        this.playing = true;
        if (this.dlyr!=null) {
                this.offsetX = this.hradius*Math.cos(this.angle);
                this.offsetY = -this.vradius*Math.sin(this.angle);
                this.baseX = this.dlyr.x-this.offsetX;
                this.baseY = this.dlyr.y+this.offsetY;
                this.dlyr.invokeEvent("circlestart");
        }
        this.start();
};
CircleAnimation.prototype.stopAnimation = function () {
        this.playing = false;
        this.stop();
        if (this.dlyr!=null) this.dlyr.invokeEvent("circlestop");
};
CircleAnimation.prototype.run = function () {
        if (!this.playing || this.dlyr==null) return;   
        this.angle += this.angleinc;
        this.offsetX = this.hradius*Math.cos(this.angle);
        this.offsetY = -this.vradius*Math.sin(this.angle);

        if (this.dlyr!=null) {
                this.dlyr.invokeEvent("circlerun");
                this.dlyr.moveTo(this.baseX+this.offsetX,this.baseY+this.offsetY);
        }
};
CircleAnimation.prototype.reset = function () {
        this.angle = this.offsetX = this.offsetY = 0;
};
CircleAnimation.prototype.generatePath = function(centerX,centerY) {
        if (centerX==null) centerX = this.dlyr!=null? this.dlyr.x : 0;
        if (centerY==null) centerY = this.dlyr!=null? this.dlyr.y : 0;
        var path = [];
        var i = 0;
/*      for (var a=this.angle;a<=this.angle+Math.PI*2;a+=this.angleinc) {
                path[i] = Math.round(centerX + this.hradius*Math.cos(a));
                path[i+1] = Math.round(centerY - this.vradius*Math.sin(a));
                i+=2;
        }*/

        if (this.angleinc>0)
                for (var a=this.angle;a<=this.angle+Math.PI*2;a+=this.angleinc) {
                        path[i] = Math.round(centerX + this.hradius*Math.cos(a));
                        path[i+1] = Math.round(centerY - this.vradius*Math.sin(a));
                        i+=2;
                }
        else
                for (var a=this.angle;a>=this.angle-Math.PI*2;a+=this.angleinc) {
                        path[i] = Math.round(centerX + this.hradius*Math.cos(a));
                        path[i+1] = Math.round(centerY - this.vradius*Math.sin(a));
                        i+=2;
                }
        return path;
};

--- NEW FILE ---
/*
        DynAPI Distribution
        HoverAnimation Class

        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.

        requires: PathAnimation
*/

// You must add this to the DynAPI library if you want to include it via 
dynapi.library.include('HoverAnimation');
// dynapi.library.add('HoverAnimation','hoveranim.js','Thread','dynapi.fx');

function HoverAnimation(dlyr) {
        this.Thread = Thread;
        this.Thread(dlyr);

        this.offsetX = 0;
        this.offsetY = 0;
        this.playing = false;
        this.amplitude = 100;
        this.angle = 0;
        this.setAngleIncrement(10);
}
HoverAnimation.prototype = new Thread;
HoverAnimation.prototype.setAmplitude = function (amp) {
        this.amplitude = amp;
};
HoverAnimation.prototype.setAngle = function (a) {
        this.angle = PathAnimation.degreeToRadian(a);
};
HoverAnimation.prototype.setAngleIncrement = function (inc) {
        this.angleinc = PathAnimation.degreeToRadian(inc);
};
HoverAnimation.prototype.playAnimation = function () {
        this.playing = true;
        if (this.dlyr!=null) {
                this.offsetX = 0;
                this.offsetY = this.amplitude*Math.sin(this.angle);
                this.baseX = this.dlyr.x;
                this.baseY = this.dlyr.y+this.offsetY;
                this.dlyr.invokeEvent("hoverstart");
        }
        this.start();
};
HoverAnimation.prototype.stopAnimation = function () {
        this.playing = false;
        this.stop();
        if (this.dlyr!=null) this.dlyr.invokeEvent("hoverstop");
};
HoverAnimation.prototype.run = function () {
        if (!this.playing || this.dlyr==null) return;
        this.angle += this.angleinc;
        this.offsetX = 0;
        this.offsetY = this.amplitude*Math.sin(this.angle);
        if (this.dlyr!=null) {
                this.dlyr.invokeEvent("hoverrun");
                this.dlyr.moveTo(this.baseX+this.offsetX,this.baseY+this.offsetY);
        }
};
HoverAnimation.prototype.reset = function () {
        this.angle = this.offsetX = this.offsetY = 0;
};

HoverAnimation.prototype.generatePath = function(centerX,centerY) {
        // to do
};

--- NEW FILE ---
/*
        DynAPI Distribution
        ImageAnimation Class

        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.

        requires: Thread
*/

function ImageAnimation(dynimage) {
        this.Thread = Thread;
        this.Thread(dynimage);

        this.imgAnim = new Array();
        this.imgAnim.playing = null;
}
ImageAnimation.prototype = new Thread;
ImageAnimation.prototype.addAnimation = function (imgArray) {
        var animNum = this.imgAnim.length;
        this.imgAnim[animNum] = imgArray;
        this.imgAnim[animNum].loops = false;
        this.imgAnim[animNum].resets = false;
        this.imgAnim[animNum].frame = 0;
        this.imgAnim[animNum].playing = true;
        this.imgAnim[animNum].direction = 0;
        this.imgAnim[animNum].alternates = false;
        return animNum;
};
ImageAnimation.prototype.getFrame = function (animNum,frameNum) {
        return this.imgAnim[animNum][frameNum];
};

ImageAnimation.prototype.setLoops = function (animNum,loop) {
        this.imgAnim[animNum].loops = loop;
};
ImageAnimation.prototype.setResets = function (animNum) {
        this.imgAnim[animNum].resets = true;
};
ImageAnimation.prototype.setAlternates = function (animNum,alt) {
        this.imgAnim[animNum].loops = true;
        this.imgAnim[animNum].alternates = alt;
};

ImageAnimation.prototype.playAnimation = function (animNum) {
        if (animNum!=null && this.imgAnim.playing!=animNum) {
                this.playing = true;
                this.imgAnim.playing = animNum;
                if (this.dlyr!=null) this.dlyr.invokeEvent("imgstart");
                this.start();
        }
};
ImageAnimation.prototype.stopAnimation = function () {
        this.imgAnim.playing = null;
        this.playing = false;
        this.stop();
        if (this.dlyr!=null) this.dlyr.invokeEvent("imgrun");
};
ImageAnimation.prototype.run = function () {
        if (!this.playing || this.imgAnim.playing==null || this.dlyr==null) return;
        
        var anim = this.imgAnim[this.imgAnim.playing];
        
        if (anim.frame==0 && this.img==anim[anim.frame]) {
                anim.frame++;   // skip 1st frame if same
        }
        if (this.dlyr!=null) this.dlyr.invokeEvent("imgrun");
        this.dlyr.setImage(anim[anim.frame]);
        
        if (anim.frame>=anim.length-1) {
                if (anim.loops) {
                        if (anim.alternates && anim.direction==0 && 
anim.frame==anim.length-1) {
                                anim.direction = 1; 
                                anim.frame = anim.length-2;
                        }
                        else anim.frame = 0;
                }
                else if (anim.resets) {
                        anim.frame = 0;
                        this.stop()
                }
                else {
                        this.stop()
                }
        }
        else {
                if (anim.alternates) {
                        if (anim.frame==0 && anim.direction==1) {
                                anim.direction = 0;
                                anim.frame = 1;
                        }
                        else if (anim.direction==0) {
                                anim.frame++;
                        }
                        else if (anim.direction==1) {
                                anim.frame--;
                        }
                }
                else {
                        anim.frame++;
                }
        }
};

--- NEW FILE ---
/*
        DynAPI Distribution
        PathAnimation Class

        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.

        requires: dynapi.fx.Thread
*/

function PathAnimation(dlyr) {
        this.inherit('Thread',dlyr);
        this.paths = new Array();
        this.pathPlaying = null;
}
var p = dynapi.setPrototype('PathAnimation','Thread');

p.add = function (path, loops, resets) {
        var n = this.paths.length;
        this.paths[n] = path;
        this.setLoops(n,loops);
        this.setResets(n,resets);
        this.setFrame(n,0);
        return n;
};
p.setLoops = function (n, loops) {
        this.paths[n].loops = (loops);
};
p.setResets = function (n, resets) {
        this.paths[n].resets = (resets);
};
p.setFrame = function (n, frame) {
        this.paths[n].frame = frame;
};
p.play = function (noevt) {
        if (!this.playing) {
                this.pathPlaying = null;
                if (arguments[0]==null) arguments[0] = 0;
                if (typeof(arguments[0]) == "number") {
                        this.pathPlaying = this.paths[arguments[0]];
                }
                else if (typeof(arguments[0]) == "object") {  
                        this.pathPlaying = arguments[0];
                        this.pathPlaying.loops = arguments[1]||false;
                        this.pathPlaying.resets = arguments[2]||false;
                        this.pathPlaying.frame = 0;
                }
                this.playing = true;
                if (this.dlyr!=null && noevt!=false) 
this.dlyr.invokeEvent("pathstart");
                this.start();
        }
};
p._Thread_stop = Thread.prototype.stop;
p.stop = function (noevt) {
        if (this.pathPlaying && this.pathPlaying.resets && !this.cancelThread && 
this.dlyr!=null) this.dlyr.setLocation(this.pathPlaying[0],this.pathPlaying[1]);
        this._Thread_stop();
        this.pathPlaying = null;
        this.playing = false;
        if (this.dlyr!=null && noevt!=false) this.dlyr.invokeEvent("pathcancel");
};
p.run = function () {
        if (!this.playing || this.pathPlaying==null) return;
        var anim = this.pathPlaying;
        if (anim.frame>=anim.length/2) {
                if (anim.loops) {
                        anim.frame = 0;
                }
                else if (anim.resets) {
                        anim.frame = 0;
                        if (this.dlyr!=null) this.dlyr.setLocation(anim[0],anim[1]);
                        this.stop();
                        this.dlyr.invokeEvent("pathfinish");
                        return;
                }
                else {
                        anim.frame = 0;
                        this.stop();
                        this.dlyr.invokeEvent("pathfinish");
                        return;
                }
        }
        if (anim.frame==0 && (this.dlyr!=null && this.dlyr.x==anim[0] && 
this.dlyr.y==anim[1])) {
                anim.frame += 1;
        }
        this.newX = anim[anim.frame*2];
        this.newY = anim[anim.frame*2+1];
        
        if (this.dlyr!=null) {
                this.dlyr.invokeEvent("pathrun");
                this.dlyr.setLocation(this.newX,this.newY);
        }
        anim.frame++;
};

// Path Functions

// Combines separate [x1,x2],[y1,y2] arrays into a path array [x1,y1,x2,y2]
PathAnimation.interlace = function(x,y) {
        var l = Math.max(x.length,y.length);
        var a = new Array(l*2);
        for (var i=0; i<l; i++) {
                a[i*2] = x[i];
                a[i*2+1] = y[i];
        }
        return a;
};
// Returns correct angle in radians between 2 points
PathAnimation.getNormalizedAngle = function(x1,y1,x2,y2) {
        var distx = Math.abs(x1-x2);
        var disty = Math.abs(y1-y2);
        if (distx==0 && disty==0) angle = 0;
        else if (distx==0) angle = Math.PI/2;
        else angle = Math.atan(disty/distx);
        if (x1<x2) {
                if (y1<y2) angle = Math.PI*2-angle;
        }
        else {
                if (y1<y2) angle = Math.PI+angle;
                else angle = Math.PI-angle;
        }
        return angle;
};

--- NEW FILE ---
/*
        DynAPI Distribution
        Glide Animation Extension

        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.

        requires: PathAnimation
*/

// To use on a DynLayer:
// dlyr.addMethod("glideTo",PathAnimation.glideTo);
// dlyr.addMethod("glideStop",PathAnimation.glideStop);
// dlyr.glideTo(x,y,"slow","slow",5,10);
// dlyr.glideStop();

// Or attach to all DynLayers or another class like this:
// DynLayer.prototype.glideTo = PathAnimation.glideTo;
// DynLayer.prototype.glideStop = PathAnimation.glideStop;

// These will invoke "pathstart", "pathrun", "pathfinish" and "pathcancel" events on 
the dynlayer

PathAnimation.glideTo = function(x2,y2,angleinc,ms,startSpeed,endSpeed) {
        if (!this._pathanim) this._pathanim = new PathAnimation(this);
        if (!ms) ms = 20;
        if (x2==null) x2 = this.x;
        if (y2==null) y2 = this.y;
        if (this.x==x2 && this.y==y2) return;
        this._pathanim.sleep(ms);
        this._pathanim.play( 
GlideAnimation(this.x,this.y,x2,y2,angleinc,startSpeed,endSpeed) );
};
PathAnimation.glideStop = function () {
        if (this._pathanim) this._pathanim.stop();
};

function GlideAnimation(x1,y1,x2,y2,angleinc,startSpeed,endSpeed) {
        var normAngle = PathAnimation.getNormalizedAngle(x1,y1,x2,y2);
        var distx = x2-x1;
        var disty = y2-y1;
        var distance = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2));
        angleinc = (angleinc==null)? 7 : Math.abs(angleinc);
        
        // a terrible mess but it works
        var r = 1;
        if (startSpeed == "fast") {
                var centerX = x1;
                var centerY = y1;
                var centerX2 = x2;
                var centerY2 = y2;
                startAngle = 0;
                endAngle = 90;
                if (endSpeed=="fast") distance = distance/2;
        }
        else {
                startAngle = -90;
                endAngle = 0;
                if (endSpeed == "fast") {
                        var centerX = x1+distx;
                        var centerY = y1+disty;
                }
                else {  // default slow,slow
                        var centerX2 = x2-distx/2;
                        var centerY2 = y2-disty/2;
                        distance = distance/2;
                        var centerX = x1+distx/2;
                        var centerY = y1+disty/2;
                        r = -1;
                }
        }

        var i,d,x,y,dx,dy,path=[];
        for (var a=startAngle; a<endAngle; a+=angleinc) {
                i = path.length;
                d = distance*Math.sin(a*Math.PI/180);
                path[i] = Math.round(centerX + d*Math.cos(normAngle));
                path[i+1] = Math.round(centerY - d*Math.sin(normAngle));
        }
        if (startSpeed==endSpeed) {
                for (var a=endAngle; a<endAngle+90; a+=angleinc) {
                        i = path.length;
                        d = distance*Math.sin(a*Math.PI/180);
                        path[i] = Math.round(centerX2 - r*d*Math.cos(normAngle));
                        path[i+1] = Math.round(centerY2 + r*d*Math.sin(normAngle));
                }
        }
        
        var l = path.length;
        if (path[l-2] != x2 && path[l-1]!=y2) {
                path[l] = x2;
                path[l+1] = y2;
        }
        return path;
};

PathAnimation.glide = GlideAnimation;

--- NEW FILE ---
/*
        DynAPI Distribution
        Slide Animation Extension

        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.

        requires: PathAnimation
*/

// To use on a DynLayer:
// dlyr.setMethod("slideTo",PathAnimation.slideTo);  // will overwrite the built in 
DynLayer.slideTo()
// dlyr.setMethod("slideStop",PathAnimation.slideStop);
// dlyr.slideTo(x,y,5,10);
// dlyr.slideStop();

// Or attach to all DynLayers or another class like this:
// DynLayer.prototype.glideTo = PathAnimation.glideTo;
// DynLayer.prototype.glideStop = PathAnimation.glideStop;

// These will invoke "pathstart", "pathrun", "pathfinish" and "pathcancel" events on 
the dynlayer

PathAnimation.slideTo = function(x,y,inc,ms) {
        if (!this._pathanim) this._pathanim = new PathAnimation(this);
        if (!ms) ms = 20;
        if (!inc) inc = 10;
        if (x==null) x = this.x;
        if (y==null) y = this.y;
        this._pathanim.sleep(ms);
        this._pathanim.play( SlideAnimation(this.x,this.y, x,y, inc) );
};
PathAnimation.slideStop = function () {
        if (this._pathanim) this._pathanim.stop();
};

// Generates a path between 2 points, stepping inc pixels at a time
function SlideAnimation(x1,y1,x2,y2,inc) {
        if (x2==null) x2 = x1;
        if (y2==null) y2 = y1;
        var distx = x2-x1;
        var disty = y2-y1;
        if (x1==x2 && y1==y2) return;
        var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/(inc||10)-1;
        var dx = distx/num;
        var dy = disty/num;
        var path = [];
        var x = x1;
        var y = y1;
        for (var i=0;i<=num;i++) {
                x += dx;
                y += dy;
                path[i*2] = Math.round(x);
                path[i*2+1] = Math.round(y);
        }
        if (path[i*2-2] != x2 || path[i*2-1] != y2) {
                path[i*2] = x2;
                path[i*2+1] = y2;
        }
        return path;
};
PathAnimation.line = SlideAnimation;

// a useful functions in some cases but not needed for slide

// Generates a path between 2 points in N steps
/*PathAnimation.lineN = function(x1,y1,x2,y2,N) {
        if (N==0) return [];
        var dx = (x2 == x1)? 0 : (x2 - x1)/N;
        var dy = (y2 == y1)? 0 : (y2 - y1)/N;
        var path = new Array();
        for (var i=0;i<=N;i++) {
                path[i*2] = Math.round(x1 + i*dx);
                path[i*2+1] = Math.round(y1 + i*dy);
        }
        return path;
};*/


--- NEW FILE ---
/*
        DynAPI Distribution
        Thread class

        The DynAPI Distribution is distributed under the terms of the GNU LGPL license.

        requires: dynapi.api.DynLayer
*/

function Thread(dlyr) {
        // Inherit from object. Provides unique ID
        this.inherit('DynObject');
        this.setDynLayer(dlyr);
}
var p = dynapi.setPrototype('Thread','DynObject');
p.active = false;
p.interval = 50;
p.cancelThread = false;
p.sleep = function (ms) {
        this.interval = Math.abs(parseInt(ms));
        if (this.active) {
                this.stop();
                setTimeout(this+'.start()',this.interval+1);
        }
};
p.setFPS = function (fps) {
        this.sleep(Math.floor(1000/fps));
};
p.cancel = function () {
        this.cancelThread = true;
        this.stop();
};
p.start = function () {
        if (!this.active) {
                this.active = true;
                if (!this.cancelThread) this.timer = 
setInterval(this+'.run()',this.interval);
        }
};
p.run = dynapi.functions.Null; // overwrite run
p.stop = function () {
        this.active = false;
        if (!this.cancelThread && this.timer) {
                clearInterval(this.timer);
                delete this.timer;
        }
};
p.setDynLayer = function (dlyr) {
        this.dlyr = dlyr;
};
p.getDynLayer = function () {
        return this.dlyr;
};


_______________________________________________
Dynapi-CVS mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dynapi-cvs

Reply via email to