CyanPrime CyanPrime - 5 months ago 11
Javascript Question

How to draw polygons on an HTML5 canvas?

I need to know how to draw polygons on a canvas. Without using jQuery or anything like that.

Answer

Create a path with moveTo and lineTo:

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

Live demo: http://jsfiddle.net/yd7Wv/1/