Painter 5
/**
* Copyright Saqoosha ( http://wonderfl.net/user/Saqoosha )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/aQem
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.system.LoaderContext;
/**
* @author Saqoosha
*/
[SWF(backgroundColor="#000000", frameRate="60", width="475", height="475")]
public class Painter5 extends Sprite
{
private var _canvasData:BitmapData = new BitmapData(475, 475, false, 0xffffff);
private var _canvas:Bitmap = new Bitmap(_canvasData);
private var _brush:Shape = new Shape();
private var _points:Vector.<Point> = new Vector.<Point>();
private var _length:Number;
private static const SPACING:Number = 5;
public function Painter5()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _loadCompleteHandler);
loader.load(new URLRequest('http://saqoo.sh/a/labs/wonderfl/brush4.png'), new LoaderContext(true));
}
private function _loadCompleteHandler(event:Event):void
{
var texture:BitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
var w:Number = texture.width;
var h:Number = texture.height;
var b:BitmapData = new BitmapData(w, h, true, 0xff458fff);
b.copyChannel(texture, texture.rect, new Point(), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
var mtx:Matrix = new Matrix();
mtx.translate(-w / 2, -h / 2);
_brush.graphics.clear();
_brush.graphics.beginBitmapFill(b, mtx, false, true);
_brush.graphics.drawRect(-w / 2, -h / 2, w, h);
_brush.graphics.endFill();
_brush.scaleX = _brush.scaleY = 0.4;
stage.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDownHandler);
addChild(_canvas);
}
private function _mouseDownHandler(event:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler);
_points.length = 0;
_length = 0;
}
private function _mouseMoveHandler(event:MouseEvent):void
{
_points.push(new Point(event.stageX, event.stageY));
switch (_points.length)
{
case 3:
{
_drawCurve(_points[0], _points[0], _points[1], _points[2]);
break;
}
case 4:
{
_drawCurve(_points[0], _points[1], _points[2], _points[3]);
_points.shift();
break;
}
}
}
private function _mouseUpHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler);
}
private function _drawCurve(p0:Point, p1:Point, p2:Point, p3:Point):void
{
var curve:CatmullRom = new CatmullRom(p0, p1, p2, p3);
var n:int = Point.distance(p1, p2);
var prev:Point = p1.clone();
var p:Point = new Point();
var l:Number = _length - Math.floor(_length / SPACING) * SPACING;
for (var i:int = 1; i <= n; i++)
{
var t:Number = i / n;
curve.getPointAt(t, p);
var d:Number = Point.distance(prev, p);
_length += d;
l += d;
if (l >= SPACING)
{
_brush.x = p.x;
_brush.y = p.y;
_brush.rotation = Math.random() * 360;
_canvasData.draw(_brush, _brush.transform.matrix, null, null, null, true);
l -= SPACING;
}
prev.x = p.x;
prev.y = p.y;
}
}
}
}
import flash.display.Graphics;
import flash.geom.Point;
class CatmullRom
{
public var p0:Point;
public var p1:Point;
public var p2:Point;
public var p3:Point;
public function CatmullRom(p0:Point = null, p1:Point = null, p2:Point = null, p3:Point = null)
{
this.p0 = p0 || new Point();
this.p1 = p1 || new Point();
this.p2 = p2 || new Point();
this.p3 = p3 || new Point();
}
public function getPointAt(t:Number, out:Point = null):Point
{
out ||= new Point();
var t2:Number = t * t;
var t3:Number = t2 * t;
var a:Number = -t3 + 2 * t2 - t;
var b:Number = 3 * t3 - 5 * t2 + 2;
var c:Number = -3 * t3 + 4 * t2 + t;
var d:Number = t3 - t2;
out.x = (p0.x * a + p1.x * b + p2.x * c + p3.x * d) * 0.5;
out.y = (p0.y * a + p1.y * b + p2.y * c + p3.y * d) * 0.5;
return out;
}
public function draw(graphics:Graphics, numSegments:int = 50):void
{
var p:Point = new Point();
for (var i:int = 1; i <= numSegments; ++i)
{
getPointAt(i / numSegments, p);
graphics.lineTo(p.x, p.y);
}
}
}