Actionscript:
-
var circle:Shape = Shape(addChild(new Shape));
-
with(circle.graphics) beginFill(0x000000), drawCircle(0,0,5);
-
-
var bezierPoint:Point = new Point();
-
function bezier(a:Number, x1:Number, y1:Number, x2:Number, y2:Number, x3:Number, y3:Number):void {
-
var b:Number =1-a;
-
var pre1:Number=a*a;
-
var pre2:Number=2*a*b;
-
var pre3:Number=b*b;
-
bezierPoint.x = pre1*x1 + pre2*x2 + pre3*x3;
-
bezierPoint.y = pre1*y1 + pre2*y2 + pre3*y3;
-
}
-
-
var inc:Number = 0;
-
var theta:Number = 0;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
-
graphics.clear();
-
graphics.lineStyle(0,0xFF0000);
-
graphics.moveTo(200,200);
-
graphics.curveTo(mouseX, mouseY, 400, 200);
-
-
theta+= .05;
-
inc = .5 + .5*Math.sin(theta);
-
-
bezier(inc, 200, 200, mouseX, mouseY, 400, 200);
-
circle.x = bezierPoint.x;
-
circle.y = bezierPoint.y;
-
}
The above animates a circle along a quadratic bezier curve. This snippet was written in response to a question spurned by some of the recent bezier posts. I used Math.sin() to animate the circle but you could just as easily use modulus... simply replace lines 25-26 with the following:
Actionscript:
-
inc += .03;
-
inc %= 1;