[AS3.0] モザイク

AS3.0で、モザイク表現してみよう! :bouzu:

mosaic

:caution: 要 Flash Player 11.8 以上

Main.as
package {

  import flash.display.BitmapData;
  import flash.display.Bitmap;
  import flash.geom.Matrix;
  import flash.utils.Timer;
  import flash.events.TimerEvent;


  [SWF(backgroundColor="#FFFFFF", width="640", height="500", frameRate="30")]

  public class Main extends Sprite {
    [Embed(source="assets/photo.png")]
    private var PhotoImage:Class;
    // プロパティ
    private static var bw:uint = 600;
    private static var bh:uint = 450;
    private var photo:Bitmap;
    private var bitmapData:BitmapData;
    private var container:Bitmap;
    private var matrix:Matrix;
    private var timer:Timer;
    private static var interval:uint = 100;
    private var dots:Array;
    private var id:uint = 0;
    private static var max:uint = 22;

    // コンストラクタ
    public function Main() {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      init();
    }

    // メソッド
    private function init():void {
      photo = new PhotoImage();
      bitmapData = new BitmapData(bw, bh, false, 0xFF000000);
      container = new Bitmap(bitmapData);
      addChild(container);
      container.x = 20;
      container.y = 30;
      initialize();
      start();
    }
    private function initialize():void {
      matrix = new Matrix();
      dots = [1, 2, 3, 5, 6, 10, 15, 25, 30, 50, 75, 150, 75, 50, 30, 25, 15, 10, 6, 5, 3, 2];
      mosaic(1);
    }
    private function mosaic(dot:uint):void {
      if (dot < 0) return;
      var scale:Number = 1/dot;
      var bd:BitmapData = new BitmapData(bw*scale, bh*scale, false, 0xFF000000);
      matrix.identity();
      matrix.scale(scale, scale);

      bd.lock();
      bd.draw(photo.bitmapData, matrix);
      bd.unlock();
      matrix.identity();
      matrix.scale(dot, dot);

      bitmapData.lock();
      bitmapData.draw(bd, matrix);
      bitmapData.unlock();
      bd.dispose();
    }
    private function start():void {
      timer = new Timer(interval);
      timer.addEventListener(TimerEvent.TIMER, transit, false, 0, true);
      timer.start();
    }
    private function transit(evt:TimerEvent):void {
      id = (id + 1)%max;
      mosaic(dots[id]);
    }

  }

}

Flash CC + Flex 4 SDK でパブリッシュ。