Canvas- stroke and fill

Stroke

stroke

Canvas commonly used strokemethod is to stroke method, which for outlining.

const myCanvas = document.getElementById('myCanvas');
const ctx = myCanvas.getContext('2d');
ctx.moveTo(0, 0);
ctx.lineTo(100, 100);
ctx.lineTo(0, 100);
ctx.stroke();

[Image dump outer link failure (img-jV77Tkz3-1562116464213) (http://note.youdao.com/yws/res/15792/E66AFE656D9140DEA6E46C2385D04593)]

strokeStyle

As the name suggests, strokeStylethat is used to describe the style of stroke, simply put, is the edge color.

const myCanvas = document.getElementById('myCanvas');
const ctx = myCanvas.getContext('2d');
ctx.moveTo(0, 0);
ctx.lineTo(100, 100);
ctx.lineTo(0, 100);
ctx.strokeStyle = 'blue';   //设置边的颜色为蓝色
ctx.stroke();

[Image dump outer link failure (img-vQH8VFl3-1562116464227) (http://note.youdao.com/yws/res/15797/32343971338D45889CE4DC3055FE6294)]

strokeRect

The rectangle drawing method is by way of stroke.

const myCanvas = document.getElementById('myCanvas');
const ctx = myCanvas.getContext('2d');
ctx.strokeRect(100, 100, 150, 100);

[Image dump outer link failure (http://note.youdao.com/yws/res/15807/F6341A2B57F74A5EB188DD9A780F0DF1) (img-3oDVDQ48-1562116464228)]

filling

fill

Filling, with a color that is a closed inner space filled.

E.g,

const myCanvas = document.getElementById('myCanvas');
const ctx = myCanvas.getContext('2d');
ctx.moveTo(0, 0);
ctx.lineTo(100, 100);
ctx.fillStyle = 'green';
ctx.fill();

[Image dump outer link failure (img-0P7v21gX-1562116464231) (http://note.youdao.com/yws/res/15815/CC1B69CD09EB4C2B9CDA0C3E60E776B9)]

Can be seen in this example, it does not draw anything, because a straight line can not form a closed area. If coupled with a can,

const myCanvas = document.getElementById('myCanvas');
const ctx = myCanvas.getContext('2d');
ctx.moveTo(0, 0);
ctx.lineTo(100, 100);
ctx.lineTo(0, 100);
ctx.fillStyle = 'green';
ctx.fill();

[外链图片转存失败(img-FXIIkHbK-1562116464232)(http://note.youdao.com/yws/res/15821/E64C83446FBB4F34B86A256A6C829B18)]

fillThe method of automatically forming and filling a closed area.

fillRect

There are strokeRect, naturally there fillRect, parameters are also the same.

const myCanvas = document.getElementById('myCanvas');
const ctx = myCanvas.getContext('2d');
ctx.fillRect(100, 100, 150, 150);

[外链图片转存失败(img-cenflQnY-1562116464232)(http://note.youdao.com/yws/res/15827/AA8755388C594D1E9EB83CDDFF04CD4F)]

fillStyle

Likewise, fillStylealso the fill style to modify, i.e., fill color.

const myCanvas = document.getElementById('myCanvas');
const ctx = myCanvas.getContext('2d');
ctx.fillStyle = 'orange';
ctx.fillRect(100, 100, 150, 150);

在这里插入图片描述

Guess you like

Origin blog.csdn.net/hjc256/article/details/94546956