效果演示:
https://files.cnblogs.com/zhoujunfeng2011/AS3%E4%B8%8A%E4%BC%A0%E5%A4%B4%E5%83%8F.swf
源码:
主文件:Main.as
package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.MouseEvent; import flash.net.FileFilter; import flash.net.FileReference; import flash.events.Event; import flash.display.Bitmap; import flash.utils.setInterval; import flash.utils.clearInterval; import flash.utils.setTimeout; import flash.display.BlendMode; import flash.geom.Rectangle; import flash.utils.getQualifiedClassName; import flash.display.BitmapData; import flash.geom.Matrix; import flash.utils.ByteArray; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLLoaderDataFormat; import flash.net.URLLoader; import flash.net.URLVariables; import flash.events.IOErrorEvent; import flash.text.TextField; import com.net.NativeContentLoader; import com.ui.interfaces.Interface; import com.ui.TextStuta.CurrentInfo; import com.tools.ImageSize; import com.tools.CutImage; import com.EditBounds; import com.images.PNGEncoder; import com.images.Base64; import com.greensock.TweenLite; import com.greensock.easing.*; public class Main extends Sprite { private var init:Interface; private var fileType:FileFilter; private var file:FileReference; private var nativeLoader:NativeContentLoader; public static var bitmap:Bitmap; private var IntervalID:uint; private var Image:Sprite; private var bounds:EditBounds; private var obj:Sprite; private var ImageOriginalWidth:Number = 0; private var ImageOriginalHeight:Number = 0; private var cutStatu:int = 1; private var alphaMask:Sprite; private var ImageSizes:Array; private var objectSize:Array = new Array(); private var ImageArray:Array = new Array(); private var url:String = "UploadHandler.ashx"; public function Main():void { this.stage.align = StageAlign.TOP_LEFT; this.stage.scaleMode = StageScaleMode.NO_SCALE; this.init = new Interface(); this.addChild(this.init); this.fileType = new FileFilter("图片文件(*.jpg;*.jpge;*.png)","*.jpg;*.jpge;*.png"); this.ImageSizes = new Array([50,50],[100,100],[140,140]); CheckCamera(); } private function CheckCamera():void { this.Image = new Sprite(); this.init.uploadphotos.addEventListener(MouseEvent.CLICK,uploadphotos_click); } private function uploadphotos_click(evt:MouseEvent):void { this.init.uploadphotos.removeEventListener(MouseEvent.CLICK,uploadphotos_click); this.file = new FileReference(); this.file.addEventListener(Event.COMPLETE,CompleteHalder); this.file.addEventListener(Event.CANCEL,CancelHalder); this.file.addEventListener(Event.SELECT,SelectHalder); this.file.browse([this.fileType]); CurrentInfo.textInfo.text = "温馨提示:只能选择jpge、jpg、png格式图片,暂不支持其他格式"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); } private function SelectHalder(evt:Event):void { this.file.removeEventListener(Event.CANCEL,CancelHalder); this.file.removeEventListener(Event.SELECT,SelectHalder); CurrentInfo.textInfo.text = "正在加载图片,请稍后..."; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); this.file.load(); } private function CancelHalder(evt:Event):void { this.init.uploadphotos.addEventListener(MouseEvent.CLICK,uploadphotos_click); this.file.removeEventListener(Event.CANCEL,CancelHalder); this.file.removeEventListener(Event.SELECT,SelectHalder); CurrentInfo.textInfo.text = "已取消选择"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); } private function CompleteHalder(evt:Event):void { this.file.removeEventListener(Event.COMPLETE,CompleteHalder); CurrentInfo.textInfo.text = "加载完成,请稍等..."; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); this.nativeLoader = new NativeContentLoader(); this.nativeLoader.load(this.file.data); this.IntervalID = setInterval(checkImageIsNull,1000); } private function checkImageIsNull():void { if (bitmap != null) { clearInterval(this.IntervalID); InitEditBounds(); } } private function InitEditBounds():void { this.init.removeBigBtn(); this.init.UIInit(); this.init.grayPlaid.addChild(this.Image); this.ImageOriginalWidth = bitmap.width; this.ImageOriginalHeight = bitmap.height; this.Image.addChild(bitmap); bitmap.smoothing = true; bitmap.x = (-bitmap.width)/2; bitmap.y = (-bitmap.height)/2; this.Image.x = 198; this.Image.y = 158; alphaMask = new Sprite(); alphaMask.graphics.beginFill(0x000000,0.5); alphaMask.graphics.drawRect(2.3,43,396,230); alphaMask.graphics.endFill(); this.addChild(alphaMask); this.obj = new Sprite(); this.obj.graphics.beginFill(0xFFFFFF); this.obj.graphics.drawRect(0,0,140,140); this.obj.graphics.endFill(); this.obj.x = 130.3; this.obj.y = 88; alphaMask.blendMode = BlendMode.LAYER; alphaMask.addChild(this.obj); this.obj.blendMode = BlendMode.ERASE; this.bounds = new EditBounds(); this.bounds.target = this.obj; this.addChild(this.bounds); this.bounds.maxWidth = 230; this.bounds.maxHeight = 230; this.bounds.minWidth = 50; this.bounds.minHeight = 50; if(this.Image.width < 60 || this.Image.height < 60) { setBoundsSize(); } CalculateBounds(); AddEventListener(); CurrentInfo.textInfo.text = "提示:点击“确定”按钮进行裁剪、点击“恢复”按钮恢复初始化状态"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); setTimeout(function ():void { CurrentInfo.textInfo.text = "请选择图片合适位置进行裁剪头像(140*140)"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); },800); } private function AddEventListener():void { this.init.leftSpinVessel.buttonMode = true; this.init.leftSpinVessel.addEventListener(MouseEvent.CLICK,FunctionHalder); this.init.leftSpinVessel.addEventListener(MouseEvent.MOUSE_OVER,FunctionHalder); this.init.leftSpinVessel.addEventListener(MouseEvent.MOUSE_OUT,FunctionHalder); this.init.rightSpinVessel.buttonMode = true; this.init.rightSpinVessel.addEventListener(MouseEvent.CLICK,FunctionHalder); this.init.rightSpinVessel.addEventListener(MouseEvent.MOUSE_OVER,FunctionHalder); this.init.rightSpinVessel.addEventListener(MouseEvent.MOUSE_OUT,FunctionHalder); this.init.zoomVessel.buttonMode = true; this.init.zoomVessel.addEventListener(MouseEvent.CLICK,FunctionHalder); this.init.zoomVessel.addEventListener(MouseEvent.MOUSE_OVER,FunctionHalder); this.init.zoomVessel.addEventListener(MouseEvent.MOUSE_OUT,FunctionHalder); this.init.narrowVessel.buttonMode = true; this.init.narrowVessel.addEventListener(MouseEvent.CLICK,FunctionHalder); this.init.narrowVessel.addEventListener(MouseEvent.MOUSE_OVER,FunctionHalder); this.init.narrowVessel.addEventListener(MouseEvent.MOUSE_OUT,FunctionHalder); this.init.clearsVessel.buttonMode = true; this.init.clearsVessel.addEventListener(MouseEvent.CLICK,FunctionHalder); this.init.clearsVessel.addEventListener(MouseEvent.MOUSE_OVER,FunctionHalder); this.init.clearsVessel.addEventListener(MouseEvent.MOUSE_OUT,FunctionHalder); this.init.okVessel.buttonMode = true; this.init.okVessel.addEventListener(MouseEvent.CLICK,FunctionHalder); this.init.okVessel.addEventListener(MouseEvent.MOUSE_OVER,FunctionHalder); this.init.okVessel.addEventListener(MouseEvent.MOUSE_OUT,FunctionHalder); this.init.backsVessel.buttonMode = true; this.init.backsVessel.addEventListener(MouseEvent.CLICK,FunctionHalder); this.init.backsVessel.addEventListener(MouseEvent.MOUSE_OVER,FunctionHalder); this.init.backsVessel.addEventListener(MouseEvent.MOUSE_OUT,FunctionHalder); } private function FunctionHalder(evt:MouseEvent):void { if (evt.type == "mouseOver") { TweenLite.to(evt.currentTarget.getChildAt(0),0.5,{alpha:1,ease:Back.easeOut}); } else if (evt.type == "mouseOut") { TweenLite.to(evt.currentTarget.getChildAt(0),1.5,{alpha:0,ease:Back.easeOut}); } else if (evt.type == "click") { switch (getQualifiedClassName(evt.target)) { case "leftSpin" : { this.leftRotationFunc(); setBoundsSize(); CalculateBounds(); }; break; case "rightSpin" : { this.rightRotationFunc(); setBoundsSize(); CalculateBounds(); }; break; case "zoom" : { this.zoomInFunc(); CalculateBounds(); }; break; case "narrow" : { this.zoomOutFunc(); CalculateBounds(); if (this.cutStatu == 1) { this.obj.width = this.obj.height = 50; this.obj.x = 175.3; this.obj.y = 133; } this.bounds.target = this.obj; }; break; case "clears" : { if (this.cutStatu == 1) { if (this.init.PreviewImage1.numChildren != 0) { this.init.PreviewImage1.removeChildAt(0); } if (this.init.PreviewImage2.numChildren != 0) { this.init.PreviewImage2.removeChildAt(0); } if (this.init.PreviewImage3.numChildren != 0) { this.init.PreviewImage3.removeChildAt(0); } this.Image.rotation = 0; ImageSize.setImageSize(this.Image,396,230,this.Image.width,this.Image.height); this.Image.x = 198; this.Image.y = 158; this.obj.width = this.obj.height = 140; this.obj.x = 130.3; this.obj.y = 88; this.bounds.target = this.obj; CalculateBounds(); this.cutStatu = 1; CurrentInfo.textInfo.text = "请选择图片合适位置进行裁剪"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); } }; break; case "ok" : { if (this.cutStatu <= 2) { var cutbitmapdata:BitmapData = CutImage.cutOutSuper(this.Image,this.obj); if (this.cutStatu == 1) { this.objectSize[0] = ([this.obj.width,this.obj.height]); this.objectSize[1] = ([this.obj.width,this.obj.height]); this.objectSize[2] = ([this.obj.width,this.obj.height]); var bitmap:Bitmap = new Bitmap(cutbitmapdata); bitmap.smoothing = true; bitmap.width = bitmap.height = 100; this.init.PreviewImage2.addChild(bitmap); bitmap.x = bitmap.y = 1; var bitmap2:Bitmap = new Bitmap(cutbitmapdata); bitmap2.smoothing = true; bitmap2.width = bitmap2.height = 50; this.init.PreviewImage1.addChild(bitmap2); bitmap2.x = bitmap2.y = 1; this.ImageArray.push(bitmap2); this.ImageArray.push(bitmap); var bitmap3:Bitmap = new Bitmap(cutbitmapdata); bitmap3.smoothing = true; bitmap3.width = bitmap3.height = 140; this.init.PreviewImage3.addChild(bitmap3); bitmap3.x = bitmap3.y = 1; this.ImageArray.push(bitmap3); this.init.CreatePreviewImage(); this.init.grayPlaid.removeChild(this.Image); this.removeChild(this.bounds); this.bounds = null; this.removeChild(this.alphaMask); this.init.deleteGrayPlaid(); this.init.addGrayDisplay(); CurrentInfo.textInfo.text = "形象照裁剪成功!系统为您生成三种尺寸(140*140、100*100、50*50)"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); setTimeout(function ():void { CurrentInfo.textInfo.text = "提示:点击确定按钮进行保存,不满意点击返回按钮重新编辑"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); },500); } else if (this.cutStatu == 2) { this.init.addOtherGrayDisplay(); saveImage(); } this.cutStatu++; } }; break; case "backs" : { if (this.cutStatu == 1) { deleteUIButton(); this.init.grayPlaid.removeChild(this.Image); this.Image = null; this.init.deleteGrayPlaid(); while (this.numChildren > 1) { this.removeChildAt(1); } this.init.addChildSeleteImage(); this.CheckCamera(); } else if (this.cutStatu == 2) { this.deleteArrayAllElement(this.ImageArray); this.init.deletePreviewImage(); this.init.addGrayPlaid(); this.init.grayPlaid.addChild(this.Image); this.addChild(this.alphaMask); this.Image.rotation = 0; ImageSize.setImageSize(this.Image,396,230,this.Image.width,this.Image.height); this.Image.x = 198; this.Image.y = 158; this.obj.width = this.obj.height = 140; this.obj.x = 130.3; this.obj.y = 88; this.bounds = new EditBounds(); this.bounds.target = this.obj; this.addChild(this.bounds); this.bounds.maxWidth = 230; this.bounds.maxHeight = 230; this.bounds.minWidth = 50; this.bounds.minHeight = 50; CalculateBounds(); this.init.removeGrayDisplay(); this.cutStatu = 1; CurrentInfo.textInfo.text = "请选择图片合适位置进行裁剪(140*140)"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); } }; break; } } } private function deleteUIButton():void { while (this.init.count.numChildren >0) { if (getQualifiedClassName(this.init.count.getChildAt(0)) == "flash.display::Sprite") { if (this.init.count.getChildAt(0).hasEventListener(MouseEvent.CLICK)) { this.init.count.getChildAt(0).removeEventListener(MouseEvent.CLICK,FunctionHalder); this.init.count.getChildAt(0).removeEventListener(MouseEvent.MOUSE_OVER,FunctionHalder); this.init.count.getChildAt(0).removeEventListener(MouseEvent.MOUSE_OUT,FunctionHalder); } } var Nulls:* = this.init.count.getChildAt(0); this.init.count.removeChildAt(0); Nulls = null; } } private function CalculateBounds():void { var numx:Number = this.Image.x - this.Image.width / 2; var numy:Number = this.Image.y - this.Image.height / 2; var numw:Number = this.Image.width + numx; var numh:Number = this.Image.height + numy; if (numx <= 2.3) { numx = 2.3; numw = 398.3; } if (numy <= 43) { numy = 43; numh = 273; } this.bounds.selectBounds = new Rectangle(numx,numy,numw,numh); } private function zoomInFunc(num:Number = 1.1) { if (this.Image.width < this.ImageOriginalWidth && this.Image.height < this.ImageOriginalHeight) { var scaleNum:Number = this.Image.scaleY * num; this.Image.scaleY = this.Image.scaleY * num; this.Image.scaleX = scaleNum; } } private function zoomOutFunc(num:Number = 0.9) { if (this.Image.width > 54 && this.Image.height > 54) { var num1:Number = this.Image.scaleY * num; this.Image.scaleY *= num; this.Image.scaleX = num1; if(this.Image.width > this.Image.height) { this.bounds.maxWidth = this.bounds.maxHeight = this.Image.height; } else { this.bounds.maxWidth = this.bounds.maxHeight = this.Image.width; } } } private function leftRotationFunc(num = 90) { this.Image.rotation -= num; } private function rightRotationFunc(num = 90) { this.Image.rotation += num; } private function setBoundsSize():void { if(this.Image.width != this.Image.height) { this.obj.width = this.obj.height = 50; this.obj.x = 175.3; this.obj.y = 133; this.bounds.target = this.obj; } } //删除数组中所有元素方法 private function deleteArrayAllElement(array:Array):void { while(array.length != 0) { array.splice(0,array.length); /*if(array.length == 0) { trace("删除完成"); }*/ } } private function saveImage():void { if (this.ImageArray != null) { var mydata:URLVariables = new URLVariables(); for(var i:int = 0;i<this.ImageArray.length;i++) { var numw:Number = this.ImageSizes[i][0] / this.objectSize[i][0]; var numh:Number = this.ImageSizes[i][1] / this.objectSize[i][1]; var bitmapdata:BitmapData = new BitmapData(this.ImageSizes[i][0],this.ImageSizes[i][1],true,0x00); bitmapdata.draw(this.ImageArray[i],new Matrix(numw,0,0,numh)); var _data:ByteArray = PNGEncoder.encode(bitmapdata); var pngString:String = Base64.encodeByteArray(_data); if(i == 0) { mydata.small = pngString; } else if(i == 1) { mydata.medium = pngString; } else if(i == 2) { mydata.big = pngString; } bitmapdata.dispose(); bitmapdata = null; } var _request:URLRequest = new URLRequest(this.url); //_request.contentType = "application/octet-stream"; _request.data = mydata; _request.method = URLRequestMethod.POST; var loader:URLLoader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.VARIABLES; try { loader.load(_request); loader.addEventListener(IOErrorEvent.IO_ERROR,UpLoadFailedHalder); loader.addEventListener(Event.COMPLETE,UpLoadCompleteHalder); } catch (error:ArgumentError) { CurrentInfo.textInfo.text = "Error:参数错误!请联系管理员!"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); } catch (error:SecurityError) { CurrentInfo.textInfo.text = "Error:安全性错误!请联系管理员!"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); } } } private function UpLoadFailedHalder(evt:IOErrorEvent):void { evt.target.removeEventListener(IOErrorEvent.IO_ERROR,UpLoadFailedHalder); CurrentInfo.textInfo.text = "图片上传失败,请检查网络!"; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); } private function UpLoadCompleteHalder(evt:Event):void { evt.target.removeEventListener(Event.COMPLETE,UpLoadCompleteHalder); evt.target.close(); CurrentInfo.textInfo.text = (evt.target as URLLoader).data; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); } } }
其他类:
package com.net { import flash.display.Bitmap; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.IOErrorEvent; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.system.LoaderContext; import flash.system.SecurityDomain; import flash.system.ApplicationDomain; import flash.utils.ByteArray; import Main; /** * 装载加载进来的图片 * @author xiaofeng */ public class NativeContentLoader extends Sprite { private var loaderContext:LoaderContext; private var loader:Loader; private var URLloader:URLLoader; public function NativeContentLoader() { this.loaderContext = new LoaderContext(true); this.loader = new Loader(); this.URLloader = new URLLoader(); this.loaderContext.securityDomain = SecurityDomain.currentDomain; this.loaderContext.applicationDomain = ApplicationDomain.currentDomain; this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, Loader_Complete); this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, Loader_IOERROR); this.URLloader.addEventListener(Event.COMPLETE, URLloader_Compelte); this.URLloader.dataFormat = URLLoaderDataFormat.BINARY; } private function Loader_Complete(evt:Event):void { try { Main.bitmap = evt.target.content; } catch (e:SecurityError) { this.dispatchEvent(new Event("SecurityError")); } } private function Loader_IOERROR(evt:IOErrorEvent):void { //忽略错误 evt.target.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, Loader_IOERROR); } private function URLloader_Compelte(evt:Event):void { var byteArray:ByteArray; try { byteArray = new ByteArray(); byteArray.writeBytes(evt.target.data); byteArray.position = 0; this.loader.loadBytes(byteArray); } catch (e:SecurityError) { this.dispatchEvent(new Event("SecurityError")); } evt.target.removeEventListener(Event.COMPLETE, URLloader_Compelte); } public function load(byteArray:ByteArray):void { this.unload(); this.loader.loadBytes(byteArray); } private function unload():void { this.loader.unload(); } } }
package com.tools { import flash.display.BitmapData; import flash.display.DisplayObject; import flash.geom.Matrix; import flash.geom.Rectangle; import flash.display.Sprite; /** * 裁剪图片类 * @author 转载(稍作修改) */ public class CutImage { public function CutImage() { } /** * 裁剪指定矩形区域并返回一个包含结果的 BitmapData 对象。 * * @param target 需要裁剪的显示对象。 * * @param width 位图图像的宽度,以像素为单位。 * * @param height 位图图像的高度,以像素为单位。 * * @param distanceX 切割矩形左上角的点到显示对象矩形左上角的点的水平距离。注意:左上角的点不一定就是注册点(0, 0)外,变形过的显示对象就是一个例外。 * * @param distanceY 切割矩形左上角的点到显示对象矩形左上角的点的垂直距离。注意:左上角的点不一定就是注册点(0, 0)外,变形过的显示对象就是一个例外。 * * @param transparent 指定裁剪后的位图图像是否支持每个像素具有不同的透明度。默认值为 true(透明)。若要创建完全透明的位图,请将 transparent 参数的值设置为 true,将 fillColor 参数的值设置为 0x00000000(或设置为 0)。将 transparent 属性设置为 false 可以略微提升呈现性能。 * * @param fillColor 用于填充裁剪后的位图图像区域背景的 32 位 ARGB 颜色值。默认值为 0x00000000(纯透明黑色)。 * * @returns 返回裁剪后的 BitmapData 对象。 */ public static function cutOutRect( target:DisplayObject, distanceX:Number, distanceY:Number, width:Number, height:Number, transparent:Boolean = true, fillColor:uint = 0x00000000 ):BitmapData { var m:Matrix = target.transform.matrix; m.tx -= target.getBounds( target.parent ).x + distanceX; m.ty -= target.getBounds( target.parent ).y + distanceY; var bmpData:BitmapData = new BitmapData( width, height, transparent, fillColor ); bmpData.draw( target, m); return bmpData; } /** * 超级裁剪工具!可裁剪任意形状!给定一个裁剪目标和一个模板,就可根据模板裁剪出形状相配的 BitmapData 数据。 * * @param target 需要裁剪的显示对象。 * * @param template 裁剪模板,可以是任意形状。 * * @returns 返回裁剪后的 BitmapData 对象。 */ public static function cutOutSuper( target:DisplayObject, template:DisplayObject):BitmapData { var rectTarget:Rectangle = target.transform.pixelBounds; var rectTemplate:Rectangle = template.transform.pixelBounds; var targetBitmapData:BitmapData = CutImage.cutOutRect( target, 0, 0, rectTarget.width, rectTarget.height, true, 0x00000000 ); var templateBitmapData:BitmapData = CutImage.cutOutRect( target, 0, 0, rectTemplate.width, rectTemplate.height, true, 0x00000000 ); for( var pixelY:int = 0; pixelY < rectTemplate.height; pixelY++ ) { for( var pixelX:int = 0; pixelX < rectTemplate.width; pixelX++ ) { if( templateBitmapData.getPixel( pixelX, pixelY ) != 0 ) { var color:uint = targetBitmapData.getPixel32( pixelX + rectTemplate.x - rectTarget.x, pixelY + rectTemplate.y - rectTarget.y ); templateBitmapData.setPixel32( pixelX, pixelY, color ); } } } return templateBitmapData; } } }
package com.tools { import flash.display.Sprite; import flash.display.Shape; /** *绘制格式图形类 *@Author:xiaofeng */ public class Draws { public function Draws():void { } /** *绘制黑白格方法,X、Y:返回的Sprite对象的x轴和y轴坐标,Width、Height:返回的Sprite的总宽度以及高度,PixelSize:绘制格子的大小,以像素为单位 */ public static function DrawLattice(X:Number = 0,Y:Number = 0,Width:int = 100,Height:int = 100,PixelSize:int = 5):Sprite { //管理小格子对象 var LatticeCount:Sprite = new Sprite(); //计算外层需要循环的次数 var num:int = Height / PixelSize; //计算内层需要循环的次数 var num2:int = Width / PixelSize; //保存颜色值,初始为白色 var colors:uint = 0xFFFFFF; for (var i:int = 0; i<num; i++) { for (var j:int = 0; j<num2; j++) { var SmallLattice:Shape = new Shape(); //判断颜色是黑还是白 if(i % 2) { if(j % 2) { colors = 0xC0C0C0; } else { colors = 0xFFFFFF; } } else { if( j % 2) { colors = 0xFFFFFF; } else { colors = 0xC0C0C0; } } SmallLattice.graphics.beginFill(colors); SmallLattice.graphics.drawRect(X,Y,PixelSize,PixelSize); SmallLattice.graphics.endFill(); LatticeCount.addChild(SmallLattice); SmallLattice.x = j * PixelSize; SmallLattice.y = i * PixelSize; } } return LatticeCount; } /** * 绘制矩形线框方法 */ public static function DrawRects(w:Number, h:Number):Sprite { var rect:Sprite = new Sprite(); rect.graphics.beginFill(0xFFFFFF); rect.graphics.lineStyle(1, 0x000000); rect.graphics.drawRect(0, 0, w, h); rect.graphics.endFill(); return rect; } } }
package com.tools { import flash.display.Sprite; /** * 设置图片大小类 * @author xiaofeng */ public class ImageSize { public function ImageSize() { } /** * 参数说明 * @param Image:需要设置大小的显示对象 * @param FrameWidth:框架宽度 * @param FrameHeight:框架高度 * @param ImageWidth:图片加载进来时宽度 * @param ImageHeight:图片加载进来时高度 */ public static function setImageSize(Image:*,FrameWidth:Number,FrameHeight:Number,ImageWidth:Number, ImageHeight:Number):void { if ((ImageWidth / ImageHeight) > (FrameWidth / FrameHeight)) { Image.width = FrameWidth; Image.height = FrameWidth / (ImageWidth / ImageHeight); } else if ((ImageWidth / ImageHeight) < (FrameWidth / FrameHeight)) { Image.height = FrameHeight; Image.width = FrameHeight * (ImageWidth / ImageHeight); } else if ((ImageWidth / ImageHeight) == (FrameWidth / FrameHeight)) { Image.width = FrameWidth; Image.height = FrameHeight; } } } }
package com.tools { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.DisplayObject; import flash.display.Shape; import flash.display.Sprite; import flash.geom.Matrix; import flash.display.Graphics; import flash.geom.Point; import flash.display.GradientType; /** * 反射倒影生成类... */ public class Reflection extends Sprite { private var _reflection:Bitmap; private var _bit:BitmapData; private var _mtx:Matrix; private var _rBox:Shape; private var _Alpha:BitmapData; public function Reflection($Target:DisplayObject,$Height:Number=200,$Alpha:Number=1,AddHeight:Number =0):void { _reflection = new Bitmap(getReflection($Target,$Height));// _reflection.smoothing = true;//平滑 _reflection.alpha = $Alpha; addChild(_reflection); this.x = $Target.x; this.y = $Target.y + $Target.height + AddHeight; } private function getReflection(target:DisplayObject, Height:Number = -1):BitmapData { if (Height < 0) { Height = target.height; } _bit = new BitmapData(target.width,Height,true,0); _bit.draw(target, new Matrix(1, 0, 0, -1, 0, target.height)); _mtx = new Matrix(); _mtx.createGradientBox(target.width, Height, 0.5 * Math.PI); _rBox = new Shape(); _rBox.graphics.beginGradientFill(GradientType.LINEAR, [0, 0], [0.5, 0], [0, 255], _mtx); _rBox.graphics.drawRect(0, 0, target.width, Height); _rBox.graphics.endFill(); _Alpha = new BitmapData(target.width,Height,true,0); _Alpha.draw(_rBox); _bit.copyPixels(_bit, _bit.rect, new Point(0, 0), _Alpha, new Point(0, 0), false); return _bit.clone(); } } }
ui包中的类:
package com.ui.interfaces { import flash.display.Sprite; import com.ui.*; import com.tools.Reflection; import com.tools.Draws; import com.ui.TextStuta.CurrentInfo; import flash.display.Bitmap; import flash.text.TextFormatAlign; import flash.display.Shape; public class Interface extends Sprite { public var count:Sprite = new Sprite(); public var uploadphotos:UpLoadPhotos; public var uploadphotos_ref:Reflection; public var leftSpinVessel:Sprite; private var leftSpinBtn:leftSpin; private var leftSpinGray:leftSpin_gray; public var rightSpinVessel:Sprite; private var rightSpinBtn:rightSpin; private var rightSpinGray:rightSpin_gray; public var zoomVessel:Sprite; private var zoomBtn:zoom; private var zoomGray:zoom_gray; public var narrowVessel:Sprite; private var narrowBtn:narrow; private var narrowGray:narrow_gray; public var clearsVessel:Sprite; private var clearsBtn:clears; private var clearsGray:clears_gray; public var okVessel:Sprite; private var okBtn:ok; private var okGray:ok_gray; public var backsVessel:Sprite; private var backsBtn:backs; private var backsGray:backs_gray; public var PreviewImage1:Sprite;//50*50 public var PreviewImage2:Sprite;//100*100 public var PreviewImage3:Sprite;//140*140 private var PreviewImage1_ref:Reflection; private var PreviewImage2_ref:Reflection; private var PreviewImage3_ref:Reflection; public var grayPlaid:Sprite; private var Int:int = 1; public function Interface():void { var backGround:Bitmap = new Bitmap(new BG()); this.addChild(backGround); var logo:Bitmap = new Bitmap(new Logo()); this.addChild(logo); logo.x = 6; logo.y = 2; this.uploadphotos = new UpLoadPhotos(); this.addChild(this.uploadphotos); this.uploadphotos.buttonMode = true; this.uploadphotos.x = (400 - this.uploadphotos.width)/2; this.uploadphotos.y = (300 - this.uploadphotos.height)/2; this.uploadphotos_ref = new Reflection(this.uploadphotos,30,0.7); this.addChild(this.uploadphotos_ref); CurrentInfo.textInfoFormat.size = 12; CurrentInfo.textInfoFormat.color = 0x999999; CurrentInfo.textInfoFormat.font = "仿宋"; CurrentInfo.textInfoFormat.align = TextFormatAlign.LEFT; CurrentInfo.textInfo.text = "温馨提示:上传的图片尽量大于140*140像素"; CurrentInfo.textInfo.selectable = false; CurrentInfo.textInfo.mouseEnabled = false; CurrentInfo.textInfo.width = 400; CurrentInfo.textInfo.setTextFormat(CurrentInfo.textInfoFormat); this.addChild(CurrentInfo.textInfo); CurrentInfo.textInfo.x = 4; CurrentInfo.textInfo.y = 276; this.PreviewImage1 = Draws.DrawRects(51,51); this.PreviewImage2 = Draws.DrawRects(101,101); this.PreviewImage3 = Draws.DrawRects(141,141); } public function addChildSeleteImage():void { this.addChild(this.uploadphotos); this.addChild(this.uploadphotos_ref); } public function removeBigBtn():void { if(this.uploadphotos.parent != null) { this.removeChild(this.uploadphotos); this.removeChild(this.uploadphotos_ref); } } public function UIInit():void { this.addChild(this.count); grayPlaid = Draws.DrawLattice(2.3,43,396,230); this.addChild(grayPlaid); var Mask:Shape = new Shape(); Mask.graphics.beginFill(0x000000); Mask.graphics.drawRect(2.3,43,396,230); Mask.graphics.endFill(); this.addChild(Mask); grayPlaid.mask = Mask; this.leftSpinVessel = new Sprite(); this.count.addChild(this.leftSpinVessel); this.leftSpinVessel.x = 40; this.leftSpinVessel.y = 2; var leftAlphaBG:alphaBG = new alphaBG(); this.leftSpinVessel.addChild(leftAlphaBG); leftAlphaBG.alpha = 0; leftSpinBtn = new leftSpin(); this.leftSpinVessel.addChild(leftSpinBtn); leftSpinBtn.x = (leftAlphaBG.width - leftSpinBtn.width)/2; leftSpinBtn.y = (leftAlphaBG.height - leftSpinBtn.height)/2; var line1:line = new line(); this.count.addChild(line1); line1.x = this.leftSpinVessel.x + this.leftSpinVessel.width + 3; this.rightSpinVessel = new Sprite(); this.count.addChild(this.rightSpinVessel); this.rightSpinVessel.x = line1.x + 4; this.rightSpinVessel.y = this.leftSpinVessel.y; var rightAlphaBG:alphaBG = new alphaBG(); this.rightSpinVessel.addChild(rightAlphaBG); rightAlphaBG.alpha = 0; rightSpinBtn = new rightSpin(); this.rightSpinVessel.addChild(rightSpinBtn); rightSpinBtn.x = (rightAlphaBG.width - rightSpinBtn.width)/2; rightSpinBtn.y = (rightAlphaBG.height - rightSpinBtn.height)/2; var line2:line = new line(); this.count.addChild(line2); line2.x = this.rightSpinVessel.x + this.rightSpinVessel.width + 3; this.zoomVessel = new Sprite(); this.count.addChild(this.zoomVessel); this.zoomVessel.x = line2.x + 4; this.zoomVessel.y = this.leftSpinVessel.y; var zoomAlphaBG:alphaBG = new alphaBG(); this.zoomVessel.addChild(zoomAlphaBG); zoomAlphaBG.alpha = 0; zoomBtn = new zoom(); this.zoomVessel.addChild(zoomBtn); zoomBtn.x = (zoomAlphaBG.width - zoomBtn.width)/2; zoomBtn.y = (zoomAlphaBG.height - zoomBtn.height)/2; var line3:line = new line(); this.count.addChild(line3); line3.x = this.zoomVessel.x + this.zoomVessel.width + 3; this.narrowVessel = new Sprite(); this.count.addChild(this.narrowVessel); this.narrowVessel.x = line3.x + 4; this.narrowVessel.y = this.zoomVessel.y; var narrowAlphaBG:alphaBG = new alphaBG(); this.narrowVessel.addChild(narrowAlphaBG); narrowAlphaBG.alpha = 0; narrowBtn = new narrow(); this.narrowVessel.addChild(narrowBtn); narrowBtn.x = (narrowAlphaBG.width - narrowBtn.width)/2; narrowBtn.y = (narrowAlphaBG.height - narrowBtn.height)/2; var line4:line = new line(); this.count.addChild(line4); line4.x = this.narrowVessel.x + this.narrowVessel.width + 3; this.clearsVessel = new Sprite(); this.count.addChild(this.clearsVessel); this.clearsVessel.x = line4.x + 4; this.clearsVessel.y = this.narrowVessel.y; var clearsAlphaBG:alphaBG = new alphaBG(); this.clearsVessel.addChild(clearsAlphaBG); clearsAlphaBG.alpha = 0; clearsBtn = new clears(); this.clearsVessel.addChild(clearsBtn); clearsBtn.x = clearsAlphaBG.width/2; clearsBtn.y = clearsAlphaBG.height/2; var line5:line = new line(); this.count.addChild(line5); line5.x = this.clearsVessel.x + this.clearsVessel.width + 3; this.okVessel = new Sprite(); this.count.addChild(this.okVessel); this.okVessel.x = line5.x + 4; this.okVessel.y = this.clearsVessel.y; var okAlphaBG:alphaBG = new alphaBG(); this.okVessel.addChild(okAlphaBG); okAlphaBG.alpha = 0; okBtn = new ok(); this.okVessel.addChild(okBtn); okBtn.x = (okAlphaBG.width - okBtn.width)/2; okBtn.y = (okAlphaBG.height - okBtn.height)/2; this.backsVessel = new Sprite(); this.count.addChild(this.backsVessel); this.backsVessel.x = this.okVessel.x + this.okVessel.width + 40; this.backsVessel.y = this.okVessel.y; var backsAlphaBG:backAlphaBG = new backAlphaBG(); this.backsVessel.addChild(backsAlphaBG); backsAlphaBG.alpha = 0; backsBtn = new backs(); this.backsVessel.addChild(backsBtn); backsBtn.x = (backsAlphaBG.width - backsBtn.width)/2; backsBtn.y = (backsAlphaBG.height - backsBtn.height)/2; } public function CreatePreviewImage():void { this.count.addChild(this.PreviewImage1); this.count.addChild(this.PreviewImage2); this.count.addChild(this.PreviewImage3); this.PreviewImage3.x = 25; this.PreviewImage3.y = 90; this.PreviewImage2.x = 195; this.PreviewImage2.y = 100; this.PreviewImage1.x = 320; this.PreviewImage1.y = 110; PreviewImage1_ref = new Reflection(this.PreviewImage1,30,0.5); PreviewImage2_ref = new Reflection(this.PreviewImage2,30,0.5); PreviewImage3_ref = new Reflection(this.PreviewImage3,30,0.5); this.count.addChild(PreviewImage1_ref); this.count.addChild(PreviewImage2_ref); this.count.addChild(PreviewImage3_ref); } public function deleteGrayPlaid():void { this.removeChild(grayPlaid); } public function addGrayPlaid():void { this.addChild(grayPlaid); } public function deletePreviewImage():void { if(this.PreviewImage3.numChildren != 0) { this.PreviewImage3.removeChildAt(0); } if(this.PreviewImage1.numChildren != 0) { this.PreviewImage1.removeChildAt(0); } if(this.PreviewImage2.numChildren != 0) { this.PreviewImage2.removeChildAt(0); } if(this.PreviewImage1.parent != null) { this.count.removeChild(this.PreviewImage1); this.count.removeChild(this.PreviewImage2); this.count.removeChild(this.PreviewImage3); } if(this.PreviewImage1_ref != null && this.PreviewImage1_ref.parent != null) { this.count.removeChild(this.PreviewImage1_ref); this.count.removeChild(this.PreviewImage2_ref); this.count.removeChild(this.PreviewImage3_ref); } } public function addGrayDisplay():void { if(this.Int == 1) { this.newGray(); } this.Int ++; this.leftSpinVessel.removeChildAt(1); this.leftSpinVessel.addChildAt(this.leftSpinGray,1); this.rightSpinVessel.removeChildAt(1); this.rightSpinVessel.addChildAt(this.rightSpinGray,1); this.zoomVessel.removeChildAt(1); this.zoomVessel.addChildAt(this.zoomGray,1); this.narrowVessel.removeChildAt(1); this.narrowVessel.addChildAt(this.narrowGray,1); this.clearsVessel.removeChildAt(1); this.clearsVessel.addChildAt(this.clearsGray,1); } public function removeGrayDisplay():void { this.leftSpinVessel.removeChildAt(1); this.leftSpinVessel.addChildAt(this.leftSpinBtn,1); this.rightSpinVessel.removeChildAt(1); this.rightSpinVessel.addChildAt(this.rightSpinBtn,1); this.zoomVessel.removeChildAt(1); this.zoomVessel.addChildAt(this.zoomBtn,1); this.narrowVessel.removeChildAt(1); this.narrowVessel.addChildAt(this.narrowBtn,1); this.clearsVessel.removeChildAt(1); this.clearsVessel.addChildAt(this.clearsBtn,1); } public function addOtherGrayDisplay():void { this.okVessel.removeChildAt(1); this.okVessel.addChildAt(this.okGray,1); this.backsVessel.removeChildAt(1); this.backsVessel.addChildAt(this.backsGray,1); } public function removeOtherGrayDisplay():void { this.okVessel.removeChildAt(1); this.okVessel.addChildAt(this.okBtn,1); this.backsVessel.removeChildAt(1); this.backsVessel.addChildAt(this.backsBtn,1); } private function newGray():void { //实例化至灰按钮 this.leftSpinGray = new leftSpin_gray(); this.leftSpinGray.x = this.leftSpinVessel.getChildAt(1).x; this.leftSpinGray.y = this.leftSpinVessel.getChildAt(1).y; this.rightSpinGray = new rightSpin_gray(); this.rightSpinGray.x = this.rightSpinVessel.getChildAt(1).x; this.rightSpinGray.y = this.rightSpinVessel.getChildAt(1).y; this.zoomGray = new zoom_gray(); this.zoomGray.x = this.zoomVessel.getChildAt(1).x; this.zoomGray.y = this.zoomVessel.getChildAt(1).y; this.narrowGray = new narrow_gray(); this.narrowGray.x = this.narrowVessel.getChildAt(1).x; this.narrowGray.y = this.narrowVessel.getChildAt(1).y; this.clearsGray = new clears_gray(); this.clearsGray.x = this.clearsVessel.getChildAt(1).x; this.clearsGray.y = this.clearsVessel.getChildAt(1).y; this.okGray = new ok_gray(); this.okGray.x = this.okVessel.getChildAt(1).x; this.okGray.y = this.okVessel.getChildAt(1).y; this.backsGray = new backs_gray(); this.backsGray.x = this.backsVessel.getChildAt(1).x; this.backsGray.y = this.backsVessel.getChildAt(1).y; } } }
package com.ui.TextStuta { import flash.text.TextField; import flash.text.TextFormat; public class CurrentInfo { public static var textInfo:TextField = new TextField(); public static var textInfoFormat:TextFormat = new TextFormat(); } }
package com.ui { import flash.display.Sprite; import flash.display.Bitmap; import flash.display.DisplayObjectContainer; import flash.events.Event; import flash.events.MouseEvent; import flash.ui.Mouse; public class MouseCursor extends Object { public static var MOUSE_MOVING:uint = 3; public static var MOUSE_RESIZABLE:uint = 1; public static var MOUSE_MOVABLE:uint = 2; private static var _status:uint = MOUSE_NORMAL; public static var MOUSE_NORMAL:uint = 0; public static var MOUSE_RESIZABLEH:uint = 4; private static var _resizable:Sprite; private static var _moving:Sprite; private static var _movable:Sprite; private static var _resizableh:Sprite; public function MouseCursor() { return; } public static function hide():void { show(MOUSE_NORMAL); return; } public static function setPoint(X:Number, Y:Number):void { _movable.x = Math.round(X); _movable.y = Math.round(Y); _resizable.x = Math.round(X); _resizable.y = Math.round(Y); _resizableh.x = Math.round(X); _resizableh.y = Math.round(Y); _moving.x = Math.round(X); _moving.y = Math.round(Y); return; } public static function init(disobjc:DisplayObjectContainer):void { var bitmap:Bitmap = null; if (disobjc.stage == null) { disobjc.addEventListener(Event.ADDED_TO_STAGE, addedHandler); return; } if (_resizable == null) { _resizable = new Sprite(); bitmap = new Bitmap(new ResizeCursor()); bitmap.x = (-bitmap.width) / 2; bitmap.y = (-bitmap.height) / 2; _resizable.addChild(bitmap); _resizable.visible = false; _resizable.mouseChildren = false; _resizable.mouseEnabled = false; disobjc.stage.addChild(_resizable); } if (_resizableh == null) { _resizableh = new Sprite(); bitmap = new Bitmap(new ResizeHCursor()); bitmap.x = (-bitmap.width) / 2; bitmap.y = (-bitmap.height) / 2; _resizableh.addChild(bitmap); _resizableh.visible = false; _resizableh.mouseChildren = false; _resizableh.mouseEnabled = false; disobjc.stage.addChild(_resizableh); } if (_movable == null) { _movable = new Sprite(); bitmap = new Bitmap(new MoveCursor()); bitmap.x = (-bitmap.width) / 2; bitmap.y = (-bitmap.height) / 2; _movable.addChild(bitmap); _movable.visible = false; _movable.mouseChildren = false; _movable.mouseEnabled = false; disobjc.stage.addChild(_movable); } if (_moving == null) { _moving = new Sprite(); bitmap = new Bitmap(new MoveCursor()); bitmap.x = (-bitmap.width) / 2; bitmap.y = (-bitmap.height) / 2; _moving.addChild(bitmap); _moving.visible = false; _moving.mouseChildren = false; _moving.mouseEnabled = false; disobjc.stage.addChild(_moving); } disobjc.stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler); disobjc.stage.addEventListener(MouseEvent.CLICK, clickHandler); disobjc.stage.addEventListener(Event.MOUSE_LEAVE, leaveHandler); return; } public static function leaveHandler(event:Event):void { _movable.visible = false; _resizable.visible = false; _resizableh.visible = false; _moving.visible = false; return; } private static function addedHandler(event:Event):void { event.target.removeEventListener(Event.ADDED_TO_STAGE, addedHandler); MouseCursor.init(event.target as DisplayObjectContainer); return; } public static function clickHandler(event:MouseEvent):void { if (_status != MOUSE_NORMAL) { Mouse.hide(); } setPoint(event.stageX, event.stageY); return; } public static function moveHandler(event:MouseEvent):void { setPoint(event.stageX, event.stageY); _movable.visible = _status == MOUSE_MOVABLE; _resizable.visible = _status == MOUSE_RESIZABLE; _resizableh.visible = _status == MOUSE_RESIZABLEH; _moving.visible = _status == MOUSE_MOVING; event.updateAfterEvent(); return; } public static function show(num1:uint, num2:uint = 0):void { if (num1 == MOUSE_RESIZABLE) { if (num2 % 90 == 0) { num1 = MOUSE_RESIZABLEH; _resizableh.rotation = num2 + 90; } else { _resizable.rotation = num2 + 45; } } if (_status == num1) { return; } _status = num1; if (num1 == MOUSE_RESIZABLE) { Mouse.hide(); _resizableh.visible = false; _movable.visible = false; _moving.visible = false; _resizable.visible = true; } else if (num1 == MOUSE_RESIZABLEH) { Mouse.hide(); _resizable.visible = false; _moving.visible = false; _movable.visible = false; _resizableh.visible = true; } else if (num1 == MOUSE_MOVABLE) { Mouse.hide(); _resizable.visible = false; _resizableh.visible = false; _moving.visible = false; _movable.visible = true; } else if (num1 == MOUSE_MOVING) { Mouse.hide(); _resizable.visible = false; _resizableh.visible = false; _movable.visible = false; _moving.visible = true; } else { _resizable.visible = false; _resizableh.visible = false; _movable.visible = false; _moving.visible = false; Mouse.show(); } return; } } }
package com { import com.ui.MouseCursor; import flash.display.Sprite; import flash.display.DisplayObject; import flash.events.MouseEvent; import flash.events.Event; import flash.geom.Point; import flash.geom.Rectangle; import flash.display.Stage; public class EditBounds extends Sprite { private var ox:Number; private var ratio:Number = 1; private var currDir:String; public var selectWidth:Number = 100; private var constraints:Boolean = false; public var selectHeight:Number = 100; public var line:Sprite; private var outRangle:Boolean = false; private var soy:Number; private var resizeDir:Object; private var cornerWidth:Number = 6; public var minHeight:Number = 4.94066e-324; public var maxHeight:Number = 1.79769e+308; public var minWidth:Number = 4.94066e-324; public var maxWidth:Number = 1.79769e+308; private var corners:Object; private var _target:DisplayObject; private var sox:Number; private var bounds:Rectangle; private var mousedown:Boolean = false; private var oh:Number; private var rox:Number; private var roy:Number; private var ow:Number; private var oy:Number; private var cursorRotations:Object; public static var busy:Boolean = false; public function EditBounds():void { corners = {}; cursorRotations = {lt:135,t:0,rt:45,r:90,rb:135,b:0,lb:45,l:90}; resizeDir = {lt:resize_lt,t:resize_t,rt:resize_rt,r:resize_r,rb:resize_rb,b:resize_b,lb:resize_lb,l:resize_l}; selectWidth = 100; selectHeight = 100; cornerWidth = 6; maxWidth = Number.MAX_VALUE; maxHeight = Number.MAX_VALUE; minWidth = Number.MIN_VALUE; minHeight = Number.MIN_VALUE; constraints = false; ratio = 1; mousedown = false; bounds = new Rectangle(0,0,100,100); outRangle = false; line = new Sprite(); line.x = cornerWidth / 2; line.y = cornerWidth / 2; line.graphics.beginFill(0x0000FF); line.graphics.drawRect(0, 0, (selectWidth - 1), 1); line.graphics.drawRect((selectWidth - 1), 0, 1, (selectHeight - 1)); line.graphics.drawRect(1, (selectHeight - 1), (selectWidth - 1), 1); line.graphics.drawRect(0, 1, 1, (selectHeight - 1)); line.graphics.endFill(); line.graphics.beginFill(0, 0); line.graphics.drawRect(1, 1, selectWidth - 2, selectHeight - 2); line.graphics.endFill(); line.scale9Grid = new Rectangle(2,2,selectWidth - 4,selectHeight - 4); corners.lt = createCorner("lt",0,0,cornerWidth); corners.t = createCorner("t",0,0,cornerWidth); corners.rt = createCorner("rt",0,0,cornerWidth); corners.r = createCorner("r",0,0,cornerWidth); corners.rb = createCorner("rb",0,0,cornerWidth); corners.b = createCorner("b",0,0,cornerWidth); corners.lb = createCorner("lb",0,0,cornerWidth); corners.l = createCorner("l",0,0,cornerWidth); addChild(line); addChild(corners.lt); addChild(corners.rt); addChild(corners.rb); addChild(corners.lb); constraints = true; addChild(corners.t); addChild(corners.r); addChild(corners.b); addChild(corners.l); MouseCursor.init(this); resetCorner(); return; } private function resize_lt(W:Number, H:Number):void { var num1:Number = 0; var num2:Number = 0; var num3:Number = 0; var num4:Number = 0; num1 = ow - W; num2 = oh - H; if (constraints) { if (Math.min(num1,num2) == num1) { num2 = Math.round(num1 / ratio); } else { num1 = Math.round(num2 * ratio); } if (num1 > sox + ow + cornerWidth / 2) { var num5:* = sox + ow + cornerWidth / 2; num1 = sox + ow + cornerWidth / 2; num2 = num5; } if (num2 > soy + oh + cornerWidth / 2) { var num6:* = soy + oh + cornerWidth / 2; num2 = soy + oh + cornerWidth / 2; num1 = num6; } } num3 = limitedResizeWidth(num1); num4 = limitedResizeHeight(num2); limitedMoveX(sox + (ow - num3)); limitedMoveY(soy + (oh - num4)); _target.x = this.x; _target.y = this.y; return; } private function panelMouseOverHandler(event:MouseEvent):void { if (mousedown) { return; } busy = true; MouseCursor.show(MouseCursor.MOUSE_MOVABLE); return; } public function set target(obj:DisplayObject):void { _target = obj; this.x = _target.x; this.y = _target.y; line.width = _target.width; line.height = _target.height; this.width = _target.width; this.height = _target.height; return; } override public function get height():Number { return line.height; } private function limitedMoveX(num:Number):Number { num = Math.round(Math.max(bounds.x,num)); num = Math.round(Math.min(bounds.width - this.width,num)); var num2:* = num; super.x = num; return num2; } private function panelMouseUpHandler(event:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_UP, panelMouseUpHandler); stage.removeEventListener(MouseEvent.MOUSE_MOVE, panelMouseMoveHandler); mousedown = false; this.dispatchEvent(new Event(Event.CHANGE)); return; } private function limitedMoveY(num:Number):Number { num = Math.round(Math.max(bounds.y,num)); num = Math.round(Math.min(bounds.height - this.height,num)); var num2:* = num; super.y = num; return num2; } override public function set height(num:Number):void { line.height = num; resetCorner(); return; } private function mouseMoveHandler(event:MouseEvent):void { var myx:Number = NaN; var myy:Number = NaN; if (mousedown) { myx = stage.mouseX; myy = stage.mouseY; resizeDir[currDir](Math.round(myx - ox),Math.round(myy - oy)); resetCorner(); } MouseCursor.setPoint(stage.mouseX, stage.mouseY); event.updateAfterEvent(); return; } private function cornerMouseOverHandler(event:MouseEvent):void { if (mousedown) { return; } busy = true; MouseCursor.show(MouseCursor.MOUSE_RESIZABLE, cursorRotations[event.target.name]); MouseCursor.setPoint(stage.mouseX, stage.mouseY); return; } private function createCorner(Name:String, X:Number, Y:Number, WH:Number):Sprite { var corner:Sprite = null; corner = new Sprite(); corner.name = Name; corner.graphics.lineStyle(1, 0xFFFFFF); corner.graphics.beginFill(0x0000FF); corner.graphics.drawRect(X, Y, WH, WH); corner.graphics.endFill(); corner.addEventListener(MouseEvent.MOUSE_OVER, cornerMouseOverHandler); corner.addEventListener(MouseEvent.MOUSE_DOWN, cornerMouseDownHandler); corner.addEventListener(MouseEvent.MOUSE_OUT, cornerMouseOutHandler); corner.addEventListener(MouseEvent.MOUSE_UP, cornerMouseUpHandler); line.addEventListener(MouseEvent.MOUSE_OVER, panelMouseOverHandler); line.addEventListener(MouseEvent.MOUSE_DOWN, panelMouseDownHandler); line.addEventListener(MouseEvent.MOUSE_OUT, panelMouseOutHandler); line.addEventListener(MouseEvent.MOUSE_UP, panelMouseUpHandler); return corner; } public function get changing():Boolean { return mousedown; } override public function get x():Number { return super.x + cornerWidth / 2; } override public function get y():Number { return super.y + cornerWidth / 2; } private function panelMouseMoveHandler(event:MouseEvent):void { if (mousedown) { limitedMoveX(sox + (stage.mouseX - ox)); limitedMoveY(soy + (stage.mouseY - oy)); _target.x = this.x; _target.y = this.y; } MouseCursor.setPoint(stage.mouseX, stage.mouseY); event.updateAfterEvent(); return; } private function resetCorner():void { corners.lt.x = Math.round(line.x - cornerWidth / 2); corners.lt.y = Math.round(line.y - cornerWidth / 2); corners.t.x = Math.round(line.width / 2); corners.t.y = corners.lt.y; corners.rt.x = corners.lt.x + line.width; corners.rt.y = corners.lt.y; corners.r.x = corners.rt.x; corners.r.y = Math.round(line.height / 2); corners.rb.x = corners.rt.x; corners.rb.y = corners.lt.y + line.height; corners.b.x = corners.t.x; corners.b.y = corners.rb.y; corners.lb.x = corners.lt.x; corners.lb.y = corners.b.y; corners.l.x = corners.lb.x; corners.l.y = corners.r.y; return; } private function panelMouseDownHandler(event:MouseEvent):void { mousedown = true; ox = stage.mouseX; oy = stage.mouseY; sox = super.x; soy = super.y; stage.addEventListener(MouseEvent.MOUSE_UP, panelMouseUpHandler); stage.addEventListener(MouseEvent.MOUSE_MOVE, panelMouseMoveHandler); return; } private function limitedResizeWidth(W:Number):Number { if (W <= minWidth) { line.width = minWidth; _target.width = line.width; return minWidth; } if (W >= maxWidth) { line.width = maxWidth; _target.width = line.width; return maxWidth; } _target.width = W; var n:Number = W; line.width = W; return n; } public function get target():DisplayObject { return _target; } override public function set width(Width:Number):void { line.width = Width; resetCorner(); return; } private function resize_b(W:Number, H:Number):void { var num1:Number = NaN; var num2:Number = NaN; var num3:Number = NaN; if (constraints) { num1 = ow; num2 = Math.round((oh + H) / 2) * 2; num1 = Math.round(num2 * ratio); if (num2 + soy > bounds.height) { var n1:Number = bounds.height - soy; num2 = bounds.height - soy; num1 = n1; } if (num1 / 2 + (sox + ow / 2) > bounds.width) { var n2:Number = (bounds.width - (sox + ow / 2)) * 2; num1 = (bounds.width - (sox + ow / 2)) * 2; num2 = n2; } if (sox + ow / 2 - num1 / 2 < bounds.x) { var n3:Number = (sox + ow / 2 + cornerWidth / 2) * 2; num1 = (sox + ow / 2 + cornerWidth / 2) * 2; num2 = n3; } num3 = limitedResizeWidth(num1); limitedResizeHeight(num2); limitedMoveX(sox + (ow - num3) / 2); _target.x = this.x; } else { limitedResizeHeight(oh + H); } return; } private function resize_l(W:Number, H:Number):void { var num1:Number = NaN; var num2:Number = NaN; var num3:Number = NaN; var num4:Number = NaN; if (constraints) { num3 = Math.round((ow - W) / 2) * 2; num4 = oh; num4 = Math.round(num3 * ratio); if (num3 > sox + ow + cornerWidth / 2) { var n1:Number = sox + ow + cornerWidth / 2; num3 = sox + ow + cornerWidth / 2; num4 = n1; } if (num4 / 2 + (soy + oh / 2) > bounds.height) { var n2:Number = (bounds.height - (soy + oh / 2)) * 2; num4 = (bounds.height - (soy + oh / 2)) * 2; num3 = n2; } if (soy + oh / 2 - num4 / 2 < bounds.y) { var n3:Number = (soy + oh / 2 + cornerWidth / 2) * 2; num4 = (soy + oh / 2 + cornerWidth / 2) * 2; num3 = n3; } num1 = limitedResizeWidth(num3); num2 = limitedResizeHeight(num4); limitedMoveX(sox + (ow - num1)); limitedMoveY(soy + (oh - num2) / 2); _target.x = this.x; _target.y = this.y; } else { num1 = limitedResizeWidth(ow - W); limitedMoveX(sox + (ow - num1)); _target.x = this.x; } return; } private function cornerMouseOutHandler(event:MouseEvent):void { if (mousedown) { return; } MouseCursor.hide(); busy = false; event.updateAfterEvent(); return; } private function resize_r(W:Number, H:Number):void { var num1:Number = NaN; var num2:Number = NaN; var num3:Number = NaN; if (constraints) { num1 = Math.round((ow + W) / 2) * 2; num2 = oh; num2 = Math.round(num1 / ratio); if (num1 + sox > bounds.width) { var n1:Number = bounds.width - sox; num1 = bounds.width - sox; num2 = n1; } if (num2 / 2 + (soy + oh / 2) > bounds.height) { var n2:Number = (bounds.height - (soy + oh / 2)) * 2; num2 = (bounds.height - (soy + oh / 2)) * 2; num1 = n2; } if (soy + oh / 2 - num1 / 2 < bounds.y) { var n3:Number = (soy + oh / 2 + cornerWidth / 2) * 2; num2 = (soy + oh / 2 + cornerWidth / 2) * 2; num1 = n3; } limitedResizeWidth(num1); num3 = limitedResizeHeight(num2); limitedMoveY(soy + (oh - num3) / 2); _target.y = this.y; } else { limitedResizeWidth(ow + W); } return; } private function resize_t(W:Number, H:Number):void { var num1:Number = 0; var num2:Number = 0; var num3:Number = 0; var num4:Number = 0; if (constraints) { num3 = ow; num4 = Math.round((oh - H) / 2) * 2; num3 = Math.round(num4 * ratio); if (num4 > soy + oh + cornerWidth / 2) { var n1:Number = soy + oh + cornerWidth / 2; num4 = soy + oh + cornerWidth / 2; num3 = n1; } if (num3 / 2 + (sox + ow / 2) > bounds.width) { var n2:Number = (bounds.width - (sox + ow / 2)) * 2; num3 = (bounds.width - (sox + ow / 2)) * 2; num4 = n2; } if (sox + ow / 2 - num3 / 2 < bounds.x) { var n3:Number = (sox + ow / 2 + cornerWidth / 2) * 2; num3 = (sox + ow / 2 + cornerWidth / 2) * 2; num4 = n3; } num1 = limitedResizeWidth(num3); num2 = limitedResizeHeight(num4); limitedMoveX(sox + (ow - num1) / 2); limitedMoveY(soy + (oh - num2)); _target.x = this.x; _target.y = this.y; } else { num2 = limitedResizeHeight(oh - H); limitedMoveY(soy + (oh - num2)); _target.y = this.y; } return; } private function resize_rt(W:Number, H:Number):void { var num1:Number = NaN; var num2:Number = NaN; var num3:Number = NaN; num1 = ow + W; num2 = oh - H; if (constraints) { if (Math.min(num1,num2) == num1) { num2 = Math.round(num1 / ratio); } else { num1 = Math.round(num2 * ratio); } if (num2 > soy + oh + cornerWidth / 2) { var n1:Number = soy + oh + cornerWidth / 2; num2 = soy + oh + cornerWidth / 2; num1 = n1; } if (num1 + this.x - cornerWidth / 2 > bounds.width) { var n2:Number = bounds.width - this.x + cornerWidth / 2; num1 = bounds.width - this.x + cornerWidth / 2; num2 = n2; } } limitedResizeWidth(num1); num3 = limitedResizeHeight(num2); limitedMoveY(soy + (oh - num3)); _target.y = this.y; return; } public function set selectBounds(rectangle:Rectangle):void { bounds.x = rectangle.x - cornerWidth / 2; bounds.y = rectangle.y - cornerWidth / 2; bounds.width = rectangle.width - (cornerWidth / 2); bounds.height = rectangle.height - (cornerWidth / 2); return; } private function resize_rb(W:Number, H:Number):void { var num1:Number = NaN; var num2:Number = NaN; num1 = ow + W; num2 = oh + H; if (constraints) { if (Math.min(num1,num2) == num1) { num2 = Math.round(num1 / ratio); } else { num1 = Math.round(num2 * ratio); } if (num1 + this.x - cornerWidth / 2 > bounds.width) { var n1:Number = bounds.width - this.x + cornerWidth / 2; num1 = bounds.width - this.x + cornerWidth / 2; num2 = n1; } if (num2 + this.y - cornerWidth / 2 > bounds.height) { var n2:Number = bounds.height - this.y + cornerWidth / 2; num2 = bounds.height - this.y + cornerWidth / 2; num1 = n2; } } limitedResizeWidth(num1); limitedResizeHeight(num2); return; } private function limitedResizeHeight(num:Number):Number { if (num <= minHeight) { line.height = minHeight; _target.height = line.height; return minHeight; } if (num >= maxHeight) { line.height = maxHeight; _target.height = line.height; return maxHeight; } _target.height = num; var H:Number = num; line.height = num; return H; } override public function get width():Number { return line.width; } private function cornerMouseUpHandler(event:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_UP, cornerMouseUpHandler); stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); mousedown = false; if (! corners[event.target.name] || ! corners[event.target.name].hitTestPoint(stage.mouseX,stage.mouseY)) { cornerMouseOutHandler(event); } this.dispatchEvent(new Event(Event.CHANGE)); return; } private function resize_lb(W:Number, H:Number):void { var num:Number = NaN; var num1:Number = NaN; var num2:Number = NaN; num = ow - W; num1 = oh + H; if (constraints) { if (Math.min(num,num1) == num) { num1 = Math.round(num / ratio); } else { num = Math.round(num1 * ratio); } if (num > sox + ow + cornerWidth / 2) { var num3:* = sox + ow + cornerWidth / 2; num = sox + ow + cornerWidth / 2; num1 = num3; } if (num1 + this.y - cornerWidth / 2 > bounds.height) { var num4:* = bounds.height - this.y + cornerWidth / 2; num1 = bounds.height - this.y + cornerWidth / 2; num = num4; } } num2 = limitedResizeWidth(num); limitedResizeHeight(num1); limitedMoveX(sox + (ow - num2)); _target.x = this.x; return; } override public function set x(num:Number):void { super.x = num - cornerWidth / 2; return; } override public function set y(num:Number):void { super.y = num - cornerWidth / 2; return; } private function cornerMouseDownHandler(event:MouseEvent):void { mousedown = true; ox = stage.mouseX; oy = stage.mouseY; ow = line.width; oh = line.height; sox = super.x; soy = super.y; rox = event.target.mouseX; roy = event.target.mouseY; this.setChildIndex(event.target as DisplayObject, (this.numChildren - 1)); currDir = event.target.name; stage.addEventListener(MouseEvent.MOUSE_UP, cornerMouseUpHandler); stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); return; } private function panelMouseOutHandler(event:MouseEvent):void { if (mousedown) { return; } MouseCursor.hide(); busy = false; return; } } }
其中还用到一些第三方类库:greensock、json、图像编码等...,最后是后台的一些脚本和页面:
saveImage.asp
<% dim strSaveFileName dim strNow strnow =replace(replace(replace(now(), ":", ""), "-", ""), " ", "") if request.servervariables("REQUEST_METHOD")="POST" then '当前为post FormSize=Request.TotalBytes FormData=Request.BinaryRead(FormSize) '接收数据到FormData strSaveFileName =strNow &".png" set dr=CreateObject("Adodb.Stream") dr.Mode=3 dr.Type=1 dr.Open dr.Write(FormData) dr.SaveToFile Server.MapPath(strSaveFileName),2 dr.Close set dr=nothing response.Write(strSaveFileName) end if %>
仅供参考!