HTML5 Canvas “红心一棵”
效果图:
笛卡尔公式:r = a(1-sin弧度)画“红心”。
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="gbk">
<title>HTML5 Canvas“红心一棵”</title>
</head>
<body>
<canvas id="canvas" width="600" height="300"></canvas>
<script>
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
var width=canvas.width;
var height=canvas.height;
context.fillStyle="red";
var x,y,r;
for(var i=0;i<=90;i++)
{
for(var j=0;j<=90;j++)
{
r=Math.PI/45*i*(1-Math.sin(Math.PI/45*j))*18;
x=r*Math.cos(Math.PI/45*j)*Math.sin(Math.PI/45*i)+width/2;
y=-r*Math.sin(Math.PI/45*j)+height/4;
context.beginPath();
context.arc(x,y,1,0,Math.PI*2);
context.fill();
}
}
</script>
<body></html>
热情奉献:HTML5 Canvas绘图与动画学习59例源码:
http://www.108js.com/example.html