Metoden getContext
Metoden getContext indstiller konteksten for
tegning før arbejde med canvas. I den første parameter
af metoden angiver vi konteksttypen - '2d' eller '3d',
og i den anden - dens attributter.
Syntaks
canvas.getContext(konteksttype, attributter);
Eksempel
Lad os lave et canvas getContext
og give det en gennemsigtig baggrund via attributten
alpha, som viser browseren tilstedeværelsen af
en alfakanal:
<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();
:
Eksempel
Og lad os nu lave en ugennemsigtig baggrund:
<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();
: