package { import flash.display.*; import flash.events.MouseEvent; [SWF(backgroundColor=0xffffff,width='1024',height='600')] public class BitmapTriangleUV2 extends Sprite { [Embed(source='image.jpg')] private var ImageClass:Class; private var handle0:Sprite; private var handle1:Sprite; private var handle2:Sprite; private var handle3:Sprite; private var bitmap:Bitmap; private var vertices:Vector.<Number> = new Vector.<Number>(); private var uvData:Vector.<Number> = new Vector.<Number>(); private var indices:Vector.<int> = new Vector.<int>(); public function BitmapTriangleUV2() { stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; //创建拖动点 handle0 = makeHandle(100,100); handle1 = makeHandle(200,100); handle2 = makeHandle(200,200); handle3 = makeHandle(100,200); uvData.push(0,0); uvData.push(1,0); uvData.push(1,1); uvData.push(0,1); //创建三角形 indices.push(0,1,2); indices.push(2,3,0); bitmap = new ImageClass() as Bitmap; draw(); } //拖动点 private function makeHandle(xpos:Number, ypos:Number):Sprite { var handle:Sprite = new Sprite(); handle.graphics.beginFill(0); handle.graphics.drawCircle(0,0,5); handle.graphics.endFill(); handle.addEventListener(MouseEvent.MOUSE_DOWN, handlerMouseDown); handle.x = xpos; handle.y = ypos; this.addChild(handle); return handle; } private function handlerMouseDown(e:MouseEvent):void { e.target.startDrag(); stage.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove ); stage.addEventListener( MouseEvent.MOUSE_UP, onMouseUp ); } private function onMouseMove( e:MouseEvent ):void { draw(); } private function onMouseUp( e:MouseEvent ):void { stopDrag(); stage.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseMove ); stage.removeEventListener( MouseEvent.MOUSE_UP, onMouseUp ); } //bitmap填充 private function draw():void { vertices[0] = handle0.x; vertices[1] = handle0.y; vertices[2] = handle1.x; vertices[3] = handle1.y; vertices[4] = handle2.x; vertices[5] = handle2.y; vertices[6] = handle3.x; vertices[7] = handle3.y; this.graphics.clear(); this.graphics.beginBitmapFill(bitmap.bitmapData); this.graphics.drawTriangles(vertices, indices, uvData); this.graphics.endFill(); } } }