HTML Canvas Graphics
The HTML <canvas>
element is used to draw graphics on a web page.
The graphic to the left is created with <canvas>
. It shows four elements: a red rectangle, a gradient rectangle, a multicolor rectangle, and a multicolor text.
What is HTML Canvas?
The HTML <canvas>
element is used to draw graphics, on the fly, via JavaScript.
The <canvas>
element is only a container for graphics. You must use JavaScript to actually draw the graphics.
Canvas has several methods for drawing paths, boxes, circles, text, and adding images.
Browser Support
The numbers in the table specify the first browser version that fully supports the <canvas>
element.
Element | Chrome | FireFox | EDGE | Safari | Opera |
---|---|---|---|---|---|
<canvas> | 4.0 | 9.0 | 2.0 | 3.1 | 9.0 |
Canvas Examples
A canvas is a rectangular area on an HTML page. By default, a canvas has no border and no content.
The markup looks like this:
<canvas id="myCanvas" width="200" height="100"></canvas>
Note: Always specify an id
attribute (to be referred to in a script), and a width
and height
attribute to define the size of the canvas. To add a border, use the style
attribute.
Example
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"> </canvas>
Add a JavaScript
After creating the rectangular canvas area, you must add a JavaScript to do the drawing.
Draw a Circle
Example
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.moveTo(0, 0);
ctx.lineTo(200, 100);
ctx.stroke();
</script>
Draw a Text
Example
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(95, 50, 40, 0, 2 * Math.PI);
ctx.stroke();
</script>
Stroke Text
Example
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Hello World", 10, 50);
</script>
Draw Linear Gradient
Example
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// Create gradient
var grd = ctx.createLinearGradient(0, 0, 200, 0);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");
// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
</script>
Draw Circular Gradient
Example
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// Create gradient
var grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");
// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 80);
</script>
Draw Image
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 10, 10);
</script>
42 Responses
purchase levaquin sale buy levaquin without prescription
dutasteride brand order generic zofran 8mg ondansetron 8mg cost
spironolactone tablet buy generic aldactone 25mg order generic fluconazole 100mg
ampicillin pills cephalexin cost oral erythromycin 250mg
fildena 50mg canada robaxin order robaxin pill
order sildenafil 100mg online order aurogra 100mg without prescription purchase estrace for sale
order lamotrigine 200mg sale order mebendazole 100mg generic buy retin gel sale
tadalafil price buy voltaren 50mg online diclofenac online
order isotretinoin 20mg generic order zithromax 250mg for sale buy azithromycin 500mg pills
indomethacin for sale online buy trimox 250mg online cheap purchase trimox sale
purchase tadalafil online cialis use order viagra 100mg online cheap
anastrozole 1 mg uk Canadian generic viagra online sildenafil 100mg cheap
acheter du tadalafil acheter 25mg du sildenafil acheter 50mg viagra
prednisone generic order sildenafil generic us viagra sales
tadalafil 10mg ohne rezept cialis 10mg bestellen viagra 100mg fГјr frauen
isotretinoin canada order generic stromectol ivermectin tablet
buy modafinil 100mg pills Cialis samples diamox price
cheap doxycycline 200mg furosemide order online buy furosemide 40mg without prescription
cost altace 5mg order azelastine 10 ml order astelin without prescription
clonidine canada purchase minocin pill order tiotropium bromide 9 mcg without prescription
buspirone 5mg cheap buy dilantin 100 mg online cheap cost ditropan 2.5mg
buy terazosin pill sulfasalazine sale buy generic sulfasalazine 500 mg
order alendronate 35mg for sale cost fosamax order famotidine online cheap
purchase benicar generic acetazolamide 250mg price oral acetazolamide 250 mg
buy prograf 1mg generic ropinirole sale purchase urso online
brand isosorbide 20mg isosorbide 40mg brand purchase telmisartan generic
bupropion 150 mg cost purchase seroquel online seroquel 50mg over the counter
molnupiravir brand buy naproxen order lansoprazole 15mg generic
buy zoloft 100mg online order zoloft 50mg generic order viagra 50mg
buy salbutamol without prescription salbutamol usa cheap sildenafil sale
buy cialis 40mg for sale order cialis 20mg generic generic amantadine
order revia 50mg generic cost naltrexone 50mg buy aripiprazole pills
buy dapsone buy nifedipine 30mg sale perindopril online
medroxyprogesterone 10mg pill cyproheptadine 4mg usa periactin 4 mg cost
buy modafinil generic modafinil 200mg us ivermectin 6mg online
cheap fluvoxamine 50mg ketoconazole 200mg usa cost glucotrol 10mg
order isotretinoin online cheap deltasone 20mg pills order prednisone 10mg online
nootropil cheap buy nootropil 800 mg for sale buy sildenafil pills
azithromycin cost neurontin 100mg ca gabapentin for sale online
purchase cialis pills buy generic sildenafil 100mg sildenafil viagra
cost lasix oral hydroxychloroquine buy plaquenil 200mg sale
cialis online order clomipramine 25mg generic order clomipramine 25mg for sale