package {
//hi.baidu.com/inuko
//bitmapdata fisheye magnifier
//原创代码,如有雷同,纯属巧合
/*
本例是使用近似算法,只是最简单的鱼眼应用。此类绑定一个以0,0为中心,半径R=50的圆即可使用。也可以自行绘制放大镜子。
原理很简单,可以baidu下。
须和被放大的object封在一个mc里。这样bitmapdata.draw比较方便。涉及到层级问题,不这么做执行效率较低。
可以用模糊滤镜使之更平滑,不建议使用。
需要近远调节,可以自行写 放大镜与物件的距离,放大镜与人眼的距离 与 放大倍数的映射关系
this is a proximation. a simple fisheye.bind it with a sprite which has a circle which r=100 in point(0,0),u can draw your own magnifier.
just put it in a movie,and then it can render the parent
the theory is concentric circles filled with different scaled original pictures
可以用tweener做动画 move with tweener class
Tweener.addTween(myMovieClip, {x:10, time:1,onUpdate:magnifier.render});
时间轴动画可以用enterFrame move in flash
function enterFrameH(e){
if(currentFrame>arg&¤tFrame<arg){ //动画范围 moving frames range
magnifier.render()
}
}
*/
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.filters.BlurFilter
public class magnifier extends Sprite {
public var scaleMax=2; //最大放缩
public var scaleMin=1; //最小放缩
public var scaleStep=32; //精细度,即同新圆数 render steps,as same as concentric circles number
//预存储提高执行效率 defined these in init function to upgrad efficiency
private var scaleArr=[];
private var matrixArr=[];
private var rArr=[];
private var spriteArr=[]
private var nowWidth; //当前放大镜显示圆的直径,即bimmapdata绘制区域 the rendercircle's width
public var bmpd=new BitmapData(10,10);
public var bmpd_=new BitmapData(10,10);
public var rect;
public var point=new Point(0,0)
//public var blur=new BlurFilter(3,3,1) 用模糊滤镜可以更平滑,但是太卡,不建议 make it smoother,but increase the scaleStep is better
public function magnifier(scaleMax=2,scaleMin=1,scaleStep=32) {
this.scaleMax=scaleMax;
this.scaleMin=scaleMin;
this.scaleStep=scaleStep;
init();
//addEventListener("enterFrame",render); //render in each frame,can wirte these outside the class
render()
}
public function init() {
var d=(scaleMax-scaleMin)/(scaleStep-1);
if (d<0) {
scaleMax=scaleMin
d=0
return;
}
nowWidth=this.scaleX*100;
for (var i=0; i<=scaleStep; i++) {
scaleArr[i]=scaleMin+i*d;//先缓冲提高效率,从大带小
rArr[i]=50*(scaleStep-i+1)/(scaleStep+1);
matrixArr[i]=new Matrix();
matrixArr[i].scale(scaleArr[i],scaleArr[i]);
matrixArr[i].translate(-50*scaleArr[i],-50*scaleArr[i])
spriteArr[i]=new Sprite()
//if(i>0){
//spriteArr[i].filters=[blur] //用模糊滤镜可以更平滑,但是太卡,不建议 make it smoother,but increase the scaleStep is better
//}
addChild(spriteArr[i])
}
}
public function render(e=null) {
var i
this.visible=false //不显示,以免绘制自身 invisible to avoid draw magnifier self
rect=new Rectangle(this.x-nowWidth/2,this.y-nowWidth/2,nowWidth,nowWidth); //实际范围 true bitmapdata.draw area
bmpd.dispose()
bmpd=new BitmapData(this.parent.width,this.parent.height,true);
bmpd.draw(this.parent,null,null,null,rect,true);
bmpd_.dispose()
bmpd_=new BitmapData(nowWidth,nowWidth)
bmpd_.copyPixels(bmpd,rect,point) //拷贝实际范围到新的bitmapdata,方便放缩的matrix计算 copy to a new bitmapdata,so can be easier to calc matrix
for (i=0; i<=scaleStep; i++) {
spriteArr[i].graphics.clear()
//spriteArr[i].graphics.lineStyle(1,0) 圆外框,此行可以帮助理解原理 help to understand&debug
spriteArr[i].graphics.beginBitmapFill(bmpd_,matrixArr[i],false,true); //填充不同matrix的圆 fill circles with difference matrix
spriteArr[i].graphics.drawCircle(0,0,rArr[i]);
spriteArr[i].graphics.endFill();
}
this.visible=true
}
}
}