Languages
[Edit]
EN

JavaScript - draw triangle on canvas element

0 points
Created by:
Majid-Hajibaba
972

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>
<head>
  <style>
    
    #my-canvas { border: 1px solid gray; }
    
  </style>
</head>
<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>
<head>
  <style>
    
    #my-canvas { border: 1px solid gray; }
    
  </style>
</head>
<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>
<head>
  <style>
    
    #my-canvas { border: 1px solid gray; }
    
  </style>
</head>
<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>

 

References

  1. CanvasRenderingContext2D.beginPath() - Web APIs | MDN
  2. CanvasRenderingContext2D.closePath() - Web APIs | MDN
  3. CanvasRenderingContext2D.moveTo() - Web APIs | MDN
  4. CanvasRenderingContext2D.lineTo() - Web APIs | MDN
  5. CanvasRenderingContext2D.fill() - Web APIs | MDN
  6. CanvasRenderingContext2D.fillStyle - Web APIs | MDN

Alternative titles

  1. JavaScript - draw triangle on HTML canvas element
  2. JavaScript - draw triangle on canvas element for provided points
  3. JavaScript - draw triangle on canvas from three points
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - HTML5 canvas tutorial

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join