用html5的canvas画布绘制贝塞尔曲线完整代码
程序员文章站
2023-12-09 15:17:09
html5的canvas很强大利用其画布可轻松绘制贝塞尔曲线,为大家以后使用方便,特于此分享实现代码,有此需求的朋友可以参考下... 13-08-14...
查看效果:http://keleyi.com/keleyi/phtml/html5/7.htm
完整代码:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>用html5的canvas画布绘制贝塞尔曲线</title>
</head>
<body>
<div>
<a href="http://keleyi.com/a/bjac/j77m9131.htm" target="_blank">原文</a></div>
<canvas id="keleyi_com" height="300" width="400"></canvas>
<script type="text/javascript">
function draw(id)
{
var canvas=document.getelementbyid(id);
if(canvas==null)
return false;
var context=canvas.getcontext('2d');
context.fillstyle="#eeeeff";
context.fillrect(0,0,400,300);
var n=0;
var dx=150;
var dy=150;
var s=100;
context.beginpath();
context.globalcompositeoperation='and';
context.fillstyle='rgb(100,255,100)';
context.strokestyle='rgb(0,0,100)';
var x=math.sin(0);
var y=math.cos(0);
var dig=math.pi/15*11;
for(var i=0;i<30;i++)
{
var x=math.sin(i*dig);
var y=math.cos(i*dig);
context.beziercurveto(dx+x*s,dy+y*s-100,dx+x*s+100,dy+y*s,dx+x*s,dy+y*s);
}
context.closepath();
context.fill();
context.stroke();
}
draw("keleyi_com");
</script>
</body>
</html>
完整代码:
复制代码
代码如下:<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>用html5的canvas画布绘制贝塞尔曲线</title>
</head>
<body>
<div>
<a href="http://keleyi.com/a/bjac/j77m9131.htm" target="_blank">原文</a></div>
<canvas id="keleyi_com" height="300" width="400"></canvas>
<script type="text/javascript">
function draw(id)
{
var canvas=document.getelementbyid(id);
if(canvas==null)
return false;
var context=canvas.getcontext('2d');
context.fillstyle="#eeeeff";
context.fillrect(0,0,400,300);
var n=0;
var dx=150;
var dy=150;
var s=100;
context.beginpath();
context.globalcompositeoperation='and';
context.fillstyle='rgb(100,255,100)';
context.strokestyle='rgb(0,0,100)';
var x=math.sin(0);
var y=math.cos(0);
var dig=math.pi/15*11;
for(var i=0;i<30;i++)
{
var x=math.sin(i*dig);
var y=math.cos(i*dig);
context.beziercurveto(dx+x*s,dy+y*s-100,dx+x*s+100,dy+y*s,dx+x*s,dy+y*s);
}
context.closepath();
context.fill();
context.stroke();
}
draw("keleyi_com");
</script>
</body>
</html>