Actionscript:
-
[SWF(width=650, height=650)]
-
var loader:Loader = new Loader();
-
loader.load(new URLRequest("http://actionsnippet.com/wp-content/chair.jpg"));
-
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
-
-
var w:Number;
-
var h:Number;
-
var rows:Number = 20;
-
var cols:Number = 20;
-
var tiles:Vector.<BitmapData> = new Vector.<BitmapData>();
-
var locX:Vector.<Number> = new Vector.<Number>();
-
var locY:Vector.<Number> = new Vector.<Number>();
-
var rX:Vector.<Number> = new Vector.<Number>();
-
var rY:Vector.<Number> = new Vector.<Number>();
-
var sX:Vector.<Number> = new Vector.<Number>();
-
var sY:Vector.<Number> = new Vector.<Number>();
-
function onLoaded(evt:Event):void{
-
w = evt.target.width;
-
h = evt.target.height;
-
var image:BitmapData = Bitmap(evt.target.content).bitmapData;
-
var tileWidth:Number = w / cols;
-
var tileHeight:Number = h / rows;
-
var inc:int = 0;
-
var pnt:Point = new Point();
-
var rect:Rectangle = new Rectangle(0,0,tileWidth,tileHeight);
-
for (var i:int = 0; i<rows; i++){
-
for (var j:int = 0; j<cols; j ++){
-
var currTile:BitmapData= new BitmapData(tileWidth, tileHeight, true, 0x00000000);
-
rect.x = j * tileWidth;
-
rect.y = i * tileHeight;
-
currTile.copyPixels(image, rect, pnt, null, null, true);
-
tiles[inc] = currTile;
-
rect.x += 25;
-
rect.y += 25;
-
sX[inc] = rect.x;
-
sY[inc] = rect.y;
-
locX[inc] = rX[inc] = -rect.width * 2
-
locY[inc] = rY[inc] = Math.random() * stage.stageHeight;
-
setTimeout(startAnimation, inc *4 + 100, inc, rect.x, rect.y);
-
inc++;
-
}
-
}
-
}
-
-
function startAnimation(index:int, dx:Number, dy:Number):void{
-
var interval:Number;
-
var animate:Function = function(index:int):void{
-
locX[index] += (dx - locX[index]) / 4;
-
locY[index] += (dy - locY[index]) / 4;
-
if (Math.abs(locX[index] - dx) <1 && Math.abs(locY[index] - dy)<1){
-
locX[index] = dx;
-
locY[index] = dy;
-
clearInterval(interval);
-
}
-
}
-
interval = setInterval(animate, 32, index);
-
}
-
-
var canvas:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0xFFFFFF);
-
addChild(new Bitmap(canvas));
-
-
var loc:Point = new Point();
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
canvas.fillRect(canvas.rect, 0xFFFFFF);
-
for (var i:int = 0; i<tiles.length; i++){
-
var tile:BitmapData= tiles[i];
-
loc.x = locX[i];
-
loc.y = locY[i];
-
canvas.copyPixels(tile, tile.rect, loc, null, null, true);
-
}
-
}
This snippet cuts a dynamically loaded image into a series of smaller BitmapData instances. These BitmapData instances are then drawn to a canvas BitmapData using copyPixels. The BitmapData.copyPixels() method is extremely fast - so this has some advantages over yesterdays post (which did basically the same thing with Sprites). I used setInterval and setTimeout to do a simple demo animation, but I recommend using TweenLite on a real project.
9 Comments
copyPixel is fast, but the fastest way to put pixels into the image is the setVector method.
try this in the loop function
function onLoop(evt:Event):void {
canvas.fillRect(canvas.rect, 0xFFFFFF);
var destRect:Rectangle;
for (var i:int = 0; i<tiles.length; i++){
var tile:BitmapData= tiles[i];
destRect = tile.rect.clone();
loc.x = destRect.x = locX[i];
loc.y = destRect.y = locY[i];
canvas.setVector(destRect,tile.getVector(tile.rect));
}
}
Did you do a speed test on that… I get a max of 12 ms with setVector() and a max of 3 ms with copyPixels()… also, tile.rect.clone() in the for loop isn’t necessary. Can you post a speed test?
I’m a bit busy today, but later on I’ll try a slightly different approach to using setVector() and compare the results…
Saw your blogpost…cool stuff… but you put the speed test in the wrong place… putting the speed test in the correct place reveals that there isn’t much of a difference….
Here are demo fla files you can use to compare speeds and see what I mean about correct speed test…
http://actionsnippet.com/source/setVector.fla
http://actionsnippet.com/source/copyPixels.fla
Let me know what results you get… maybe it varies from computer to computer… on my machine both seems to run at about 25 - 30 ms (I upped the number of tiles).
Nice work I wouldn’t have thought to use setVector() as a replacement for copyPixels.
I think it makes sense to write a snippet specifically targeted at testing the speed of copyPixels vs setVector… I’ll try that in the near future….
yes, you’re right, rect.clone is unnecessary on that loop.
i did some more accurate speed tests here you can find here on my blog http://www.flashfuck.it/2009/07/15/bitmapdata-manipulation-benchmark/
i tried out some ways to get the same example you did here and here’s the results is
get\setvector is the fastest way, followed by get\setPixels, copyPixels is only third in this chart, and the slowest one is bitmapdata.merge.
i’m running these tests on a mbp, waiting for someone to benchmark on windows
just saw your comment here…i was updating the code on the post -.-’ cool synchronized coding!
heh nice… what kind of results to you get if you up the rows and cols vars?
for me setVector and copyPixels seems to be giving almost exactly the same speed results….
just tried with 50×50 with every tests and each of them crashed before finishing the task, i think we’d better to use some tween engine instead of setinterval (which i suppose to be the crasher)…
yeah … I think the best thing to do is use a different snippet, specifically designed to test this… weird that it crashes at 50×50.. I did 100×100 and it worked fine … on a macbook pro laptop 2.4 duo
hey Piergiorgio… if you have a minute check out this post and let me know what results you get… or if you would change anything about the setVector() implementation to improve speed…
One Trackback
[...] I encountered this blog post from Zevan (which blog is REALLY a good daily reading I suggest everyone to take) about bitmapData merging and [...]