15

How to draw polygons on an HTML5 canvas?

 2 years ago
source link: https://www.codesd.com/item/how-to-draw-polygons-on-an-html5-canvas.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to draw polygons on an HTML5 canvas?

advertisements

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


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/

Related Articles

How can I draw polygons on an HTML5 canvas via a JavaScript function

i want the user to be able to click somewhere on the canvas and the polygon will appear on it <DIV id="canvasarea" class="rounded"> <CANVAS id="canvas" width="800px" height="800px"></CANVAS&

How to draw a star using HTML5 canvas?

Am trying to draw a star by using canvas ,but the code is not ruining.I want to understand the steps of measure the Y and X coordinate ? How to find them ? to draw any shape ? <html> <head> <meta charset = "utf-8"> <title>

Error while drawing the polygon on an HTML5 canvas

I have the following code to draw any polygon on an HTML5 canvas on a button click. The user supplies the radius, sides, x and y co-ordinates. Using the sides any regular polygon should be drawn. First we move to the perimeter using moveTo() and then

how to draw polygons in OpenGL that have a schema drawn with a black pen and another fill color

How can I draw a shape like this in OpenGL? I mean, I know how to draw polygons in OpenGL. I want to know how to make the outline black and the fill color ( for example ) yellow?You have 5 vertices. Draw a GL_POLYGON with them and then then a GL_LINE

Drawing Dashed lines on HTML5 Canvas?

I would like to draw some dashed lines on HTML5 canvas. But I couldn't find there is such a feature. the canvas path could only draw solid lines. And people have tried to use some border feature (dotted, dashed) in CSS to draw dashed lines, but they

How to eliminate feather edges on HTML5 Canvas strokes?

Currently, when using the HTML5 canvas element, stroked paths have slightly feathered edges. Here is some example code I am using: this.context.lineJoin = "round"; this.context.lineTo(x1, y1); this.context.lineTo(x2, y2); this.context.closePath(

Draw SVG on the HTML5 canvas with support for the font element

Is there a good library for converting SVG to HTML canvas that supports the font element? I have already tried canvg, but it does not support Font.Browsers that support HTML5 Canvas also support SVG pretty well themselves. As such, you could do this:

How to draw high resolution on a canvas on Chrome? And why if devicePixelRatio === webkitBackingStorePixelRatio does scaling on 2x improve resolution?

I am trying to draw a 300dpi image to a canvas object but in Chrome it shows in very poor quality. When I used the below code, it did not improve but that was because devicePixelRatio was the same as backingStoreRatio (both were 1). I then tried to f

How to draw polygons with CGPath?

I have been reading thru the documentation however it is not immediatly clear to me how to draw a polygon using CGPath. All I need to do is to draw CGPath around something like this: __ \ \ \ \ \__\ Could anyone please provide an snippet on how to do

How to make text not select HTML5 canvas by double-clicking?

(In every browser I've tried) double-clicking on an HTML5 canvas selects any text immediately following the canvas element. I'd prefer to keep the clicks confined to the canvas. (N.b.: I don't want to disable text selection entirely (e.g. like this):

Problem when drawing a line on HTML5 Canvas

<body> <canvas id="myCanvas" style="border:1px solid #000000; width: 800px;height:800px"> </canvas> </body> I have a canvas defined like this. And on view ready I am drawing an arrow over the canvas graphics. wi

How to animate the size of the polygon in the HTML5 canvas?

I have drawn a polygon with the following code. Now I want to resize that polygon animatedly. In detail, I want to set an angular movement to one side of the polygon, such that it makes an arc, and so changes the size of polygon. I googled for everyt

How to draw a game on Android Canvas with lots of primitives

I draw a lot of lines rectangles each frame in my game - it's a recreation of an old school handheld electronic game. The ones that had crude dot matrix display for the main game and custom images for text or some images. I have 20x20 big "pixels&quo

Draw a circle without HTML5 canvas

I want to draw a circle witout using HTML5 GRAPHICS. This because I want to be able to edit the radius of the circle. And the circle needs to be selectable. Is there a possibility to do this with javascript? <!DOCTYPE html> <html> <head>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK