code on 2009-1-7 ... @author DefaultUser (Tools -> Custom Arguments...) chikathreesi.. forked:0favorite:0lines:111license : All rights reserved modified : 2009-01-08 16:53:34 Embed Tweet // write as3 code here.. package { import flash.display.*; import flash.events.Event; import flash.events.MouseEvent; import flash.net.*; /** * ... * @author DefaultUser (Tools -> Custom Arguments...) */ public class DrawTriangle extends Sprite { private var _img:Bitmap; private var _bmp:BitmapData; private var _vertices:Vector.<Number>; private var _indexes:Vector.<int>; private var _uvtData:Vector.<Number>; private var _dots:Vector.<Sprite>; private const _DIV:int = 10; private var _stepW:Number; private var _stepH:Number; private const _IMG_PATH:String = "http://www.chikathreesix.com/test/sample1.jpg"; public function DrawTriangle() { _loadIMG(); } private function _loadIMG():void{ var loader:Loader = new Loader(); loader.addEventListener(Event.COMPLETE, _loadedIMG); loader.load(new URLRequest(_IMG_PATH)); } private function _loadedIMG(e:Event):void{ _img = Bitmap(e.currentTarget.data); _stepW = _img.width / _DIV; _stepH = _img.height / _DIV; _bmp = Bitmap(e.currentTarget.data).bitmapData; _setDot(); _setDrawingData(); _render(); } private function _setDot():void { _dots = new Vector.<Sprite>; for (var i:int = 0; i <= _DIV; i++) { for (var j:int = 0; j <= _DIV; j++) { var dot:Sprite = new Dot(); //dot.x = inu.x + j * _stepW; //dot.y = inu.y + i * _stepH; dot.x = j * _stepW; dot.y = i * _stepH; dot.addEventListener(MouseEvent.MOUSE_DOWN, _startDrag); dot.addEventListener(MouseEvent.MOUSE_UP, _stopDrag); addChild(dot); _dots.push(dot); } } } private function _startDrag(e:Event):void { this.addEventListener(Event.ENTER_FRAME, _enterFrameAction); this.stage.addEventListener(MouseEvent.MOUSE_UP, _stopDrag); e.currentTarget.startDrag(); } private function _enterFrameAction(e:Event):void { _setDrawingData(); _render(); } private function _stopDrag(e:Event):void { this.stage.removeEventListener(MouseEvent.MOUSE_UP, _stopDrag); this.removeEventListener(Event.ENTER_FRAME, _enterFrameAction); e.currentTarget.stopDrag(); } private function _setDrawingData():void { var stepNum:int = 0; _vertices = new Vector.<Number>(); _indexes = new Vector.<int>(); _uvtData = new Vector.<Number>(); for (var i:int = 0; i < _DIV; i++) { for (var j:int = 0; j < _DIV; j++) { var k:int = i * (_DIV + 1) + j; var k1:int = (i + 1) * (_DIV + 1) + j; _vertices.push( _dots[k].x, _dots[k].y, _dots[k + 1].x, _dots[k + 1].y, _dots[k1 + 1].x, _dots[k1 + 1].y, _dots[k1].x, _dots[k1].y ); _uvtData.push( j / _DIV, i / _DIV, (j + 1) / _DIV, i / _DIV, (j + 1) / _DIV, (i + 1) / _DIV, j / _DIV, (i + 1) / _DIV ); _indexes.push(stepNum, stepNum + 1, stepNum + 3, stepNum + 1, stepNum + 2, stepNum + 3); stepNum += 4; } } } private function _render():void { this.graphics.clear(); this.graphics.beginBitmapFill(_bmp); this.graphics.drawTriangles(_vertices, _indexes, _uvtData); } } } import flash.display.*; class Dot extends Sprite{ private const _radius:Number = 2; public function Dot(){ graphics.drawCircle(_radius /2 * -1, _radius / 2 * -1, _radius); } } Code Fullscreen Preview Fullscreen MouseEvent.MOUSE_UP data Loader stopDrag MouseEvent.ENTER_FRAME startDrag beginBitmapFill MouseEvent.COMPLETE addEventListener drawTriangles width load height loader bitmapData removeEventListener MouseEvent.MOUSE_DOWN clear URLRequest drawCircle