Created
April 25, 2011 06:04
-
-
Save claus/940219 to your computer and use it in GitHub Desktop.
as3potrace demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import com.bit101.components.CheckBox; | |
import com.bit101.components.ComboBox; | |
import com.bit101.components.PushButton; | |
import com.powerflasher.as3potrace.POTrace; | |
import com.powerflasher.as3potrace.backend.GraphicsDataBackend; | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import flash.display.CapsStyle; | |
import flash.display.GraphicsEndFill; | |
import flash.display.GraphicsSolidFill; | |
import flash.display.GraphicsStroke; | |
import flash.display.IGraphicsData; | |
import flash.display.JointStyle; | |
import flash.display.LineScaleMode; | |
import flash.display.Loader; | |
import flash.display.LoaderInfo; | |
import flash.display.PixelSnapping; | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.events.MouseEvent; | |
import flash.filters.BitmapFilter; | |
import flash.filters.BitmapFilterQuality; | |
import flash.filters.BlurFilter; | |
import flash.filters.ColorMatrixFilter; | |
import flash.geom.Matrix; | |
import flash.geom.Point; | |
import flash.net.FileFilter; | |
import flash.net.FileReference; | |
import flash.utils.ByteArray; | |
[SWF(backgroundColor="#FFFFFF", frameRate="31", width="475", height="475")] | |
public class Main extends Sprite | |
{ | |
[Embed(source="../bitmaps/Powerflasher.png")] | |
public var Powerflasher:Class; | |
[Embed(source="../bitmaps/TheFlash.jpg")] | |
public var TheFlash:Class; | |
[Embed(source="../bitmaps/Bulldog.jpg")] | |
public var Bulldog:Class; | |
[Embed(source="../bitmaps/Baby.jpg")] | |
public var Baby:Class; | |
private var canvas:Sprite; | |
private var imageContainer:Sprite; | |
private var comboBoxImage:ComboBox; | |
private var comboBoxZoom:ComboBox; | |
private var dragStartX:Number; | |
private var dragStartY:Number; | |
private var imageX:Number; | |
private var imageY:Number; | |
private var showBitmap:Boolean = true; | |
public function Main() | |
{ | |
stage.align = StageAlign.TOP_LEFT; | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
imageContainer = new Sprite(); | |
var canvasMask:Sprite = new Sprite(); | |
canvasMask.graphics.beginFill(0xffffff, 0); | |
canvasMask.graphics.drawRect(-1, -1, stage.stageWidth - 18, stage.stageHeight - 58); | |
canvasMask.graphics.endFill(); | |
canvas = new Sprite(); | |
canvas.x = 10; | |
canvas.y = 40; | |
canvas.addChild(imageContainer); | |
canvas.addChild(canvasMask); | |
canvas.mask = canvasMask; | |
addChild(canvas); | |
var canvasHitArea:Sprite = new Sprite(); | |
canvasHitArea.graphics.beginFill(0xffffff, 0); | |
canvasHitArea.graphics.drawRect(0, 0, stage.stageWidth - 20, stage.stageHeight - 60); | |
canvasHitArea.graphics.endFill(); | |
canvasHitArea.x = 10; | |
canvasHitArea.y = 40; | |
canvasHitArea.buttonMode = true; | |
canvasHitArea.addEventListener(MouseEvent.MOUSE_DOWN, canvasMouseDownHandler); | |
addChild(canvasHitArea); | |
comboBoxImage = new ComboBox(this, 10, 10, "Select image", [ | |
"Powerflasher", | |
"TheFlash", | |
"Bulldog", | |
"Baby" | |
]); | |
comboBoxImage.addEventListener(Event.SELECT, function(event:Event):void { | |
var ImageClass:Class; | |
switch(String(comboBoxImage.selectedItem)) { | |
case "Powerflasher": ImageClass = Powerflasher; break; | |
case "TheFlash": ImageClass = TheFlash; break; | |
case "Bulldog": ImageClass = Bulldog; break; | |
case "Baby": ImageClass = Baby; break; | |
} | |
if(ImageClass != null) { | |
prepareImage(new ImageClass() as Bitmap); | |
} | |
}); | |
comboBoxImage.numVisibleItems = 4; | |
comboBoxImage.selectedIndex = 0; | |
var checkBox:CheckBox = new CheckBox(this, 230, 15, "Hide bitmap", function():void { | |
showBitmap = checkBox.selected; | |
if(imageContainer.numChildren > 0) { | |
imageContainer.getChildAt(0).visible = showBitmap; | |
} | |
}); | |
checkBox.selected = true; | |
comboBoxZoom = new ComboBox(this, stage.stageWidth - 110, 10, "Zoom", ["100%", "200%", "300%", "400%"]); | |
comboBoxZoom.addEventListener(Event.SELECT, function(event:Event):void { | |
imageContainer.scaleX = imageContainer.scaleY = comboBoxZoom.selectedIndex + 1; | |
}); | |
comboBoxZoom.numVisibleItems = 4; | |
comboBoxZoom.selectedIndex = 2; | |
addChild(new PushButton(this, 120, 10, "Upload Image", function():void { | |
var ref:FileReference = new FileReference(); | |
ref.addEventListener(Event.SELECT, function(e:Event):void { ref.load(); }); | |
ref.addEventListener(Event.COMPLETE, function(e:Event):void { loadBytes(ref.data); }); | |
ref.browse([new FileFilter("PNG (*.png)", "*.png"), new FileFilter("JPG (*.jpg)", "*.jpg"), new FileFilter("GIF (*.gif)", "*.gif")]); | |
})); | |
} | |
private function canvasMouseDownHandler(event:MouseEvent):void | |
{ | |
dragStartX = event.stageX; | |
dragStartY = event.stageY; | |
imageX = imageContainer.x; | |
imageY = imageContainer.y; | |
stage.addEventListener(MouseEvent.MOUSE_UP, canvasMouseUpHandler); | |
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); | |
} | |
private function mouseMoveHandler(event:MouseEvent):void | |
{ | |
imageContainer.x = imageX + event.stageX - dragStartX; | |
imageContainer.y = imageY + event.stageY - dragStartY; | |
} | |
private function canvasMouseUpHandler(event:MouseEvent):void | |
{ | |
stage.removeEventListener(MouseEvent.MOUSE_UP, canvasMouseUpHandler); | |
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); | |
} | |
protected function loadBytes(image:ByteArray):void | |
{ | |
comboBoxImage.selectedIndex = -1; | |
var loader:Loader = new Loader(); | |
loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler); | |
loader.loadBytes(image); | |
} | |
protected function initHandler(event:Event):void | |
{ | |
var loaderInfo:LoaderInfo = event.target as LoaderInfo; | |
var loader:Loader = loaderInfo.loader; | |
prepareImage(loader.content as Bitmap); | |
} | |
protected function prepareImage(bitmap:Bitmap):void | |
{ | |
imageContainer.x = imageContainer.y = 0; | |
if(comboBoxZoom != null) { | |
comboBoxZoom.selectedIndex = 0; | |
} | |
var xs:Number = (stage.stageWidth - 20) / bitmap.width; | |
var ys:Number = (stage.stageHeight - 60) / bitmap.height; | |
var s:Number = Math.min(xs, ys, 1); | |
var bmd:BitmapData = new BitmapData(bitmap.width * s, bitmap.height * s, false); | |
var matrix:Matrix = new Matrix(); | |
matrix.createBox(s, s); | |
bmd.draw(bitmap, matrix, null, null, null, true); | |
bmd.applyFilter(bmd, bmd.rect, new Point(0, 0), grayscaleFilter); | |
bmd.applyFilter(bmd, bmd.rect, new Point(0, 0), blurFilter); | |
var bm:Bitmap = new Bitmap(bmd, PixelSnapping.AUTO, true); | |
traceImage(bm); | |
} | |
protected function traceImage(bitmap:Bitmap):void | |
{ | |
while(imageContainer.numChildren > 0) { | |
imageContainer.removeChildAt(0); | |
} | |
var bm:Bitmap = bitmap; | |
bm.visible = showBitmap; | |
imageContainer.addChild(bm); | |
var curves:Sprite = new Sprite(); | |
imageContainer.addChild(curves); | |
var gd:Vector.<IGraphicsData> = new Vector.<IGraphicsData>(); | |
var strokeFill:GraphicsSolidFill = new GraphicsSolidFill(0x008800, 1); | |
gd.push(new GraphicsStroke(1, false, LineScaleMode.NONE, CapsStyle.ROUND, JointStyle.ROUND, 3, strokeFill)); | |
gd.push(new GraphicsSolidFill(0x00ff00, 0.75)); | |
var potrace:POTrace = new POTrace(); | |
potrace.backend = new GraphicsDataBackend(gd); | |
potrace.potrace_trace(bitmap.bitmapData); | |
gd.push(new GraphicsEndFill()); | |
curves.graphics.drawGraphicsData(gd); | |
} | |
protected function get grayscaleFilter():BitmapFilter | |
{ | |
var r:Number = 0.212671; | |
var g:Number = 0.715160; | |
var b:Number = 0.072169; | |
return new ColorMatrixFilter([ | |
r, g, b, 0, 0, | |
r, g, b, 0, 0, | |
r, g, b, 0, 0, | |
0, 0, 0, 1, 0 | |
]); | |
} | |
protected function get blurFilter():BitmapFilter | |
{ | |
return new BlurFilter(2, 2, BitmapFilterQuality.HIGH); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import com.bit101.components.CheckBox;
import com.bit101.components.ComboBox;
import com.bit101.components.PushButton;