ebook
/**
* Copyright lizhi ( http://wonderfl.net/user/lizhi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/gcAz
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.GradientType;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
/**
* ...
* @author lizhi
*/
public class EBOOK extends Sprite
{
private var px:Number=230;
private var py:Number=50;
private var mx:Number=0;
private var my:Number=0;
private var pw:Number=200;
private var ph:Number = 300;
private var p1:BitmapData;
private var p2:BitmapData;
private var p3:BitmapData;
private var debug:TextField = new TextField();
public function EBOOK()
{
p1 = createPage();
p2 = createPage();
p3 = createPage();
addChild(debug);
render();
stage.addEventListener(MouseEvent.MOUSE_MOVE, update);
}
private function createPage():BitmapData {
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.multiline = tf.wordWrap = true;
tf.width = pw;
tf.textColor = Math.random() * 0xffffff;
tf.background = true;
tf.backgroundColor = Math.random() * 0xffffff;
var tfm:TextFormat = new TextFormat("宋体", 6 + Math.random() * 30);
tf.defaultTextFormat = tfm;
while (true) {
tf.appendText(String.fromCharCode(97+int(Math.random() * (122-97))));
if (tf.textHeight > ph+100) break;
}
var bmd:BitmapData = new BitmapData(pw, ph);
bmd.draw(tf);
return bmd;
}
private function update(e:MouseEvent):void
{
mx = mouseX;
my = mouseY;
render();
e.updateAfterEvent();
}
private function render():void {
graphics.clear();
graphics.lineStyle(0);
var a:Number = Math.atan2(py + ph - my, px + pw - mx);
a += Math.PI / 2;
var x:Number = px / 2 + pw / 2 + mx / 2;
var y:Number = py / 2 + ph / 2 + my / 2;
var l:Number;
var x1:Number = px + pw;//right
l = (x1 - x) / Math.cos(a);
var y1:Number = y + Math.sin(a) * l;
var y2:Number = py + ph;//bottom
l = (y2 - y) / Math.sin(a);
var x2:Number = x + Math.cos(a) * l;
var x3:Number = px;//left
l = (x3 - x) / Math.cos(a);
var y3:Number = y + Math.sin(a) * l;
var y4:Number = py;//top
l = (y4 - y) / Math.sin(a);
var x4:Number = x + Math.cos(a) * l;
var a2:Number = a +a;
var l2:Number = px + pw - x4;
var x5:Number = x4 + l2 * Math.cos(a2);
var y5:Number = y4 + l2 * Math.sin(a2);
var matr:Matrix = new Matrix();
matr.rotate(a2 - Math.PI);
matr.translate(x5, y5);
var gmatr:Matrix = new Matrix();
gmatr.createGradientBox(-pw, ph, -a+Math.PI/2, x4, y4);
debug.text = ""+int((a-Math.PI/2) * 180 / Math.PI + 0.5);
graphics.beginBitmapFill(p3, new Matrix(1, 0, 0, 1, px, py));
graphics.drawRect(px, py, pw, ph);
if (y1 >= py && y1 <= py + ph && x2 >= px && x2 <= px + pw) {
graphics.beginBitmapFill(p1, new Matrix(1, 0, 0, 1, px, py));
graphics.moveTo(px, py);
graphics.lineTo(px + pw, py);
graphics.lineTo(x1, y1);
graphics.lineTo(x2, y2);
graphics.lineTo(px, py + ph);
graphics.lineTo(px, py);
graphics.beginBitmapFill(p2,matr,false,true);
graphics.moveTo(mx, my);
graphics.lineTo(x1, y1);
graphics.lineTo(x2, y2);
graphics.lineTo(mx, my);
/*graphics.beginGradientFill(GradientType.LINEAR, [0, 0xff0000],[1,1] ,[0, 0xff/10], gmatr);
//graphics.drawRect(x5, y5, pw, ph);
graphics.moveTo(mx, my);
graphics.lineTo(x1, y1);
graphics.lineTo(x2, y2);
graphics.lineTo(mx, my);*/
}else if (x2 >= px && x2 <= px + pw && x4 >= px && x4 <= px + pw) {
graphics.beginBitmapFill(p1, new Matrix(1, 0, 0, 1, px, py));
graphics.moveTo(px, py);
graphics.lineTo(x4, y4);
graphics.lineTo(x2, y2);
graphics.lineTo(px, py + ph);
graphics.lineTo(px, py);
graphics.beginBitmapFill(p2,matr,false,true);
graphics.moveTo(mx, my);
graphics.lineTo(x2, y2);
graphics.lineTo(x4, y4);
graphics.lineTo(x5, y5);
graphics.lineTo(mx, my);
/*graphics.beginGradientFill(GradientType.LINEAR, [0, 0xff0000],[1,1] ,[0, 0xff/10], gmatr);
graphics.moveTo(mx, my);
graphics.lineTo(x2, y2);
graphics.lineTo(x4, y4);
graphics.lineTo(x5, y5);
graphics.lineTo(mx, my);*/
}else {
graphics.beginBitmapFill(p1, new Matrix(1, 0, 0, 1, px, py));
graphics.drawRect(px, py, pw, ph);
}
}
}
}