html:canvas画布绘图简单入门-绘制时钟-3

简介: html:canvas画布绘图简单入门-绘制时钟-3

image.png

<canvas id="canvas"
            width="600"
            height="600"></canvas>
    <script>
        let canvas = document.querySelector('#canvas');
        let ctx = canvas.getContext('2d');
        let width = canvas.width
        let height = canvas.height
        // 绘制内容区域相对画布的大小 80%
        let scale = 0.8;
        // 计算半径
        let radius = Math.floor(Math.min(width, height) * scale * 0.5);
        console.log('radius', radius);
        // 绘制外边框
        // ctx.beginPath();
        // ctx.strokeRect(0, 0, width, height);
        // ctx.closePath()
        // 绘制时钟刻度
        function drawClockScale({
            number = 12,
            strokeStyle = 'black',
            lineWidth = 3,
            lineLength = 20,
        }) {
            ctx.save();
            ctx.strokeStyle = strokeStyle;
            ctx.lineWidth = lineWidth;
            let rotateAngle = (360 / number);
            for (let i = 0; i < number; i++) {
                ctx.rotate((Math.PI / 180) * rotateAngle);
                ctx.beginPath();
                ctx.moveTo(radius - lineLength, 0);
                ctx.lineTo(radius, 0);
                ctx.stroke();
                ctx.closePath()
            }
            ctx.restore();
        }
        function drawClock() {
            ctx.clearRect(0, 0, width, height);
            ctx.save();
            // 将坐标轴原点移动到画布中心
            ctx.translate(width / 2, height / 2);
            // 旋转坐标轴x指向画布正上方
            ctx.rotate(-Math.PI / 180 * 90);
            // 分针和秒针的刻度
            drawClockScale({
                number: 60,
                strokeStyle: 'green',
                lineWidth: 4,
                lineLength: 14
            });
            // 时针刻度
            drawClockScale({
                number: 12,
                strokeStyle: 'red',
                lineWidth: 8,
                lineLength: 20
            });
            // 绘制指针
            let now = new Date();
            let hour = now.getHours();
            let minute = now.getMinutes();
            let second = now.getSeconds();
            console.log(`${hour}: ${minute}: ${second}`);
            // 绘制秒针
            ctx.save();
            ctx.beginPath();
            ctx.rotate((Math.PI / 180) * (360 / 60) * second);
            ctx.strokeStyle = "deepskyblue";
            ctx.lineWidth = 2;
            ctx.moveTo(-20, 0);
            ctx.lineTo(radius - 30, 0);
            ctx.stroke();
            ctx.closePath()
            ctx.restore();
            // 绘制分针
            ctx.save();
            ctx.rotate((Math.PI / 180) * ((360 / 60) * minute + (360 / 60 / 60) * second));
            ctx.beginPath();
            ctx.strokeStyle = "green";
            ctx.lineWidth = 4;
            ctx.moveTo(-20, 0);
            ctx.lineTo(radius - 40, 0);
            ctx.stroke();
            ctx.closePath()
            ctx.restore();
            // 处理成12小时制
            if (hour > 12) {
                hour = hour - 12
            }
            // 绘制时针
            ctx.save();
            ctx.beginPath();
            ctx.rotate((Math.PI / 180) * ((360 / 12) * hour + (360 / 12 / 60) * minute + (360 / 12 / 60 / 60) *
                second));
            ctx.strokeStyle = "red";
            ctx.lineWidth = 8;
            ctx.moveTo(-20, 0);
            ctx.lineTo(radius - 50, 0);
            ctx.stroke();
            ctx.closePath()
            ctx.restore();
            // 绘制圆心
            ctx.beginPath();
            ctx.fillStyle = "grey";
            ctx.lineWidth = 10;
            ctx.arc(0, 0, 10, 0, Math.PI / 180 * 360);
            ctx.fill();
            ctx.closePath()
            // 绘制圆
            ctx.beginPath();
            ctx.strokeStyle = "grey";
            ctx.lineWidth = 10;
            ctx.arc(0, 0, radius, 0, Math.PI / 180 * 360);
            ctx.stroke();
            ctx.closePath()
            ctx.restore();
        }
        // 每隔1秒重绘一次
        setInterval(() => {
            drawClock();
        }, 1000)
    </script>
相关文章
|
27天前
|
移动开发 前端开发 JavaScript
基于 HTML5 和 Canvas 开发的在线图片编辑器
基于 HTML5 和 Canvas 开发的在线图片编辑器
34 0
|
3天前
|
缓存 Java 应用服务中间件
SpringMVC入门到实战------七、SpringMVC创建JSP页面的详细过程+配置模板+实现页面跳转+配置Tomcat。JSP和HTML配置模板的差异对比(二)
这篇文章详细介绍了在SpringMVC中创建JSP页面的全过程,包括项目的创建、配置、Tomcat的设置,以及如何实现页面跳转和配置模板解析器,最后还对比了JSP和HTML模板解析的差异。
SpringMVC入门到实战------七、SpringMVC创建JSP页面的详细过程+配置模板+实现页面跳转+配置Tomcat。JSP和HTML配置模板的差异对比(二)
|
2月前
杨老师课堂之网页制作HTML的学习入门-含有案例1
杨老师课堂之网页制作HTML的学习入门-含有案例
24 3
|
2月前
|
存储 移动开发 API
HTML5本地存储:从入门到精通
HTML5本地存储:从入门到精通
34 1
|
2月前
|
移动开发 前端开发 JavaScript
HTML5 Canvas实现360度全景图
HTML5 Canvas实现360度全景图
28 1
|
2月前
|
数据安全/隐私保护
杨老师课堂之网页制作HTML的学习入门-含有案例2
杨老师课堂之网页制作HTML的学习入门-含有案例
14 0
|
2月前
|
移动开发 前端开发 JavaScript
浏览器端图表渲染技术SVG, VML HTML Canvas
浏览器端图表渲染技术SVG, VML HTML Canvas
24 0
|
Web App开发 移动开发 前端开发
|
7天前
|
JSON 前端开发 JavaScript
使用html,css,js 实现一个龙年春节祝福卡片效果
使用html,css,js 实现一个龙年春节祝福卡片效果
28 4
|
1月前
|
前端开发 UED
使用HTML和CSS创建响应式表格
在网页设计中,表格是一种组织和展示数据的有效方式。本文档将指导你如何使用HTML和CSS来创建一个既美观又响应式的表格,以便在不同设备和屏幕尺寸上都能良好显示。我们将涵盖基础的HTML表格结构,样式美化以及如何实现响应式布局,使得表格内容在小屏设备上也能清晰阅读
79 0