AS3 Pixelate bitmapData


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

Credit for this goes to Terry Paton


Copy this code and paste it in your HTML
  1. package com.terrypaton.effect {
  2.  
  3. import flash.display.*
  4. import flash.geom.Matrix;
  5.  
  6. public class PixelateBitmap {
  7. public function PixelateBitmap ():void {
  8. _instance = this
  9.  
  10. }
  11. public function setup (_bitmapData:BitmapData):void {
  12. _bmpData = _bitmapData
  13. }
  14. public function process (_source:BitmapData,amount:Number):void {
  15.  
  16. var scaleFactor:Number = 1 / amount
  17. var bmpX:int = scaleFactor*_bmpData.width
  18. var bmpY:int = scaleFactor * _bmpData.height
  19. if (bmpX < 1) {
  20. bmpX = 10
  21. }
  22. if (bmpY < 1) {
  23. bmpY = 10
  24. }
  25. // scale image down
  26. _pixelateMatrix.identity ()
  27. _pixelateMatrix.scale (scaleFactor, scaleFactor)
  28. try {
  29. var _tempBmpData:BitmapData = new BitmapData (bmpX,bmpY,false,0xFF0000)
  30. }catch (e:Error) {
  31. trace("bmpX = "+bmpX)
  32. trace("bmpY = "+bmpY)
  33. }
  34.  
  35. _tempBmpData.draw (_source, _pixelateMatrix)
  36. // now scale it back
  37. _pixelateMatrix.identity ()
  38. _pixelateMatrix.scale (amount, amount)
  39.  
  40. _bmpData.draw(_tempBmpData,_pixelateMatrix)
  41. }
  42. public var _pixelateMatrix:Matrix = new Matrix()
  43. public var _bmpData:BitmapData
  44.  
  45. public static function getInstance():PixelateBitmap {
  46. return _instance
  47. }
  48. public static var _instance:PixelateBitmap
  49. }
  50. }
  51.  
  52. // Usage Example
  53. //
  54. // import com.terrypaton.effect.PixelateBitmap;
  55. //
  56. // var myBitmapData:BitmapData = new myImage(320,240); // myImage is a bitmap in the library with Export for ActionScript set
  57. // var myPixelatedBitmapData:BitmapData = new BitmapData(myBitmapData.width,myBitmapData.height);
  58. // var myBitmap:Bitmap = new Bitmap(myPixelatedBitmapData);
  59. // var pixelateBitmap:PixelateBitmap = new PixelateBitmap();
  60. // pixelateBitmap.setup(myPixelatedBitmapData);
  61. //
  62. // addChild(myBitmap);
  63. //
  64. // addEventListener(Event.ENTER_FRAME, loop);
  65. // function loop(event:Event):void {
  66. // var amount:Number = 320 / mouseX;
  67. // if (amount > 0) {
  68. // pixelateBitmap.process(myBitmapData, amount);
  69. // }
  70. // }

URL: http://www.terrypaton.com/as3-pixelate-bitmapdata/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.