2017-08-31 10 views
1

時計の数字が正しく表示されない正しい位置を回転する方法。 私は各桁の正しい角度を計算しないと思います。コードのJavaスクリプトのキャンバス時計正しい番号で回転する番号

これらの行は、エラーを有し、ここで各桁の角度を計算

エラーfindeいずれか及びどのようにこの

を解決するために、どのようにアナログ時計回り桁を

for (var n = 1; n <=12; n++) { 
     var theta = (n - 12) * (Math.PI * 2)/12; 
     var x = clockRadius * 0.7 * Math.cos(theta); 
     var y = clockRadius * 0.7 * Math.sin(theta); 
     ctx.fillText(n, x, y); 
     ctx.rotate(theta); 
    } 
を計算することです

clock image here

クロッククロック

<canvas id="canvas" width="150" height="150"></canvas> 
<script> 


function init(){ 

    clock(); 
    setInterval(clock, 1000); 
} 
function toRad(degrees) { 
    return degrees * (Math.PI/180); 
} 
function clock(){ 

    var ctx = document.getElementById('canvas').getContext('2d'); 
    var clockRadius = 110; 
    ctx.save(); 
    ctx.clearRect(0,0,150,150); 
    ctx.translate(75,75); 
    ctx.scale(0.4,0.4); 
    ctx.rotate(-Math.PI/2); 
    ctx.fillStyle = "white"; 
    ctx.lineWidth = 8; 
    ctx.lineCap = "round"; 
ctx.font = '22px Helvetica,Arial,sans-serif'; 
    ctx.fillStyle = '#fff'; 
    ctx.textAlign = 'center'; 
    ctx.textBaseline = 'middle'; 

    var now = new Date($("#datetime").val()); 
    //alert(now); 
    var sec = now.getSeconds(); 
    var min = now.getMinutes(); 
    var hr = now.getHours(); 
    hr = hr>=12 ? hr-12 : hr; 

    for (var n = 1; n <=12; n++) { 
     var theta = (n - 12) * (Math.PI * 2)/12; 
     var x = clockRadius * 0.7 * Math.cos(theta); 
     var y = clockRadius * 0.7 * Math.sin(theta); 
     ctx.fillText(n, x, y); 
     ctx.rotate(theta); 
    } 

    ctx.strokeStyle = "white"; 
    ctx.save(); 
    for (var i=0; i < 12; i++){ 
    ctx.beginPath(); 
    ctx.rotate(Math.PI/6); 
    ctx.moveTo(100,0); 
    ctx.lineTo(120,0); 
    ctx.stroke(); 
    } 
    ctx.restore(); 

    // Minute marks 
    ctx.save(); 
    ctx.lineWidth = 5; 
    for (i=0;i<60;i++){ 
    if (i%5!=0) { 
     ctx.beginPath(); 
     ctx.moveTo(117,0); 
     ctx.lineTo(120,0); 
     ctx.stroke(); 
    } 
    ctx.rotate(Math.PI/30); 
    } 
    ctx.restore(); 

    ctx.fillStyle = "black"; 

    // write Hours 

    ctx.strokeStyle = "#4D514E"; 
    ctx.save(); 
    ctx.rotate(hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec) 
    ctx.lineWidth = 14; 
    ctx.beginPath(); 
    ctx.moveTo(-20,0); 
    ctx.lineTo(80,0); 
    ctx.stroke(); 
    ctx.restore(); 

    // write Minutes 
    ctx.save(); 
    ctx.rotate((Math.PI/30)*min + (Math.PI/1800)*sec) 
    ctx.lineWidth = 10; 
    ctx.beginPath(); 
    ctx.moveTo(-28,0); 
    ctx.lineTo(110,0); 
    ctx.stroke(); 
    ctx.restore(); 

    // Write seconds 
    ctx.save(); 
    ctx.rotate(sec * Math.PI/30); 
    ctx.strokeStyle = "#D40000"; 
    ctx.fillStyle = "#D40000"; 
    ctx.lineWidth = 6; 
    ctx.beginPath(); 
    ctx.moveTo(-30,0); 
    ctx.lineTo(110,0); 
    ctx.stroke(); 
    ctx.beginPath(); 
    ctx.arc(0,0,10,0,Math.PI*2,true); 
    ctx.fill(); 
    ctx.beginPath(); 
    ctx.arc(0,0,10,0,Math.PI*2,true); 
    ctx.stroke(); 
    ctx.fillStyle = "rgba(0,0,0,0)"; 
    ctx.arc(0,0,3,0,Math.PI*2,true); 
    ctx.fill(); 
    ctx.restore(); 

    ctx.beginPath(); 
    ctx.lineWidth = 14; 
    ctx.strokeStyle = '#494545'; 
    ctx.arc(0,0,142,0,Math.PI*2,true); 
    ctx.stroke(); 


    ctx.restore(); 
} init(); 
</script> 

答えて

0

あなたは時計機能の7行目で全体の文脈を回転させます。

