Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 775
CyanPrime Avatar asked Jan 29 '11 22:01

CyanPrime


People also ask

How do I create a polygon in HTML5?

To draw a polygon in HTML SVG, use the SVG <polygon> element. The <polygon> element creates a graphic containing at least three sides.

Can we draw shapes in HTML5?

We can create many shapes like rectangle, circle, squares, cone, and other custom shapes. HTML5 provides us many elements to create different shapes like Canvas curves and Canvas paths. In this article, we are trying to create some basic shapes of the HTML5 Canvas.


1 Answers

Create a path with moveTo and lineTo (live demo):

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(); 
like image 107
phihag Avatar answered Oct 02 '22 06:10

phihag