getContext Metodu
getContext metodu, canvas ile çalışmadan önce
çizim bağlamını belirler. Metodun ilk parametresinde
bağlam türünü - '2d' veya '3d' - belirtiriz,
ikincisinde ise özelliklerini.
Sözdizimi
canvas.getContext(bağlam türü, özellikler);
Örnek
Hadi bir canvas getContext yapalım
ve tarayıcıya alfa kanalının varlığını gösteren
alpha özelliği aracılığıyla şeffaf bir arka plan ayarlayalım:
<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();
:
Örnek
Ve şimdi hadi şeffaf olmayan bir arka plan yapalım:
<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();
: