javascriptdeでcanvasに 時計を表示したいのですが5分おきに少し長いメモリを追加したのですがどうすればいいのでしょうか。
時針、分針、秒針はできてます。下がHTMLとjavascriptで記述したものです
<code>
</head> <body> <div id="container"> <header> 人口置換水準 </header> <main> <iframe id="iframeVideo" title="Youtube video player" width="640" height="510" src="https://www.youtube.com/embed/D0mQAzuE7mM" frameborder="0" allowfullscreen> </iframe><!--元の動画--> 人口が増減しない均衡した状態となる水準を人口置換水準または合計特殊出生率という。 若年期の死亡率が低下すると人口が減りにくくなるので、この水準値は減少する。 2015年の日本の人口置換水準は2.07である。一人の女性が産む子供の数の平均が2.07人であるということである。 </main> <nav> <canvas id="tokei" width="250" height="250"></canvas> <button class="videoButton" onclick="playTheVideo('D0mQAzuE7mM')">置換水準 </button> <button class="videoButton" onclick="playTheVideo('X4wSs4lJoMc')">平均寿命 </button> <button class="videoButton" onclick="playTheVideo('PEI7CP747Bw')">平均年齢 </button><style type="text/css"> html { margin: 0; padding: 0; height: 100%; min-height: 100%; } body { margin: 0; padding: 0; height: 100%; min-height: 100%; background-color: gray; text-align: center; line-height: 175%; } #container { /* id名にはシャープ(#)が必要 */ margin: 0px auto; padding: 0; position: relative; width: 980px; text-align: left; } header { margin: 0; padding: 0; position: relative; width: 100%; height: 60px; /* プロパティと値の間にはコロンが必要 */ text-align: center; } main { margin: 0; padding: 0 20px 20px 20px; position: relative; float: right; /* プロパティと値の間にはコロンが必要 */ width: 600px; /* 単位がない */ } nav { margin: 0; padding: 10px 10px; position: relative; float: left; width: 280px; height: auto; } footer { margin: 0; padding: 10px 10px; position: relative; clear: both; } </style>
</div><!--end id = container--> </body> </html> </code></nav> <footer> </footer>
analogclock.js
<code>
/*
context=canvas.getContext(contextId);
context.beginPath();
*/
var clockInterval;
function drawClock() {
var r = 100;
var x0 = r + 25;
var y0 = r + 25;
var rs = r * 0.85;//針の長さ
var rm = r * 0.6;
var rh = r * 0.4;
var ctx = document.getElementById('tokei').getContext('2d');
//時刻取得 var now = new Date(); var h = now.getHours(); var m = now.getMinutes(); var s = now.getSeconds(); //文字盤 ctx.beginPath(); ctx.arc(x0,y0,r,0,Math.PI*2,true); ctx.fillStyle = '#000'; ctx.fill(); //秒針:分針時針は自分で
//秒針の先端のxy座標
ctx.beginPath();
ctx.moveTo(x0,y0);
var s_theta = sMath.PI/30.0;
var sx = x0 + rsMath.sin(s_theta);
var sy = y0 - rsMath.cos(s_theta);
ctx.lineTo(sx,sy);
ctx.strokeStyle ='#f00';
ctx.lineWidth = 2;
ctx.stroke();
//分針の先端のxy座標
ctx.beginPath();
ctx.moveTo(x0,y0);
var m_theta = mMath.PI/30.0;
var mx = x0 + rmMath.sin(m_theta+(s_theta/60));//秒針の60分1の角度を足していき1分経過したらm_thetaが1増えて,s_thetaが0になるので分針に秒針の動きを合わせる
var my = y0 - rmMath.cos(m_theta+s_theta/60);
ctx.lineTo(mx,my);
ctx.strokeStyle ='#fff';
ctx.lineWidth = 5
ctx.stroke();
//時針の先端のxy座標
ctx.beginPath();
ctx.moveTo(x0,y0);
var h_theta = hMath.PI/30.0;
var hx = x0 + rhMath.sin(h_theta);
var hy = y0 - rh*Math.cos(h_theta);
ctx.lineTo(hx,hy);
ctx.strokeStyle ='#999';
ctx.lineWidth = 7;
ctx.stroke();
//時計の枠メモリ ctx.beginPath(); var x1 = x0 + Math.sin(s_theta); var y1 = y0 - Math.cos(s_theta); ctx.moveTo(x1,y1); var s_theta = s*Math.PI/30.0; var x = x0 + 0.95*Math.sin(s_theta); var y = y0 - 0.95*Math.cos(s_theta); ctx.lineTo(x,y); ctx.strokeStyle ='#fff'; ctx.lineWidth = 3; ctx.stroke();
};
function start(){
clearInterval(clockInterval);
clockInterval = setInterval(drawClock,1000);
};
window.onload = start;
回答1件
あなたの回答
tips
プレビュー