forked from: curveTo とかベジェとか
// forked from soundkitchen's curveTo とかベジェとか
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import caurina.transitions.Equations;
import caurina.transitions.Tweener;
import caurina.transitions.properties.CurveModifiers;
[SWF(frameRate=30, width=465, height=465, backgroundColor=0xffffff)]
public class Main extends Sprite
{
private var points:Vector.<Sprite>;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, initialize);
}
private function initialize(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, initialize);
CurveModifiers.init();
points = new Vector.<Sprite>();
var info:TextField = new TextField();
info.text = "click で作って、 drag で動かす。\n要らなくなったら shift + click で消せるよ。\nスペースでラインをなぞります。";
info.autoSize = TextFieldAutoSize.LEFT;
info.y = stage.stageHeight - info.textHeight;
addChild(info);
addEventListener(Event.ENTER_FRAME, step);
stage.addEventListener(MouseEvent.CLICK, stageClickHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, stageKeyDownHandler);
}
private function step(evt:Event):void
{
var i:uint,
l:uint,
g:Graphics,
ps:Sprite,
cs:Sprite,
pp:Point,
cp:Point;
g = graphics;
g.clear();
l = points.length;
for (i=1; i<l; i++)
{
ps = points[i-1];
cs = points[i];
g.lineStyle(1, 0xd8d8d8);
g.moveTo(ps.x, ps.y);
g.lineTo(cs.x, cs.y);
cp = new Point();
cp.x = ps.x + (cs.x - ps.x) / 2;
cp.y = ps.y + (cs.y - ps.y) / 2;
g.lineStyle(1, 0x4c4c4c);
if (pp)
{
g.moveTo(pp.x, pp.y);
g.curveTo(ps.x, ps.y, cp.x, cp.y);
}
else
{
g.moveTo(ps.x, ps.y);
g.lineTo(cp.x, cp.y);
}
pp = cp;
}
if (cs)
{
g.moveTo(cp.x, cp.y);
g.lineTo(cs.x, cs.y);
}
}
private function leave(s:Sprite):void
{
var i:uint;
removeChild(s);
i = points.indexOf(s);
points.splice(i, 1);
s.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
s.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
s.removeEventListener(MouseEvent.CLICK, clickHandler);
}
private function mouseDownHandler(evt:MouseEvent):void
{
var s:Sprite = Sprite(evt.target);
if (evt.shiftKey)
{
leave(s);
}
else
{
s.startDrag();
}
}
private function mouseUpHandler(evt:MouseEvent):void
{
stopDrag();
}
private function clickHandler(evt:MouseEvent):void
{
evt.stopPropagation();
}
private function stageClickHandler(evt:MouseEvent):void
{
var s:Sprite,
g:Graphics;
s = new Sprite();
s.x = mouseX;
s.y = mouseY;
s.buttonMode = true;
s.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
s.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
s.addEventListener(MouseEvent.CLICK, clickHandler);
g = s.graphics;
g.beginFill(0x000000);
g.drawCircle(0, 0, 3);
g.endFill();
points.push(s);
addChild(s);
}
private function stageKeyDownHandler(evt:KeyboardEvent):void
{
if (evt.charCode != 32 || points.length < 3) return;
var p:Sprite = points[0];
var m:Sprite = new Sprite();
var g:Graphics = m.graphics;
g.beginFill(0xFF0000);
g.drawCircle(0, 0, 10);
g.endFill();
m.x = p.x;
m.y = p.y;
var a:Array = [];
var i:int, l:int = points.length - 1;
for (i=0; i<l; i++) {
p = points[i];
a.push({
x: p.x,
y: p.y
});
}
trace(i);
p = points[i];
Tweener.addTween(m, {
x: p.x,
y: p.y,
_bezier: a,
time: 5,
transition: Equations.easeInOutSine,
onStart: function ():void { addChild(m); },
onComplete: function ():void { removeChild(m); }
});
}
}
}