
Код AS1/AS2:
// author: Ivan Dembicki
Path = function () {
this.path_points=arguments.toString(), this.path_length=0;
var x_array = [], y_array = [], k = 0, i, len = arguments.length, x0 = arguments[0] || 0, y0 = arguments[1] || 0, x1, y1, x2, y2, ln, o, a1, a2, a3, a4, a, b, c, d, e, a2t, sa;
var s = Math.sqrt, l = Math.log, p = Math.pow;
for (i=2; i<len; i += 4) {
x1=arguments[i] || 0, y1=arguments[i+1] || 0, x2=arguments[i+2] || 0, y2=arguments[i+3] || 0, ln=this["ln"+k++]=[{_y:y0, _x:x0}, {_y:y1, _x:x1}, {_y:y2, _x:x2}];
// ASSetPropFlags(this, "ln"+(k-1), 7, 1)
o=ln[3]={}, a1=o.a1=x0-2*x1+x2, a2=o.a2=y0-2*y1+y2, a3=o.a3=x0-x1, a4=o.a4=y0-y1, a=o.a=4*(a1*a1+a2*a2), b=o.b=-8*(a1*a3+a2*a4), c=o.c=4*(a3*a3+a4*a4), e=o.e=Math.sqrt(c), d=s(c+b+a), sa=s(a), a2t=a*2, ln[4]=(2*sa*(d*(b+a2t)-e*b)+(b*b-4*a*c)*(l(2*e+b/sa)-l(2*d+(b+a2t)/sa)))/(8*p(a, (3/2)));
this.path_length += ln[4];
x0=x2, y0=y2;
}
this.segments = k--;
_root.lineStyle(0, 0, 30), _root.moveTo(this.ln0[0]._x, this.ln0[0]._y), _root.curveTo(this.ln0[1]._x, this.ln0[1]._y, this.ln0[2]._x, this.ln0[2]._y), _root.curveTo(this.ln1[1]._x, this.ln1[1]._y, this.ln1[2]._x, this.ln1[2]._y);
_root.curveTo(this.ln2[1]._x,this.ln2[1]._y,this.ln2[2]._x,this.ln2[2]._y);
};
tmp = Path.prototype={};
tmp.getPointByPosition = function(poz) {
if (poz<0 || poz>this.path_length) {
return {_x:this.ln0[0]._x, _y:this.ln0[0]._y};
}
if (this.segments<1) {
return false;
}
var i = 0, ln, len = 0, ff = 0;
for (i; i<=this.segments; i++) {
ln=this["ln"+i], len += ln[4];
if (len>poz) {
ff = (poz-(len-ln[4]))/ln[4];
break;
}
}
var s = Math.sqrt, l = Math.log, p = Math.pow, abs = Math.abs;
var fn = function (ff) {
var o = ln[3], a1 = o.a1, a2 = o.a2, a3 = o.a3, a4 = o.a4, a = o.a, b = o.b, c = o.c, e = o.e, i = 1, st = 1, f_l = ln[4], t_l = ff*f_l, max_i = 100, d, sa, a2i;
while (max_i--) {
d=s(c+i*(b+a*i)), sa=s(a), a2i=a*2*i, f_l=(2*sa*(d*(b+a2i)-e*b)+(b*b-4*a*c)*(l(2*e+b/sa)-l(2*d+(b+a2i)/sa)))/(8*p(a, (3/2)));
if (abs(f_l-t_l)<.000001) {
return i;
}
st /= 2, i += f_l<t_l ? st : f_l>t_l ? -st : 0;
}
return i;
};
var f = fn(ff), p0 = ln[0], p1 = ln[1], p2 = ln[2], e = 1-f, ee = e*e, ff = f*f, b = 2*f*e;
return {_x:p2._x*ff+p1._x*b+p0._x*ee, _y:p2._y*ff+p1._y*b+p0._y*ee};
};
delete tmp;
//EXAMPLE
_root.createEmptyMovieClip("mc",0);
_root.mc.lineStyle(5,0xFF0000,100);
_root.mc.lineTo(.2,0);
_root.mc.tween_path = new Path(0, 0, 100, 0, 100, 100, 100, 200, 450, 380, 0, 300, 0, 0);
_root.mc.speed = _root.mc.path_position=0;
_root.mc.onEnterFrame = function() {
if (this.path_position>this.tween_path.path_length) {
this.path_position = 0;
}
this.path_position += this.speed += .01;
var poz = this.tween_path.getPointByPosition(this.path_position);
this._x=poz._x, this._y=poz._y;
};