Wajah Jam Kanvas
Bagian II - Menggambar Wajah Jam
Jam membutuhkan tampilan jam. Buat fungsi JavaScript untuk menggambar tampilan jam:
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 Dijelaskan
Buat fungsi drawFace() untuk menggambar tampilan jam:
function drawClock() {
drawFace(ctx, radius);
}
function drawFace(ctx, radius) {
}
Gambarlah lingkaran putih:
ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2 * Math.PI);
ctx.fillStyle = 'white';
ctx.fill();
Buat gradien radial (95% dan 105% dari radius jam asli):
grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05);
Buat 3 perhentian warna, sesuai dengan tepi dalam, tengah, dan luar busur:
grad.addColorStop(0, '#333');
grad.addColorStop(0.5, 'white');
grad.addColorStop(1, '#333');
Warna berhenti membuat efek 3D.
Tentukan gradien sebagai gaya goresan objek gambar:
ctx.strokeStyle = grad;
Tentukan lebar garis objek gambar (10% dari radius):
ctx.lineWidth = radius * 0.1;
Menggambar lingkaran:
ctx.stroke();
Gambar pusat jam:
ctx.beginPath();
ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI);
ctx.fillStyle = '#333';
ctx.fill();