AS3基础-放大镜

 

预览图片时,我们常常用到的放大镜效果:

 

 

 

package 
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.net.URLRequest;

/**
* 
* @author hacker47...
*/
public class Main extends MovieClip
{

		private var bitmap:Bitmap;
		private var bitmap2:Bitmap;
		private var shape:Sprite = new Sprite();
		private var loader:Loader;
		
		public function Main() {
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
			loader.load(new URLRequest("assets/child.jpg"));
		}
		
		private function onLoadComplete(e:Event):void {
			bitmap = e.target.content as Bitmap;
			addChild(bitmap);

			initMap();
		}
		
		
		private function initMap() {
			
			var bitdata:BitmapData = bitmap.bitmapData;
			var bitdata2:BitmapData = bitdata.clone();
			bitmap.scaleX = bitmap.scaleY = 0.9;
			bitmap.x = stage.stageWidth / 2 - bitmap.width / 2;
			bitmap.y = stage.stageHeight / 2 - bitmap.height / 2;
			bitmap2 = new Bitmap(bitdata2);
			addChild(bitmap2);
			
			bitmap2.cacheAsBitmap = true;
			bitmap2.x = stage.stageWidth / 2 - bitmap2.width / 2;
			bitmap2.y = stage.stageHeight / 2 - bitmap2.height / 2;
			addMaskShap();
		
		}
		
		
		private function addMaskShap() {
			with (shape.graphics) {
				beginFill(0x000000);
				drawCircle(0, 0, 100);
				endFill();
			}
			
			addChild(shape);
			shape.buttonMode = true;
			shape.cacheAsBitmap = true;
			shape.filters = [new BlurFilter(5,5)];
			bitmap2.mask = shape;
			addEventListener(Event.ENTER_FRAME, enterFrame);
		}
		
		private function enterFrame(e:Event) {
			shape.x += (mouseX - shape.x) / 10;
			shape.y += (mouseY - shape.y) / 10;
		}
			
			
		}

} 

 

你可能感兴趣的:(as3)