getContext metodu
getContext metodu
kənvasla işləməzdən əvvəl çəkmək üçün konteksti təyin edir. Metodun birinci parametrində
kontekstin növünü - '2d' və ya '3d' göstəririk,
ikincidə isə onun atributlarını.
Sintaksis
canvas.getContext(kontekstin tipi, atributlar);
Nümunə
Gəlin bir kənvas getContext edək
və ona alpha atributu vasitəsilə
brauzerə alfa kanalının olmasını göstərən
şəffaf fon təyin edək:
<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();
:
Nümunə
İndi isə gəlin şəffaf olmayan fon edək:
<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();
: