Bezier curve demo
/**
* Copyright Jarvis.weng ( http://wonderfl.net/user/Jarvis.weng )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/c62D
*/
package {
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.geom.Point;
public class BezierCurveDemo extends Sprite {
private static const COLOR_LIST:Vector.<uint> = new <uint>[0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0x00ffff];
private var _ticker:Ticker;
private var _txt:TextField;
private var _anchorPoints:Vector.<Point> = new Vector.<Point>();
private var _bezierPoints:Vector.<Point> = new Vector.<Point>();
private var _currentTime:Number = 0.0;
private var _maxTime:Number = 5.0;
private var _start:Boolean;
public function BezierCurveDemo() {
_txt = new TextField();
_txt.text = "Click the stage to draw a bezier curve(max anchor points == 5)";
_txt.width = stage.stageWidth;
_txt.height = _txt.textHeight + 10;
addChild(_txt);
_ticker = new Ticker();
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}
private function mouseDownHandler(e:MouseEvent):void
{
reset();
_anchorPoints.push(new Point(e.localX, e.localY));
while (_anchorPoints.length > COLOR_LIST.length)
{
_anchorPoints.shift();
}
}
private function enterFrameHandler(e:Event):void
{
advanceTime(_ticker.getTick() / 1000);
}
private function advanceTime(passedTime:Number):void
{
if (!_start)
{
return;
}
_currentTime += passedTime;
var t:Number = _currentTime / _maxTime;
if (t >= 1.0)
{
stop();
return;
}
graphics.clear();
var anchors:Vector.<Point> = _anchorPoints;
var level:int = 0;
while (anchors.length > 1)
{
drawCircles(anchors, COLOR_LIST[level]);
drawLine(anchors, COLOR_LIST[level]);
var tempPoints:Vector.<Point> = new Vector.<Point>;
for (var i:int = 1; i < anchors.length; ++i)
{
var dX:Number = anchors[i-1].x + (anchors[i].x - anchors[i-1].x) * t;
var dY:Number = anchors[i-1].y + (anchors[i].y - anchors[i-1].y) * t;
tempPoints.push(new Point(dX, dY));
}
anchors = tempPoints;
++level;
}
_bezierPoints.push(anchors[0]);
drawCircle(anchors[0], COLOR_LIST[level]);
drawLine(_bezierPoints, COLOR_LIST[level]);
}
private function reset():void
{
_currentTime = 0.0;
_bezierPoints.length = 0;
_start = true;
}
private function stop():void
{
_start = false;
_anchorPoints.length = 0;
}
private function drawLine(points:Vector.<Point>, color:uint):void
{
if (points.length < 2)
{
return;
}
graphics.moveTo(points[0].x, points[0].y);
graphics.lineStyle(2, color);
for (var i:int = 1; i < points.length; ++i)
{
graphics.lineTo(points[i].x, points[i].y);
}
}
private function drawCircles(points:Vector.<Point>, color:uint):void
{
for (var i:int = 0; i < points.length; ++i)
{
drawCircle(points[i], color);
}
}
private function drawCircle(point:Point, color:uint):void
{
graphics.lineStyle(0.1, color);
graphics.beginFill(color);
graphics.drawCircle(point.x, point.y, 5);
graphics.endFill();
}
}
}
import flash.utils.getTimer;
class Ticker
{
private var _currentTime:int;
public function Ticker()
{
reset();
}
public function reset():void
{
_currentTime = getTimer();
}
public function getTick():int
{
var previousTime:int = _currentTime;
_currentTime = getTimer();
return _currentTime - previousTime;
}
}