`
sdh88hf
  • 浏览: 70667 次
  • 性别: Icon_minigender_1
  • 来自: 绍兴
社区版块
存档分类
最新评论

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>
分享到:
评论

相关推荐

    Koch-Snowflake-JavaScript:生成Koch雪花

    在JavaScript中,我们可以利用循环和数组来实现科赫雪花的绘制。JavaScript是一种广泛应用于网页开发的动态类型语言,支持事件驱动、函数式以及基于原型的编程风格。要生成科赫雪花,我们需要掌握以下几个JavaScript...

    Fractals:使用 javascriptcanvas 的分形

    常见的分形包括曼德勃罗集、朱利亚集、科赫雪花、谢尔宾斯基三角形等。这些分形的生成通常涉及复数运算和迭代过程。 以曼德勃罗集为例,它是复平面上的一个点集合,定义为对于每个复数c,如果序列z_n=zn+1=c+z_n^2...

    分形:与tkinter一起使用

    - **类型**:常见的分形包括曼德勃罗集、 Julia集、科赫雪花、谢尔宾斯基三角等。 - **生成算法**:如迭代法、分数维计算等,用于构建分形图案。 - **自相似性**:分形的每个部分都与整体保持相似性,这一特性...

    玩乱:在Javascript中玩乱,分形和奇怪的吸引子

    在JavaScript中,我们可以使用迭代函数系统(IFS)来生成分形,如科赫雪花、曼德勃罗集合等。IFS通过重复应用简单的规则来构造复杂的模式,这在代码中体现为递归函数的调用。 奇怪吸引子则是混沌理论中的核心概念,...

    Fundamentos-IFS:工厂模型和迭代函数系统

    通过选择不同的函数组合并反复应用,可以生成各种各样的复杂形状,包括著名的科赫雪花、曼德勃罗集等。在植物模型中,IFS特别有用,因为自然界的许多植物结构如树叶、树枝分布都展现出明显的自相似性,IFS能够很好地...

    Sacred-Fractal:基于神圣几何的分形,用JS实现

    JavaScript中的Canvas API是一个强大的绘图工具,允许开发者在网页上直接绘制图形。在这个项目中,开发者可能会使用Canvas的fillRect、arc等方法,结合数学公式,动态地绘制出神圣几何分形。 3. 动态交互 ...

Global site tag (gtag.js) - Google Analytics