/*************************************************************************************************************** Page Curl with Vector2D Class for AS3 --------------------------------------------------------------------------------------------------------------- This demo shows how the Vector2D class can be used to make a simple page curl To use this source: 1. Download 'Curl.as' (this file) and 'Vector2D.as' for AS3 2. Create a new Flash movie named 'Curl.fla' in the same directory as 'Vector2D.as' and 'Curl.as' 3. In Flash, using the properties panel, set the document class of 'Curl.fla' to 'Curl' 4. Publish Note: This effect is also possible in earlier versions of Actionscript ***************************************************************************************************************/ package { import flash.display.*; import flash.events.*; import flash.utils.*; import flash.geom.*; import Vector2D; // comment this line out if you are using Flash CS3 [SWF(width="425", height="200", frameRate="60", backgroundColor="#CCCC31")] public class Curl extends MovieClip { var squareFront:Sprite; var squareBack:Sprite; var maskFront:Sprite; var maskBack:Sprite; var mouseEased:Point = new Point(); var mouseEasing:Number = 0.2; var curlVector:Vector2D = new Vector2D(); var curlDraw:Sprite; public function Curl():void { // place origin at center of stage this.x += stage.stageWidth / 2; this.y += stage.stageHeight / 2; // create front face of square squareFront = new Sprite(); squareFront.graphics.beginFill(0xFFFF00); squareFront.graphics.drawRect(-50, -50, 100, 100); squareFront.graphics.endFill(); addChild(squareFront); // create back face of square squareBack = new Sprite(); squareBack.graphics.beginFill(0xCE0031); squareBack.graphics.drawRect(-50, -50, 100, 100); squareBack.graphics.endFill(); addChild(squareBack); // create mask for front face maskFront = new Sprite(); maskFront.graphics.beginFill(0xFF00FF); maskFront.graphics.drawRect(-400, 0, 800, -400); maskFront.graphics.endFill(); maskFront.alpha=0.25; addChild(maskFront); // create mask for back face maskBack = new Sprite(); maskBack.graphics.beginFill(0xFF0F00); maskBack.graphics.drawRect(-400, 0, 800, -400); maskBack.graphics.endFill(); maskBack.alpha=0.25; addChild(maskBack); // set masks squareFront.mask=maskFront; squareBack.mask=maskBack; // create blank sprite for drawing vectors curlDraw = new Sprite(); curlDraw.graphics.clear(); addChild(curlDraw); // call curl function every frame this.addEventListener(Event.ENTER_FRAME,doCurl); } private function doCurl(evt:Event):void { // ease mouse mouseEased.x += (mouseX - mouseEased.x) * mouseEasing; mouseEased.y += (mouseY - mouseEased.y) * mouseEasing; // set curl vector to mouse position curlVector.set(mouseEased.x, mouseEased.y); // position and rotate masks maskFront.x = maskBack.x = curlVector.x; maskFront.y = maskBack.y = curlVector.y; maskFront.rotation = maskBack.rotation = curlVector.angle - 90; // make vector twice as long so back face is at appropriate distance curlVector.times(2); // position and rotate back face squareBack.x = curlVector.x; squareBack.y = curlVector.y; var curlVectorNormal = curlVector.getRightNormal(); squareBack.rotation = curlVector.angle + curlVectorNormal.angle; // paint the vectors for debugging //curlDraw.graphics.clear(); //curlVector.paint(curlDraw, 0x0000FF); //curlVectorNormal.paint(curlDraw, 0xFF00FF); } //class closing } //package closing }