Numbers of Canvas Clock
- Previous Page Clock Introduction
- Next Page Clock Numbers
Deel 2 - Tekenen van het klokoppervlak
Een klok heeft een klokoppervlak nodig. Maak een JavaScript-functie om het klokoppervlak te tekenen:
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(); }
Codeverklaring
Maak een drawFace() functie om het klokoppervlak te tekenen:
function drawClock() { drawFace(ctx, radius); } function drawFace(ctx, radius) { }
Teken van een witte cirkel:
ctx.beginPath(); ctx.arc(0, 0, radius, 0, 2 * Math.PI); ctx.fillStyle = 'white'; ctx.fill();
Maak een radiaal verloop (95% en 105% van de oorspronkelijke klokstraal):
grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05);
Maak drie kleurendoelen, die overeenkomen met de binnenzijde, het midden en de buitenzijde van de boog:
grad.addColorStop(0, '#333'); grad.addColorStop(0.5, 'white'); grad.addColorStop(1, '#333');
Tip: Deze drie kleurendoelen kunnen een 3D-effect produceren.
Stel de verloop als de penstijl van het tekenobject in:
ctx.strokeStyle = grad;
Definieer de lijndikte van het tekenobject (10% van de straal):
ctx.lineWidth = radius * 0.1;
Tekenen van een cirkel:
ctx.stroke();
Teken het midden van de klok:
ctx.beginPath(); ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI); ctx.fillStyle = '#333'; ctx.fill();
See also:
- Previous Page Clock Introduction
- Next Page Clock Numbers