Painter 2
@author Saqoosha
/**
* Copyright Saqoosha ( http://wonderfl.net/user/Saqoosha )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/onHS
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.geom.Point;
/**
* @author Saqoosha
*/
[SWF(backgroundColor="#000000", frameRate="60", width="475", height="475")]
public class Painter2 extends Sprite
{
private var _canvasData:BitmapData = new BitmapData(475, 475, false, 0xffffff);
private var _canvas:Bitmap = new Bitmap(_canvasData);
private var _stroke:Shape = new Shape();
private var _prev:Point = new Point();
public function Painter2()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
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);
_prev.x = event.stageX;
_prev.y = event.stageY;
}
private function _mouseMoveHandler(event:MouseEvent):void
{
_stroke.graphics.clear();
_stroke.graphics.lineStyle(5, 0xff0000, 0.5);
_stroke.graphics.moveTo(_prev.x, _prev.y);
_stroke.graphics.lineTo(event.stageX, event.stageY);
_canvasData.draw(_stroke);
_prev.x = event.stageX;
_prev.y = event.stageY;
}
private function _mouseUpHandler(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler);
}
}
}