HTML5 Canvas锯齿图代码实例
程序员文章站
2023-11-13 08:41:34
这篇文章主要介绍了HTML5 Canvas锯齿图代码实例,需要的朋友可以参考下... 14-04-10...
效果图:
提示:把代码复制到一个html文件中并保存,直接打开即可看到效果。
实现代码:
复制代码
代码如下:<!doctype html>
<html lang="zh">
<head>
<meta charset="gbk">
<title>锯齿图</title>
<script type="text/javascript">
window.addeventlistener("load", eventwindowloaded, false);
function eventwindowloaded(){
var x,y;
var thecanvas = document.getelementbyid("canvas");
var context = thecanvas.getcontext("2d");
//box
context.strokestyle = '#00f';
context.linewidth=10;
context.strokerect(0, 0, thecanvas.width-0, thecanvas.height-0);
context.fillstyle = "#00f";
for(x=5;x<=canvas.width;x=x+10){
context.beginpath();
context.arc(x,5,5,0,math.pi,false);
context.closepath();
context.fill();
context.beginpath();
context.arc(x,canvas.height-5,5,0,math.pi,true);
context.closepath();
context.fill();
}
for(y=5;y<=canvas.height;y=y+10){
context.beginpath();
context.arc(5,y,5,0,math.pi*2,true);
context.arc(canvas.width-5,y,5,0,math.pi*2,true);
context.closepath();
context.fill();
}
}
</script>
</head>
<body>
<div style="position: absolute; top: 100px; left: 100px;">
<canvas id="canvas" width="300" height="300">
</div>
</body>
</html>
<html lang="zh">
<head>
<meta charset="gbk">
<title>锯齿图</title>
<script type="text/javascript">
window.addeventlistener("load", eventwindowloaded, false);
function eventwindowloaded(){
var x,y;
var thecanvas = document.getelementbyid("canvas");
var context = thecanvas.getcontext("2d");
//box
context.strokestyle = '#00f';
context.linewidth=10;
context.strokerect(0, 0, thecanvas.width-0, thecanvas.height-0);
context.fillstyle = "#00f";
for(x=5;x<=canvas.width;x=x+10){
context.beginpath();
context.arc(x,5,5,0,math.pi,false);
context.closepath();
context.fill();
context.beginpath();
context.arc(x,canvas.height-5,5,0,math.pi,true);
context.closepath();
context.fill();
}
for(y=5;y<=canvas.height;y=y+10){
context.beginpath();
context.arc(5,y,5,0,math.pi*2,true);
context.arc(canvas.width-5,y,5,0,math.pi*2,true);
context.closepath();
context.fill();
}
}
</script>
</head>
<body>
<div style="position: absolute; top: 100px; left: 100px;">
<canvas id="canvas" width="300" height="300">
</div>
</body>
</html>
上一篇: HTML5+CSS3应用详解
下一篇: HTML5注册页面示例代码