เข็มนาฬิกาแคน วาส
ตอนที่ IV - วาดเข็มนาฬิกา
นาฬิกาต้องการมือ สร้างฟังก์ชัน JavaScript เพื่อวาดเข็มนาฬิกา:
จาวาสคริปต์:
function drawClock() {
drawFace(ctx, radius);
drawNumbers(ctx, radius);
drawTime(ctx, radius);
}
function drawTime(ctx, radius){
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
//hour
hour = hour%12;
hour = (hour*Math.PI/6)+(minute*Math.PI/(6*60))+(second*Math.PI/(360*60));
drawHand(ctx, hour, radius*0.5, radius*0.07);
//minute
minute = (minute*Math.PI/30)+(second*Math.PI/(30*60));
drawHand(ctx, minute, radius*0.8, radius*0.07);
// second
second = (second*Math.PI/30);
drawHand(ctx, second,
radius*0.9, radius*0.02);
}
function drawHand(ctx,
pos, length, width) {
ctx.beginPath();
ctx.lineWidth = width;
ctx.lineCap = "round";
ctx.moveTo(0,0);
ctx.rotate(pos);
ctx.lineTo(0, -length);
ctx.stroke();
ctx.rotate(-pos);
}
ตัวอย่างที่อธิบาย
ใช้วันที่เพื่อรับชั่วโมง นาที วินาที:
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
คำนวณมุมของเข็มชั่วโมง และวาดความยาว (50% ของรัศมี) และความกว้าง (7% ของรัศมี):
hour = hour%12;
hour = (hour*Math.PI/6)+(minute*Math.PI/(6*60))+(second*Math.PI/(360*60));
drawHand(ctx, hour, radius*0.5, radius*0.07);
ใช้เทคนิคเดียวกันสำหรับนาทีและวินาที
รูทีน drawHand() ไม่ต้องการคำอธิบาย มันก็แค่ลากเส้นตามความยาวและความกว้างที่กำหนด