In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

絵をかくテスト

Get Adobe Flash player
by takaaki024 19 Dec 2008
package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    import flash.geom.Point;
    import flash.text.TextField;
    
    [SWF(width="460", height="460", backgroundColor="#ffffff", frameRate="24")]
    public class TaPaint extends Sprite
    {
        private var canvas        :Sprite;
        private var GRID_INTERVAL :int = 10;
        private var mousePointer  :Sprite;
        private var draft         :Sprite;
        private var drawMode      :String = "rect";
        private var points        :Array; // <Point>
        private var shapes        :Array; // <Shape>
        private var currentP      :Point;
        private var lineColor     :uint = 0x000000;
        private var fillColor     :uint = 0xFFFFFF;
        
        public function TaPaint()
        {
            canvas = new Sprite();
            canvas.x = 10;
            canvas.y = 10;
            
            points = new Array();
            shapes = new Array();

            currentP = new Point(0, 0);
            
            // グリッド線を描く
            canvas.graphics.beginFill(0xFFFFFF);
            canvas.graphics.drawRect(0, 0, 600, 600);
            canvas.graphics.endFill();

            canvas.graphics.beginFill(0x888888);
            for (var i:int = 0; i < 50; i++) {
                for (var j:int = 0; j < 50; j++) {
                    canvas.graphics.drawCircle(i * GRID_INTERVAL, j * GRID_INTERVAL, 1);
                }
            }
            canvas.graphics.endFill();

            // イベント登録
            canvas.addEventListener(MouseEvent.CLICK,       onClick);
            canvas.addEventListener(MouseEvent.MOUSE_MOVE,  onMouseMove);
            canvas.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            canvas.addEventListener(KeyboardEvent.KEY_UP,   onKeyUp);

            // マウスポインタ表示用
            mousePointer = new Sprite();
            mousePointer.visible = false;
            mousePointer.graphics.beginFill(0xFF0000);
            mousePointer.graphics.drawCircle(0, 0, 3);
            mousePointer.graphics.endFill();

            // 下書き線用
            draft = new Sprite();

            // 脚注
            var txtFooter:TextField = new TextField();
            txtFooter.text = " ・1回目のクリックで始点の指定、2回目のクリックで終点の指定\n ・ 普通にクリック: 線、Ctrl押しながら: 四角形、Shift押しながら:矢印\naaaa\naaaa";
            txtFooter.background      = true;
            txtFooter.backgroundColor = 0xFFFFDD;
            txtFooter.x               = 0;
            txtFooter.y               = 400;
            txtFooter.width           = 420;
            txtFooter.height          = 35;
            txtFooter.selectable      = false;
            txtFooter.multiline       = true;
            txtFooter.border          = true;

            // ログ表示用
            var txtLog:TextField = new TextField();
            
            // 配置
            canvas.addChild(draft);
            canvas.addChild(txtFooter);
//            canvas.addChild(txtLog);
            canvas.addChild(mousePointer);

            addChild(canvas);
        }

        /*
         * CLICKイベント
         */
        private function onClick(event:MouseEvent):void {
            if (event.target == event.currentTarget) {
                var x:int = int(event.localX / GRID_INTERVAL) * GRID_INTERVAL;
                var y:int = int(event.localY / GRID_INTERVAL) * GRID_INTERVAL;

                points.push(new Point(x, y));
                
                if (points.length % 2 == 0) {
                    drawShape(points[points.length - 2], points[points.length - 1]);
                }
            }
        }

        /*
         * MOUSE_MOVEイベント
         */
        private function onMouseMove(event:MouseEvent):void {
            if (event.target == event.currentTarget) {
                var x:int = int(event.localX / GRID_INTERVAL) * GRID_INTERVAL;
                var y:int = int(event.localY / GRID_INTERVAL) * GRID_INTERVAL;

                mousePointer.x = x;
                mousePointer.y = y;

                mousePointer.visible = true;
                
                if (points.length % 2 == 1) {
                    setDrawMode(event.shiftKey, event.ctrlKey);
                    drawShape(points[points.length - 1], new Point(x, y), draft);
                }
                currentP.x = x;
                currentP.y = y;
            }
        }

        /*
         * KEY_DOWNイベント
         */
        private function onKeyDown(event:MouseEvent):void {
            // マウスイベントを発生させたいんだけどうまくいってないみたい
            new MouseEvent(MouseEvent.MOUSE_MOVE, true, false, currentP.x, currentP.y, null, event.ctrlKey, event.altKey, event.shiftKey, false, 0);
        }

        /*
         * KEY_UPイベント
         */
        private function onKeyUp(event:MouseEvent):void {
            // マウスイベントを発生させたいんだけどうまくいってないみたい
            new MouseEvent(MouseEvent.MOUSE_MOVE, true, false, currentP.x, currentP.y, null, event.ctrlKey, event.altKey, event.shiftKey, false, 0);
        }

        /*
         * 図形を描く
         */
        private function drawShape(startP:Point, endP:Point, target:Sprite = null):void {
            var endP_m:Point = endP;
            
            if (target == null) {
                target = new Sprite();
            }
            if (target == draft) {
                lineColor = 0x0000FF;
                fillColor = 0x0000FF;
            } else {
                lineColor = 0x000000;
                fillColor = 0x000000;
            }

            switch (drawMode) {
            case "rect":
                target.graphics.clear();
                target.graphics.beginFill(0xFFFFFF);
                target.graphics.lineStyle(1, lineColor);
                target.graphics.drawRect(startP.x, startP.y, endP.x - startP.x, endP.y - startP.y);
                target.graphics.endFill();
                break;
            case "line":
                target.graphics.clear();
                target.graphics.lineStyle(1, lineColor);
                drawLine(startP, endP, target);
                break;
            case "arrow":
                target.graphics.clear();
                target.graphics.lineStyle(1, lineColor);
                drawArrow(startP, endP, target);
                break;
            default:
                break;
            }
            
            if (target == draft) {
                draft.visible = true;
            } else {
                canvas.addChild(target);
                canvas.setChildIndex(draft,        canvas.numChildren - 1);
                canvas.setChildIndex(mousePointer, canvas.numChildren - 1);
                draft.visible = false;
            }
        }

        /*
         * 図形の種類を決める
         */
        private function setDrawMode(shiftKey:Boolean, ctrlKey:Boolean):void {
            if (ctrlKey) {
                drawMode = "rect";
            } else if (shiftKey) {
                drawMode = "arrow";
            } else {
                drawMode = "line";
            }
        }

        /*
         * 線を引く
         */
        private function drawLine(startP:Point, endP_org:Point, target:Sprite, isArrow:Boolean = false):Point {
            var endP:Point = new Point(endP_org.x, endP_org.y);

            var dx:Number = endP.x - startP.x;
            var dy:Number = endP.y - startP.y;

            var angle:Number = (dx < 0 ? 180 : 0) + Math.atan(dy / dx) / Math.PI * 180;

/*
            //TODO: textにするときに45で割り切れない角度は割り切れないので・・・
            var LIMIT_ANGLE:Number = 20.0;

            if (Math.abs(angle) < LIMIT_ANGLE || 180 - LIMIT_ANGLE < Math.abs(angle)) {
                endP.y = startP.y;
            } else if (90 - LIMIT_ANGLE < Math.abs(angle) && Math.abs(angle) < 90 + LIMIT_ANGLE) {
                endP.x = startP.x;
            } else {
                if (Math.abs(dx) > Math.abs(dy)) {
                    endP.y = startP.y + dx;
                } else {
                    endP.x = startP.x + dy;
                }
            }
*/

            target.graphics.moveTo(startP.x, startP.y);
            target.graphics.lineTo(endP.x, endP.y);

            while (target.numChildren > 0) {
                target.removeChildAt(0);
            }
            if (isArrow) {
                var triangle:Sprite = new Sprite();
                triangle.graphics.beginFill(fillColor);
                triangle.graphics.moveTo( 0,  0);
                triangle.graphics.lineTo(-8,  5);
                triangle.graphics.lineTo(-8, -5);
                triangle.graphics.moveTo( 0,  0);
                triangle.graphics.endFill();

                triangle.rotation = angle;
                triangle.x = endP.x;
                triangle.y = endP.y;

                target.addChild(triangle);
            }

            return endP;
        }

        /*
         * 矢印を引く
         */
        private function drawArrow(startP:Point, endP_org:Point, target:Sprite):Point {
            return drawLine(startP, endP_org, target, true);
        }
    }
}