ActionScript 3 SliceBitmap Class


/ Published in: ActionScript 3
Save to your folder(s)

just slice bitmap class, takes DisplayObject, return array of BitmapData's

Can be used to do something like this: http://burnandbass.com/basstaslider


Copy this code and paste it in your HTML
  1. package {
  2.  
  3. import flash.display.BitmapData;
  4. import flash.display.DisplayObject;
  5. import flash.geom.Matrix;
  6. import flash.geom.Rectangle;
  7.  
  8. public class SliceBitmap {
  9.  
  10. public function SliceBitmap() {
  11. /*
  12. Author: Chrysto Panayotov ( burnandbass[at]gmail[dot]com )
  13. AS3 util to slice horizontal bitmaps... The names of the static functions are self-explanary, returns array of bitmapData's
  14.  
  15. */
  16. }
  17.  
  18. public static function sliceBitmap(_bitmapToSlice:DisplayObject, pieces:Number = 2, disposeOriginal:Boolean = false):Array{
  19. var slicesArray:Array = [];
  20. var tempWidth:Number = 0;
  21. for(var i:uint = 0; i< pieces; i++){
  22. var cropMatrix:Matrix = new Matrix();
  23. cropMatrix.translate(-tempWidth,0);
  24. var cropRectangle:Rectangle = new Rectangle(0,0,(_bitmapToSlice.width/pieces),_bitmapToSlice.height);
  25. var tempBitmap:BitmapData = new BitmapData(_bitmapToSlice.width/pieces,_bitmapToSlice.height);
  26. tempBitmap.draw(_bitmapToSlice,cropMatrix,null,null,cropRectangle,true);
  27. tempWidth += tempBitmap.width;
  28. slicesArray.push(tempBitmap);
  29. }
  30. return slicesArray;
  31. if(disposeOriginal){ _bitmapToSlice.dispose() }
  32. }
  33.  
  34. public static function sliceBitmapOffset(_bitmapToSlice:DisplayObject, pieces:Number = 2, disposeOriginal:Boolean = false,piecesOffset:Number = 2):Array{
  35. var slicesArray:Array = [];
  36. var tempWidth:Number = 0;
  37. for(var i:uint = 0; i< pieces; i++){
  38. var cropMatrix:Matrix = new Matrix();
  39. cropMatrix.translate(-tempWidth,0);
  40. var cropRectangle:Rectangle = new Rectangle(0,0,(_bitmapToSlice.width - (pieces-1)*piecesOffset)/pieces,_bitmapToSlice.height);
  41. var tempBitmap:BitmapData = new BitmapData( (_bitmapToSlice.width-(pieces-1)*piecesOffset)/pieces,_bitmapToSlice.height);
  42. tempBitmap.draw(_bitmapToSlice,cropMatrix,null,null,cropRectangle,true);
  43. tempWidth += tempBitmap.width + piecesOffset;
  44. slicesArray.push(tempBitmap);
  45. }
  46. return slicesArray;
  47. if(disposeOriginal){ _bitmapToSlice.dispose() }
  48. }
  49.  
  50. }//end
  51. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.