Draw Clock

利用HTML5的canvas来绘制一个时钟;

编写html文件

index.html
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <title>Clock</title>
</head>
<body>
    <canvas id="drawClock" width="500" height="500"></canvas>
    <script src="js/file1.js"></script>
</body>
</html>

编写js文件

file1.js
var canvas = document.getElementById("drawClock");
var clock = canvas.getContext("2d");

setInterval(drawClock, 1000);

function drawClock() {
    //清除像素
    clock.clearRect(0, 0, 500, 500);

    //绘制表盘
    clock.lineWidth = 10;
    clock.strokeStyle = "black";
    clock.beginPath();
    clock.arc(250, 250, 200, 0, 2*Math.PI);
    clock.stroke();

    //获得需要显示的时间
    var time = new Date();
    var second = time.getSeconds();
    var minute = time.getMinutes();
    var hour = time.getHours();//返回0~23之间的整数
    hour += minute / 60;
    hour = hour > 12 ? hour - 12 : hour;

    //绘制表盘上的大刻度
    for (var i = 0; i < 12; i++) {
        //保存前一个状态
        clock.save();
        //重新定义画布(0,0)点
        clock.translate(250, 250);
        clock.lineWidth = 7;
        clock.strokeStyle = "black";
        clock.rotate(i * 30 * Math.PI / 180);
        clock.beginPath();
        clock.moveTo(0, -170);
        clock.lineTo(0, -195);
        clock.stroke();
        clock.restore();
    }

    //绘制表盘上的小刻度
    for (var i = 0; i < 60; i++) {
        //保存前一个状态
        clock.save();
        //重新定义画布(0,0)点
        clock.translate(250, 250);
        clock.lineWidth = 5;
        clock.strokeStyle = "black";
        clock.rotate(i * 6 * Math.PI / 180);
        clock.beginPath();
        clock.moveTo(0, -180);
        clock.lineTo(0, -195);
        clock.stroke();
        clock.restore();
    }

    //绘制对应刻度上的数字
    for (var i = 0; i < 12; i++) {
        clock.save();
        clock.translate(250, 250);
        var digit = i == 0 ? 12 : i;
        var len = 160;
        var x = Math.abs(len * Math.sin(i * 30 * Math.PI / 180));
        var y = Math.abs(len * Math.cos(i * 30 * Math.PI / 180));
        x = i > 6 ? x * -1 : x;
        y = i >= 3 && i <= 9 ? y : y * -1;
        clock.font = "normal normal bold 10px arial";
        clock.fillText(digit, x - 5, y, 50);
        clock.restore();
    }

    //绘制时针
    clock.save();
    //重新定义画布(0,0)点
    clock.translate(250, 250);
    clock.lineWidth = 5;
    clock.strokeStyle = "black";
    clock.rotate(hour * 30 * Math.PI / 180);
    clock.beginPath();
    clock.moveTo(0, -100);
    clock.lineTo(0, 10);
    clock.stroke();
    clock.restore();

    //绘制分针
    clock.save();
    clock.translate(250, 250);
    clock.lineWidth = 4;
    clock.strokeStyle = "black";
    clock.rotate(minute * 6 * Math.PI / 180);
    clock.beginPath();
    clock.moveTo(0, -125);
    clock.lineTo(0, 10);
    clock.stroke();
    clock.restore();

    //绘制秒针
    clock.save();
    clock.translate(250, 250);
    clock.lineWidth = 3;
    clock.strokeStyle = "black";
    clock.rotate(second * 6 * Math.PI / 180);
    clock.beginPath();
    clock.moveTo(0, -150);
    clock.lineTo(0, 10);
    clock.stroke();
    clock.restore();
}

最终效果图

这里写图片描述

#include <Windows.h> #include <math.h> #include <time.h> #include LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void DrawClock(HDC hdc, RECT rect); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT(“Clock); HWND hWnd; MSG msg; WNDCLASS wc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = szAppName; if (!RegisterClass(&wc)) { MessageBox(NULL, TEXT("Program requires Windows NT!"), szAppName, MB_ICONERROR); return 0; } hWnd = CreateWindow(szAppName, TEXT("Analog Clock"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_CREATE: SetTimer(hWnd, 1, 1000, NULL); break; case WM_TIMER: InvalidateRect(hWnd, NULL, TRUE); break; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); RECT rect; GetClientRect(hWnd, &rect); DrawClock(hdc, rect); EndPaint(hWnd, &ps); } break; case WM_DESTROY: KillTimer(hWnd, 1); PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } void DrawHand(HDC hdc, int cx, int cy, double angle, int length, int width, COLORREF color) { double radians = (angle - 90.0) * 3.1415926535 / 180.0; int x = cx + (int)(length * cos(radians)); int y = cy + (int)(length * sin(radians)); HPEN hPen = CreatePen(PS_SOLID, width, color); HPEN hOldPen = (HPEN)
最新发布
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值