logo Learn HTML
  • Home
  • Tutorials
  • Practice
  • Quiz
  • Projects
  • About
  • Contact

HTML Course

  • HTML HOME
  • HTML Introduction
  • HTML Basic
  • HTML Element
  • HTML Attributes
  • HTML Headings
  • HTML Paragrapha
  • HTML Style
  • HTML Formattin
  • HTML Quotations
  • HTML Colors
  • HTML Css
  • HTML Links
  • HTML Image
  • HTML Favicon
  • Page Tittle
  • HTML Tables
  • HTML Lists
  • Block & Inline
  • HTML Div
  • HTML Classes
  • HTML Id
  • HTML Buttons
  • HTML Iframes
  • HTML File Paths
  • HTML Head
  • HTML Layout
  • HTML Symbols
  • HTML Emojis
  • HTML URL Encode
  • HTML VS XHTML
  • HTML FROM
  • HTML Form
  • Form Attributes
  • Form Elements
  • HTML Input Types
  • HTML Input Attributes
  • Input Form Attributes
  • HTML Graphics
  • HTML Canvas
  • HTML SVG
  • HTML Media
  • HTML Media
  • HTML Video
  • HTML Audio
  • HTML Plug-ins
  • HTML Youtube
  • HTML APIS
  • HTML Web APIS
  • HTML Geolocation
  • HTML Drag & Drop
  • Web Storage
  • Web Workers
  • HTML SSE
  • HTML Examples
  • HTML Examples
  • HTML Editor
  • HTML Quiz
  • HTML Exercises
  • HTML References
  • HTML Tag List
  • HTML Attributes
  • HTML Global Attributes
  • HTML Brower Support
  • HTML Events
  • HTML Colors
  • HTML Canvas
  • HTML Audio & Video
  • HTML Doctypes
  • HTML Character Sets
  • HTML URL Encode
  • Language Codes
  • HTTP Messages
  • HTTP Methods
  • PX to EM Converter
  • keyboard Shortcuts

HTML Canvas


What is <canvas> ?

The <canvas> element is used to draw graphics on a web page using JavaScript.

Canvas is only a container.

You must use JavaScript to draw shapes, text, images, etc.

Supported by all major browsers.


Basic Canvas Structure

<canvas id="myCanvas" width="200" height="100"> </canvas>

Always define:

  • id (for JavaScript)
  • width
  • height

Add border using CSS:

<canvas id="myCanvas" width="200" height="100" style="border:1px solid black;"> </canvas>

Step 1: Get Canvas in JavaScript

<script>

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

</script>

getContext("2d")` → allows 2D drawing.

Drawing Examples


Draw a Line

<script>

ctx.moveTo(0, 0);
ctx.lineTo(200, 100);
ctx.stroke();

</script>

Draw a Circle

<script>

ctx.beginPath();
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();

</script>

Draw Text

Normal Text

<script>

ctx.font = "30px Arial";
ctx.fillText("Hello World", 10, 50);

</script>

Border Text

<script>

ctx.font = "30px Arial";
ctx.strokeText("Hello World", 10, 50);

</script>

Linear Gradientt

<script>

var grd = ctx.createLinearGradient(0, 0, 200, 0);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");

ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);

</script>

Radial (Circular) Gradient

<script>

var grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");

ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);

</script>

Draw Image

<script>
<img id="img1" src="image.jpg" width="200" style="display:none">

var img = document.getElementById("img1");
ctx.drawImage(img, 10, 10);

</script>

Important Methods


Method Purpose
moveTo() Start position
lineTo() Draw line
arc() Draw circle
fillText() Fill text
strokeText() Border text
fillRect() Draw filled rectangle
drawImage() Draw image

Try It Your Self

Sign Up For New
Update

HTML Basic
  1. HTML Element
  2. HTML Attributes
  3. HTML Headings
  4. HTML Paragrapha
  5. HTML Tables
  6. HTML Lists
HTML Advance
  1. HTML Div
  2. HTML Classes
  3. HTML Id
  4. HTML Buttons
  5. HTML Iframes
  6. HTML Symbols
  7. HTML Emojis