Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

HTML Canvas Coordinates


Canvas Coordinates

The HTML canvas is a two-dimensional grid.

The upper-left corner of the canvas has the coordinates (0,0).

Mouse over the rectangle below to see its x and y coordinates:

X
Y

Draw a Rectangle

To draw a rectangle on the canvas, use the following method:

  • fillRect(x, y, width, height) - defines the start-point and the width and height of the rectangle

Example

Sorry, your browser does not support canvas.

Define a start-point in position (0,0), and a width and height of 150px and 75px:

<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.fillRect(0, 0, 150, 75);
</script>
Try it Yourself »

Draw a Line

To draw a straight line on a canvas, use the following methods:

  • moveTo(x, y) - defines the starting point of the line
  • lineTo(x, y) - defines the ending point of the line

To actually draw the line, you must use one of the "ink" methods, like stroke().

Example

Sorry, your browser does not support canvas.

Define a start-point in position (0,0), and an end-point in position (200,100). Then use the stroke() method to actually draw the line:

<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.moveTo(0, 0);
ctx.lineTo(200, 100);
ctx.stroke();
</script>
Try it Yourself »


Draw a Circle

To draw a circle on a canvas, use the following methods:

  • beginPath() - begins a path
  • arc(x, y, r, startangle, endangle) - creates an arc/curve. To create a circle with arc(): Set startangle to 0 and endangle to 2*Math.PI. The x- and y-coordinates define the center of the circle. r defines the radius of the circle

Example

Sorry, your browser does not support canvas.

Define a circle with the arc() method. Then use the stroke() method to actually draw the circle:

<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.beginPath();
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();
</script>
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.