※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

 notice: Flash editor updated! Join the development! Thanks to MiniBuilder


FORKED

[1日1Wonderfl]8日目: Reflection やってみたよ [1日1Wonderfl]10日目: Camera でも Reflection [diff(117)]

  1. // forked from soundkitchen's [1日1Wonderfl]8日目: Reflection やってみたよ
  2. // 1日1Wonderfl 10/30日目
  3. package
  4. {
  5.     import flash.display.Bitmap;
  6.     import flash.display.BitmapData;
  7.     import flash.display.Graphics;
  8.     import flash.display.GradientType;
  9.     import flash.display.Shape;
  10.     import flash.display.Sprite;
  11.     import flash.events.Event;
  12.     import flash.geom.Matrix;
  13.     import flash.media.Camera;
  14.     import flash.media.Video;
  15.     import com.flashdynamix.utils.SWFProfiler;
  16.     [SWF(width=465, height=465, frameRate=30, backgroundColor=0x000000)]
  17.     /**
  18.      *
  19.      */
  20.     public class Main extends Sprite
  21.     {
  22.         public static const CAMERA_WIDTH:Number = 240;
  23.         public static const CAMERA_HEIGHT:Number = 180;
  24.         public static const REFLECT_SCALE:Number = .5;
  25.         public static const REFLECT_MARGIN:Number = 0;
  26.         private var video:Video;
  27.         private var container:Sprite;
  28.         private var film:BitmapData;
  29.         /**
  30.          *
  31.          */
  32.         public function Main()
  33.         {
  34.             addEventListener(Event.ADDED_TO_STAGE, initialize);
  35.         }
  36.         /**
  37.          *
  38.          */
  39.         private function initialize(evt:Event):void
  40.         {
  41.             removeEventListener(Event.ADDED_TO_STAGE, initialize);
  42.             SWFProfiler.init(this);
  43.             container = new Sprite();
  44.             var webCam:Camera,
  45.                 original:Bitmap,
  46.                 reflect:Bitmap,
  47.                 msk:Shape,
  48.                 mtx:Matrix,
  49.                 g:Graphics;
  50.             //  get default web camera.
  51.             webCam = Camera.getCamera();
  52.             //  stop process if couldn't found any camera.
  53.             if (!webCam) return;
  54.             //  setup camera.
  55.             webCam.setMode(CAMERA_WIDTH, CAMERA_HEIGHT, stage.frameRate >> 1);
  56.             //  create and setup video.
  57.             video = new Video(CAMERA_WIDTH, CAMERA_HEIGHT);
  58.             //  reverse and fix position.
  59.             video.scaleX *= -1;
  60.             video.x += CAMERA_WIDTH;
  61.             //  bind camera.
  62.             video.attachCamera(webCam);
  63.             //  create bitmapdata as a snapshot film.
  64.             film = new BitmapData(CAMERA_WIDTH, CAMERA_HEIGHT, true0);
  65.             //  create original bitmap.
  66.             original = new Bitmap(film);
  67.             container.addChild(original);
  68.             //  create reflection bitmap.
  69.             reflect = new Bitmap(film);
  70.             //  reverse and fix position.
  71.             reflect.scaleY *= -1;
  72.             reflect.y = REFLECT_MARGIN + (CAMERA_HEIGHT << 1);
  73.             //  cache for mask and fade.
  74.             reflect.cacheAsBitmap = true;
  75.             container.addChild(reflect);
  76.             //  create mask shape.
  77.             msk = new Shape();
  78.             //  fix position.
  79.             msk.y = CAMERA_HEIGHT + REFLECT_MARGIN;
  80.             //  cache for mask and fade.
  81.             msk.cacheAsBitmap = true;
  82.             //  create matrix.
  83.             mtx = new Matrix();
  84.             mtx.createGradientBox(CAMERA_WIDTH, CAMERA_HEIGHT, Math.PI/2);
  85.             //  draw gradient box.
  86.             g = msk.graphics;
  87.             g.beginGradientFill(
  88.                 GradientType.LINEAR,
  89.                 [0x00, 0x00],
  90.                 [REFLECT_SCALE, 0],
  91.                 [0255 * REFLECT_SCALE],
  92.                 mtx
  93.             );
  94.             g.drawRect(00, CAMERA_WIDTH, CAMERA_HEIGHT);
  95.             g.endFill();
  96.             container.addChild(msk);
  97.             reflect.mask = msk;
  98.             //  fix position.
  99.             container.x = (stage.stageWidth - original.width) >> 1;
  100.             container.y = (stage.stageHeight - original.height) >> 1;
  101.             addChild(container);
  102.             addEventListener(Event.ENTER_FRAME, step);
  103.         }
  104.         private function step(evt:Event):void
  105.         {
  106.             film.lock();
  107.             film.fillRect(film.rect, 0);
  108.             film.draw(video, video.transform.matrix);
  109.             film.unlock();
  110.         }
  111.     }
  112. }
noswf
Get Adobe Flash Player