Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * This software is released under the MIT License
  3.  *
  4.  * Copyright (c) 2009 Philippe Van Kessel
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person
  7.  * obtaining a copy of this software and associated documentation
  8.  * files (the "Software"), to deal in the Software without
  9.  * restriction, including without limitation the rights to use,
  10.  * copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the
  12.  * Software is furnished to do so, subject to the following
  13.  * conditions:
  14.  *
  15.  * The above copyright notice and this permission notice shall be
  16.  * included in all copies or substantial portions of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22.  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23.  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  25.  * OTHER DEALINGS IN THE SOFTWARE.
  26.  */
  27.  
  28. package com.phillippevk.utils
  29. {
  30. import flash.display.Bitmap;
  31. import flash.display.IBitmapDrawable;
  32.     import flash.display.Sprite;
  33.    
  34.     import flash.display.BitmapData;
  35.     import flash.geom.Point;
  36.     import flash.geom.Matrix;
  37. import flash.geom.Rectangle;
  38.  
  39. public class DistortGrid
  40.     {
  41.         // Reference to the BitmapData capture of the source MovieClip.
  42.         // You should never change the value of this property.
  43.         public var _source:BitmapData;
  44.        
  45.         // Reference to the MovieClip containing the distorted image.
  46.         // If you change it, future calls to the render method will render to this MovieClip.
  47.         public var _container:Sprite;
  48.        
  49.         // Points array. Organized like this _pp[row][column].
  50.         // After you change points, call the render method for the changes to take effect.
  51.         public var _pp:Array;
  52.        
  53.         // Number of columns (horizontal precision), number of rows (vertical position). Should never be set.
  54.         // You should never change the value of these properties.
  55.         public var _nc:int, _nr:int;
  56.        
  57.         // Width and height of the source MovieClip or BitmapData.
  58.         // You should never change the value of these properties.
  59.         protected var _w:Number, _h:Number;
  60.        
  61.         // Normal width and height of the triangles (when there is no distorsion).
  62.         // You should never change the value of these properties.
  63.         protected var _lx:Number, _ly:Number;
  64.        
  65.         // Controls whether to apply smoothing or not.
  66.         // Call the render method after changing the value of this property for the change to take effect.
  67.         public var _smoothing:Boolean;
  68.        
  69.         // Controls the line style of the triangles.
  70.         // Call the render method after changing the value of those properties for the changes to take effect.
  71.         public var _lineThickness:Number, _lineColor:uint, _lineAlpha:Number; // visual options (smoothing, line style)
  72.  
  73.         //public var dest:BitmapData;
  74.         //public var destB:Bitmap;
  75.        
  76.         // ---------------------------------------------------------------------------------------------------------
  77.         //  Initialization
  78.         // ---------------------------------------------------------------------------------------------------------
  79.        
  80.         /**
  81.          * Constructor.
  82.          *
  83.          * @param   $container  The MovieClip which will contain the distorted Bit
  84.          * @param   $source     The BitmapData, Sprite or Shape to distort
  85.          * @param   $nc         The number of columns (horizontal precision). Default value is 1
  86.          * @param   $nr         The number of rows (vertical precision). Default value is 1
  87.          */
  88.         public function DistortGrid($container:Sprite, $source:*, $nc:int, $nr:int)
  89.         {
  90.             if ($source is BitmapData)
  91.             {
  92.                 _source = $source;
  93.             }
  94.             else
  95.             {
  96.                 _source = new BitmapData($source.width, $source.height, true, 0x00000000);
  97.                 _source.draw($source);
  98.             }
  99.             //dest = new BitmapData($source.width, $source.height, true, 0x00000000);
  100.             //destB = new Bitmap(dest);
  101.             _container = $container;
  102.             //_container.addChild(destB);
  103.            
  104.             _pp = [];
  105.             _nc = (isNaN($nc) || $nc < 1) ? 1 : $nc;
  106.             _nr = (isNaN($nr) || $nr < 1) ? 1 : $nr;
  107.            
  108.             _w = $source.width;
  109.             _h = $source.height;
  110.             _lx = _w / _nc;
  111.             _ly = _h / _nr;
  112.            
  113.             _smoothing = false;
  114.             _lineThickness = 0;
  115.             _lineColor = 0x000000;
  116.             _lineAlpha = 0;
  117.            
  118.             // -- Create points --------------------------------------------------
  119.             //
  120.                 var ir:int, ic:int, x:Number, y:Number;
  121.                
  122.                 for (ir = 0 ; ir < _nr + 1 ; ir++)
  123.                 {
  124.                     y = _h / _nr * ir;
  125.                     _pp[ir] = [];
  126.                    
  127.                     for (ic = 0 ; ic < _nc + 1 ; ic++)
  128.                     {
  129.                         x = _w / _nc * ic;
  130.                         _pp[ir][ic] = new Point(x, y);
  131.                     }
  132.                 }
  133.             //
  134.             // -------------------------------------------------------------------
  135.            
  136.             render();
  137.         }
  138.        
  139.         /**
  140.          * Clears every property to make them elibigle for garbage collection.
  141.          */
  142.         public function clear():void
  143.         {
  144.             var ir:int, ic:int;
  145.            
  146.             for (ir = 0 ; ir < _nr + 1 ; ir++)
  147.             {
  148.                 for (ic = 0 ; ic < _nc + 1 ; ic++)
  149.                     _pp[ir][ic] = null;
  150.                
  151.                 _pp[ir] = null;
  152.             }
  153.            
  154.             _pp = null;
  155.             _nc = 0;
  156.             _nr = 0;
  157.            
  158.             _w = 0;
  159.             _h = 0;
  160.             _lx = 0;
  161.             _ly = 0;
  162.            
  163.             _smoothing = false;
  164.             _lineThickness = 0;
  165.             _lineColor = 0;
  166.             _lineAlpha = 0;
  167.            
  168.             _container = null;
  169.             _source = null;
  170.         }
  171.        
  172.        
  173.        
  174.         // ---------------------------------------------------------------------------------------------------------
  175.         //  Public methods
  176.         // ---------------------------------------------------------------------------------------------------------
  177.        
  178.         /**
  179.          * Changes the distorted BitmapData or MovieClip
  180.          *
  181.          * @param   $source The BitmapData or the MovieClip to distort
  182.          */
  183.         public function setSource($source):void
  184.         {
  185.             if ($source is BitmapData)
  186.             {
  187.                 _source = $source;
  188.             }
  189.             else
  190.             {
  191.                 _source = new BitmapData($source.width, $source.height, true, 0x00FFFFFF);
  192.                 _source.draw($source);
  193.             }
  194.            
  195.             _w = _source.width;
  196.             _h = _source.height;
  197.             _lx = _w / _nc;
  198.             _ly = _h / _nr;
  199.            
  200.             render();
  201.         }
  202.        
  203.         /**
  204.          * Renders the distorted image. Must be called for any change to take effect (NOT automatically called)
  205.          */
  206.         public function render():void
  207.         {
  208.             var ir:int, ic:int, tx1:Number, ty1:Number, m:Matrix = new Matrix();
  209.             var p1:Point, p2:Point, p3:Point, p4:Point;
  210.  
  211.             //dest.fillRect(new Rectangle(0,0,dest.width, dest.height), 0x00000000);
  212.             _container.graphics.clear();
  213.             _container.graphics.lineStyle(_lineThickness, _lineColor, _lineAlpha);
  214.            
  215.             for (ir = 0 ; ir < _nr ; ir++)
  216.             {
  217.                 ty1 = -_ly * ir;
  218.                
  219.                 for (ic = 0 ; ic < _nc ; ic++)
  220.                 {
  221.                     p1 = _pp[ir][ic];
  222.                     p2 = _pp[ir][ic + 1];
  223.                     p3 = _pp[ir + 1][ic];
  224.                     p4 = _pp[ir + 1][ic + 1];
  225.                    
  226.                     tx1 = -_lx * ic;
  227.                    
  228.                     m.tx = p1.x;
  229.                     m.ty = p1.y;
  230.                     m.a = (p2.x - m.tx) / _lx;
  231.                     m.b = (p2.y - m.ty) / _lx;
  232.                     m.c = (p3.x - m.tx) / _ly;
  233.                     m.d = (p3.y - m.ty) / _ly;
  234.                    
  235.                     // Concat shortcut
  236.                     m.tx += tx1 * m.a + ty1 * m.c;
  237.                     m.ty += tx1 * m.b + ty1 * m.d;
  238.  
  239.                     //dest.draw(_source, m);
  240.                     _container.graphics.beginBitmapFill(_source, m, false, _smoothing);
  241.                     _container.graphics.moveTo(p1.x, p1.y);
  242.                     _container.graphics.lineTo(p2.x, p2.y);
  243.                     _container.graphics.lineTo(p3.x, p3.y);
  244.                     _container.graphics.endFill();
  245.  
  246.                     m.tx = p3.x + (p2.x - p4.x);
  247.                     m.ty = p3.y + (p2.y - p4.y);
  248.                     m.a = (p2.x - m.tx) / _lx;
  249.                     m.b = (p2.y - m.ty) / _lx;
  250.                     m.c = (p3.x - m.tx) / _ly;
  251.                     m.d = (p3.y - m.ty) / _ly;
  252.                    
  253.                     // Concat shortcut
  254.                     m.tx += tx1 * m.a + ty1 * m.c;
  255.                     m.ty += tx1 * m.b + ty1 * m.d;
  256.                     //dest.draw(_source, m);
  257.                     _container.graphics.beginBitmapFill(_source, m, false, _smoothing);
  258.                     _container.graphics.moveTo(p2.x, p2.y);
  259.                     _container.graphics.lineTo(p3.x, p3.y);
  260.                     _container.graphics.lineTo(p4.x, p4.y);
  261.                     _container.graphics.endFill();
  262.                 }
  263.             }
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement