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


embed

FORKED
  1. // forked from y_tti's てらこ23で発表したものの説明2
  2. /*
  3. マトリックスを使って台形な変形
  4. 4点のポイントを用意して、その位置を元に変形。
  5. 台形といっても三角形が2つ合体させてます。
  6. だらだら長いソースになってしまったけど、
  7. _draw
  8. _getTransformMatrix
  9. の部分が重要なんで、他は無視無視無視無視無視無視無視無視!!
  10. 格子状にしたら面白いんじゃないかとやってみた。
  11. 分割した矩形の1個目の描画部分が良く分からず・・・。
  12. わざわざ、bitmapDataを別に用意して、矩形にコピーしてそれを変形させて_canvasに再描画しているが
  13. 2個目の分割した矩形はそんなことをせず、直接_canvasに変形させて描画しても問題ない。
  14. 1個目は直接変形させて描画すると、順番がめちゃめちゃになる。
  15. テスト用画像を使ってみるとよく分かる。
  16. 出来そうで出来ないのが悔しい
  17. */
  18. package
  19. {
  20.     import flash.display.Bitmap;
  21.     import flash.display.BitmapData;
  22.     import flash.display.Loader;
  23.     import flash.display.Sprite;
  24.     import flash.events.Event;
  25.     import flash.events.MouseEvent;
  26.     import flash.geom.Matrix;
  27.     import flash.geom.Point;
  28.     import flash.net.URLRequest;
  29.     import flash.system.LoaderContext;
  30.     
  31.     import net.hires.debug.Stats;
  32.     
  33.     [SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "0x000000")]
  34.     public class box extends Sprite
  35.     {
  36.         private var _container:Sprite;
  37.         private var _canvas:Sprite;
  38.         private var _bmd:BitmapData;
  39.         private var _stageW:Number = 465;
  40.         private var _stageH:Number = 465;
  41.         private var _w:Number;
  42.         private var _cP0:CirclePoint;
  43.         private var _cP1:CirclePoint;
  44.         private var _cP2:CirclePoint;
  45.         private var _cP3:CirclePoint;
  46.         private var _matrixArray:Array;
  47.         //格子のどこに画像が描画されているかを見るためのテスト用画像
  48.         //private const IMAGE_URL:String = "http://www.planet-ape.net/wonderfl/base.jpg";        
  49.         
  50.         private const IMAGE_URL:String = "http://www.planet-ape.net/wonderfl/kuri.jpg";        
  51.         
  52.         public function box()
  53.         {
  54.             if (stage) init();
  55.             else addEventListener(Event.ADDED_TO_STAGE, init);
  56.         }
  57.         
  58.         private function init(e:Event = null):void 
  59.         {
  60.             removeEventListener(Event.ADDED_TO_STAGE, init);
  61.             //画像の読み込み
  62.             var req:URLRequest = new URLRequest(IMAGE_URL);
  63.             var loader:Loader = new Loader();
  64.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);    
  65.             loader.load( req, new LoaderContext(true));
  66.             // take a capture after 10 sec
  67.             //Wonderfl.capture_delay( 120 );
  68.         }
  69.         
  70.         //画像読み込み後の処理
  71.         private function loadComplete(e:Event):void 
  72.         {
  73.             e.target.removeEventListener(Event.COMPLETE, loadComplete);
  74.         
  75.             //取得した画像を切り取って_bmdに入れる
  76.             _bmd = new BitmapData (350350true, 0xFFFFFF);
  77.             _bmd.draw (e.target.loader.content.bitmapData);
  78.             
  79.             //キャンバスを用意してコンテナに入れる(重要じゃないので無視無視)
  80.             _canvas = new Sprite();
  81.             _container = new Sprite();
  82.             _container.addChild(_canvas);
  83.             addChild(_container);
  84.             _container.x = (_stageW - _bmd.width)/2;
  85.             _container.y = (_stageH - _bmd.height)/2;
  86.             
  87.             //画像の大きさに合わせ、ポイントを5x5の格子に配置する(配列に入れる)
  88.             //CirclePointのコンストラクタの中で初期状態のポイント位置を保持してます。(initPoint)
  89.             //その初期ポイントと、マウスドラッグで移動したポイントを比較してるのが、_drawの中。
  90.             _w = _bmd.width / 5;
  91.             _matrixArray = new Array ();
  92.             //x軸とy軸の2重ループ(配列に入れる
  93.             for (var j:int = 0; j <= _bmd.width; j += _w) {
  94.                 for (var i:int = 0; i <= _bmd.height; i += _w) {
  95.                     var cp:CirclePoint = new CirclePoint (i, j);
  96.                     trace ('cp'+i+':' + cp.initPoint);
  97.                     _matrixArray.push (cp);
  98.                     //ポイントのドラッグとかの設定
  99.                     //※ドラッグした時に_drawを呼び出してます。
  100.                     cp.addEventListener(MouseEvent.MOUSE_DOWN , _cPMouseDownHandler );
  101.                     _container.addChild (cp);
  102.                 }
  103.             }
  104.             _draw();
  105.             
  106.             //速度とかメモリとかチェック(重要じゃないので無視無視)
  107.             addChild(new Stats());
  108.         }
  109.         
  110.         private function _draw():void {
  111.             /**
  112.              * ポイントの番号は↓な感じ。
  113.              *   0--------1
  114.              *   ----------
  115.              *   ----------
  116.              *   2--------3
  117.              */            
  118.             //マトリックスを適応
  119.             _canvas.graphics.clear();
  120.             //配列のループ
  121.             for (var k:String in _matrixArray) {
  122.                 //
  123.                 if (int(k) > 29break;
  124.                 if ((int (k) + 1) % 6 == 0continue;
  125.                 //初期状態のポイント取得
  126.                 _cP0 = _matrixArray[int (k)];
  127.                 _cP1 = _matrixArray[int (k) + 1];
  128.                 _cP2 = _matrixArray[int (k) + 6];
  129.                 _cP3 = _matrixArray[int (k) + 7];
  130.                 
  131.                 var aP0:Point = _cP0.initPoint;
  132.                 var aP1:Point = _cP1.initPoint;
  133.                 var aP2:Point = _cP2.initPoint;
  134.                 var aP3:Point = _cP3.initPoint;
  135.                 //初期状態のポイントよりマトリックス取得
  136.                 var initMatrix1:Matrix = _getTransformMatrix( aP0 , aP1 , aP2 );
  137.                 var initMatrix2:Matrix = _getTransformMatrix( aP3 , aP2 , aP1 );
  138.                 //変更状態のポイント取得
  139.                 var bP0:Point = new Point(_cP0.x , _cP0.y );
  140.                 var bP1:Point = new Point(_cP1.x , _cP1.y );
  141.                 var bP2:Point = new Point(_cP2.x , _cP2.y );
  142.                 var bP3:Point = new Point (_cP3.x , _cP3.y );
  143.                 //変更状態のポイントよりマトリックス取得
  144.                 var editMatrix1:Matrix = _getTransformMatrix( bP0 , bP1 , bP2 );
  145.                 var editMatrix2:Matrix = _getTransformMatrix( bP3 , bP2 , bP1 );
  146.                 //初期状態と変更状態のマトリックスを合体
  147.                 var newMatrix1:Matrix = initMatrix1.clone();
  148.                 newMatrix1.concat(editMatrix1);
  149.                 var newMatrix2:Matrix = initMatrix2.clone();
  150.                 newMatrix2.concat(editMatrix2);
  151.                 
  152.                 //(無理やり)分割した1個目の描写のロジック
  153.                 //2個目はわざわざ、別のbitmapDataを用意しなくても直接描画できる。
  154.                 //1個目は別のbitmapDataを用意ないと順番がめちゃくちゃになる。
  155.                 var matBmd:BitmapData = new BitmapData (7070true);
  156.                 var mat:Matrix = new Matrix ();
  157.                 mat.tx = - aP0.x;
  158.                 mat.ty = - aP0.y;
  159.                 matBmd.draw (_bmd, mat);
  160.                 //分割した1個目を描画
  161.                 _canvas.graphics.lineStyle(1 , 0x00FF00);
  162.                 _canvas.graphics.beginBitmapFill(matBmd , newMatrix1 );
  163.                 _canvas.graphics.moveTo(bP0.x , bP0.y );
  164.                 _canvas.graphics.lineTo(bP1.x , bP1.y );
  165.                 _canvas.graphics.lineTo(bP2.x , bP2.y );
  166.                 _canvas.graphics.endFill ();
  167.                 
  168.                 //分割した2個目を描画
  169.                 _canvas.graphics.lineStyle(1 , 0x00FF00);
  170.                 _canvas.graphics.beginBitmapFill(_bmd , newMatrix2 );
  171.                 _canvas.graphics.moveTo(bP3.x , bP3.y );
  172.                 _canvas.graphics.lineTo(bP1.x , bP1.y );
  173.                 _canvas.graphics.lineTo(bP2.x , bP2.y );
  174.                 _canvas.graphics.endFill();
  175.             }            
  176.         }
  177.         
  178.         
  179.         private function _getTransformMatrix($pt0:Point, $pt1:Point, $pt2:Point):Matrix
  180.         {
  181.             /*
  182.             http://www.adobe.com/jp/devnet/flash/articles/matrix_class.html
  183.             a: 水平方向の伸縮率 = 変換後の幅/もとの幅
  184.             b: 垂直方向の傾斜率 = 垂直方向の傾斜/もとの幅
  185.             c: 水平方向の傾斜率 = 水平方向の傾斜/もとの高さ
  186.             d: 垂直方向の伸縮率 = 変換後の高さ/もとの高さ
  187.             tx: 水平方向の移動ピクセル数
  188.             ty: 垂直方向の移動ピクセル数
  189.             */
  190.             var w:Number = _w;
  191.             var h:Number = _w;
  192.             var mat:Matrix = new Matrix();
  193.             mat.a = ($pt1.x - $pt0.x) / w;
  194.             mat.b = ($pt1.y - $pt0.y) / w;
  195.             mat.c = ($pt2.x - $pt0.x) / h;
  196.             mat.d = ($pt2.y - $pt0.y) / h;
  197.             mat.tx = $pt0.x;
  198.             mat.ty = $pt0.y;
  199.             return mat;
  200.         }
  201.         
  202.         //こっからマウスのドラッグとかの設定(無視無視)
  203.         private function _cPMouseDownHandler(e:MouseEvent):void {
  204.             CirclePoint(e.target).startDrag();
  205.             CirclePoint(e.target).addEventListener(MouseEvent.MOUSE_MOVE , _cPMouseMoveHandler );
  206.             stage.addEventListener(MouseEvent.MOUSE_UP , _cPMouseUpHandler );
  207.         }
  208.         
  209.         private function _cPMouseUpHandler(e:MouseEvent):void {
  210.             CirclePoint(e.target).stopDrag();
  211.             CirclePoint(e.target).removeEventListener(MouseEvent.MOUSE_MOVE , _cPMouseMoveHandler );
  212.             stage.removeEventListener(MouseEvent.MOUSE_UP , _cPMouseUpHandler );
  213.         }
  214.         
  215.         private function _cPMouseMoveHandler(e:MouseEvent):void {
  216.             _draw();
  217.         }
  218.         
  219.         
  220.     }
  221. }
  222. import flash.display.Bitmap;
  223. import flash.display.Shape;
  224. import flash.display.BitmapData;
  225. import flash.display.Sprite;
  226. import flash.geom.Point;
  227. /**
  228.  * 赤い点を書いてる。
  229.  * それと初期状態のポイントをinitPointに保持してる。
  230.  */
  231. class CirclePoint extends Sprite {
  232.     public var initPoint:Point;
  233.     public function CirclePoint($x:Number , $y:Number) {
  234.         this.x = $x;
  235.         this.y = $y;
  236.         this.initPoint = new Point( $x , $y );
  237.         this.graphics.beginFill(0xFF0000,1);
  238.         this.graphics.drawCircle(0,0,5);
  239.         this.graphics.endFill();
  240.         this.buttonMode = true;
  241.     }
  242. }
  243. /**
  244.  * このTestImageクラスは画像の代わりに無理矢理、
  245.  * BitmapDataを作成してるだけなので無視無視無視無視無視。
  246.  */
  247. class TestImage extends Bitmap {
  248.     private var _w:Number = 200;
  249.     private var _h:Number = 200;
  250.     public function TestImage():void {
  251.         var s:Shape = new Shape();
  252.         s.graphics.beginFill(0xCCCCCC , 1 );
  253.         s.graphics.drawRect(0,0,_w,_h);
  254.         s.graphics.beginFill(0xFCFCFC , 1 );
  255.         s.graphics.drawRoundRect(30 , 30 , 30 , 10 , 20 ,20);
  256.         s.graphics.drawRoundRect(140 , 30 , 30 , 10 , 20 ,20);
  257.         s.graphics.drawCircle(50,70,10);
  258.         s.graphics.drawCircle(150,70,10);
  259.         s.graphics.drawCircle(75,120,5);
  260.         s.graphics.drawCircle(125,120,5);
  261.         s.graphics.drawRoundRect(40 , 170 , 120 , 10 , 20 ,20);
  262.         s.graphics.endFill();
  263.         
  264.         var bmd:BitmapData = new BitmapData(_w,_h);
  265.         bmd.draw(s);
  266.         this.bitmapData = bmd;
  267.     }
  268. }
