is a new element in HTML5. It can combine Javascript scripts to draw various graphics. How much do you know about canvas? Can you draw straight lines with canvas? This article will tell you how to draw a straight line with canvas. It has certain reference value. Interested friends can take a look.
First of all, let me introduce the attributes needed to draw a straight line with canvas
moveTo(x,y) can set the coordinates of the line start position
lineTo(x,y) You can set the coordinates of the end position of the line
lineWidth can set the width of the line
strokeStyle can set the color of the line
Use canvas to draw a line, the steps are detailed:
1 , Use the tag to define a canvas on the browser, and give it an ID name, you can set the size, background color, etc. of the canvas according to your needs.
2. Use var c=document.getElementById(“myCanvas”) to get the canvas element
3. Create a context object by var ctx=c.getContext(“2d”)
4. Set the starting point and end point of the line, start position moveTo(25,25), end position lineTo(175,175)
5. You can use ctx.lineWidth = 5 to set the width of the line , ctx.strokeStyle = “red” to set the color of the line
6. Finally, use ctx.stroke() to draw a straight line, and you are done
The specific code is as follows:
Effect picture:
3. bootstrap tutorial
The above is the detailed content of how to draw a straight line with html5 canvas, please pay attention to other related articles on 1024programmer.com !