forked from: forked from: ベジェをスムーズにつないでいく
マウスに反応するようにした。
課題→いまいち楽しくないなあ。スクロールするようにしてみようか。
// forked from dalmacija's forked from: ベジェをスムーズにつないでいく
package {
//マウスに反応するようにした。
//課題→いまいち楽しくないなあ。スクロールするようにしてみようか。
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Bitmap;
import flash.display.Stage;
import flash.display.BitmapData;
import flash.display.DisplayObjectContainer;
import flash.display.LineScaleMode;
import flash.display.BlendMode;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.filters.BlurFilter;
public class curve extends Sprite {
var shape1:Shape;
var shape2:Shape;
var bmp:Bitmap;
var curves:Object;
var rotationColor:Number;
var stg:DisplayObjectContainer;
var targetPoint:Point;
var mouseRad:Number;
var mouseRadScale:Number;
public function curve() {
shape1=new Shape();//主線描画用
shape2=new Shape();//補助線描画用
bmp=new Bitmap(new BitmapData(stage.stageWidth,stage.stageHeight,true,0x00000000),"auto",true);//bitmap処理用
shape2.graphics.lineStyle(0, 0x0000ff, .2, true , LineScaleMode.NONE);
addChild(bmp);
addChild(shape2);
addChild(shape1);
curves=new Object();
init();
targetPoint = new Point();
mouseRad=0;
mouseRadScale = 150;
addEventListener(Event.ENTER_FRAME, enterframeHandlr);
stage.addEventListener(MouseEvent.CLICK, mouseclickHandlr);
stage.addEventListener(MouseEvent.MOUSE_MOVE, checkMouse);
}
function init(){
rotationColor=Math.random()*0xffffff;
shape1.graphics.clear();
shape2.graphics.clear();
//bmp.bitmapData=new BitmapData(stg.stage.stageWidth,stg.stage.stageHeight,true,0x00000000);//bitmap処理用
curves.x = Math.random() * (stage.stageWidth - stage.stageWidth * .2) + stage.stageWidth * .1;
curves.y = Math.random() * (stage.stageHeight - stage.stageHeight * .2) + stage.stageHeight * .1;
shape1.graphics.moveTo(curves.x,curves.y);
shape1.graphics.lineStyle(0, rotationColor, .5, true , LineScaleMode.NONE);
curves.before=clone(curves);
curves.x = curves.before.x + Math.random() * stage.stageWidth * .2 - stage.stageWidth * .1;
curves.y = curves.before.y + Math.random() * stage.stageHeight * .2 - stage.stageHeight * .1;
var cx = (curves.before.x + curves.x) / 2;
var cy = (curves.before.y + curves.y) / 2;
var line=new Rectangle(curves.x,curves.y,curves.before.x,curves.before.y);
var turnLi:Point=getCrossLine(line);
var lineL=getLineLength(line);
trace(cx+" "+cy+" "+turnLi.x+" "+lineL);
var cpS=cy-turnLi.x*cx;
curves.ControlX=Math.sqrt(Math.abs(Math.pow(1/turnLi.x,2)))*lineL/2*(Math.round(Math.random())*2-1)+cx;
curves.ControlY=turnLi.x*curves.ControlX+cpS;
trace(curves.ControlX+":"+curves.ControlY);
curves.before=clone(curves);
shape1.graphics.curveTo(curves.ControlX,curves.ControlY,curves.x,curves.y);
shape2.graphics.lineStyle(0, 0x00ff00, .25, true , LineScaleMode.NONE);
shape2.graphics.drawCircle(curves.ControlX,curves.ControlY,5);
}
function update()
{
var vec1=new Object();
vec1.vx=curves.before.x-curves.before.ControlX;
vec1.vy=curves.before.y-curves.before.ControlY;
vec1.vl=Math.sqrt(Math.pow(vec1.vx,2)+Math.pow(vec1.vy,2));
vec1.ux=vec1.vx/vec1.vl;//単位ベクトル
vec1.uy=vec1.vy/vec1.vl;//単位ベクトル
vec1.t=vec1.vy/vec1.vx;
vec1.s=curves.before.y-vec1.t*curves.before.x;
//shape2.graphics.moveTo(0,vec1.s);
//shape2.graphics.lineTo(550,vec1.t*550+vec1.s);
//---
//var cnt=0;
//mouseRad+=.2;
//do{
var mL = getLineLength(new Rectangle(curves.before.x, curves.before.y, targetPoint.x, targetPoint.y));
mouseRad+=0.1;
mouseRadScale = (mL < 50)?(55 - mL)*mL*.1:0;
var tagetpointX=targetPoint.x+Math.cos(mouseRad)*mouseRadScale;
var tagetpointY=targetPoint.y+Math.sin(mouseRad)*mouseRadScale;
var vectpX=tagetpointX-(curves.before.x);//+vec1.vx);
var vectpY=tagetpointY-(curves.before.y);//+vec1.vy);
var vectpL=Math.sqrt(Math.pow(vectpX,2)+Math.pow(vectpY,2));
var vectpUX = vectpX / vectpL;
var vectpUY = vectpY / vectpL;
if (vectpL < 80) return;
var vecBTL = (vectpL < 200)?vectpL:200;
curves.x = curves.before.x + vectpUX * vecBTL + (Math.random() * mouseRadScale/3 - mouseRadScale/6);
curves.y = curves.before.y + vectpUY * vecBTL + (Math.random() * mouseRadScale/3 - mouseRadScale/6);
if(curves.x<0 || curves.x>stage.stageWidth || curves.y<0 || curves.y>stage.stageHeight){rotationColor=Math.random()*0xffffff;}
/*
curves.x=curves.before.x+Math.random()*100-50+vec1.vx;
curves.y=curves.before.y+Math.random()*100-50+vec1.vy;
var vl=Math.sqrt(Math.pow(curves.x-curves.before.x,2)+Math.pow(curves.y-curves.before.y,2));
cnt++;
if(cnt>10000){trace("行き詰った");init();return;}// "+vl+":"+curves.x+":"+curves.y);init();return;}
}while(vl<50 || vl>200 || curves.x<0 || curves.x>550 || curves.y<0 || curves.y>400);
//curves.ControlX=Math.random()*(Number(vec1.vx>0)*450-curves.before.x)+curves.before.x+(Number(vec1.vx>0)*2-1)*100;
curves.ControlX=curves.before.x+vec1.vx;
curves.ControlY=vec1.t*curves.ControlX+vec1.s;
*/
curves.ControlX=curves.before.x+vec1.ux*vecBTL;
curves.ControlY=vec1.t*curves.ControlX+vec1.s;
//if(vecBTL<50){curves.ControlX=curves.x;curves.ControlY=curves.y;}
rotationColor=(rotationColor+7)*int(rotationColor<0xdddddd);
shape1.graphics.lineStyle(0, rotationColor, .5, true , LineScaleMode.NONE);
shape1.graphics.curveTo(curves.ControlX,curves.ControlY,curves.x,curves.y);
shape2.graphics.drawCircle(curves.x,curves.y,2);
shape2.graphics.drawCircle(curves.ControlX,curves.ControlY,5);
shape2.graphics.moveTo(curves.before.x,curves.before.y);
shape2.graphics.lineTo(curves.ControlX,curves.ControlY);
shape2.graphics.lineTo(curves.x,curves.y);
shape2.graphics.lineTo(curves.before.x,curves.before.y);
curves.before=null;
curves.before=clone(curves);
bmp.bitmapData.draw(shape1,null,null,"screen");
bmp.bitmapData.applyFilter(bmp.bitmapData,new Rectangle(0,0,this.width,this.height),new Point(0,0),new BlurFilter(4,4));
}
private function mouseclickHandlr(e:Event)
{
shape1.graphics.clear();
shape2.graphics.clear();
shape1.graphics.moveTo(curves.before.x,curves.before.y);
//init();
}
private function enterframeHandlr(e:Event)
{
update();
}
private function checkMouse(e:MouseEvent)
{
trace(e.currentTarget.mouseX + ":" + e.currentTarget.mouseY);
targetPoint.x = this.mouseX;//e.currentTarget.mouseX;
targetPoint.y = this.mouseY;//e.currentTarget.mouseY;
update();
}
function getLineLength(p:Rectangle):Number
{
var vx=p.x-p.width;
var vy=p.y-p.height;
var vl=Math.sqrt(Math.pow(vx,2)+Math.pow(vy,2));
return vl;
}
function getLine(p:Rectangle):Point
{
var vx=p.x-p.width;
var vy=p.y-p.height;
//var vl=Math.sqrt(Math.pow(vx,2)+Math.pow(vy,2));
var ts=new Point();
ts.x=vy/vx;
ts.y=p.height-ts.x*p.width;
return ts;
}
function getCrossLine(p:Rectangle):Point
{
var vx=p.x-p.width;
var vy=p.y-p.height;
var ts=new Point();
ts.x=-vx/vy;
ts.y=p.y-ts.x*p.x;
return ts;
}
function clone( arg:* ) :*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(arg);
myBA.position = 0;
return( myBA.readObject() );
}
}
}