EN
JavaScript - draw triangle on canvas element
0 points
In this article, we would like to show you how to draw a triangle on an HTML canvas element using JavaScript.
Quick solution:
xxxxxxxxxx
1
function drawTriangle(context, point1, point2, point3, filled) {
2
context.beginPath();
3
context.moveTo(point1.x, point1.y);
4
context.lineTo(point2.x, point2.y);
5
context.lineTo(point3.x, point3.y);
6
context.closePath();
7
filled ? context.fill() : context.stroke();
8
}
In this section, we present a practical example of how to create a universal method that draws a triangle for provided points. Additionally, we can specify if the triangle should be filled or not.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
#my-canvas { border: 1px solid gray; }
7
8
</style>
9
</head>
10
<body>
11
<canvas id="my-canvas" width="200" height="200"></canvas>
12
<script>
13
14
function drawTriangle(context, point1, point2, point3, filled) {
15
context.beginPath();
16
context.moveTo(point1.x, point1.y);
17
context.lineTo(point2.x, point2.y);
18
context.lineTo(point3.x, point3.y);
19
context.closePath();
20
filled ? context.fill() : context.stroke();
21
}
22
23
24
// Usage example:
25
26
var canvas = document.querySelector('#my-canvas');
27
var context = canvas.getContext('2d');
28
29
/*
30
point3
31
/*\
32
/ \
33
/ \
34
*-------*
35
point1 point2
36
*/
37
38
const point1 = {x: 30, y: 180};
39
const point2 = {x: 170, y: 160};
40
const point3 = {x: 100, y: 30 };
41
42
drawTriangle(context, point1, point2, point3);
43
44
</script>
45
</body>
46
</html>
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
#my-canvas { border: 1px solid gray; }
7
8
</style>
9
</head>
10
<body>
11
<canvas id="my-canvas" width="200" height="200"></canvas>
12
<script>
13
14
function drawTriangle(context, point1, point2, point3, filled) {
15
context.beginPath();
16
context.moveTo(point1.x, point1.y);
17
context.lineTo(point2.x, point2.y);
18
context.lineTo(point3.x, point3.y);
19
context.closePath();
20
filled ? context.fill() : context.stroke();
21
}
22
23
24
// Usage example:
25
26
var canvas = document.querySelector('#my-canvas');
27
var context = canvas.getContext('2d');
28
29
/*
30
point3
31
/*\
32
/ \
33
/ \
34
*-------*
35
point1 point2
36
*/
37
38
const point1 = {x: 30, y: 180};
39
const point2 = {x: 170, y: 160};
40
const point3 = {x: 100, y: 30 };
41
42
drawTriangle(context, point1, point2, point3, true);
43
44
</script>
45
</body>
46
</html>
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
#my-canvas { border: 1px solid gray; }
7
8
</style>
9
</head>
10
<body>
11
<canvas id="my-canvas" width="200" height="200"></canvas>
12
<script>
13
14
function drawTriangle(context, point1, point2, point3, color) {
15
context.beginPath();
16
context.moveTo(point1.x, point1.y);
17
context.lineTo(point2.x, point2.y);
18
context.lineTo(point3.x, point3.y);
19
context.closePath();
20
context.fillStyle = color;
21
context.fill();
22
}
23
24
25
// Usage example:
26
27
var canvas = document.querySelector('#my-canvas');
28
var context = canvas.getContext('2d');
29
30
/*
31
point3
32
/*\
33
/ \
34
/ \
35
*-------*
36
point1 point2
37
*/
38
39
const point1 = {x: 30, y: 180};
40
const point2 = {x: 170, y: 160};
41
const point3 = {x: 100, y: 30 };
42
43
drawTriangle(context, point1, point2, point3, 'red');
44
45
</script>
46
</body>
47
</html>
- CanvasRenderingContext2D.beginPath() - Web APIs | MDN
- CanvasRenderingContext2D.closePath() - Web APIs | MDN
- CanvasRenderingContext2D.moveTo() - Web APIs | MDN
- CanvasRenderingContext2D.lineTo() - Web APIs | MDN
- CanvasRenderingContext2D.fill() - Web APIs | MDN
- CanvasRenderingContext2D.fillStyle - Web APIs | MDN