前端--HTML5简单绘图

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

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

  1. 获取<canvas.../>元素对应的DOM对象,这是一个Canvas对象。
  2. 调用Canvas对象的getContext()方法,该方法返回一个CanvasRenderingContext2D对象,该对象即可绘制图形。
  3. 调用CanvasRenderingContext2D对象的方法绘图。

绘制几何图形:

CanvasRenderingContext2D只提供了两个方法来绘制几何图形:

  • fillRect(double x,double y,double width,double height):填充一个矩形区域。
  • strokeRect(double x,double y,double width,double height):绘制一个矩形边框。

注:此处的(x,y)坐标相对于画布左上角顶点定位,左上角顶点默认坐标为(0,0),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> 绘制矩形 </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>

效果图:

使用路径绘制圆角矩形:

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

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

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

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

  1. 调用CanvasRenderingContext2D对象的beginPath()方法开始定义路径。
  2. 调用CanvasRenderingContext2D的各种方法添加子路径。
  3. 调用CanvasRenderingContext2D的closePath()方法关闭路径。
  4. 调用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">
	/*
		该方法负责绘制圆角矩形
		x1、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>

效果图:

猜你喜欢

转载自blog.csdn.net/qq_36470686/article/details/82811354
今日推荐