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ė
canvas.getContext(konteksto tipas, atributai);
Pavyzdys
Sukurkime canvas getContext
ir nustatykime jam permatomą foną per atributą
alpha, rodantį naršyklei, kad yra
alfa kanalas:
<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
const 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ą:
<canvas id="canvas" width="200" height="200" style="background: #f4f4f4;"></canvas>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext('2d', {alpha: false});
ctx.rect(50, 50, 100, 100);
ctx.fillStyle = 'red';
ctx.fill();
: