STRETCH
/**
* Copyright spanvega ( http://wonderfl.net/user/spanvega )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6rL6
*/
package
{
// clic & drag to apply effect
import flash.events.Event;
import flash.display.Sprite;
import starling.core.Starling;
/* @author SPANVEGA // CHRISTIAN */
public class STRETCH extends Sprite
{
public function STRETCH () { addEventListener (Event.ADDED_TO_STAGE, init); }
private function init (e : Event) : void
{
removeEventListener (Event.ADDED_TO_STAGE, init);
with (stage) { align = 'TL'; scaleMode = 'noScale'; frameRate = 60; color = 0x0; }
new Starling (Main, stage).start ();
}
}
}
import flash.geom.*;
import starling.events.*;
import starling.display.*;
import starling.textures.*;
import starling.core.Starling;
import starling.utils.AssetManager;
import starling.animation.Transitions;
class Main extends starling.display.Sprite
{
private var m : Matrix = new Matrix ();
private var quarters : Vector.<Image>;
private var px : Number, py : Number;
private var toggled : Boolean = true;
private var texture : RenderTexture;
private var manager : AssetManager;
private var w : uint, h : uint;
private var position : Point;
public function Main ()
{
manager = new AssetManager ();
manager.enqueue ('http://assets.wonderfl.net/images/related_images/5/57/5777/' + filename);
manager.loadQueue (function (ratio : Number) : void { if (ratio == 1.0) init (); });
}
private function init () : void
{
prepare (manager.getTexture (filename));
texture = new RenderTexture (w, h, true);
addChild (new Image (texture));
update (); // initial position
Starling.current.stage.addEventListener (TouchEvent.TOUCH, touch);
}
private function prepare (texture : Texture) : void
{
// split original image in quarters
w = texture.width;
h = texture.height;
if (odd (w)) w -= 1;
if (odd (h)) h -= 1;
px = w >> 1; py = h >> 1;
//
var quarter : Image;
var region : Rectangle = new Rectangle (0, 0, px, py);
quarters = new Vector.<Image>(4, true);
for (var col : int = 0; col < 2; col++)
for (var row : int = 0; row < 2; row++)
{
region.x = col * px;
region.y = row * py;
quarter = new Image (Texture.fromTexture (texture, region));
quarter.smoothing = TextureSmoothing.TRILINEAR;
// quarter.textureSmoothing = TextureSmoothing.TRILINEAR; in Starling 2.1
quarters [row * 2 + col] = quarter;
}
position = new Point (px, py);
}
private function touch (event : TouchEvent) : void
{
var touch : Touch = event.getTouch (Starling.current.stage);
if (touch)
{
if (touch.phase == TouchPhase.BEGAN) toggled = true;
if (touch.phase == TouchPhase.ENDED) toggled = false;
if (toggled)
{
px = touch.globalX,
py = touch.globalY;
if (px > 0 && py > 0 &&
px < w && py < h)
{
Starling.juggler.purge ();
Starling.juggler.tween (position, 0.5,
{
transition : Transitions.EASE_OUT,
onUpdate : update,
x : px,
y : py
});
}
}
}
}
private function update () : void
{
px = position.x;
py = position.y;
var L : Number = 2 * px / w,
R : Number = 2 * (w - px) / w,
T : Number = 2 * py / h,
B : Number = 2 * (h - py) / h;
texture.drawBundled (function () : void
{
texture.draw (quarters [0], matrix (L, T, 0, 0));
texture.draw (quarters [1], matrix (R, T, px, 0));
texture.draw (quarters [2], matrix (L, B, 0, py));
texture.draw (quarters [3], matrix (R, B, px, py));
});
}
private function matrix (a : Number, d : Number, x : Number, y : Number) : Matrix
{
m.a = a;
m.d = d;
m.tx = x;
m.ty = y;
return m;
}
private function odd (n : int) : Boolean
{
return n % 2 != 0;
}
private var filename : String = '5777f39bc4ee79b532ffea48d724e71ea1b90317';
}