noswf

てらこ23で発表したものの説明2 forked from: てらこ23で発表したものの説明2 [diff(3)]

  1. // forked from y_tti's てらこ23で発表したものの説明2
  2. /*
  3. マトリックスを使って台形な変形
  4. 4点のポイントを用意して、その位置を元に変形。
  5. 台形といっても三角形が2つ合体させてます。
  6. だらだら長いソースになってしまったけど、
  7. _draw
  8. _getTransformMatrix
  9. の部分が重要なんで、他は無視無視無視無視無視無視無視無視!!
  10. */
  11. package
  12. {
  13.     import flash.display.Bitmap;
  14.     import flash.display.BitmapData;
  15.     import flash.display.Sprite;
  16.     import flash.events.MouseEvent;
  17.     import flash.geom.Matrix;
  18.     import flash.geom.Point;
  19.     
  20.     import net.hires.debug.Stats;
  21.     
  22.     [SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "0x000000")]
  23.     public class Teraco23_Matrix2 extends Sprite
  24.     {
  25.         private var _container:Sprite;
  26.         private var _canvas:Sprite;
  27.         private var _bmd:BitmapData;
  28.         private var _stageW:Number = 465;
  29.         private var _stageH:Number = 465;
  30.         
  31.         private var _cP0:CirclePoint;
  32.         private var _cP1:CirclePoint;
  33.         private var _cP2:CirclePoint;
  34.         private var _cP3:CirclePoint;
  35.         
  36.         public function Teraco23_Matrix2()
  37.         {
  38.             //テスト用のBitmapData取得(重要じゃないので無視無視)
  39.             _bmd = Bitmap(new TestImage()).bitmapData;
  40.             
  41.             //キャンバスを用意してコンテナに入れる(重要じゃないので無視無視)
  42.             _canvas = new Sprite();
  43.             _container = new Sprite();
  44.             _container.addChild(_canvas);
  45.             addChild(_container);
  46.             _container.x = (_stageW - _bmd.width)/2;
  47.             _container.y = (_stageH - _bmd.height)/2;
  48.             
  49.             //画像の大きさに合わせ、3点のポイントを配置(左上、右上、左下の3点)
  50.             //CirclePointのコンストラクタの中で初期状態のポイント位置を保持してます。(initPoint)
  51.             //その初期ポイントと、マウスドラッグで移動したポイントを比較してるのが、_drawの中。
  52.             _cP0 = new CirclePoint(0,0);
  53.             _cP1 = new CirclePoint(_bmd.width,0);
  54.             _cP2 = new CirclePoint(0,_bmd.height);
  55.             _cP3 = new CirclePoint(_bmd.width,_bmd.height);
  56.             _container.addChild(_cP0);
  57.             _container.addChild(_cP1);
  58.             _container.addChild(_cP2);
  59.             _container.addChild(_cP3);
  60.             
  61.             //ポイントのドラッグとかの設定
  62.             //※ドラッグした時に_drawを呼び出してます。
  63.             _cP0.addEventListener(MouseEvent.MOUSE_DOWN , _cPMouseDownHandler );
  64.             _cP1.addEventListener(MouseEvent.MOUSE_DOWN , _cPMouseDownHandler );
  65.             _cP2.addEventListener(MouseEvent.MOUSE_DOWN , _cPMouseDownHandler );
  66.             _cP3.addEventListener(MouseEvent.MOUSE_DOWN , _cPMouseDownHandler );
  67.             
  68.             _draw();
  69.             
  70.             //速度とかメモリとかチェック(重要じゃないので無視無視)
  71.             addChild(new Stats());
  72.         }
  73.         
  74.         private function _draw():void {
  75.             /**
  76.              * ポイントの番号は↓な感じ。
  77.              *   1--------2
  78.              *   ----------
  79.              *   ----------
  80.              *   3--------4
  81.              */            
  82.             //初期状態のポイント取得
  83.             var aP0:Point = _cP0.initPoint;
  84.             var aP1:Point = _cP1.initPoint;
  85.             var aP2:Point = _cP2.initPoint;
  86.             var aP3:Point = _cP3.initPoint;
  87.             //初期状態のポイントよりマトリックス取得
  88.             var initMatrix1:Matrix = _getTransformMatrix( aP0 , aP1 , aP2 );
  89.             var initMatrix2:Matrix = _getTransformMatrix( aP3 , aP2 , aP1 );
  90.             
  91.             //変更状態のポイント取得
  92.             var bP0:Point = new Point(_cP0.x , _cP0.y );
  93.             var bP1:Point = new Point(_cP1.x , _cP1.y );
  94.             var bP2:Point = new Point(_cP2.x , _cP2.y );
  95.             var bP3:Point = new Point(_cP3.x , _cP3.y );
  96.             //変更状態のポイントよりマトリックス取得
  97.             var editMatrix1:Matrix = _getTransformMatrix( bP0 , bP1 , bP2 );
  98.             var editMatrix2:Matrix = _getTransformMatrix( bP3 , bP2 , bP1 );
  99.             
  100.             //初期状態と変更状態のマトリックスを合体
  101.             var newMatrix1:Matrix = initMatrix1.clone();
  102.             newMatrix1.concat(editMatrix1);
  103.             var newMatrix2:Matrix = initMatrix2.clone();
  104.             newMatrix2.concat(editMatrix2);
  105.             
  106.             //マトリックスを適応
  107.             _canvas.graphics.clear();
  108.             
  109.             //分割した1個目を描画
  110.             _canvas.graphics.lineStyle(1 , 0xFFFF00);
  111.             _canvas.graphics.beginBitmapFill(_bmd , newMatrix1 );
  112.             _canvas.graphics.moveTo(bP0.x , bP0.y );
  113.             _canvas.graphics.lineTo(bP1.x , bP1.y );
  114.             _canvas.graphics.lineTo(bP2.x , bP2.y );
  115.             _canvas.graphics.endFill();
  116.             
  117.             //分割した2個目を描画
  118.             _canvas.graphics.lineStyle(1 , 0xFFFF00);
  119.             _canvas.graphics.beginBitmapFill(_bmd , newMatrix2 );
  120.             _canvas.graphics.moveTo(bP3.x , bP3.y );
  121.             _canvas.graphics.lineTo(bP1.x , bP1.y );
  122.             _canvas.graphics.lineTo(bP2.x , bP2.y );
  123.             _canvas.graphics.endFill();
  124.         }
  125.         
  126.         
  127.         private function _getTransformMatrix($pt0:Point, $pt1:Point, $pt2:Point):Matrix
  128.         {
  129.             /*
  130.             http://www.adobe.com/jp/devnet/flash/articles/matrix_class.html
  131.             a: 水平方向の伸縮率 = 変換後の幅/もとの幅
  132.             b: 垂直方向の傾斜率 = 垂直方向の傾斜/もとの幅
  133.             c: 水平方向の傾斜率 = 水平方向の傾斜/もとの高さ
  134.             d: 垂直方向の伸縮率 = 変換後の高さ/もとの高さ
  135.             tx: 水平方向の移動ピクセル数
  136.             ty: 垂直方向の移動ピクセル数
  137.             */
  138.             var w:Number = _bmd.width;
  139.             var h:Number = _bmd.height;
  140.             var mat:Matrix = new Matrix();
  141.             mat.a = ($pt1.x - $pt0.x) / w;
  142.             mat.b = ($pt1.y - $pt0.y) / w;
  143.             mat.c = ($pt2.x - $pt0.x) / h;
  144.             mat.d = ($pt2.y - $pt0.y) / h;
  145.             mat.tx = $pt0.x;
  146.             mat.ty = $pt0.y;
  147.             return mat;
  148.         }
  149.         
  150.         //こっからマウスのドラッグとかの設定(無視無視)
  151.         private function _cPMouseDownHandler(e:MouseEvent):void {
  152.             CirclePoint(e.target).startDrag();
  153.             CirclePoint(e.target).addEventListener(MouseEvent.MOUSE_MOVE , _cPMouseMoveHandler );
  154.             stage.addEventListener(MouseEvent.MOUSE_UP , _cPMouseUpHandler );
  155.         }
  156.         
  157.         private function _cPMouseUpHandler(e:MouseEvent):void {
  158.             CirclePoint(e.target).stopDrag();
  159.             CirclePoint(e.target).removeEventListener(MouseEvent.MOUSE_MOVE , _cPMouseMoveHandler );
  160.             stage.removeEventListener(MouseEvent.MOUSE_UP , _cPMouseUpHandler );
  161.         }
  162.         
  163.         private function _cPMouseMoveHandler(e:MouseEvent):void {
  164.             _draw();
  165.         }
  166.         
  167.         
  168.     }
  169. }
  170. import flash.display.Bitmap;
  171. import flash.display.Shape;
  172. import flash.display.BitmapData;
  173. import flash.display.Sprite;
  174. import flash.geom.Point;
  175. /**
  176.  * 赤い点を書いてる。
  177.  * それと初期状態のポイントをinitPointに保持してる。
  178.  */
  179. class CirclePoint extends Sprite {
  180.     public var initPoint:Point;
  181.     public function CirclePoint($x:Number , $y:Number) {
  182.         this.x = $x;
  183.         this.y = $y;
  184.         this.initPoint = new Point( $x , $y );
  185.         this.graphics.beginFill(0xFF0000,1);
  186.         this.graphics.drawCircle(0,0,10);
  187.         this.graphics.endFill();
  188.         this.buttonMode = true;
  189.     }
  190. }
  191. /**
  192.  * このTestImageクラスは画像の代わりに無理矢理、
  193.  * BitmapDataを作成してるだけなので無視無視無視無視無視。
  194.  */
  195. class TestImage extends Bitmap {
  196.     private var _w:Number = 200;
  197.     private var _h:Number = 200;
  198.     public function TestImage():void {
  199.         var s:Shape = new Shape();
  200.         s.graphics.beginFill(0xCCCCCC , 1 );
  201.         s.graphics.drawRect(0,0,_w,_h);
  202.         s.graphics.beginFill(0xFCFCFC , 1 );
  203.         s.graphics.drawRoundRect(30 , 30 , 30 , 10 , 20 ,20);
  204.         s.graphics.drawRoundRect(140 , 30 , 30 , 10 , 20 ,20);
  205.         s.graphics.drawCircle(50,70,10);
  206.         s.graphics.drawCircle(150,70,10);
  207.         s.graphics.drawCircle(75,120,5);
  208.         s.graphics.drawCircle(125,120,5);
  209.         s.graphics.drawRoundRect(40 , 170 , 120 , 10 , 20 ,20);
  210.         s.graphics.endFill();
  211.         
  212.         var bmd:BitmapData = new BitmapData(_w,_h);
  213.         bmd.draw(s);
  214.         this.bitmapData = bmd;
  215.     }
  216. }
noswf
Get Adobe Flash Player