Mbinu getContext
Mbinu getContext huweka mazingira ya
kuchora kabla ya kufanya kazi na canvas. Katika kigezo cha kwanza cha
mbinu tunaonyesha aina ya mazingira - '2d' au '3d',
na kwa pili - sifa zake.
Syntax
canvas.getContext(aina ya mazingira, sifa);
Mfano
Wacha tutengeneze canvas getContext
na tuiweke msingi uwazi kupitia sifa
alpha, kuonyesha kivinjari uwepo
wa ukoko wa alfa:
<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();
:
Mfano
Na sasa wacha tufanye msingi usio na uwazi:
<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();
: