代码供上,请大神指教
time.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="clock.js"></script>
<style type="text/css">
div{
text-align: center;
margin-top: 250px;
}
#clock{
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div>
<canvas id="clock" height="200px" width="200px"></canvas>
</div>
</body>
</html>
clock.js
var dom = document.getElementById('clock');
var ctx = dom.getContext('2d');
var width = ctx.canvas.width;
var height = ctx.canvas.height;
var r = width / 2;
function drawBackground() {
"use strict";
ctx.translate(r, r);
ctx.beginPath();
ctx.arc(0, 0, r, 0, 2 * Math.PI, false);
ctx.stroke();
}drawBackground();
<script type="text/javascript" src="clock.js"></script> 这句要放到body中div的后边
<script>
clock.js
var dom = document.getElementById('clock');
var ctx = dom.getContext('2d');
var width = ctx.canvas.width;
var height = ctx.canvas.height;
var r = width / 2;
function drawBackground() {
"use strict";
ctx.translate(r, r);
ctx.beginPath();
ctx.arc(0, 0, r, 0, 2 * Math.PI, false);
ctx.stroke();
}
drawBackground();
</script>
加一个<script></script>就可出来啊