hybrid-brush-05
...
@author bouze
// forked from bouze's hybrid-brush-04
// forked from bouze's hybrid-brush-03
// forked from bouze's hybrid-brush-02
// forked from bouze's hybrid-brush-01
package
{
import flash.display.GradientType;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
/**
* ...
* @author bouze
*/
[SWF(frameRate="60", backgroundColor="#BBCC99")]
public class Main extends Sprite
{
private var drawLayer:Sprite;
private var particles:Array = new Array();
private var pNum:int = 15;
private var maxDistance:Number = 60;
private var friction:Number = 0.95;
public function Main()
{
init();
}
private function init():void
{
stage.quality = "LOW";
drawLayer = new Sprite();
addChild(drawLayer);
initParticles();
addEventListener(Event.ENTER_FRAME, update);
}
private function initParticles():void
{
var i:int;
for (i = 0; i < pNum; i++)
{
var sp:Sprite = new Sprite();
addChild(sp);
particles[ i ] =
{
x: mouseX,
y: mouseY,
ref: sp,
vx: 0,
vy: 0
};
}
}
private function move():void
{
particles[ 0 ].x = stage.mouseX;
particles[ 0 ].y = stage.mouseY;
var c:Number;
for ( c = 1; c < pNum; c++)
{
var p:Object = particles[ c ];
var pp:Object = particles[ c - 1 ];
p.vx *= friction //- ( pNum - c ) * 0.01;
p.vy *= friction //- ( pNum - c ) * 0.01;
p.x += p.vx;
p.y += p.vy;
var X:Number = p.x - pp.x;
var Y:Number = p.y - pp.y;
var distance:Number = Math.sqrt( X * X + Y * Y );
var angle:Number = Math.atan2( Y, X );
if ( distance > maxDistance )
{
var ox:Number = p.x;
var oy:Number = p.y;
p.x = pp.x + maxDistance * Math.cos( angle );
p.y = pp.y + maxDistance * Math.sin( angle );
p.vx += ( p.x - ox ) * 0.1;
p.vy += ( p.y - oy ) * 0.1;
}
}
}
private function draw():void
{
var i:int;
for (i = 0; i < pNum - 5; i++)
{
var mtrx:Matrix = new Matrix();
mtrx = createLinearGradientMatrix( particles[ i ].x, particles[ i ].y, particles[ i + 4 ].x, particles[ i + 4 ].y)
var g:Graphics = particles[ i ].ref.graphics;
g.clear();
g.beginGradientFill(GradientType.LINEAR, [ 0x660099, 0xFF0066 ], [ 0.3, 0.5 ], [ 0, 255 ], mtrx);
g.moveTo( particles[ i ].x, particles[ i ].y );
//curveThrough3Pts(g, particles[ i ].x, particles[ i ].y, particles[ i + 1 ].x, particles[ i + 1 ].y, particles[ i + 2 ].x, particles[ i + 2 ].y );
//curveThrough3Pts(g, particles[ i + 2 ].x, particles[ i + 2 ].y, particles[ i + 3 ].x, particles[ i + 3 ].y, particles[ i + 4 ].x, particles[ i + 4 ].y );
g.lineTo( particles[ i + 1 ].x, particles[ i + 1 ].y );
g.lineTo( particles[ i + 2 ].x, particles[ i + 2 ].y );
g.lineTo( particles[ i + 3 ].x, particles[ i + 3 ].y );
g.lineTo( particles[ i + 4 ].x, particles[ i + 4 ].y );
g.lineTo( particles[ i + 5 ].x, particles[ i + 5 ].y );
g.lineTo( particles[ i ].x, particles[ i ].y );
}
g.endFill();
}
/**
* 三点を通る二次元曲線
* via 詳説ActionScript 3.0(p.700)
*/
/*
private function curveThrough3Pts
(
g:Graphics,
startX:Number,
startY:Number,
throughX:Number,
throughY:Number,
endX:Number,
endY:Number
):void
{
var controlX:Number = ( 2 * throughX ) - 0.5 * ( startX + endX );
var controlY:Number = ( 2 * throughY ) - 0.5 * ( startY + endY );
g.moveTo( startX, startY );
g.curveTo( controlX, controlY, endX, endY );
}
*/
/**
* Make matrix for linear gradient
* via http://nutsu.com/blog/2009/020921_as_gradient_matrix.html
*/
private function createLinearGradientMatrix( x0:Number, y0:Number, x1:Number, y1:Number ):Matrix
{
var mtrx:Matrix = new Matrix();
mtrx.createGradientBox( 1, 1, 0, -0.5, -0.5 );
var vx:Number = x1 - x0;
var vy:Number = y1 - y0;
var w:Number = Math.sqrt( vx * vx + vy * vy );
var r:Number = Math.atan2( vy, vx );
var cx:Number = ( x0 + x1 ) / 2;
var cy:Number = ( y0 + y1 ) / 2;
mtrx.scale( w, 1 );
mtrx.rotate( r );
mtrx.translate( cx, cy );
return mtrx;
}
private function update(e:Event):void
{
move();
draw();
}
}
}