function clock(){ 

    var ctx = document.getElementById('canvas').getContext('2d'); 
    var clockRadius = 110; 
    ctx.save(); 
    ctx.clearRect(0,0,150,150); 
    ctx.translate(75,75); 
    ctx.scale(0.4,0.4); 
    ctx.rotate(-Math.PI/2); // <-- remove this 
    ctx.fillStyle = "white"; 
    ... 

これを削除する必要があります。そうするのではなく、アームを描くときに回転させるだけです。

// write Hours 
ctx.rotate(-Math.PI/2 + hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec) 

// write Minutes 
ctx.rotate(-Math.PI/2 + (Math.PI/30)*min + (Math.PI/1800)*sec) 

// Write seconds 
ctx.rotate(-Math.PI/2 + sec * Math.PI/30); 

(描画桁のためのコードがマイナーな修正が必要になります)

var theta = (n - 3) * (Math.PI * 2)/12; 

function init(){ 
 

 
    clock(); 
 
    setInterval(clock, 1000); 
 
} 
 
function toRad(degrees) { 
 
    return degrees * (Math.PI/180); 
 
} 
 
function clock(){ 
 

 
    var ctx = document.getElementById('canvas').getContext('2d'); 
 
    var clockRadius = 110; 
 
    ctx.save(); 
 
    ctx.clearRect(0,0,150,150); 
 
    ctx.translate(75,75); 
 
    ctx.scale(0.4,0.4); 
 
    ctx.fillStyle = "white"; 
 
    ctx.lineWidth = 8; 
 
    ctx.lineCap = "round"; 
 
ctx.font = '22px Helvetica,Arial,sans-serif'; 
 
    ctx.fillStyle = '#fff'; 
 
    ctx.textAlign = 'center'; 
 
    ctx.textBaseline = 'middle'; 
 

 
    var now = new Date('2000/1/1 ' + $("#datetime").val()); 
 
    //alert(now); 
 
    var sec = now.getSeconds(); 
 
    var min = now.getMinutes(); 
 
    var hr = now.getHours(); 
 
    hr = hr>=12 ? hr-12 : hr; 
 

 
    for (var n = 1; n <=12; n++) { 
 
     var theta = (n - 3) * (Math.PI * 2)/12; 
 
     var x = clockRadius * 0.7 * Math.cos(theta); 
 
     var y = clockRadius * 0.7 * Math.sin(theta); 
 
     ctx.fillText(n, x, y); 
 
    } 
 

 
    ctx.strokeStyle = "white"; 
 
    ctx.save(); 
 
    for (var i=0; i < 12; i++){ 
 
    ctx.beginPath(); 
 
    ctx.rotate(Math.PI/6); 
 
    ctx.moveTo(100,0); 
 
    ctx.lineTo(120,0); 
 
    ctx.stroke(); 
 
    } 
 
    ctx.restore(); 
 

 
    // Minute marks 
 
    ctx.save(); 
 
    ctx.lineWidth = 5; 
 
    for (i=0;i<60;i++){ 
 
    if (i%5!=0) { 
 
     ctx.beginPath(); 
 
     ctx.moveTo(117,0); 
 
     ctx.lineTo(120,0); 
 
     ctx.stroke(); 
 
    } 
 
    ctx.rotate(Math.PI/30); 
 
    } 
 
    ctx.restore(); 
 

 
    ctx.fillStyle = "black"; 
 

 
    // write Hours 
 

 
    ctx.strokeStyle = "#4D514E"; 
 
    ctx.save(); 
 
    ctx.rotate(-Math.PI/2 + hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec) 
 
    ctx.lineWidth = 14; 
 
    ctx.beginPath(); 
 
    ctx.moveTo(-20,0); 
 
    ctx.lineTo(80,0); 
 
    ctx.stroke(); 
 
    ctx.restore(); 
 

 
    // write Minutes 
 
    ctx.save(); 
 
    ctx.rotate(-Math.PI/2 +(Math.PI/30)*min + (Math.PI/1800)*sec) 
 
    ctx.lineWidth = 10; 
 
    ctx.beginPath(); 
 
    ctx.moveTo(-28,0); 
 
    ctx.lineTo(110,0); 
 
    ctx.stroke(); 
 
    ctx.restore(); 
 

 
    // Write seconds 
 
    ctx.save(); 
 
    ctx.rotate(-Math.PI/2 +sec * Math.PI/30); 
 
    ctx.strokeStyle = "#D40000"; 
 
    ctx.fillStyle = "#D40000"; 
 
    ctx.lineWidth = 6; 
 
    ctx.beginPath(); 
 
    ctx.moveTo(-30,0); 
 
    ctx.lineTo(110,0); 
 
    ctx.stroke(); 
 
    ctx.beginPath(); 
 
    ctx.arc(0,0,10,0,Math.PI*2,true); 
 
    ctx.fill(); 
 
    ctx.beginPath(); 
 
    ctx.arc(0,0,10,0,Math.PI*2,true); 
 
    ctx.stroke(); 
 
    ctx.fillStyle = "rgba(0,0,0,0)"; 
 
    ctx.arc(0,0,3,0,Math.PI*2,true); 
 
    ctx.fill(); 
 
    ctx.restore(); 
 

 
    ctx.beginPath(); 
 
    ctx.lineWidth = 14; 
 
    ctx.strokeStyle = '#494545'; 
 
    ctx.arc(0,0,142,0,Math.PI*2,true); 
 
    ctx.stroke(); 
 

 

 
    ctx.restore(); 
 
} init();
canvas { 
 
background: blue; 
 
}
<input type="time" id="datetime" value="03:45:01"> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<canvas id="canvas" width="150" height="150"></canvas> 
 
<script> 
 

 

 

 
</script>

0

clock関数の冒頭でctx.rotate(-Math.PI/2)と呼び出したので、数字を元に戻す必要があります。これを行うには、まずコンテキストを数字の座標に変換してから、コンテキストをMath.PI/2で回転させる必要があります。

あなたのforループ内のコードのこの部分を置き換えます。これで

ctx.fillText(n, x, y); 
ctx.rotate(theta); 

を:

ctx.save(); 
ctx.translate(x, y); 
ctx.rotate(Math.PI/2); 
ctx.fillText(n, 0, 0); 
ctx.restore();