/ Published in: ActionScript 3
just slice bitmap class, takes DisplayObject, return array of BitmapData's
Can be used to do something like this: http://burnandbass.com/basstaslider
Can be used to do something like this: http://burnandbass.com/basstaslider
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package { import flash.display.BitmapData; import flash.display.DisplayObject; import flash.geom.Matrix; import flash.geom.Rectangle; public class SliceBitmap { public function SliceBitmap() { /* Author: Chrysto Panayotov ( burnandbass[at]gmail[dot]com ) AS3 util to slice horizontal bitmaps... The names of the static functions are self-explanary, returns array of bitmapData's */ } public static function sliceBitmap(_bitmapToSlice:DisplayObject, pieces:Number = 2, disposeOriginal:Boolean = false):Array{ var slicesArray:Array = []; var tempWidth:Number = 0; for(var i:uint = 0; i< pieces; i++){ var cropMatrix:Matrix = new Matrix(); cropMatrix.translate(-tempWidth,0); var cropRectangle:Rectangle = new Rectangle(0,0,(_bitmapToSlice.width/pieces),_bitmapToSlice.height); var tempBitmap:BitmapData = new BitmapData(_bitmapToSlice.width/pieces,_bitmapToSlice.height); tempBitmap.draw(_bitmapToSlice,cropMatrix,null,null,cropRectangle,true); tempWidth += tempBitmap.width; slicesArray.push(tempBitmap); } return slicesArray; if(disposeOriginal){ _bitmapToSlice.dispose() } } public static function sliceBitmapOffset(_bitmapToSlice:DisplayObject, pieces:Number = 2, disposeOriginal:Boolean = false,piecesOffset:Number = 2):Array{ var slicesArray:Array = []; var tempWidth:Number = 0; for(var i:uint = 0; i< pieces; i++){ var cropMatrix:Matrix = new Matrix(); cropMatrix.translate(-tempWidth,0); var cropRectangle:Rectangle = new Rectangle(0,0,(_bitmapToSlice.width - (pieces-1)*piecesOffset)/pieces,_bitmapToSlice.height); var tempBitmap:BitmapData = new BitmapData( (_bitmapToSlice.width-(pieces-1)*piecesOffset)/pieces,_bitmapToSlice.height); tempBitmap.draw(_bitmapToSlice,cropMatrix,null,null,cropRectangle,true); tempWidth += tempBitmap.width + piecesOffset; slicesArray.push(tempBitmap); } return slicesArray; if(disposeOriginal){ _bitmapToSlice.dispose() } } }//end }