HTML5实现绘制几何图形

HTML5新增了一个<canvas.../>属性。该元素自身并不绘制图形,只是至关于一张空画布。若是开发者须要向<canvas.../>上绘制图形则必须使用JavaScript脚本进行绘制。javascript

为了向<canvas.../>元素上绘图,必须通过以下3步。html

获取<canvas.../>元素对应的DOM对象,这是一个Canvas对象。
调用Canvas对象的getContext()方法,该方法返回一个CanvasRenderingContext2D对象,该对象便可绘制图形。
调用CanvasRenderingContext2D对象的方法绘图。
绘制几何图形:
CanvasRenderingContext2D只提供了两个方法来绘制几何图形:java

fillRect(double x,double y,double width,double height):填充一个矩形区域。
strokeRect(double x,double y,double width,double height):绘制一个矩形边框。
注:此处的(x,y)坐标相对于画布左上角顶点定位,左上角顶点默认坐标为(0,0),x轴向右为正方向,y轴向下为正方向。canvas

下面程序使用这两个方法来绘制几个简单的矩形:ui

<!DOCTYPE html>
<html>
<head>
<meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 绘制矩形 </title>
</head>
<body>
<h2> 绘制矩形 </h2>
<canvas id="mc" width="400" height="280"
style="border:1px solid black"></canvas>
<script type="text/javascript">
// 获取canvas元素对应的DOM对象
var canvas = document.getElementById('mc');
// 获取在canvas上绘图的CanvasRenderingContext2D对象
var ctx = canvas.getContext('2d');
// 设置填充颜色
ctx.fillStyle = '#f00';
// 填充一个矩形
ctx.fillRect(30 , 20 , 120 , 60);
// 设置线条颜色
ctx.strokeStyle = "#00f";
// 设置线条宽度
ctx.lineWidth = 10;
// 绘制一个矩形边框
ctx.strokeRect(30 , 130 , 120 , 60);
// 设置线条颜色
ctx.strokeStyle = "#0ff";
// 设置线条链接风格
ctx.lineJoin = "round";
// 绘制一个矩形边框
ctx.strokeRect(80 , 160 , 120 , 60);
// 设置线条颜色
ctx.strokeStyle = "#f0f";
// 设置线条链接风格
ctx.lineJoin = "bevel";
// 绘制一个矩形边框
ctx.strokeRect(130 , 190 , 120 , 60);
</script>
</body>
</html>
效果图:htm

 

 

使用路径绘制圆角矩形:
正如前面所提,CanvasRenderingContext2D对象只提供了两个绘制矩形的方法,并无直接提供绘制图形,椭圆等几何图形的方法,为了在Canvas上绘制更复杂的图形,必须在Canvas上启用路径,借助于路径来绘制图形。对象

在Canvas上使用路径,可按以下步骤进行:ip

         beginPath():丢弃任何当前定义的路径而且开始一条新的路径。它把当前的点设置为 (0,0)。  utf-8

         closePath() :方法建立从当前点到开始点的路径。 开发

调用CanvasRenderingContext2D对象的beginPath()方法开始定义路径。
调用CanvasRenderingContext2D的各类方法添加子路径。
调用CanvasRenderingContext2D的closePath()方法关闭路径。
调用CanvasRenderingContext2D的fill()或stroke()方法来填充路径或绘制路径边框。
咱们接下来调用调用CanvasRenderingContext2D提供的几个方法来绘制一个圆角矩形:

  arcTo(double x1,double y1,double x2,double y2,double radius):向Canvas的当前路径上添加一段弧,肯定这段弧的方式是:假设从当前点到P1(x1,y1)绘制一条线条,再从P1(x1,y1)到P2(x2,y2)绘制一条线条,arcTo则绘制一段同时与上面两条线条相切,且半径为radius的圆弧。lineTo(double x,double y):把Canvas的当前路径从当前结束点链接到x,y对应的点。moveTo(double x,double y):把Canvas的当前路径的结束点移动到x,y对应的点。<!DOCTYPE html><html><head> <meta name="author" content="Yeeku.H.Lee(CrazyIt.org)" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title> arcTo示意 </title></head><body><h2> arcTo示意 </h2><canvas id="mc" width="400" height="280" style="border:1px solid black"></canvas><script type="text/javascript"> /* 该方法负责绘制圆角矩形 x一、y2:是圆角矩形左上角的坐标。 width、height:控制圆角举行的宽、高 radius:控制圆角矩形的四个圆角的半径 */ function createRoundRect(ctx , x1 , y1 , width , height , radius) { ctx.beginPath(); // 移动到左上角 ctx.moveTo(x1 + radius , y1); // 添加一条链接到右上角的线段 ctx.lineTo(x1 + width - radius, y1); // 添加一段圆弧 ctx.arcTo(x1 + width , y1, x1 + width, y1 + radius, radius); // 添加一条链接到右下角的线段 ctx.lineTo(x1 + width, y1 + height - radius); // 添加一段圆弧 ctx.arcTo(x1 + width, y1 + height , x1 + width - radius , y1 + height , radius); // 添加一条链接到左下角的线段 ctx.lineTo(x1 + radius, y1 + height); // 添加一段圆弧 ctx.arcTo(x1, y1 + height , x1 , y1 + height - radius , radius); // 添加一条链接到左上角的线段 ctx.lineTo(x1 , y1 + radius); // 添加一段圆弧 ctx.arcTo(x1 , y1 , x1 + radius , y1 , radius); ctx.closePath(); } // 获取canvas元素对应的DOM对象 var canvas = document.getElementById('mc'); // 获取在canvas上绘图的CanvasRenderingContext2D对象 var ctx = canvas.getContext('2d'); ctx.lineWidth = 3; createRoundRect(ctx , 30 , 30 , 200 , 100 , 20); ctx.stroke();</script></body></html>效果图: