canvas drow method triangle code example

Example 1: js canvas draw polygon

var ctx = canvas.getContext('2d');
ctx.fillStyle = '#f00';
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(100,50);
ctx.lineTo(50, 100);
ctx.lineTo(0, 90);
ctx.closePath();
ctx.fill();

Example 2: how to draw a triangle using javascript

<!doctype html>
<html>
<head>
<body>
<canvas></canvas>
<script>
var canvas=document.querySelector('canvas');
var context=canvas.getContext(2d);
context.beginPath();
    context.moveTo(75,75);
    context.lineTo(10,75);
    context.lineTo(10,25);
    context.fill();
    </script>
    </body>
    </head>
    </html>