欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

HTML5 Canvas实现玫瑰曲线和心形图案的代码实例

程序员文章站 2023-11-29 11:59:34
这篇文章主要介绍了HTML5 Canvas实现玫瑰曲线和心形图案的代码实例,需要的朋友可以参考下... 14-04-10...

效果图:
HTML5 Canvas实现玫瑰曲线和心形图案的代码实例 

HTML5 Canvas实现玫瑰曲线和心形图案的代码实例

提示:把代码复制到一个html文件中并保存,直接打开即可看到效果。

实现代码:
 

 

<!doctype html>
<html>
<head>
<meta charset = "gbk">
<title>html5 demo</title>
<style type="text/css">
#apdiv1 {
position:absolute;
width:120px;
height:300px;
z-index:1;
left: 840px;
top: 80px;
}
</style>
</head>
<body>
<canvas id="canvas" width="800" height="600" style="border:1px solid #c3c3c3;"> 
your browser does not support the canvas element. </canvas> 
<div id="apdiv1">
<form>
玫瑰曲线方程:<br>
r=a+bsin(m/n*x)<br><br>
选择参数:<br><br>
m: <input type="number" name="m" min="2" max="29" value="29"/><br><br>
n: <input type="number" name="n" min="1" max="12" value="11"/><br><br>
a: <input type="number" name="a" min="0" max="5" value="1"/><br><br>
b: <input type="number" name="b" min="1" max="7" value="5"/><br><br>
<input type="button" value=" 画 图 " onclick="draw();"><br><br>
<hr><br>
<input type="button" value=" 画 图 2 " onclick="draw2();"><br><br>
<hr><br>
<input type="button" value=" 心形图 " onclick="draw3();"><br>
</form>
</div>
<script type="text/javascript"> 
function draw() { 
var ctx = document.getelementbyid('canvas').getcontext('2d'); 
ctx.save(); 
ctx.translate(400,300); 
ctx.clearrect(-400,-300,800,600);
ctx.strokestyle = "#cc0000"; 
var a = 0, b = 1, m = 6, n = 1;
m = document.forms[0].m.value;
n = document.forms[0].n.value;
a = document.forms[0].a.value; 
b = document.forms[0].b.value; 
drawrose(ctx,a,b,m,n); 
ctx.restore(); 
} 
function drawrose(ctx,a,b,m,n){ 
ctx.beginpath(); 
var e = 0, c = 120; 
var k = 2 * math.pi / 360;
do { 
var r = a/b + math.sin( m * e / n * k);
r = r * c; 
var x = r * math.cos( e * k );
var y = r * math.sin( e * k );
e += 0.1; 
ctx.lineto(x,y); 
} while ( e <= 4320 ); 
ctx.stroke(); 
} 
function draw2(){ 
var ctx = document.getelementbyid('canvas').getcontext('2d'); 
ctx.save(); 
ctx.translate(400,300); 
ctx.clearrect(-400,-300,800,600);
ctx.strokestyle = "#cc0000"; 
ctx.beginpath(); //ctx.moveto(0,0);
var e = 0, c = 150; 
var k = 2 * math.pi / 360;
do { 
x = 150*math.cos( 5/2 * e*k ) + 50*math.cos( 15/16 * 5/2 * e*k ); 
y = 150*math.sin( 5/2 * e*k ) - 50*math.sin( 15/16 * 5/2 * e*k ); 
e += 0.1; 
ctx.lineto(x,y); 
} while ( e <= 3600 ); 
ctx.stroke(); 
ctx.restore();
} 
function draw3(){ 
var ctx = document.getelementbyid('canvas').getcontext('2d'); 
ctx.save(); 
ctx.translate(400,300); 
ctx.clearrect(-400,-300,800,600);
ctx.strokestyle = "#ff0000"; 
ctx.beginpath(); 
var x = 1, y;
do { 
y = -80*(math.sqrt(1-x*x) + math.pow(x*x,1/3)); 
x -= 0.001; 
ctx.lineto(100*x,y); 
} while ( x >= -1 ); 

do { 
y = 80*(math.sqrt(1-x*x) - math.pow(x*x,1/3)); 
x += 0.001; 
ctx.lineto(100*x,y); 
} while ( x <= 1 );
ctx.closepath(); 

var grad = ctx.createradialgradient(-40,-60,10,-40,-40,200);
grad.addcolorstop(0, "#ffcc00"); 
grad.addcolorstop(1, "#ff0000"); 
ctx.fillstyle = grad;
ctx.fill();
// ctx.stroke(); 
ctx.restore();
} 
window.onload = function (){ 
draw();
} 
</script>
</body>
</html>