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:

 

Practical examples

Edit

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

Edit

2. Filled triangle example

Edit

3. Triangle filled with specified color

Edit

 

References

Edit
  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
1
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