使用canvas绘制动画时钟

一代码

<!DOCTYPE html >
<head>
<meta charset="UTF-8" >
    <title>绘制动态时钟</title>
    <script type="text/javascript">
        var canvas;
        var context;
        function window_onload() {
            canvas = document.getElementById("canvas");
            context = canvas.getContext(‘2d‘);
            setInterval("draw()",1000);
        }
        function draw() {
            var radius = Math.min(canvas.width / 2, canvas.height / 2) - 25;
            var centerx = canvas.width / 2;
            var centery = canvas.height / 2;
            context.clearRect(0, 0, canvas.width, canvas.height);
            context.save();
            context.fillStyle = ‘#efefef‘;
            context.strokeStyle = ‘#c0c0c0‘;
            context.beginPath();
            context.arc(centerx, centery, radius, 0, Math.PI*2, 0);
            context.fill();
            context.stroke();
            context.closePath();
            context.restore();
            var r = radius - 10;
            context.font = ‘bold 16px 宋体‘;
            Drawtext(‘1‘, centerx + (0.5 * r), centery - (0.88 * r));
            Drawtext(‘2‘, centerx + (0.866 * r), centery - (0.5 * r));
            Drawtext(‘3‘, centerx + radius-10, centery);
            Drawtext(‘4‘, centerx + (0.866 * r), centery + (0.5 * r));
            Drawtext(‘5‘, centerx + (0.5 * r), centery + (0.866 * r));
            Drawtext(‘6‘, centerx , centery +r);
            Drawtext(‘7‘, centerx - (0.5 * r), centery + (0.866 * r));
            Drawtext(‘8‘, centerx - (0.866 * r), centery + (0.49 * r));
            Drawtext(‘9‘, centerx-radius+10, centery);
            Drawtext(‘10‘, centerx - (0.866 * r), centery - (0.50 * r));
            Drawtext(‘11‘, centerx - (0.51 * r), centery - (0.88 * r));
            Drawtext(‘12‘, centerx, 35);

            context.save();
            context.fillStyle = ‘black‘;
            context.beginPath();
            context.arc(centerx, centery, 3, 0, Math.PI * 2, 0);
            context.closePath();
            context.fill();
            context.stroke();
            context.restore();

            //var date = new date();
           // var h = date.getHours();
            //var m = date.getMinutes();
            //var s = date.getSeconds();
            //var a = ((h / 12) * Math.PI * 2) - 1.57 + ((m / 60) * 0.524);

            var h = 13;
            var m = 24;
            var s = 59;

            var a = ((h / 12) * Math.PI * 2) - 1.57 + ((m / 60) * 0.524);

            context.save();
            context.lineWidth = 3;
            context.fillStyle = ‘darkgray‘;
            context.strokeStyle = ‘gray‘;
            context.beginPath();
            context.arc(centerx, centery, radius - 55, a + 0.01, a, 1);
            context.lineTo(centerx, centery);
            context.arc(centerx, centery, radius - 40, ((m / 60) * 6.27) - 1.57, ((m / 60) * 6.28) - 1.57, 0);
            context.lineTo(canvas.width / 2, canvas.height / 2);
            context.arc(centerx, centery, radius - 30, ((s / 60) * 6.27) - 1.57, ((s / 60) * 6.28) - 1.57, 0);
            context.lineTo(centerx, centery);
            context.closePath();
            context.fill();
            context.stroke();
            context.restore();

            var hours = String(h);
            var minutes = String(m);
            var seconds = String(s);

            if (hours.length == 1) h = ‘0‘ + h;
            if (minutes.length == 1) m = ‘0‘ + m;
            if (seconds.length == 1) s = ‘0‘ + s;
            var str = h + ‘:‘ + m + ‘:‘ + s;
            Drawtext(str, centerx, centery + radius + 12);

        }
        function Drawtext(text, x, y) {
            context.save();
            x -= (context.measureText(text).width / 2);
            y += 9;
            context.beginPath();
            context.translate(x, y);
            context.fillText(text, 0, 0);
            context.restore();
        }
    </script>
    <style type="text/css">
    div
    {
        display:-moz-box;
        display:-webkit-box;
        -moz-box-pack:center;
        -webkit-box-pack:center;
        width:100%;

    }
    canvas
    {
        background-color:White;

     }
    </style>
</head>
<body onload="window_onload()">
<div><h1>使用canvas元素绘制指针动画时钟</h1></div>
<div><canvas id="canvas" width="200px" height="200px"></canvas></div>
</body>
</html>
时间: 2024-10-14 08:27:37

使用canvas绘制动画时钟的相关文章

h5+canvas绘制动画时钟

h5+canvas绘制动画时钟 效果图,这个是截图,可以动的, 代码如下: <!DOCTYPE html><html><head><title>动画效果</title></head> <body> <canvas id="anm" width="600" height="600" style="border:1px solid gray"

HTML5 Canvas绘制实时时钟

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>try to draw the colock</title> <script src="js/modernizr-1.7.js"></script> <script type="text/javascript"> wind

应用canvas绘制动态时钟--每秒自动动态更新时间

使用canvas绘制时钟 下文是部分代码,完整代码参照:https://github.com/lemoncool/canvas-clock,可直接下载. 首先看一下效果图:每隔一秒会动态更新时间 一.前期准备 1. HTML中准备一个容器存放画布,并为其设置width,height. <div> <canvas id="clock" height="200px" width="200px"></canvas>

canvas绘制指针时钟

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>canvas时钟</title> </head> <body> <canvas id="c" width="300" height="300"></canv

用canvas绘制一个时钟

实现一个时钟的绘制和时间的显示 一,首先是页面的搭建html部分以及一点点的css代码,因为css这块用的比较少,所以就没有单独出来: <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>时钟</title>    <style type="text/css">    

canvas绘制动画效果

绘制一个笑脸,实现笑脸的左右匀速运动 html代码: <canvas id="myCanvas" width="400px" height="400px"></canvas> js代码: <script type="text/javascript"> function $(id) { return document.getElementById(id); } var i=0,j=300,t;

[Canvas学习]动画

学习目的:通过JavaScript操控<canvas>对象,实现交互动画. 动画的基本步骤 1. 清空canvas,使用clearRect方法 2. 保存canvas状态 3. 绘制动画图形 4. 恢复canvas状态 操控动画Controlling an animation setInterval(function, delay) setTimeout(function, delay) requestAnimationFrame(callback) 太阳系运动的小例子 <canvas

Android中使用SurfaceView和Canvas来绘制动画

其实每个View中都有Canvas可以用来绘制动画,只需要在这个View中重载onDraw()方法就可以,但是SurfaceView类是一个专门用来制动动画的类. Canvas(中文叫做"画布")就和HTML5中的canvas标签一样可以在一定区域内自由绘制图形.Canvas+SurfaceView制作的动画与View Animation和Property Animation这类动画比起来更加适合大量的集中播放的动画,比如游戏画面.相机的图像显示等. 因为SurfaceView通常会在

canvas绘制简易动画

在canvas画布中制作动画相对来说很简单,实际上就是不断变化的坐标.擦除.重绘的过程 1.使用setInterval方法设置动画的间隔时间. setInterval(code,millisec) setInterval方法html中固有方法,这个方法接受两个参数,第一个函数表示执行动画的函数,第二个参数为间隔时间,单位是(毫秒). 2.用来绘图的函数 1)通过不断变换X和Y的坐标实现动画的效果. 2)在该函数中先用clearRect方法将画布整体或者局部擦除. 擦除图像clearRect方法: