AS3 Create a TV Static Type Effect Using SetPixel


/ Published in: ActionScript 3



Copy this code and paste it in your HTML
  1. var imgData:BitmapData = new BitmapData(550, 400, false, 0x000000);
  2. var bmp:Bitmap = new Bitmap(imgData);
  3.  
  4. fillBitmapDataWithRandomPixels(imgData);
  5. blackAndWhite(bmp, true);
  6. addChild(bmp);
  7.  
  8. var myTimer:Timer = new Timer(50);
  9. myTimer.addEventListener(TimerEvent.TIMER, onMyTimer_TIMER);
  10. myTimer.start();
  11.  
  12. function onMyTimer_TIMER(event:TimerEvent):void {
  13. fillBitmapDataWithRandomPixels(imgData);
  14. }
  15.  
  16. function blackAndWhite($target:DisplayObject, $enabled:Boolean):void
  17. {
  18. var rc:Number = 1/3;
  19. var gc:Number = 1/3;
  20. var bc:Number = 1/3;
  21. var cmf:ColorMatrixFilter = new ColorMatrixFilter([rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, 0, 0, 0, 1, 0]);
  22.  
  23. if ($enabled) {
  24. $target.filters = [cmf];
  25. } else {
  26. $target.filters = [];
  27. }
  28. }
  29.  
  30. function fillBitmapDataWithRandomPixels($bmp:BitmapData):void {
  31. $bmp.lock();
  32. var colour:uint;
  33. for (var i:int = 0; i < $bmp.height ; i++) {
  34. for (var j:int = 0; j < $bmp.width; j++) {
  35. colour = Math.random() * 0xFFFFFF;
  36. $bmp.setPixel(j, i, colour);
  37. }
  38. }
  39. imgData.unlock();
  40. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.