Home Communities
IT Knowledge
Inspiration
Languages
EN

# JavaScript - draw triangle on canvas element

0 points
Created by:
612

In this article, we would like to show you how to draw a triangle on an HTML canvas element using JavaScript.

Quick solution:

``````function drawTriangle(context, point1, point2, point3, filled) {
context.beginPath();
context.moveTo(point1.x, point1.y);
context.lineTo(point2.x, point2.y);
context.lineTo(point3.x, point3.y);
context.closePath();
filled ? context.fill() : context.stroke();
}``````

## Practical examples

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.

### 1. Unfilled triangle example

``````// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<style>

#my-canvas { border: 1px solid gray; }

</style>
<body>
<canvas id="my-canvas" width="200" height="200"></canvas>
<script>

function drawTriangle(context, point1, point2, point3, filled) {
context.beginPath();
context.moveTo(point1.x, point1.y);
context.lineTo(point2.x, point2.y);
context.lineTo(point3.x, point3.y);
context.closePath();
filled ? context.fill() : context.stroke();
}

// Usage example:

var canvas = document.querySelector('#my-canvas');
var context = canvas.getContext('2d');

/*
point3
/*\
/   \
/     \
*-------*
point1  point2
*/

const point1 = {x: 30,  y: 180};
const point2 = {x: 170, y: 160};
const point3 = {x: 100, y: 30 };

drawTriangle(context, point1, point2, point3);

</script>
</body>
</html>``````

### 2. Filled triangle example

``````// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<style>

#my-canvas { border: 1px solid gray; }

</style>
<body>
<canvas id="my-canvas" width="200" height="200"></canvas>
<script>

function drawTriangle(context, point1, point2, point3, filled) {
context.beginPath();
context.moveTo(point1.x, point1.y);
context.lineTo(point2.x, point2.y);
context.lineTo(point3.x, point3.y);
context.closePath();
filled ? context.fill() : context.stroke();
}

// Usage example:

var canvas = document.querySelector('#my-canvas');
var context = canvas.getContext('2d');

/*
point3
/*\
/   \
/     \
*-------*
point1  point2
*/

const point1 = {x: 30,  y: 180};
const point2 = {x: 170, y: 160};
const point3 = {x: 100, y: 30 };

drawTriangle(context, point1, point2, point3, true);

</script>
</body>
</html>``````

### 3. Triangle filled with specified color

``````// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<style>

#my-canvas { border: 1px solid gray; }

</style>
<body>
<canvas id="my-canvas" width="200" height="200"></canvas>
<script>

function drawTriangle(context, point1, point2, point3, color) {
context.beginPath();
context.moveTo(point1.x, point1.y);
context.lineTo(point2.x, point2.y);
context.lineTo(point3.x, point3.y);
context.closePath();
context.fillStyle = color;
context.fill();
}

// Usage example:

var canvas = document.querySelector('#my-canvas');
var context = canvas.getContext('2d');

/*
point3
/*\
/   \
/     \
*-------*
point1  point2
*/

const point1 = {x: 30,  y: 180};
const point2 = {x: 170, y: 160};
const point3 = {x: 100, y: 30 };

drawTriangle(context, point1, point2, point3, 'red');

</script>
</body>
</html>``````

## Alternative titles

Join to our subscribers to be up to date with content, news and offers.