1
無法讓時刻時區在模擬時鐘上工作。想要獲得時鐘顯示洛杉磯時間。時鐘適用於「新日期()」而不是「moment.tz(」America/Los_Angeles「)」。使用文本格式時鐘時庫功能正常。在JavaScript模擬時鐘中使用時刻時區
JS
var canvas = document.getElementById("clock");
var ctx = canvas.getContext("2d");
var radius = canvas.height/2;
ctx.translate(radius, radius);
setInterval(drawClock, 1000);
function drawClock() {
ctx.clearRect(-radius, -radius, canvas.width, canvas.height);
drawNumbers(ctx, radius);
drawTime(ctx, radius);
}
function drawNumbers(ctx, radius) {
var ang;
var num;
ctx.font = 14 + "px arial";
ctx.textBaseline="middle";
ctx.textAlign="center";
for(num = 1; num < 13; num++){
ang = num * Math.PI/6;
ctx.rotate(ang);
ctx.translate(0, -radius*0.80);
ctx.rotate(-ang);
ctx.fillText(num.toString(), 0, 0);
ctx.rotate(ang);
ctx.translate(0, radius*0.80);
ctx.rotate(-ang);
}
}
function drawTime(ctx, radius){
var timestamp = moment.tz("America/Los_Angeles");
var hour = timestamp.getHours();
var minute = timestamp.getMinutes();
var second = timestamp.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.7, radius*0.07);
}
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);
}
感謝清除此了! – Qwerty