Home  >  Article  >  Web Front-end  >  How to draw lines with canvas

How to draw lines with canvas

php中世界最好的语言
php中世界最好的语言Original
2018-03-14 09:45:172307browse

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.

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8"/>
    <title>Canvas绘图与动画基础</title></head><body><style>
    #canvas{        border: 1px solid #aaa;        text-align: center;
    }</style><canvas id="canvas" width="1024" height="768">
    当用户浏览器不支持Canvas,请更换浏览器重试!</canvas></body></html>

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 drawing


var 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:

How to draw lines with canvas

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 website

Other related articles!

Recommended reading:

How to use canvas to draw a colorful tangram

What does class="no-js" mean?

The above is the detailed content of How to draw lines with canvas. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn