หน้าปัดนาฬิกาผ้าใบ
ส่วนที่ II - วาดหน้าปัดนาฬิกา
นาฬิกาต้องการหน้าปัดนาฬิกา สร้างฟังก์ชัน 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();
}
รหัสอธิบาย
สร้างฟังก์ชัน drawFace() สำหรับการวาดหน้าปัดนาฬิกา:
function drawClock() {
drawFace(ctx, radius);
}
function drawFace(ctx, radius) {
}
วาดวงกลมสีขาว:
ctx.beginPath();
ctx.arc(0, 0, radius, 0, 2 * Math.PI);
ctx.fillStyle = 'white';
ctx.fill();
สร้างการไล่ระดับสีในแนวรัศมี (95% และ 105% ของรัศมีนาฬิกาดั้งเดิม):
grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05);
สร้างแถบสี 3 สต็อป ซึ่งสอดคล้องกับขอบด้านใน ตรงกลาง และด้านนอกของส่วนโค้ง:
grad.addColorStop(0, '#333');
grad.addColorStop(0.5, 'white');
grad.addColorStop(1, '#333');
การหยุดสีสร้างเอฟเฟกต์ 3 มิติ
กำหนดความลาดชันเป็นลักษณะจังหวะของวัตถุรูปวาด:
ctx.strokeStyle = grad;
กำหนดความกว้างของเส้นของวัตถุรูปวาด (10% ของรัศมี):
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();