HTML5 Canvas锯齿图代码实例

2022-10-20,,,,

效果图:

提示:把代码复制到一个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>

《HTML5 Canvas锯齿图代码实例.doc》

下载本文的Word格式文档,以方便收藏与打印。