透明度ありBitmapDataを重ねた時の負荷
/**
* Copyright omochieater ( http://wonderfl.net/user/omochieater )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4thI
*/
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.ui.Keyboard;
import net.hires.debug.Stats;
[SWF(backgroundColor=0xFFFFFF, frameRate=60)]
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
var i:uint, _i:uint, layer:Layer, button:TFButton;
//Wonderfl.capture_delay(10);
addChild(mBG);
mBG.x = 5;
mBG.y = 5;
for (i = 0, _i = mLayers.length; i < _i; ++i) {
layer = mLayers[i];
layer.x = 5;
layer.y = 5;
addChild(layer);
}
mTFLabel1.x = 5;
mTFLabel1.y = mLayers[0].height + 10;
mTFLabel1.text = "レイヤーの表示/非表示を切り替えます。";
addChild(mTFLabel1);
for (i = 0, _i = mTFButtons1.length; i < _i; ++i) {
button = mTFButtons1[i];
button.x = 5 + (button.width + 2) * i;
button.y = mTFLabel1.y + mTFLabel1.height + 5;
addChild(button);
button.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown1);
}
mTFLabel2.x = 5;
mTFLabel2.y = mTFButtons1[0].y + mTFButtons1[0].height + 5;
mTFLabel2.text = "描画処理の実行/停止を切り替えます。";
addChild(mTFLabel2);
for (i = 0, _i = mTFButtons1.length; i < _i; ++i) {
button = mTFButtons2[i];
button.x = 5 + (button.width + 2) * i;
button.y = mTFLabel2.y + mTFLabel2.height + 5;
addChild(button);
button.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown2);
}
addChild(mStats);
mStats.x = stage.stageWidth - mStats.width;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onMouseDown1(event:MouseEvent):void {
var layer:Layer = mLayers[int(event.target.text)];
layer.visible = (layer.visible)? false: true;
}
private function onMouseDown2(event:MouseEvent):void {
var layer:Layer = mLayers[int(event.target.text)];
layer.enableDraw = (layer.enableDraw)? false: true;
}
private function onEnterFrame(event:Event):void {
var layer:Layer, i:uint, _i:uint;
for (i = 0, _i = mLayers.length; i < _i; ++i) {
layer = mLayers[i];
layer.update();
layer.draw(mEffect);
}
}
private const MAX_BALLS:uint = 16;
private const BALL_SIZE:uint = 8;
private const LAYER_WIDTH:uint = stage.stageWidth * 0.6;
private const LAYER_HEIGHT:uint = stage.stageHeight * 0.6;
private const mStats:Stats = new Stats();
private var mLayers:Vector.<Layer> = Vector.<Layer>([
new Layer(LAYER_WIDTH, LAYER_HEIGHT, 0x0000FF, MAX_BALLS, BALL_SIZE),
new Layer(LAYER_WIDTH, LAYER_HEIGHT, 0x00FFFF, MAX_BALLS, BALL_SIZE),
new Layer(LAYER_WIDTH, LAYER_HEIGHT, 0x00FF00, MAX_BALLS, BALL_SIZE),
new Layer(LAYER_WIDTH, LAYER_HEIGHT, 0xFFFF00, MAX_BALLS, BALL_SIZE),
new Layer(LAYER_WIDTH, LAYER_HEIGHT, 0xFF0000, MAX_BALLS, BALL_SIZE),
new Layer(LAYER_WIDTH, LAYER_HEIGHT, 0xFF00FF, MAX_BALLS, BALL_SIZE),
new Layer(LAYER_WIDTH, LAYER_HEIGHT, 0xFFFFFF, MAX_BALLS, BALL_SIZE),
new Layer(LAYER_WIDTH, LAYER_HEIGHT, 0x777777, MAX_BALLS, BALL_SIZE),
]);
private var mBG:Bitmap = new Bitmap(new BitmapData(LAYER_WIDTH, LAYER_HEIGHT, false, 0x0));
private var mTFLabel1:TFLabel = new TFLabel();
private var mTFLabel2:TFLabel = new TFLabel();
private var mTFButtons1:Vector.<TFButton> = Vector.<TFButton>([
new TFButton("0"), new TFButton("1"), new TFButton("2"), new TFButton("3"),
new TFButton("4"), new TFButton("5"), new TFButton("6"), new TFButton("7")
]);
private var mTFButtons2:Vector.<TFButton> = Vector.<TFButton>([
new TFButton("0"), new TFButton("1"), new TFButton("2"), new TFButton("3"),
new TFButton("4"), new TFButton("5"), new TFButton("6"), new TFButton("7")
]);
private var mEffect:Boolean = true;
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Shape;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
internal class Layer extends Bitmap {
public function Layer(stageWidth:uint, stageHeight:uint, color:uint, numBalls:uint, ballSize:uint) {
super(new BitmapData(stageWidth, stageHeight, true, 0x0));
mBalls = createBalls(numBalls);
mImage = createImage(color, ballSize);
initBalls();
}
private function createBalls(numBalls:uint):Vector.<Ball> {
var vector:Vector.<Ball> = new Vector.<Ball>(numBalls, true);
var i:uint, _i:uint;
for (i = 0, _i = numBalls; i < _i; ++i) {
vector[i] = new Ball();
}
return vector;
}
private function initBalls():void {
var ball:Ball, radian:Number, dist:Number, i:uint, _i:uint;
dist = width * 0.4;
for (i = 0, _i = mBalls.length; i < _i; ++i) {
ball = mBalls[i];
radian = Math.PI*2/_i*i;
ball.x = Math.cos(radian) * dist + width * 0.5;
ball.y = Math.sin(radian) * dist + height * 0.5;
radian = Math.PI*2*Math.random();
ball.vx = Math.cos(radian) * (1 + Math.random());
ball.vy = Math.sin(radian) * (1 + Math.random());
}
}
public function update():void {
var ball:Ball, top:Number, bottom:Number, left:Number, right:Number, i:uint, _i:uint;
top = 0;
bottom = height;
left = 0;
right = width;
for (i = 0, _i = mBalls.length; i < _i; ++i) {
ball = mBalls[i];
ball.x += ball.vx;
ball.y += ball.vy;
if (ball.x < left || right < ball.x) ball.vx *= -1;
if (ball.y < top || bottom < ball.y) ball.vy *= -1;
}
}
public function draw(effect:Boolean):void {
var ball:Ball, i:uint, _i:uint, centerX:Number, centerY:Number;
if (!enableDraw) return;
bitmapData.lock();
if (effect) {
bitmapData.colorTransform(bitmapData.rect, mColorTransform);
}
else {
bitmapData.fillRect(bitmapData.rect, 0x0);
}
centerX = mImage.width * 0.5;
centerY = mImage.width * 0.5;
for (i = 0, _i = mBalls.length; i < _i; ++i) {
ball = mBalls[i];
mPoint.x = ball.x - centerX;
mPoint.y = ball.y - centerY;
//mBitmapData.copyPixels(mImage, mImage.rect, mPoint);
bitmapData.copyPixels(mImage, mImage.rect, mPoint, null, null, true);
}
bitmapData.unlock();
}
private function createImage(color:uint, size:uint):BitmapData {
var bitmapData:BitmapData;
var shape:Shape;
var g:Graphics;
shape = new Shape();
g = shape.graphics;
g.beginFill(color, 0.5);
g.drawCircle(size * 0.5, size * 0.5, size * 0.5);
g.beginFill(color);
g.drawCircle(size * 0.5, size * 0.5, size * 0.3);
g.endFill();
bitmapData = new BitmapData(size, size, true, 0x0);
bitmapData.draw(shape);
return bitmapData;
}
private static const mPoint:Point = new Point();
private static const mMatrix:Matrix = new Matrix();
private var mBalls:Vector.<Ball>;
private var mImage:BitmapData;
private var mColorTransform:ColorTransform = new ColorTransform(1.05, 1.05, 1.05, 0.95);
public var enableDraw:Boolean = true;
}
internal class Ball {
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
}
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
internal class TFButton extends TextField {
public function TFButton(label:String, buttonWidth:Number=NaN) {
initTFButton();
text = label;
if (!isNaN(buttonWidth)) {
width = buttonWidth;
autoSize = TextFieldAutoSize.NONE;
}
}
public function initTFButton():void {
var textFormat:TextFormat = defaultTextFormat;
autoSize = TextFieldAutoSize.LEFT;
background = true;
backgroundColor = 0xCCCCCC;
border = true;
borderColor = 0xAAAAAA;
selectable = false;
textFormat.align = TextFormatAlign.CENTER;
textFormat.color = 0x000000;
textFormat.leftMargin = 8;
textFormat.rightMargin = 8;
textFormat.size = 16;
defaultTextFormat = textFormat;
}
}
internal class TFLabel extends TextField {
public function TFLabel(color:uint=0x0) {
initTFLabel(color);
}
private function initTFLabel(color:uint):void {
var textFormat:TextFormat = defaultTextFormat;
autoSize = TextFieldAutoSize.LEFT;
textFormat.color = color;
defaultTextFormat = textFormat;
}
}