Seilhorlosie _


Deel II - Teken 'n horlosie

Die horlosie benodig 'n horlosie. Skep 'n JavaScript-funksie om 'n horlosie te teken:

JavaScript:

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

function drawFace(ctx, radius) {
  var grad;

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

  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.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();
}


Kode verduidelik

Skep 'n drawFace() funksie om die horlosie te teken:

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

function drawFace(ctx, radius) {
}

Teken die wit sirkel:

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

Skep 'n radiale gradiënt (95% en 105% van oorspronklike klokradius):

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

Skep 3 kleurstoppe, wat ooreenstem met die binneste, middelste en buitenste rand van die boog:

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

Die kleurstoppe skep 'n 3D-effek.

Definieer die gradiënt as die slagstyl van die tekenvoorwerp:

ctx.strokeStyle = grad;

Definieer die lynwydte van die tekenvoorwerp (10% van radius):

ctx.lineWidth = radius * 0.1;

Teken die sirkel:

ctx.stroke();

Teken die middel van die klok:

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