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

多重連結バネ+画像変形

参考
drawTriangles for Flash Player 9
http://wonderfl.net/c/vBEV
/**
 * Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/45U9
 */

// forked from kawamura's 多重連結バネ
//参考
//drawTriangles for Flash Player 9
//http://wonderfl.net/c/vBEV
package  {
    import flash.display.Loader;
    import net.hires.debug.Stats;
    import flash.display.*;
    import flash.media.Microphone;
    import flash.display.Sprite;
    import flash.display.Graphics;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.system.LoaderContext;
    import flash.net.URLRequest;
    import flash.geom.*;
    public class FlashTest extends MovieClip {
        private var url:String = "http://farm4.static.flickr.com/3338/3639232519_88cda55f17_o.jpg";
        private var loader:Loader = new Loader;
       
        private var dxy:Number=30
        private var w:uint=Math.floor(stage.stageWidth/dxy);
        private var h:uint=Math.floor(stage.stageHeight/dxy);
        //
        private var d_mc:MovieClip;
        private var drag:*;
        private var mi_ar:Array;
        private var pt:Array;
        private var canvas:MovieClip
        private var base:ResizeMap;
        private var bitmap:Bitmap;
        public function FlashTest() {
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
            loader.load( new URLRequest(url),new LoaderContext(true) );
        }
        private function onImageLoaded(e:Event):void {
            stage.quality = "low";
            stage.addEventListener(MouseEvent.MOUSE_DOWN,mdwn);
            stage.addEventListener(MouseEvent.MOUSE_UP,mup);
        
            var stats:Stats=new Stats();
            addChild( stats );
            canvas=new MovieClip();
            addChild(canvas)
            // constructor code
            var i:uint;
            var ii:uint;
            var scx:Number=stage.stageWidth*.5;
            var scy:Number=stage.stageHeight*.5;
            //
            w=Math.floor(stage.stageWidth/dxy);
            h=Math.floor(stage.stageHeight/dxy);
            base = new ResizeMap(w*dxy,h*dxy,loader);
            //
            var sx:Number=(stage.stageWidth-dxy*(w-1))*.5
            var sy:Number=(stage.stageHeight-dxy*(h-1))*.5
            //
            var p:MovieClip;
            mi_ar=new Array()
            pt=new Array()
            for(i=0;i<h;i++){
                for(ii=0;ii<w;ii++){
                    if(i==0 || i==h-1 || ii==0 ||  ii==w-1){
                        p=new Pin();
                    }else{
                        p=new Mi();
                        p.visible = false;
                    }
                    canvas.addChild(p);
                    p.x=sx+ii*dxy;
                    p.y=sy+i*dxy;
                    mi_ar.push(p)
                    pt.push(new Point(p.x,p.y));
                }
            }
            for(i=0;i<mi_ar.length;i++){
                if(i>w-1 && i<mi_ar.length-w && i%w!=0 && i%w !=w-1){
                    p=mi_ar[i];
                    p.x+=-10+20*Math.random();
                    p.y+=-10+20*Math.random();
                    p.init([mi_ar[i-1],mi_ar[i+1],mi_ar[i-w],mi_ar[i+w]])
                }
            }
            //
            addEventListener(Event.ENTER_FRAME,ent)
        }
        //

        private function ent(evt:Event):void{
            var i:uint;
            var ii:uint;
            var g:Graphics=canvas.graphics;
            g.clear()
            
            for(i=0;i<mi_ar.length;i++){
                if( i<mi_ar.length-w && i%w !=w-1){
                    GraphicUtil.drawTriangle( g, base, mi_ar[i], mi_ar[i+1], mi_ar[i+w], pt[i], pt[i+1], pt[i+w]);
                    GraphicUtil.drawTriangle( g, base, mi_ar[i+w+1], mi_ar[i+1], mi_ar[i+w], pt[i+w+1], pt[i+1], pt[i+w]);
                    /*
                    g.moveTo(mi_ar[i].x,mi_ar[i].y);
                    g.lineTo(mi_ar[i+1].x,mi_ar[i+1].y);
                    g.lineTo(mi_ar[i+w+1].x,mi_ar[i+w+1].y);
                    g.lineTo(mi_ar[i+w].x,mi_ar[i+w].y);
                    g.lineTo(mi_ar[i].x,mi_ar[i].y);
                    */
                }
            }
        }
        //
        private function mdwn(evt:MouseEvent):void{
            var nr:int = 1000000; 
            drag = null;
            for(var i:int=0;i<mi_ar.length;i++){
                var xx:int = mi_ar[i].x - mouseX;
                var yy:int = mi_ar[i].y - mouseY;
                var r:int =xx*xx+yy*yy;
                if(nr>r){
                    nr = r;
                    drag = mi_ar[i];
                }
            }
            if(drag != null){
                drag.startDrag()
                drag.flag =false;
            }
        }
        private function mup(evt:MouseEvent):void{
            if(drag != null){
                drag.stopDrag();
                drag.vx=0;
                drag.vy=0;
                drag.flag =true;
                drag = null;
            }
        }
    }
}


