Skip to content

Instantly share code, notes, and snippets.

@jeremyfromearth
Created February 27, 2011 04:24
Show Gist options
  • Save jeremyfromearth/845899 to your computer and use it in GitHub Desktop.
Save jeremyfromearth/845899 to your computer and use it in GitHub Desktop.
Creates tweens along a bezier path based on semi random radius and angle values using the Eaze tween engine
package makemachine.display.demos
{
import aze.motion.eaze;
import com.bit101.components.*;
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
/**
* Creates tweens along a bezier path based on semi random radius and angle values using the Eaze tween engine
*/
[SWF( backgroundColor="0x222222", frameRate="60", width="620", height="400" )]
public class RandomBezier extends Sprite
{
protected var control:Point;
protected var start:Point;
protected var target:Point;
protected var radius:Number;
protected var radians:Number;
protected var shape:Shape;
protected var hit:Sprite;
protected var button:PushButton;
protected var radiusSlider:VUISlider;
protected var angleSlider:VUISlider;
public function RandomBezier()
{
stage.quality = StageQuality.LOW;
radius = 100;
control = new Point();
start = new Point();
target = new Point();
shape = new Shape();
shape.graphics.beginFill( 0xFF0000 );
shape.graphics.drawCircle( 0, 0, 4 );
shape.graphics.endFill();
shape.x = stage.stageWidth * .5;
shape.y = stage.stageHeight * .5;
addChild( shape );
hit = new Sprite();
hit.x = 75;
hit.graphics.beginFill( 0, 0 );
hit.graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight );
hit.graphics.endFill();
addChild( hit );
radiusSlider = new VUISlider( this, 10, 10, 'Radius', onRadiusSlider );
radiusSlider.minimum = 0;
radiusSlider.maximum = stage.stageHeight * .5;
radiusSlider.height = 200;
radiusSlider.x =
radiusSlider.y = 10;
angleSlider = new VUISlider( this, 35, 10, 'Angle', onAngleSlider );
angleSlider.minimum = 0;
angleSlider.maximum = 360;
angleSlider.x = 40;
angleSlider.y = 10;
angleSlider.height = 200;
new PushButton( this, 10, 230, 'GO!', tween ).width =
new PushButton( this, 10, 265, 'RND', onRandom ).width = 60;
hit.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
addEventListener( Event.ENTER_FRAME, validate );
}
protected function validate( event:Event ):void
{
if( !stage ) return;
if( !stage.stageWidth ) return;
onRandom();
removeEventListener( Event.ENTER_FRAME, validate );
}
/**
* Randomizes the angle of the control point, the radius of the curve & the target position
*/
protected function randomize():void
{
radians =
angleSlider.value = Math.PI * Math.random() * 360 / 180
radius =
radiusSlider.value = Math.random() * radiusSlider.maximum;
target.x = Math.random() * stage.stageWidth;
target.y = Math.random() * stage.stageHeight;
}
/**
* Reset the shape position and start the tween
*/
protected function tween( event:Event = null ):void
{
shape.x = stage.stageWidth * .5;
shape.y = stage.stageHeight * .5;
eaze( shape ).to( 1, { x: [ control.x, target.x ], y: [ control.y, target.y ] } );
}
protected function draw():void
{
var g:Graphics = graphics;
// -- reset the position of the sprite
shape.x = stage.stageWidth * .5;
shape.y = stage.stageHeight * .5;
// -- set the control point x and y
control.x = shape.x + Math.cos( radians ) * radius;
control.y = shape.y + Math.sin( radians ) * radius;
g.clear();
// -- origin
g.lineStyle( 1, 0xFFCC00 );
g.drawCircle( shape.x, shape.y, 5 );
// -- radius
g.lineStyle( 1, 0x00FF00 );
g.drawCircle( shape.x, shape.y, radius );
// -- target
g.lineStyle( 1, 0x0000FF );
g.drawCircle( target.x, target.y, 5 );
// -- control point
g.lineStyle( 1, 0xFF00FF );
g.beginFill( 0xFF00FF );
g.drawCircle( control.x, control.y, 2 );
g.endFill();
// -- control point to origin
g.lineTo( shape.x, shape.y );
// -- bezier path
g.lineStyle( 1, 0x00CCFF );
g.moveTo( shape.x, shape.y );
g.curveTo( control.x, control.y, target.x, target.y );
}
protected function onMouseDown( event:MouseEvent ):void
{
eaze( shape ).killTweens();
target.x = stage.mouseX;
target.y = stage.mouseY;
draw();
}
protected function onRandom( event:Event = null ):void
{
eaze( shape ).killTweens();
randomize();
draw();
}
protected function onRadiusSlider( event:Event ):void
{
radius = radiusSlider.value;
draw();
}
protected function onAngleSlider( event:Event ):void
{
radians = Math.PI * angleSlider.value / 180;
draw();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment