Flash / Flex / ActionScript/Graphics/Bitmap — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 09:19, 26 мая 2010
Содержание
- 1 Add Bitmap to Sprite
- 2 Adding a Bitmap Fill
- 3 An image-based color picker
- 4 Apply filter to bitmap
- 5 Applying Flood Fills
- 6 Applying Rectangular Fills
- 7 Copying Images
- 8 Copying Pixels: bitmap.copyPixels(sourceBmp, srcRect, destPoint);
- 9 Create bitmap and set pixel
- 10 Display objects composited into a bitmap
- 11 Dissolving Between Two Bitmaps
- 12 Embedding a bitmap at compile time
- 13 Examining a Bitmap
- 14 Image Smoothing
- 15 Pixel Snapping
- 16 Runtime Bitmap
- 17 Transforming Colors
- 18 Use perlinNoise
Add Bitmap to Sprite
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 bmp1:Bitmap = new Bitmap(imgData);
addChild(bmp1);
var bmp2:Bitmap = new Bitmap(imgData);
bmp2.rotation = 45;
bmp2.x = 50;
bmp2.scaleX = 2; // 200%
bmp2.scaleY = 2; // 200%
addChild(bmp2);
}
}
}
Adding a Bitmap Fill
package
{
import flash.display.*;
import flash.events.Event;
import flash.net.URLRequest;
public class Main extends Sprite
{
private var shape:Sprite;
private var loader:Loader;
private var bmpImage:BitmapData;
private var mShape:Sprite;
public function Main()
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.ruPLETE, picLoaded);
loader.load(new URLRequest("image1.jpg"));
mShape = new Sprite();
mShape.x = 100;
mShape.y = 200;
}
private function picLoaded(event:Event):void
{
bmpImage = new BitmapData(loader.width, loader.height);
bmpImage.draw(loader);
mShape.graphics.lineStyle(10);
mShape.graphics.beginBitmapFill(bmpImage);
mShape.graphics.curveTo(100, -50, 200, 0);
mShape.graphics.lineTo(200, 100);
mShape.graphics.lineTo(0, 100);
mShape.graphics.lineTo(0, 0);
mShape.graphics.endFill();
addChild(mShape);
}
}
}
An image-based color picker
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.net.*;
public class Main extends Sprite {
private var img:Bitmap; // The Bitmap object
private var imgContainer:Sprite; // Container for the Bitmap object
private var t:TextField = new TextField( );
public function Main( ) {
t.text = "Please come to my party...";
t.autoSize = TextFieldAutoSize.LEFT;
addChild(t);
var loader:Loader = new Loader( );
loader.contentLoaderInfo.addEventListener(Event.INIT,
initListener);
loader.load(new URLRequest("s.jpg"));
}
private function initListener (e:Event):void {
img = e.target.content;
imgContainer = new Sprite( );
imgContainer.addChild(img);
addChild(imgContainer);
imgContainer.y = 30;
imgContainer.addEventListener(MouseEvent.MOUSE_MOVE,
mouseMoveListener);
}
private function mouseMoveListener (e:MouseEvent):void {
t.textColor = img.bitmapData.getPixel32(e.localX, e.localY);
}
}
}
Apply filter to bitmap
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.filters.BlurFilter;
import flash.events.Event;
import flash.geom.Point;
public class Main extends Sprite {
private var _bitmap:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight,
false, 0xff000000);
private var _image:Bitmap = new Bitmap(_bitmap);
private var _blurFilter:BlurFilter = new BlurFilter( );
public function Main( ) {
addChild(_image);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
for(var i:int = 0; i < 500; i++) {
_bitmap.setPixel(mouseX + Math.random( ) * 20 - 10,
mouseY + Math.random( ) * 20 - 10,
0xffffffff);
}
_bitmap.applyFilter(_bitmap, _bitmap.rect, new Point( ), _blurFilter);
}
}
}
Applying Flood Fills
package {
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
public class Main extends Sprite {
private var _bitmapData:BitmapData = new BitmapData(200, 200);
public function Main () {
_bitmapData.fillRect(new Rectangle(0, 0, 100, 100), 0xFFFF0000);
_bitmapData.fillRect(new Rectangle(100, 0, 100, 100), 0xFF00FF00);
_bitmapData.fillRect(new Rectangle(0, 100, 100, 100), 0xFF0000FF);
_bitmapData.fillRect(new Rectangle(100, 100, 100, 100), 0xFFFFFF00);
var container:Sprite = new Sprite();
addChild(container);
var bitmap:Bitmap = new Bitmap(_bitmapData);
container.addChild(bitmap);
container.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(event:MouseEvent):void {
_bitmapData.floodFill(mouseX, mouseY, 0xFF000000 | Math.random() * 0xFFFFFF);
}
}
}
Applying Rectangular Fills
package{
import flash.display.*;
import flash.geom.*;
public class Main extends Sprite{
public function Main(){
var bitmapData:BitmapData = new BitmapData(200, 200);
bitmapData.fillRect(new Rectangle(0, 0, 100, 100), 0xFFFF0000);
bitmapData.fillRect(new Rectangle(100, 0, 100, 100), 0xFF00FF00);
bitmapData.fillRect(new Rectangle(0, 100, 100, 100), 0xFF0000FF);
bitmapData.fillRect(new Rectangle(100, 100, 100, 100), 0xFFFFFF00);
var bitmap:Bitmap = new Bitmap(bitmapData);
addChild(bitmap);
}
}
}
Copying Images
package{
import flash.display.*;
import flash.geom.*;
public class Main extends Sprite{
public function Main(){
var shape:Shape = new Shape();
shape.graphics.lineStyle(0, 0, 1);
shape.graphics.drawCircle(100, 100, 100);
shape.graphics.drawCircle(100, 100, 50);
shape.graphics.drawRect(25, 50, 150, 100);
shape.graphics.drawRect(50, 25, 100, 150);
var bitmapDataA:BitmapData = new BitmapData(200, 200, false, 0xFFFFCCCC);
bitmapDataA.draw(shape, new Matrix(), null, null, new Rectangle(0, 0, 100, 100));
var bitmapA:Bitmap = new Bitmap(bitmapDataA);
addChild(bitmapA);
var bitmapDataB:BitmapData = new BitmapData(200, 200, false, 0xFFCCFFCC);
bitmapDataB.draw(shape, new Matrix(), null, null, new Rectangle(100, 0, 100, 100));
var bitmapB:Bitmap = new Bitmap(bitmapDataB);
addChild(bitmapB);
bitmapB.x = 200;
var bitmapDataC:BitmapData = new BitmapData(200, 200, false, 0xFFCCCCFF);
bitmapDataC.draw(shape, new Matrix(), null, null, new Rectangle(0, 100, 100, 100));
var bitmapC:Bitmap = new Bitmap(bitmapDataC);
addChild(bitmapC);
bitmapC.y = 200;
var bitmapDataD:BitmapData = new BitmapData(200, 200, false, 0xFFFFFFCC);
bitmapDataD.draw(shape, new Matrix(), null, null, new Rectangle(100, 100, 100, 100));
var bitmapD:Bitmap = new Bitmap(bitmapDataD);
addChild(bitmapD);
bitmapD.x = 200;
bitmapD.y = 200;
addChild(shape);
shape.x = 100;
shape.y = 100;
}
}
}
Copying Pixels: bitmap.copyPixels(sourceBmp, srcRect, destPoint);
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
public class Main extends Sprite {
private var _bitmap:BitmapData= new BitmapData(stage.stageWidth,stage.stageHeight,false, 0xffffffff);
private var _loader:Loader = new Loader( );
public function Main( ) {
_loader.contentLoaderInfo.addEventListener(Event.ruPLETE, onLoad);
_loader.load(new URLRequest("m.jpg"));
var image:Bitmap = new Bitmap(_bitmap);
addChild(image);
}
public function onLoad(event:Event):void {
var loaderBmp:Bitmap = Bitmap(_loader.content);
var w:Number = loaderBmp.width / 5;
for(var i:int = 0; i < 10; i++) {
_bitmap.copyPixels(loaderBmp.bitmapData,
new Rectangle(i * w, 0,
w, loaderBmp.height),
new Point(i * (w + 2), i));
}
}
}
}
Create bitmap and set pixel
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.filters.BlurFilter;
import flash.events.Event;
import flash.geom.Point;
public class Main extends Sprite {
private var _bitmap:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight,
false, 0xff000000);
private var _bitmap2:BitmapData= new BitmapData(stage.stageWidth, stage.stageHeight,
false, 0xff000000);
private var _image:Bitmap= new Bitmap(_bitmap);
private var _blurFilter:BlurFilter = new BlurFilter( );
public function Main( ) {
addChild(_image);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
for(var i:int = 0; i < 100; i++) {
_bitmap2.setPixel(mouseX + Math.random( ) * 20 - 10,
mouseY + Math.random( ) * 20 - 10,
0xffffffff);
}
_bitmap.applyFilter(_bitmap2, _bitmap.rect, new Point( ), _blurFilter);
}
}
}
Display objects composited into a bitmap
package{
import flash.display.*;
import flash.geom.*;
public class Main extends Sprite{
public function Main(){
var rect:Shape = new Shape( );
rect.graphics.beginFill(0xFF0000);
rect.graphics.drawRect(0,0,25,50);
var ellipse:Shape = new Shape( );
ellipse.graphics.beginFill(0x0000FF);
ellipse.graphics.drawEllipse(0,0,35,25);
var canvas:BitmapData = new BitmapData(100, 100, false, 0xFFFFFFFF);
canvas.draw(rect);
var matrix:Matrix = new Matrix( );
matrix.translate(10, 10);
canvas.draw(ellipse, matrix);
var bmp:Bitmap = new Bitmap(canvas);
addChild(bmp);
}
}
}
Dissolving Between Two Bitmaps
seed = srcBmp.pixelDissolve(destBmp, sourceRect, destPoint,
seed, numPixels, fillColor);
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 _bitmap2:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false, 0xff000000);
private var _image:Bitmap = new Bitmap(_bitmap);
private var _seed:Number = 11111111;
private var _pixelCount:int = 0;
public function Main( ) {
addChild(_image);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
_seed = _bitmap.pixelDissolve(_bitmap2,
_bitmap.rect,
new Point( ),
_seed,
1000);
_pixelCount += 1000;
if(_pixelCount > _bitmap.width * _bitmap.height) {
removeEventListener(Event.ENTER_FRAME,
onEnterFrame);
}
}
}
}
Embedding a bitmap at compile time
package {
import flash.display.*;
import flash.events.*;
import mx.core.BitmapAsset;
[Embed(source="photo.jpg")]
public class Main extends Sprite {
private var Photo:Class;
public function Main( ) {
var photo:BitmapAsset = new Photo( );
addChild(photo);
trace(photo.bitmapData.getPixel(0, 0));
}
}
}
Examining a Bitmap
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var imgData:BitmapData = new BitmapData(20, 20, false, 0xFF0000FF);
trace(imgData.getPixel32(0, 0)); // Displays: 4278190335
}
}
}
Image Smoothing
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var squareData:BitmapData = new BitmapData(200, 200, false, 0xFFCC00CC);
var bitmap:Bitmap = new Bitmap(bitmapData, PixelSnapping.AUTO, false);
bitmap.smoothing = false;
addChild(bitmap);
}
}
}
Pixel Snapping
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var squareData:BitmapData = new BitmapData(200, 200, false, 0xFFCC00CC);
var bitmap:Bitmap = new Bitmap(bitmapData, PixelSnapping.ALWAYS);
bitmap.pixelSnapping = PixelSnapping.NEVER;
addChild(bitmap);
}
}
}
Runtime Bitmap
package {
import flash.net.URLRequest;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Loader;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.events.Event;
public class Main extends Sprite {
private var _loaderA:Loader = new Loader();
private var _loaderB:Loader = new Loader();
private var _loadCount:Number;
public function Main () {
_loadCount = 0;
_loaderA.load(new URLRequest("http://www.wbex.ru/image1.jpg"));
_loaderA.contentLoaderInfo.addEventListener(Event.ruPLETE, completeHandler);
_loaderB.load(new URLRequest("http://www.wbex.ru/image2.jpg"));
_loaderB.contentLoaderInfo.addEventListener(Event.ruPLETE,completeHandler);
}
private function completeHandler(event:Event):void {
_loadCount++;
if(_loadCount == 2) {
mergeImages();
}
}
private function mergeImages():void {
var w:Number = _loaderA.width;
var h:Number = _loaderA.height;
var bitmapDataA:BitmapData = new BitmapData(w, h);
bitmapDataA.draw(_loaderA);
var bitmapDataB:BitmapData = new BitmapData(w, h);
bitmapDataB.draw(_loaderB);
bitmapDataA.merge(bitmapDataB, new Rectangle(0, 0, w, h), new Point(0, 0), 0, 256, 0, 100);
var bitmap:Bitmap = new Bitmap(bitmapDataA);
addChild(bitmap);
}
}
}
Transforming Colors
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
public class Main extends Sprite {
private var _loader:Loader = new Loader();
private var _current:int;
public function Main () {
_loader.load(new URLRequest("http://www.wbex.ru/image.jpg"));
addChild(_loader);
_loader.addEventListener(MouseEvent.CLICK, clickHandler);
_loader.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}
private function mouseMoveHandler(event:MouseEvent):void {
var value:Number = _loader.mouseX / _loader.width;
var colorTransform:ColorTransform = _loader.transform.colorTransform;
if(_current == 0) {
colorTransform.redMultiplier = value;
}
else if(_current == 1) {
colorTransform.greenMultiplier = value;
}
else if(_current == 2) {
colorTransform.blueMultiplier = value;
}
else if(_current == 3) {
colorTransform.alphaMultiplier = value;
}
_loader.transform.colorTransform = colorTransform;
}
private function clickHandler(event:MouseEvent):void {
_current++;
if(_current == 4) {
_current = 0;
}
}
}
}
Use perlinNoise
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,
true, 0xffffffff);
private var _xoffset:int = 0;
public function Main( ) {
addChild(new Bitmap(_bitmap));
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
public function onEnterFrame(event:Event):void {
_xoffset++;
var point:Point = new Point(_xoffset, 0);
_bitmap.perlinNoise(200, 100, 2, 1000, false, true,1, true, [point, point]);
}
}
}