Playing with BitmapData


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /homepages/8/d317303823/htdocs/blog/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

At school, we made a cool exercise on Bitmap & BitmapData. That was great, I don’t work a lot with them.
The goal was to repeat a picture (make it roll and apply a blur) an on an enter frame without performance drop!

Nothing too complex, but a really nice post-it 😉

Click here to see the result.

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
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
 
var bmpDataStage:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0);
bmpDataStage.draw(stage);
 
var bmpStage:Bitmap = new Bitmap(bmpDataStage);
addChild(bmpStage);
 
// Coming from the flash library
var flower:Flower = new Flower();
 
var bmpDataFlower:BitmapData = new BitmapData(flower.width, flower.height, true, 0);
bmpDataFlower.draw(flower);
 
var time:Number = 0;
 
addEventListener(Event.ENTER_FRAME, ef);
 
function ef(evt:Event):void {
 
	time += 0.3;
 
	var mat:Matrix = new Matrix();
	mat.translate(-flower.width * 0.5, -flower.height * 0.5);
	mat.rotate(time);
	mat.translate(flower.width * 0.5, flower.height * 0.5);
 
	bmpDataFlower.fillRect(new Rectangle(0, 0, flower.width, flower.height), 0x00000000);
	bmpDataFlower.draw(flower, mat);
 
	bmpDataFlower.applyFilter(bmpDataFlower, bmpDataFlower.rect, new Point(0, 0), getBitmapFilter());
 
	bmpDataStage.copyPixels(bmpDataFlower, bmpDataStage.rect, new Point(mouseX - flower.width * 0.5, mouseY - flower.height * 0.5), null, null, true);
}
 
function getBitmapFilter():BitmapFilter {
	var blurX:Number = 5;
	var blurY:Number = 5;
	return new BlurFilter(blurX, blurY, BitmapFilterQuality.HIGH);
}

In a next lesson, we will see Blitting… can’t wait!

Leave a Reply

Your email address will not be published. Required fields are marked *