Leinwand Uhr Zahlen


Teil III - Uhrzahlen ziehen

Die Uhr braucht Zahlen. Erstellen Sie eine JavaScript-Funktion zum Zeichnen von Uhrennummern:

JavaScript:

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

function drawNumbers(ctx, radius) {
  var ang;
  var num;
  ctx.font = radius * 0.15 + "px arial";
  ctx.textBaseline = "middle";
  ctx.textAlign = "center";
  for(num = 1; num < 13; num++){
    ang = num * Math.PI / 6;
    ctx.rotate(ang);
    ctx.translate(0, -radius * 0.85);
    ctx.rotate(-ang);
    ctx.fillText(num.toString(), 0, 0);
    ctx.rotate(ang);
    ctx.translate(0, radius * 0.85);
    ctx.rotate(-ang);
  }
}


Beispiel erklärt

Stellen Sie die Schriftgröße (des Zeichenobjekts) auf 15 % des Radius ein:

ctx.font = radius * 0.15 + "px arial";

Stellen Sie die Textausrichtung auf die Mitte und die Mitte der Druckposition ein:

ctx.textBaseline = "middle";
ctx.textAlign = "center";

Berechnen Sie die Druckposition (für 12 Zahlen) auf 85 % des Radius, gedreht (PI/6) für jede Zahl:

for(num = 1; num < 13; num++) {
  ang = num * Math.PI / 6;
  ctx.rotate(ang);
  ctx.translate(0, -radius * 0.85);
  ctx.rotate(-ang);
  ctx.fillText(num.toString(), 0, 0);
  ctx.rotate(ang);
  ctx.translate(0, radius * 0.85);
  ctx.rotate(-ang);
}