Created
November 10, 2013 21:35
-
-
Save ifukazoo/7404287 to your computer and use it in GitHub Desktop.
HTML5を使って,時計を書く
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
$("document").ready(init); | |
var canvas; | |
var context; | |
var centerX; | |
var centerY; | |
var r; | |
var LINEWIDTH = 2; | |
var step = 0; | |
var dialTimer; | |
var onOff = false; | |
function init() { | |
$("#button").click(toggleDial); | |
canvas = document.getElementById("hangman"); | |
context = canvas.getContext("2d"); | |
context.lineWidth = LINEWIDTH; | |
centerX = canvas.width / 2; | |
centerY = canvas.height / 2; | |
r = canvas.height / 2 * 4 / 5; | |
clearCanvas(); | |
drawCircle(); | |
drawLine(0); | |
drawFont(); | |
} | |
var toggleDial = (function() { | |
var on = function() { | |
clearCanvas(); | |
drawCircle(); | |
drawFont(); | |
step = (step + 1) % 60; | |
drawLine(step); | |
dialTimer = setTimeout(on, 10); | |
onOff = true; | |
}; | |
var off = function() { | |
clearTimeout(dialTimer); | |
onOff = false; | |
}; | |
return function() { | |
if (!onOff) { | |
on(); | |
} else { | |
off(); | |
} | |
}; | |
})(); | |
var clearCanvas = function() { | |
context.clearRect(0, 0, canvas.width, canvas.height); | |
}; | |
var drawCircle = function() { | |
context.beginPath(); | |
context.arc(centerX, centerY, r, 0, Math.PI*2, false); | |
context.stroke(); | |
}; | |
var drawFont = function() { | |
context.beginPath(); | |
context.font ="20px sans-selif"; | |
context.textAlign = "center"; | |
context.textBaseline = "middle"; | |
var one_step_rad = 2 * Math.PI / 12; | |
var start_rad = Math.PI / 2; | |
for (var i = 1; i <= 12; i++ ) { | |
var rad = start_rad - one_step_rad * i; | |
context.strokeText(i, | |
centerX + ( (r - 20) * (Math.cos(rad))), | |
centerY + (-((r - 20) * (Math.sin(rad))))); | |
} | |
context.stroke(); | |
}; | |
var drawLine = function(step) { | |
var one_step_rad = 2 * Math.PI / 60; | |
var start_rad = Math.PI / 2; | |
var rad = start_rad - one_step_rad * step; | |
context.beginPath(); | |
context.moveTo(centerX, centerY); | |
context.lineTo( | |
centerX + ( (r - 5) * (Math.cos(rad))), | |
centerY + (-((r - 5) * (Math.sin(rad))))); | |
context.stroke(); | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<title>dial</title> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> | |
<script src="dial.js"></script> | |
</head> | |
<body> | |
<canvas id="dial" width="400" height="400"></canvas> | |
<input id="button" type="button"/> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment