Leinwand -Zifferblatt
Teil II – Zeichne ein Zifferblatt
Die Uhr braucht ein Zifferblatt. Erstellen Sie eine JavaScript-Funktion, um ein Zifferblatt zu zeichnen:
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();
}
Code erklärt
Erstellen Sie eine Funktion drawFace() zum Zeichnen des Ziffernblatts:
function drawClock() {
drawFace(ctx, radius);
}
function drawFace(ctx, radius) {
}
Zeichne den weißen Kreis:
ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2 * Math.PI);
ctx.fillStyle = 'white';
ctx.fill();
Erstellen Sie einen radialen Farbverlauf (95 % und 105 % des ursprünglichen Uhrenradius):
grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05);
Erstellen Sie 3 Farbstopps, die dem inneren, mittleren und äußeren Rand des Bogens entsprechen:
grad.addColorStop(0, '#333');
grad.addColorStop(0.5, 'white');
grad.addColorStop(1, '#333');
Die Farbstopps erzeugen einen 3D-Effekt.
Definieren Sie den Farbverlauf als Strichstil des Zeichenobjekts:
ctx.strokeStyle = grad;
Definieren Sie die Linienstärke des Zeichenobjekts (10 % des Radius):
ctx.lineWidth = radius * 0.1;
Zeichne den Kreis:
ctx.stroke();
Zeichnen Sie das Zentrum der Uhr:
ctx.beginPath();
ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();