[as3] Bitmap Pattern Tool

Dans: Actionscript|Labo

23 juil 2011


[Flash 10]

Juillet 2011

BitmapPatternBuilder, a simple and usefull tool Download

/**
 * @author YopSolo
 */
package
{
	import flash.display.BitmapData;

	public class BitmapPatternBuilder
	{
	/**
	*
	* @parameter pattern:array, pixels position and color index.
	* @parameter colors:arrray, colors.
	* @returns BitmapData
	*
	var colors:Array = [0xFF000000, 0xFFFFFFFF];
	var pattern:BitmapData = build(BitmapPatternBuilder.HORIZONTAL, colors);
	*
	* */
	    public static function build(pattern:Array, colors:Array):BitmapData
		{
			var bitmapW:int = pattern[0].length;
			var bitmapH:int = pattern.length;
			var bmd:BitmapData = new BitmapData(bitmapW, bitmapH, true, 0x00000000);
			for (var yy:int = 0; yy < bitmapH; yy++) {
				for (var xx:int = 0; xx < bitmapW; xx++) {
					var color:int = colors[pattern[yy][xx]];
					bmd.setPixel32(xx, yy, color);
				}
			}

			return bmd;
	    }

		// -- pattern
		public static const HORIZONTAL:Array = [
		[1],
		[0]
		];

		public static const VERTICAL:Array = [
		[1, 0]
		];

		public static const DIAGONAL3x3:Array = [
		[1, 0, 0],
		[0, 0, 1],
		[0, 1, 0]
		];		

		public static const DIAGONAL_4x4:Array = [
		[0, 1, 0, 0],
		[1, 0, 0, 0],
		[0, 0, 0, 1],
		[0, 0, 1, 0],
		];

		public static const STAR_5x5:Array = [
		[0, 0, 0, 0, 0],
		[0, 0, 1, 0, 0],
		[0, 1, 0, 1, 0],
		[0, 0, 1, 0, 0],
		[0, 0, 0, 0, 0]
		];		

		public static const SQUARE_5x5:Array = [
		[0, 1, 0, 1, 0],
		[1, 1, 0, 1, 1],
		[0, 0, 0, 0, 0],
		[1, 1, 0, 1, 1],
		[0, 1, 0, 1, 0]
		];	

		public static const CIRCLE_5x5:Array = [
		[0, 0, 1, 0, 0],
		[0, 1, 0, 1, 0],
		[1, 0, 0, 0, 1],
		[0, 1, 0, 1, 0],
		[0, 0, 1, 0, 0]
		];			

		public static const WAVE_5x5:Array = [
		[0, 0, 0, 0, 0],
		[0, 1, 0, 0, 0],
		[0, 0, 0, 0, 0],
		[0, 0, 0, 1, 0],
		[1, 0, 0, 0, 0]
		];			

		public static const GRID_5x5:Array = [
		[1, 0, 0, 0, 1],
		[0, 1, 0, 1, 0],
		[0, 0, 1, 0, 0],
		[0, 1, 0, 1, 0],
		[1, 0, 0, 0, 1]
		];		

		//... create your custom patterns here...
	}
}

StreetFighter/Tron ArtWork from bosslogic


You can use this tool in many ways :
.you can add it on the top of another displayObject :

var shape:Shape = new Shape;
shape.graphics.beginBitmapFill( BitmapPatternBuilder.build( BitmapPatternBuilder.GRID_5x5 , [0xFF000000, 0xFFFFFFFF] ) );
shape.graphics.drawRect( 0, 0, 256, 256 );
shape.graphics.endFill();
shape.blendMode = BlendMode.OVERLAY;
addChild( shape );

.You can apply a full or semi transparent pattern to any Bitmap with the copyPixels and take advantage of the alpha merge.

.You can also obtain cool result by using a transform Matrix when you draw your bitmap pattern.



les commentaires sont fermés.