In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

エアブラシ的なテスト

LIVE CODING TEST
GraphicsをBitmapData.draw()するのと
BitmapDataをcopyPixel()するのを比較してみる。
(比較対象: 処理時間,出力されるBitmapData)
Get Adobe Flash player
by moyashipan 23 Feb 2010
    Embed
/**
 * Copyright moyashipan ( http://wonderfl.net/user/moyashipan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cNCq
 */

package {
	// LIVE CODING TEST
	
	// GraphicsをBitmapData.draw()するのと
	// BitmapDataをcopyPixel()するのを比較してみる。
	// (比較対象: 処理時間,出力されるBitmapData)
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.display.Graphics;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;
    import flash.geom.Point;
    import flash.utils.getTimer;
    import flash.display.BitmapDataChannel;
    public class FlashTest extends Sprite {
    		private static const loopLen:uint = 500
    		private static const ALPHA:Number = .01;
    	
        public function FlashTest() {
            // write as3 code here..
            
            for(var i:uint = 0; i<10; i++){
            		graphicsLoop(i);
            }
            
            for(i = 0; i<10; i++){
            		bitmapLoop(i);
            }
            
            for(i = 0; i<10; i++){
            		fixedBitmapLoop(i);
            }
        }
        
        public function graphicsLoop(index:Number):void{
        		var time:uint = getTimer();
        		
        		var sprite:Sprite = new Sprite();
        		var g:Graphics = sprite.graphics;
        		g.beginFill(0x800000, (10-index) * 0.01);
        		g.drawCircle(0, 0, 20);
        		g.endFill();
        		
        		var bitmap:Bitmap = new Bitmap();
        		bitmap.bitmapData = new BitmapData(200, 40, true, 0);
        		this.addChild(bitmap);
        		bitmap.x = 50;
        		bitmap.y = index * 40;
        		var bmpd:BitmapData = bitmap.bitmapData;
        		
        		var matrix:Matrix = new Matrix();
        		matrix.translate(20,20);
        		for(var i:uint = 0; i < loopLen; i++){
        			bmpd.draw(sprite, matrix);
        			matrix.tx += .1;
        		}
        		time = getTimer() - time;
        		
 			var textField:TextField = new TextField();
			textField.appendText("Alpha" + (10-index)*0.01 + "\n");
			textField.appendText(time + " ms");
			textField.border = true;
			textField.width = 60;
			textField.height = 34;
			textField.x= 0;
			textField.y = 40 * index;
        		this.addChild(textField);
        	}
        	
        public function bitmapLoop(index:Number):void{
        		var time:uint = getTimer();
        		
         	var sprite:Sprite = new Sprite();
        		var g:Graphics = sprite.graphics;
        		g.beginFill(0x800000, (10-index) * 0.01);
        		g.drawCircle(0, 0, 20);
        		g.endFill();
        		
        		var matrix:Matrix = new Matrix();
        		matrix.translate(20, 20);
        		var flyWeightSource:BitmapData = new BitmapData(40, 40, true, 0);
        		flyWeightSource.draw(sprite, matrix);
        		
        		var bitmap:Bitmap = new Bitmap();
        		bitmap.bitmapData = new BitmapData(200, 40, true, 0);
        		var bmpd:BitmapData = bitmap.bitmapData;
        		this.addChild(bitmap);
        		bitmap.x = 200;
        		bitmap.y = index * 40;
        		
        		for(var i:uint = 0; i < loopLen; i++){
        			bmpd.copyPixels(flyWeightSource, 	flyWeightSource.rect, new Point(i*0.1, 0), null, null, true);
        		}
        		time = getTimer() - time;
        		
 			var textField:TextField = new TextField();
			textField.appendText("Alpha" + (10-index)*0.01 + "\n");
			textField.appendText(time + " ms");
			textField.border = true;
			textField.width = 60;
			textField.height = 34;
			textField.x= 150;
			textField.y = 40 * index;
        		this.addChild(textField);
      	}
      	
        public function fixedBitmapLoop(index:Number):void{
        		var time:uint = getTimer();
        		
         	var sprite:Sprite = new Sprite();
        		var g:Graphics = sprite.graphics;
        		g.beginFill(0x800000, (10-index) * 0.01);
        		g.drawCircle(0, 0, 20);
        		g.endFill();
        		
        		var matrix:Matrix = new Matrix();
        		matrix.translate(20, 20);
        		var flyWeightSource:BitmapData = new BitmapData(40, 40, true, 0);
        		flyWeightSource.draw(sprite, matrix);
        		
        		var bitmap:Bitmap = new Bitmap();
        		bitmap.bitmapData = new BitmapData(200, 40, true, 0);
        		var bmpd:BitmapData = bitmap.bitmapData;
        		this.addChild(bitmap);
        		bitmap.x = 350;
        		bitmap.y = index * 40;
        		
        		for(var i:uint = 0; i < loopLen; i++){
        			bmpd.copyPixels(flyWeightSource, 	flyWeightSource.rect, new Point(i*0.1, 0), null, null, true);
        		}
        		flyWeightSource.dispose();
        		
        		var temp:BitmapData = bmpd.clone();
        		bmpd.fillRect(temp.rect, 0xff800000);
        		bmpd.copyChannel(temp, temp.rect, new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
        		temp.dispose();
        		
        		time = getTimer() - time;
        		
 			var textField:TextField = new TextField();
			textField.appendText("Alpha" + (10-index)*0.01 + "\n");
			textField.appendText(time + " ms");
			textField.border = true;
			textField.width = 60;
			textField.height = 34;
			textField.x= 300;
			textField.y = 40 * index;
        		this.addChild(textField);
      	}
    }
}