[转]BitmapData实现的透镜折射效果

http://bbs.9ria.com/viewthread.php?tid=80586&extra=page%3D1%26amp%3Borderby%3Ddateline%26amp%3Bfilter%3D2592000



package{
        import flash.display.MovieClip;
        import flash.display.Bitmap;
        import flash.display.BitmapData;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.utils.ByteArray;
        import flash.geom.Rectangle;
        import flash.geom.Point;
        import Math;    
        public class main extends MovieClip{
                const pictureWidth:int=1000;
                const pictureHeight:int=800;
                const lensWidth:int=100;
                var srcBmp:BitmapData=new picData(0,0);
                var srcArray:ByteArray=new ByteArray();
                var lensBmp:BitmapData=new BitmapData(lensWidth,lensWidth,true,0x00FFFF);
                var lensArray:ByteArray=new ByteArray();
                var lensData:Array=new Array();
                var rect:Rectangle=new Rectangle(0,0,pictureWidth,pictureWidth);
                var image:Bitmap=new Bitmap(lensBmp);
                public function main():void{
                        stage.addChild(image);
                        init();
                        srcArray=srcBmp.getPixels(rect);
                        rect.width=lensWidth;
                        rect.height=lensWidth;
                        stage.addEventListener(MouseEvent.MOUSE_MOVE,drawLens);
                        drawLens(new MouseEvent("MouseEvent.MOUSE_MOVE",true,true,100,100));
                }
                private function init(){
                        var jl:Number=0;
                        var centeroint=new Point(Number(lensWidth/2),Number(lensWidth/2));
                        var temp:Number=(lensWidth/2)*(lensWidth/2);
                        for (var i:Number=0;i<lensWidth;i++){
                                lensData.push(new Array());
                                for (var j:Number=0;j<lensWidth;j++){
                                        jl=Point.distance(center,new Point(i,j));
                                        if(jl*2<=lensWidth){
                                                lensData[j]=Math.sqrt(temp-jl*jl)*5;
                                        }else{
                                                lensData[j]=0;
                                        }
                                }
                        }
                }
                private function drawLens(event:MouseEvent):void{
                        var lx:int=int(event.localX*2)-50;
                        var ly:int=int(event.localY*2)-50;
                        var tx:int,ty:int;
                        
                        rect.x=event.localX*2-50;
                        rect.y=event.localY*2-50;
                        lensArray=new ByteArray();
                        for (var i:int=0;i<lensWidth-1;i++){
                                for (var j:int=0;j<lensWidth-1;j++){
                                        tx=(lensData[j]-lensData[i+1][j]);
                                        ty=(lensData[j]-lensData[j+1]);
                                        if (lx+i+tx<pictureWidth && lx+i+tx>0 && ly+j+ty<pictureHeight && ly+j+ty>0 && lensData[j]!=0){
                                                
                                                lensArray[converLens(i,j)]=srcArray[conver(lx+i+tx,ly+j+ty)];
                                                lensArray[converLens(i,j)+1]=srcArray[conver(lx+i+tx,ly+j+ty)+1];
                                                lensArray[converLens(i,j)+2]=srcArray[conver(lx+i+tx,ly+j+ty)+2];
                                                lensArray[converLens(i,j)+3]=srcArray[conver(lx+i+tx,ly+j+ty)+3];
                                        }
                                }
                        }
                        lensArray[converLens(lensWidth-1,lensWidth-1)+3]=0;
                        lensArray.position=0;
                        rect.x=0;
                        rect.y=0;
                        lensBmp.lock();
                        lensBmp.setPixels(rect,lensArray);
                        lensBmp.unlock();
                        image.x=event.localX-50;
                        image.y=event.localY-50;
                }
                private function conver(tx:int,ty:int):int{
                        return ty*pictureWidth*4+tx*4;
                }
                private function converLens(tx:int,ty:int):int{
                        return ty*lensWidth*4+tx*4;
                }
        }
}

你可能感兴趣的:(PHP,Flash,J#,bbs)