日本看板
广告位招租 详细信息请联系 jkanban@gmail.com キャンペーン中!


 
标题: [转帖] 超酷的js:永不停止的分形地毯之旅
admin (蕨菜)
一代文豪
Rank: 5Rank: 5Rank: 5Rank: 5Rank: 5


UID 1
精华 1
积分 12784
帖子 4553
威望 170
金钱 22652
慈善捐款(日元) 400
阅读权限 200
注册 2007-3-25
 
发表于 2008-6-20 11:34  资料  个人空间  短消息  加为好友 
超酷的js:永不停止的分形地毯之旅

Hypno trip down the fractal rug

http://www.p01.org/releases/DHTM ... wn_the_fractal_rug/

//      chain( func )
//      make func chainable by making it return itsReturnValue||this
function chain( func )
{
        return function()
        {
                return func.apply( this, arguments )||this;
        }
}

//      initialize everything
onload = function()
{
        //      initialize the contexts and the fractal
        window.fx =
        {
            'barCtx':      document.getElementById('bar'      ).getContext('2d'),
            'fooCtx':      document.getElementById('foo'      ).getContext('2d'),
            'logicCtx':  document.getElementById('logic'  ).getContext('2d'),
            'renderCtx':        document.getElementById('render').getContext('2d'),
            'fractal':    [0,0,0,0,2,0,0,0,0],
            CanvasRenderingContext2D:   (window.CanvasRenderingContext2D?CanvasRenderingContext2D.prototype:document.getElementById('bar'  ).getContext('2d').__proto__)
        }
        //      set( what, to )
        //      sets a property of the CanvasRenderingContext2D, making such operation chainable
        window.fx.CanvasRenderingContext2D.set = function( what, to )
        {
                this[what] = to;
        }
        //      switchTo( context )
        //      return another CanvasRenderingContext2D, making such operation chainable
        window.fx.CanvasRenderingContext2D.switchTo = function( context )
        {
                return context;
        }
        //      chain a bunch of CanvasRenderingContext2D methods

        for( chainThat in {set:1,switchTo:1,clearRect:1,save:1,translate:1,rotate:1,drawImage:1,scale:1,restore:1,fillRect:1,moveTo:1,lineTo:1,beginPath:1,closePath:1,stroke:1,fill:1,arc:1} )
        {
                window.fx.CanvasRenderingContext2D[chainThat] = chain( window.fx.CanvasRenderingContext2D[chainThat] );
        }
       //      let's get the party started
        render();
}

//      render()
function render()
{
        //      the time is now
        var     now    = new Date().getTime();

        //      mutate the outer cells of the rug
        fx
                .fractal[ Math.floor(Math.random()*8+5)%9 ] = Math.floor( Math.random()*3 );
        //      softly kills the previous generations of the rug
        fx
                .fooCtx
                        .set( 'fillStyle', 'rgba(0,0,0,.1)' )
                        .fillRect( 0, 0, 192, 192 )
                        .set( 'fillStyle', '653' )
                .switchTo( fx.barCtx )
                        .clearRect( 0, 0, 192, 192 );
        //      render 1st generation of the rug

        for( var i=-1; i<9; fx.fooCtx.fillRect( (++i%3)*64+1,Math.floor(i/3)*64+1,(fx.fractal[i]&1)*62,(fx.fractal[i]&1)*62 ))
        {
        }
        //      render next generations of the rug
        for( var j=0; j++<3; fx.fooCtx.drawImage( fx.barCtx.canvas,0,0 ) )
        {
                for( var i=-1; ++i<9; fx.barCtx.drawImage( fx.fooCtx.canvas,0,0,192,192, (i%3)*64+1,Math.floor(i/3)*64+1, (fx.fractal[i]&2)*31, (fx.fractal[i]&2)*31 ) )
                {
                }
        }
        //      render rotozoomed rug
        fx
                .logicCtx

                        .set( 'globalCompositeOperation', 'source-over' )

                        .clearRect( 0, 0, 256, 192 )

                        .save()

                        .translate( 96, 96 )

                        .rotate( (now/5841%2)*Math.PI )

                        .scale( 1+2*((now/1274)%1), 1+2*((now/1274)%1) )

                        .drawImage( fx.fooCtx.canvas,0,0,192,192, -288,-288,576,576 )

                        .drawImage( fx.fooCtx.canvas,0,0,192,192, -96 ,-96 ,192,192 )

                        .drawImage( fx.fooCtx.canvas,0,0,192,192, -32 ,-32 ,64 ,64  )

                        .restore()

                        .set( 'globalCompositeOperation', 'copy' )

                //      prepare for hypnoglow

                .switchTo( fx.renderCtx )

                        .set( 'globalCompositeOperation', 'source-over' )

                        .clearRect( 0, 0, 192, 192 )

                        .drawImage( fx.logicCtx.canvas, 0, 0 )

                        .set( 'globalCompositeOperation', 'lighter' );



        //      hypnoglow

        for( var i=-1; ++i<6; fx.renderCtx.drawImage( fx.logicCtx.drawImage( fx.logicCtx.canvas, 0, 0, 192>>i, 192>>i, 0, 0, 96>>i, 96>>i ).canvas, 0, 0, 96>>i,  96>>i, 0, 0, 192, 192 ) )

        {

        }



        //      here we go again

        setTimeout( render, 25 );

}









天下熙熙,皆为利来;天下攘攘,皆为利往。
顶部
www.facebooks.jp 在日帅哥美女花名册 www.i-kanban.com 日本大众点评网 www.rakudoor.com 乐道 www.j-price.com 日本价格网
 



当前时区 GMT+9, 现在时间是 2008-8-21 01:17

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
清除 Cookies - 联系我们 - 日本看板 - Archiver