import flash.display.Graphics;
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;

class Mi extends MovieClip {
    private var m:Number=1
    private var k:Number=0.01;
    private var t:Number=0.02;
    private var m_ar:Array
    
    private var ax:Number;
    private var ay:Number;
    public var vx:Number;
    public var vy:Number;
    public var flag:Boolean=true;
    public function Mi() {
        // constructor code
        var g:Graphics=this.graphics;
        g.beginFill(0xFF0000);
        g.drawCircle(0,0,5);
        
    }
    public function init(_ar:Array):void{
        m_ar=_ar;
        ax=0;
        vx=0;
        ay=0;
        vy=0;
        this.buttonMode=true;
        this.addEventListener(Event.ENTER_FRAME,ent);
    }
    private function ent(evt:Event):void{
        if(flag){
            var i:uint
            var dx:Number=0;
            var dy:Number=0;
            for(i=0;i<m_ar.length;i++){
                var mc:MovieClip=m_ar[i];
                dx+=mc.x-this.x;
                dy+=mc.y-this.y;
            }
            ax=k*dx//
            ay=k*dy//
            vx+=ax-t*vx;
            vy+=ay-t*vy;
            this.x+=vx;
            this.y+=vy;
        }
    }
}

import flash.geom.*;
import flash.display.MovieClip;
import flash.display.Graphics;
import flash.events.MouseEvent;
class Pin extends MovieClip {
    public var vx:Number;
    public var vy:Number;
    public var flag:Boolean=true;
    public function Pin() {
        // constructor code
        var g:Graphics=this.graphics;
        g.beginFill(0x000000);
        g.drawCircle(0,0,5);
        this.buttonMode=true;
    }
}

class ResizeMap extends BitmapData{
    function ResizeMap(w:int,h:int,spr:DisplayObject=null):void{
        super(w,h,true,0xFF000000);
        if(spr != null){ 
            var r:Rectangle = spr.getRect(spr);       
            var rate:Number = 1;
            var tw:int = r.width*rate;
            var th:int = r.height*rate;
            if(tw * rate > w){
                rate *= (w / tw);
                th = r.height * rate;
                tw = r.width * rate;
            }
            if(th * rate > h){
                rate *= (h / th);
                th = r.height * rate;
                tw = r.width * rate;
            }
            draw(spr, new Matrix(rate,0,0,rate, (w-tw)/2 , (h-th)/2  ));
        }
    }
}

class GraphicUtil {    
    public static function drawTriangle(
        g:Graphics, bitmapData:BitmapData, 
        a0:*, a1:*, a2:*,
        p0:Point, p1:Point, p2:Point):void{
            
        var matrix:Matrix = _buildTransformMatrix(p0, p1, p2, a0, a1, a2);
        g.beginBitmapFill(bitmapData, matrix, false, true);
        _drawTriangle(g, a0, a1, a2);
        g.endFill();
    }
    
    private static function _buildTransformMatrix(
        a0:*, a1:*, a2:*,
        b0:*, b1:*, b2:*):Matrix {
        var matrixA:Matrix = new Matrix(
            a1.x - a0.x, a1.y - a0.y,
            a2.x - a0.x, a2.y - a0.y);
        matrixA.invert();
        var matrixB:Matrix = new Matrix(
            b1.x - b0.x, b1.y - b0.y,
            b2.x - b0.x, b2.y - b0.y);
        var matrix:Matrix = new Matrix();
        matrix.translate(-a0.x, -a0.y); // (原点)へ移動 
        matrix.concat(matrixA); // 単位行列に変換(aの座標系の逆行列)
        matrix.concat(matrixB); // bの座標系に変換 
        matrix.translate(b0.x, b0.y); // b0へ移動 
        return matrix;
    }
    private static function _drawTriangle(g:Graphics, p0:*, p1:*, p2:*):void {
        //g.lineStyle(1, 0x808080); // debug
        g.moveTo(p0.x, p0.y);
        g.lineTo(p1.x, p1.y);
        g.lineTo(p2.x, p2.y);
        g.lineTo(p0.x, p0.y);
    }
}