本页主题: [转贴][超级NB!] 20行javascript写成的超漂亮分形动画 打印 | 加为IE收藏 | 复制链接 | 收藏主题 | 上一主题 | 下一主题

bbsriver
杀人游戏MVP勋章I 杀人游戏MVP勋章II
级别: 管理员


精华: 52
发帖: 17391
威望: 8729 点
金钱: 7064 静电币
支持度: 19601 点
在线时间:13725(小时)
注册时间:2002-11-21
最后登录:2016-12-22

 [转贴][超级NB!] 20行javascript写成的超漂亮分形动画

看过256字节编程大赛,也看过疯狂科学家的dHTML Scene地下比赛

这次暴了个猛的,用canvas标签,加上20行javascript,就成了这个效果(你需要一个支持canvas标签的浏览器,例如SafariFirefox或者Opera):



http://www.p01.org/releases/DHTML_contests/files/20lines_hypno_trip_down_the_fractal_rug/

Copy code
/*    */
/*    */
/*    */    //    chain( func )
/*    */    //    make func chainable by making it return itsReturnValue||this
/*    */    function chain( func )
/*    */    {
/* 01 */         return function()
/*    */        {
/* 02 */            return func.apply( this, arguments )||this;
/*    */        }
/*    */    }
/*    */
/*    */
/*    */    //    initialize everything
/*    */    onload = function()
/*    */    {
/*    */        //    initialize the contexts and the fractal
/* 03 */        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 )
/*       */        {
/* 04 */            this[what] = to;
/*    */        }
/*    */
/*    */        //    switchTo( context )
/*    */        //    return another CanvasRenderingContext2D, making such operation chainable
/*    */        window.fx.CanvasRenderingContext2D.switchTo = function( context )
/*       */        {
/* 05 */            return context;
/*    */        }
/*    */
/*    */        //    chain a bunch of CanvasRenderingContext2D methods
/* 06 */        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} )
/*       */        {
/* 07 */            window.fx.CanvasRenderingContext2D[chainThat] = chain( window.fx.CanvasRenderingContext2D[chainThat] );
/*    */        }
/*    */
/*    */        //    let's get the party started
/* 08 */        render();
/*    */    }
/*    */
/*    */
/*    */    //    render()
/*    */    function render()
/*    */    {
/*    */        //    the time is now
/* 09 */        var    now        = new Date().getTime();
/*    */
/*    */        //    mutate the outer cells of the rug
/* 10 */        fx
/*    */            .fractal[ Math.floor(Math.random()*8+5)%9 ] = Math.floor( Math.random()*3 );
/*    */
/*    */        //    softly kills the previous generations of the rug
/* 11 */        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
/* 12 */        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
/* 13 */        for( var j=0; j++<3; fx.fooCtx.drawImage( fx.barCtx.canvas,0,0 ) )
/*    */        {
/* 14 */            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
/* 15 */        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
/* 16 */        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
/* 17 */        setTimeout( render, 25 );
/*    */    }
/*    */
/*    */


你可以看看类似的256byte汇编版本 12(友情提示,Vista以上操作系统没法看。因为微软的cmd显示驱动不兼容DOS全屏绘图,不过可以试试DOSBox

reddit上评论说:这个哪里才20行呀,要80个字符一行,20行写出这个效果才牛B,嘿嘿~~
Posted: 2008-06-18 23:22 | [楼 主]
dkwang
DK
级别: 贵宾


精华: 0
发帖: 4774
威望: 2110 点
金钱: -10 静电币
支持度: 2547 点
在线时间:2107(小时)
注册时间:2005-07-29
最后登录:2020-03-06

 

完全看不懂
DK
Posted: 2008-06-19 19:22 | 1 楼
帖子浏览记录 版块浏览记录
狗狗静电BBS - wwW.DoGGiEhoMe.CoM » 哇啦哇啦 Discuss & Talk aloud

沪ICP备05008186号
Powered by PHPWind Styled by MagiColor