<canvas>
allows the user to draw 2D graphics using JS
width
and height
<canvas>
requires a closing tag
getContext()
allows you select the type of drawing to make
2d
for 2D imagesgetContext
method
let canvas = document.querySelector('#main');
if(canvas.getContext) {
let ctx = main.getContext('2d');
}
getContext()
returns a render context object2d
getContext(2d)
method, you can draw shapes, text, images, and other objects<canvas>
element (0,0)(0,0)
with x
increasing to the right and y
increasing to the bottomfillStyle
& strokeStyle
determine fill and stroke (line) styles#000000
(() => {
const canvas = document.querySelector('#main');
if (!canvas.getContext) {
return;
}
// get the context
let ctx = canvas.getContext('2d');
// set fill and stroke styles
ctx.fillStyle = '#F0DB4F';
ctx.strokeStyle = 'red';
// draw a rectangle with fill and stroke
ctx.fillRect(50, 50, 150, 100);
ctx.strokeRect(50, 50, 150, 100);
})();