首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

canvas作图科赫雪花

2012-12-29 
canvas绘制科赫雪花htmlheadtitleJS/title/headbodycanvas idsquare width500 height50

canvas绘制科赫雪花

<html><head><title>JS</title></head><body><canvas id="square" width=500 height=500></canvas><script>var deg = Math.PI/180;function snowflake(c, n, x, y, len) {    c.save();           // Save current transformation    c.translate(x,y);   // Translate origin to starting point    c.moveTo(0,0);      // Begin a new subpath at the new origin    leg(n);             // Draw the first leg of the snowflake    c.rotate(-120*deg); // Now rotate 120 degrees counterclockwise    leg(n);             // Draw the second leg    c.rotate(-120*deg); // Rotate again    leg(n);             // Draw the final leg    c.closePath();      // Close the subpath    c.restore();        // And restore original transformation    // Draw a single leg of a level-n Koch snowflake.    // This function leaves the current point at the end of the leg it has    // drawn and translates the coordinate system so the current point is (0,0).    // This means you can easily call rotate() after drawing a leg.    function leg(n) {        c.save();               // Save the current transformation        if (n == 0) {           // Nonrecursive case:            c.lineTo(len, 0);   //   Just draw a horizontal line        }                       //                                       _  _        else {                  // Recursive case: draw 4 sub-legs like:  \/            c.scale(1/3,1/3);   // Sub-legs are 1/3rd the size of this leg            leg(n-1);           // Recurse for the first sub-leg            c.rotate(60*deg);   // Turn 60 degrees clockwise            leg(n-1);           // Second sub-leg            c.rotate(-120*deg); // Rotate 120 degrees back            leg(n-1);           // Third sub-leg            c.rotate(60*deg);   // Rotate back to our original heading            leg(n-1);           // Final sub-leg        }        c.restore();            // Restore the transformation        c.translate(len, 0);    // But translate to make end of leg (0,0)    }}var canvas = document.getElementById("square");c2 = canvas.getContext("2d");snowflake(c2,4,150,115,125);c2.stroke();</script><!--<svg><defs><linearGradient id="fade"><stop offset="0%" stop-color="#008"/><stop offset="100%" stop-color="#ccf"/></linearGradient></defs><rect x="100" y="100" width="300" height="200" stroke="black" stroke-width="25" fill="url(#fade)"/></svg>--><input type="button" value="input" onclick="aaa()"/></body></html>

热点排行