Numbers ng Clock ng Canvas

Ikalawang bahagi - Magpipinta ng porma ng orasan

Ang orasan ay nangangailangan ng porma ng orasan. Tukuyin ang function na JavaScript upang magpipinta ng porma ng orasan:

JavaScript:

function drawClock() {
  drawFace(ctx, radius);
}
function drawFace(ctx, radius) {
  const grad = ctx.createRadialGradient(0, 0 ,radius * 0.95, 0, 0, radius * 1.05);
  grad.addColorStop(0, '#333');
  grad.addColorStop(0.5, 'white');
  grad.addColorStop(1, '#333');
  ctx.beginPath();
  ctx.arc(0, 0, radius, 0, 2 * Math.PI);
  ctx.fillStyle = 'white';
  ctx.fill();
  ctx.strokeStyle = grad;
  ctx.lineWidth = radius*0.1;
  ctx.stroke();
  ctx.beginPath();
  ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI);
  ctx.fillStyle = '#333';
  ctx.fill();
}

Subukan nang sarili

Mga paliwanag ng kodigo

Tukuyin ang function na drawFace() upang magpipinta ng porma ng orasan:

function drawClock() {
  drawFace(ctx, radius);
}
function drawFace(ctx, radius) {
}

Magpipinta ng puting bilog:

ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2 * Math.PI);
ctx.fillStyle = 'white';
ctx.fill();

Tukuyin ang radial gradient (95% at 105% ng oras na radius ng orasan):

grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05);

Tukuyin ang tatlong tanda ng kulay, na tumutugma sa panloob, sa gitna at sa labas ng arkong bilog:

grad.addColorStop(0, '#333');
grad.addColorStop(0.5, 'white');
grad.addColorStop(1, '#333');

Mga paalala: Ang tatlong tanda ng kulay na ito ay magbibigay ng epekto ng 3D.

Tukuyin ang pagbabagong kulay ng porma ng pagpipinta ng linya:

ctx.strokeStyle = grad;

Tukuyin ang linya ng krokis ng porma ng pagpipinta (10% ng radius):

ctx.lineWidth = radius * 0.1;

Magpipinta ng bilog:

ctx.stroke();

Mga porma ng sentro ng orasan:

ctx.beginPath();
ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();

Mga ibang pagkakatuklasan:

Kompleto na Manwal ng Canvas ng CodeW3C.com