
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to draw on the canvas with JavaScript?
Drawing on the HTML canvas is to be done with JavaScript. Use the HTML DOM Method getElementById() and getContext() before drawing on the canvas.
For that, follow some steps −
- You need to use the getElementById() method to find the canvas element.
- Use the getContext(), which is drawing object for the canvas. It provides the methods and properties for drawing.
- After that draw on the canvas.
Example
You can try to run the following code to draw canvas on JavaScript −
<!DOCTYPE html> <html> <head> <title>HTML Canvas</title> </head> <body> <canvas id="newCanvas" width="400" height="250" style="border:2px solid #000000;"></canvas> <script> var canvas = document.getElementById("newCanvas"); var ctxt = canvas.getContext("2d"); ctxt.fillStyle = "#56A7E2"; ctxt.fillRect(0,0,250,120); </script> </body> </html>
- Related Articles
- How to draw large font on HTML5 Canvas?
- How to draw a rectangle on HTML5 Canvas?
- How to draw a quadratic curve on HTML5 Canvas?
- How to draw a rounded Rectangle on HTML Canvas?
- How to draw a line on a Tkinter canvas?
- How to draw an arc on a tkinter canvas?
- How to draw an SVG file on an HTML5 canvas?
- How to draw a dashed line on a Tkinter canvas?
- How to draw a Bezier Curve with HTML5 Canvas?
- How to draw a png image on a Python tkinter canvas?
- Draw a circle filled with random color squares on HTML5 canvas
- Draw Bezier Curve with HTML5 Canvas
- Draw a shadow with HTML5 Canvas
- How to draw a dot on a canvas on a click event in Tkinter Python?
- How to draw lines using HTML5 Canvas?

Advertisements