public
Created

Drawing directly to a bitmap

  • Download Gist
BitmapDrawComparison.as
ActionScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
package {
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
[SWF(width="500", height="500", frameRate="50")]
public class BitmapDrawComparison extends Sprite {
public var timeDisplay:TextField;
public var lastTime:int = 0;
public var bitmap:Bitmap;
public var bitmapData:BitmapData;
public var shapeLayer:Sprite;
public function BitmapDrawComparison() {
addEventListener(Event.ENTER_FRAME, calculateTime);
bitmapData = new BitmapData(500,500,false, 0xFFFFFF);
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
shapeLayer = new Sprite();
var circle:Circle;
var i:int = 0;
for (; i < 400; i+=1) {
circle = new Circle();
circle.x = 500 * Math.random();
circle.y = 500 * Math.random();
circle.cacheAsBitmap = true;
shapeLayer.addChild(circle);
}
timeDisplay = new TextField();
addChild (timeDisplay);
}
public function calculateTime(event:Event):void {
bitmapData.fillRect(new Rectangle(0,0,500,500), 0xFFFFFF);
bitmapData.draw(shapeLayer);
var now:int = getTimer();
timeDisplay.text = int(1000/(now - lastTime)) + "fps"; // runs around 10-12 fps
lastTime = now;
}
}
}
 
import flash.display.*;
import flash.events.*;
import flash.geom.*;
 
/** Colored circle useded in demo. */
internal class Circle extends Sprite {
public var v:Point;
public function Circle() {
super();
graphics.beginFill([0xFF00FF,0xFFFF00,0x00FFFF][int(Math.random() * 3)], 1);
graphics.drawCircle(0, 0, 20);
blendMode = BlendMode.MULTIPLY;
scaleX = scaleY = .2 + (2 * Math.random());
v = new Point(Math.random() * 2 - 1, Math.random() * 2 - 1);
addEventListener(Event.ENTER_FRAME, update);
}
private function update(event:Event):void {
x += v.x;
y += v.y;
if (x < 0 || x > 500) { v.x = v.x * -1; }
if (y < 0 || y > 500) { v.y = v.y * -1; }
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.