<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>
canvas绘制科赫雪花
猜你喜欢
转载自sdh88hf.iteye.com/blog/1751219
今日推荐
周排行