Flash / Flex / ActionScript/Graphics/BitmapData — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 08:15, 26 мая 2010
Содержание
- 1 Adding Noise
- 2 Adding Perlin Noise
- 3 Applying Color Transformations
- 4 Assign a pixel value using a number that specifies an Alpha channel of CC
- 5 Assigning the Color of a Region of Pixels
- 6 Create animation by setting the bitmap pixel
- 7 Creating a BitmapData Object
- 8 Creating a Flood Fill
- 9 Creating a New Bitmap Image from ImageData
- 10 Detecting Areas by Color
- 11 Displaying BitmapData Images
- 12 Dissolve
- 13 File BitmapData with Rectangle
- 14 getPixel32() Versus getPixel( )
- 15 Get pixel data from BitmapData
- 16 How draw( ) handles Alpha channel values
- 17 Modifying a BitmapData
- 18 PerlinNoise animation
- 19 Retrieve a pixel with Alpha set to 255, from a transparent image
- 20 Retrieving the Color of a Region of Pixels
- 21 The alpha value of pixels can be set in transparent bitmaps only (i.e., bitmaps created with the value true passed to the transparent parameter of the BitmapData constructor)
- 22 The following code makes a BitmapData object with Perlin noise, and then uses that object as a bitmap fill. Because the stitch parameter is false, the edges are visible as it tiles.
- 23 The randomSeed parameter is responsible for the randomness of the noise
- 24 Updates the octaves parameter
- 25 Using BitmapData.lock( ) to improve performance
Adding Noise
package {
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends Sprite {
private var _bitmapData:BitmapData = new BitmapData(400, 400);
public function Main () {
var bitmap:Bitmap = new Bitmap(_bitmapData);
addChild(bitmap);
var timer:Timer = new Timer(50);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function timerHandler(event:TimerEvent):void {
_bitmapData.noise(Math.random() * 1000000);
}
}
}
Adding Perlin Noise
package {
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends Sprite {
private var _bitmapData:BitmapData = new BitmapData(400, 400);
public function Main () {
var bitmap:Bitmap = new Bitmap(_bitmapData);
addChild(bitmap);
var timer:Timer = new Timer(50);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function timerHandler(event:TimerEvent):void {
_bitmapData.perlinNoise(mouseX, mouseY, 1, 1, false, false);
}
}
}
Applying Color Transformations
package {
import flash.net.URLRequest;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Loader;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.geom.ColorTransform;
public class Main extends Sprite {
private var _loader:Loader = new Loader();
public function Main () {
_loader.load(new URLRequest("http://www.wbex.ru/samplefiles/image2.jpg"));
_loader.contentLoaderInfo.addEventListener(Event.ruPLETE,completeHandler);
}
private function completeHandler(event:Event):void {
var w:Number = _loader.width;
var h:Number = _loader.height;
var bitmapData:BitmapData = new BitmapData(w, h);
bitmapData.draw(_loader);
bitmapData.colorTransform(new Rectangle(0, 0, w/2, h/2), new ColorTransform(1, 0, 0, 1, 0, 0, 0, 0));
bitmapData.colorTransform(new Rectangle(w/2, 0, w/2, h/2), new ColorTransform(0, 1, 0, 1, 0, 0, 0, 0));
bitmapData.colorTransform(new Rectangle(0, h/2, w/2, h/2), new ColorTransform(0, 0, 1, 1, 0, 0, 0, 0));
bitmapData.colorTransform(new Rectangle(w/2, h/2, w/2, h/2), new ColorTransform(1, 1, 0, 1, 0, 0, 0, 0));
var bitmap:Bitmap = new Bitmap(bitmapData);
addChild(bitmap);
}
}
}
Assign a pixel value using a number that specifies an Alpha channel of CC
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(20, 20, true, 0x660000FF);
imgData.setPixel(0, 0, 0xFFFFFF);
imgData.setPixel(0, 0, 0xCCFFFFFF);
}
}
}
Assigning the Color of a Region of Pixels
package{
import flash.display.*;
import flash.utils.*;
import flash.geom.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(4, 4, false, 0xFFFF0000);
var byteArray:ByteArray = new ByteArray( );
byteArray.writeUnsignedInt(0xFF00FF00);
byteArray.position = 0;
imgData.setPixels(new Rectangle(1,0,3,2), byteArray);
}
}
}
Create animation by setting the bitmap pixel
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.filters.BlurFilter;
import flash.events.Event;
public class Main extends Sprite {
private var _bitmap:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight,
false, 0xff000000);
private var _image:Bitmap = new Bitmap(_bitmap);
public function Main( ) {
addChild(_image);
_image.filters = [new BlurFilter( )];
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
for(var i:int = 0; i < 100; i++) {
_bitmap.setPixel(mouseX + Math.random( ) * 20 - 10,
mouseY + Math.random( ) * 20 - 10,
0xffffffff);
}
}
}
}
Creating a BitmapData Object
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var bmpRectangle:BitmapData = new BitmapData(500, 200);
}
}
}
Creating a Flood Fill
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class Main extends Sprite {
private var _bitmap:BitmapData= new BitmapData(stage.stageWidth,stage.stageHeight,false, 0xffffffff);
public function Main ( ) {
var sprite:Sprite = new Sprite( );
addChild(sprite);
for(var i:int = 0; i < 200; i++) {
_bitmap.fillRect(new Rectangle(
Math.random( ) * stage.stageWidth,
Math.random( ) * stage.stageHeight,
20, 20), Math.random( ) * 0xffffffff);
}
var image:Bitmap = new Bitmap(_bitmap);
sprite.addChild(image);
sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
public function onMouseDown(event:MouseEvent):void {
_bitmap.floodFill(mouseX, mouseY, 0xffff0000);
}
}
}
Creating a New Bitmap Image from ImageData
package{
import flash.display.Sprite;
import flash.display.*;
import flash.geom.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(20, 20, false, 0xFF00FF00);
imgData.fillRect(new Rectangle(5, 5, 10, 10), 0xFF0000FF);
var bmp:Bitmap = new Bitmap(imgData);
bmp.bitmapData = imgData;
addChild(bmp);
}
}
}
Detecting Areas by Color
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
public class Main extends Sprite {
private var _bitmapData:BitmapData;
private var _loader:Loader = new Loader();
private var _outline:Shape;
public function Main () {
_loader.load(new URLRequest("http://www.wbex.ru/samplefiles/image2.jpg"));
_loader.contentLoaderInfo.addEventListener(Event.ruPLETE, completeHandler);
addEventListener(MouseEvent.CLICK, clickHandler);
}
private function completeHandler(event:Event):void {
var w:Number = _loader.width;
var h:Number = _loader.height;
_bitmapData = new BitmapData(w, h);
_bitmapData.draw(_loader);
var bitmap:Bitmap = new Bitmap(_bitmapData);
var container:Sprite = new Sprite();
addChild(container);
container.addChild(bitmap);
_outline = new Shape();
addChild(_outline);
}
private function clickHandler(event:MouseEvent):void {
var color:uint = _bitmapData.getPixel32(mouseX, mouseY);
var rectangle:Rectangle = _bitmapData.getColorBoundsRect(0xFFFFFFFF, color);
_outline.graphics.clear();
_outline.graphics.lineStyle(0, 0x000000, 1);
_outline.graphics.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
}
}
Displaying BitmapData Images
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var squareData:BitmapData = new BitmapData(200, 200, false, 0xFFCC00CC);
var square:Bitmap = new Bitmap(squareData);
addChild(square);
}
}
}
Dissolve
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
public class Main extends Sprite {
private var _bitmap:BitmapData= new BitmapData(stage.stageWidth,stage.stageHeight,false,0xffffffff);
private var _image:Bitmap = new Bitmap(_bitmap);
private var _seed:Number = 10000;
private var _pixelCount:int = 0;
public function Main( ) {
addChild(_image);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
_seed = _bitmap.pixelDissolve(_bitmap,
_bitmap.rect,
new Point( ),
_seed,
1000,
0xff000000);
_pixelCount += 1000;
if(_pixelCount > _bitmap.width * _bitmap.height)
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
}
}
File BitmapData with Rectangle
package{
import flash.display.Sprite;
import flash.display.*;
import flash.geom.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(20, 20, false, 0xFF00FF00);
imgData.fillRect(new Rectangle(5, 5, 10, 10), 0xFF0000FF);
var bmp:Bitmap = new Bitmap(imgData);
addChild(bmp);
}
}
}
getPixel32() Versus getPixel( )
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(20, 20, false, 0xFF0000FF);
trace(imgData.getPixel(0, 0)); // Displays: 255
}
}
}
Get pixel data from BitmapData
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(20, 20, false, 0x330000FF);
trace(imgData.getPixel32(0, 0)); // Displays: 4278190335
// (Alpha is 0xFF, not 0x33)
}
}
}
How draw( ) handles Alpha channel values
package{
import flash.display.*;
import flash.utils.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
public class Main extends Sprite{
public function Main(){
var redSquare:BitmapData = new BitmapData(20, 20, true, 0xFFFF0000);
var blueSquare:BitmapData = new BitmapData(20, 20, true, 0xFF0000FF);
var sourceRect:Rectangle = new Rectangle(5, 5, 10, 5);
var destPoint:Point = new Point(0,0);
redSquare.copyPixels(blueSquare, sourceRect, destPoint);
var b:Bitmap = new Bitmap(redSquare);
addChild(b);
}
}
}
Modifying a BitmapData
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(20, 20, false, 0xFF0000FF);
imgData.setPixel32(0, 0, 0xFFFFFFFF);
}
}
}
PerlinNoise animation
package {
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Shape;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.geom.Point;
public class Main extends Sprite {
private var _bitmapData:BitmapData = new BitmapData(200, 200);
private var _offset:Number = 0;
public function RuntimeBitmap() {
var bitmap:Bitmap = new Bitmap(_bitmapData);
addChild(bitmap);
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function timerHandler(event:TimerEvent):void {
_bitmapData.perlinNoise(100, 100, 1, 1, false, false, 1, false, [new Point(_offset++, 0)]);
}
}
}
Retrieve a pixel with Alpha set to 255, from a transparent image
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(20, 20, true, 0xFFFFFFFF);
trace(imgData.getPixel32(0, 0)); // Displays: 4294967295
// (original data was preserved)
}
}
}
Retrieving the Color of a Region of Pixels
package{
import flash.display.*;
import flash.geom.*;
import flash.utils.ByteArray;
public class Main extends Sprite{
public function Main(){
var blueSquare:BitmapData = new BitmapData(20, 20, false, 0xFF0000FF);
var greenSquare:BitmapData = new BitmapData(30, 30, false, 0xFF00FF00);
var rectRegion:Rectangle = new Rectangle(5, 5, 10, 10);
var greenPixels:ByteArray = greenSquare.getPixels(rectRegion);
greenPixels.position = 0;
blueSquare.setPixels(rectRegion, greenPixels);
var blueBmp:Bitmap = new Bitmap(blueSquare);
var greenBmp:Bitmap = new Bitmap(greenSquare);
addChild(blueBmp);
addChild(greenBmp);
greenBmp.x = 40;
}
}
}
The alpha value of pixels can be set in transparent bitmaps only (i.e., bitmaps created with the value true passed to the transparent parameter of the BitmapData constructor)
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(20, 20, true, 0x330000FF);
trace(imgData.getPixel32(0, 0)); // Displays: 855638271
// (Alpha is 0x33)
}
}
}
The following code makes a BitmapData object with Perlin noise, and then uses that object as a bitmap fill. Because the stitch parameter is false, the edges are visible as it tiles.
package {
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends Sprite {
private var _bitmapData:BitmapData = new BitmapData(200, 200);
public function Main() {
var shape:Shape = new Shape();
shape.graphics.lineStyle(0, 0, 0);
shape.graphics.beginBitmapFill(_bitmapData);
shape.graphics.drawRect(0, 0, 600, 400);
shape.graphics.endFill();
addChild(shape);
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function timerHandler(event:TimerEvent):void {
_bitmapData.perlinNoise(100, 100, 1, Math.random() * 100000, false, false);
}
}
}
The randomSeed parameter is responsible for the randomness of the noise
package {
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends Sprite {
private var _bitmapData:BitmapData = new BitmapData(400, 400);
public function Main () {
var bitmap:Bitmap = new Bitmap(_bitmapData);
addChild(bitmap);
var timer:Timer = new Timer(50);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function timerHandler(event:TimerEvent):void {
_bitmapData.perlinNoise(100, 100, 1, Math.random() * 1000, false, false);
}
}
}
Updates the octaves parameter
package {
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends Sprite {
private var _bitmapData:BitmapData = new BitmapData(400, 400);
public function Main () {
var bitmap:Bitmap = new Bitmap(_bitmapData);
addChild(bitmap);
var timer:Timer = new Timer(50);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}
private function timerHandler(event:TimerEvent):void {
_bitmapData.perlinNoise(100, 100, mouseX * 10 / 400, 1, false, false);
}
}
}
Using BitmapData.lock( ) to improve performance
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(500, 500, true, 0x00000000);
var bmp:Bitmap = new Bitmap(imgData);
imgData.lock( );
var color:uint;
for (var i:int = 0; i < imgData.height ; i++) {
for (var j:int = 0; j < imgData.width; j++) {
color = Math.floor(Math.random( )*0xFFFFFFFF);
imgData.setPixel32(j, i, color);
}
}
imgData.unlock( );
}
}
}