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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值