This time I will show you how to use canvas to draw lines, and what are the precautions for using canvas to draw lines. The following is a practical case, let's take a look. Canvas is a new tag in html5. The tag is just a graphics container, and you must use a script to draw graphics. Next, we use canvas to draw lines. First, create a new html file and add the canvas tag to the file, as shown below. Canvas绘图与动画基础 #canvas{ border: 1px solid #aaa; text-align: center; } 当用户浏览器不支持Canvas,请更换浏览器重试!If the browser does not support the canvas tag, the fonts in the canvas tag will be displayed. Under normal circumstances, the paintings in the canvas will be displayed. Now, let's start drawing a line. First, get the canvas: var canvas = document.getElementById("canvas");The width and height of the canvas are generally not set in style. They can be set in the same level of canvas and id, as shown in the html above, or set using js. . canvas.width=1014; canvas.height=768;Come down and get the context of drawingvar context = canvas.getContext("2d"); We generally use context to operate canvas. Come on, let’s learn more about it in the code: var context = canvas.getContext("2d");//得到绘图的上下文环境 context.beginPath();//开始绘制线条,若不使用beginPath,则不能绘制多条线条 context.moveTo(100, 100);//线条开始位置 context.lineTo(700, 700);//线条经过点 context.lineTo(100, 700); context.lineTo(100, 100); context.closePath();//结束绘制线条,不是必须的 context.lineWidth = 5;//设置线条宽度 context.strokeStyle = "red";//设置线条颜色 context.stroke();//用于绘制线条 context.beginPath(); context.moveTo(200, 100); context.lineTo(700, 600); context.closePath(); context.strokeStyle = "black"; context.stroke();Run the above code and get a red closed triangle and a black solid line, as shown below: I believe you have seen it You have mastered the method in the case of this article. For more exciting information, please pay attention to php Chinese websiteOther related articles! Recommended reading: How to use canvas to draw a colorful tangram What does class="no-js" mean?