Metodas getContext
Metodas getContext nustato piešimo kontekstą
prieš dirbant su canvas elementu. Pirmame metodo parametre
nurodome konteksto tipą - '2d' arba '3d',
o antrame - jo atributus.
Sintaksė
javascriptcanvas.getContext(konteksto tipas, atributai);
Pavyzdys
Sukurkime canvas getContext
ir nustatykime jam permatomą foną per atributą
alpha, rodantį naršyklei, kad yra
alfa kanalas:
html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>javascriptconst canvas = document.getElementById("canvas");
const ctx = canvas.getContext('2d', {alpha: true});
ctx.rect(50, 50, 100, 100);
ctx.fillStyle = 'red';
ctx.fill();
Результат:
Pavyzdys
Dabar sukurkime nepermatomą foną:
html<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>javascriptconst canvas = document.getElementById("canvas");
const ctx = canvas.getContext('2d', {alpha: false});
ctx.rect(50, 50, 100, 100);
ctx.fillStyle = 'red';
ctx.fill();
Результат: