当前位置:网站首页>Summer Challenge harmonyos canvas realize clock
Summer Challenge harmonyos canvas realize clock
2022-07-01 15:53:00 【51CTO】
author : Zheng Yao
This article is participating in the starlight project 3.0– Summer Challenge
Preface
Studying recently HarmonyOS Relevant stuff , Read a lot of online canvas Realize the clock , Now I also want to write a story about HarmonyOS Your clock .
Project description
Tool version :DevEco Studio 3.0 Beta3
SDK edition ;3.1.5.5
It mainly uses knowledge :canvas js
Effect display

Implementation steps
1. On the page index.hml Define a canvas Elements
2. Need to be in index.js data Two variables are defined in to store the target element and draw 2d
3. Need to be in onShow Get the target element in the life cycle and draw 2d
4. Define an initialization function initData(), Call the draw function
5. Draw function clock function
5.1 First draw the largest circle on the outside
At this time, the page is so long :

5.2 Draw time ( when ) scale (12 Hour scale )
Now the page :

5.3 Draw a small dot in the center
At this time, the page is so long :

5.4 Get time and convert it into Beijing time
At this time, you need to get the local time of the current system , I have a little trouble here , Hongmeng developer tool new Date() The obtained time is Greenwich mean time , Now we use Beijing time , You need to write a function to convert time , The conversion function is getTimeStamp()
Get the current Beijing time :
5.5 Draw the pointer of the clock
At this time, the page is so long :

5.6 Draw the pointer of the minute hand
At this time, the page looks like this :

5.7 Draw a second hand
At this time, the page looks like this :

5.8 Finally, we need to write an update function to call all painting functions and get the corresponding Beijing time in the update function :
6. The last step is to make the clock turn
So you need to be in initData Write a timer in the function and execute the drawing function every second : The final picture is as shown in the first picture above
summary :
That's all canvas All the steps of drawing a clock . The most important thing is to get the time correctly ( Beijing time. ) But I found in the editor new Date() The time of arrival is eight hours away from our Beijing time , Only under the reminder of a colleague did I know that the obtained time was Greenwich mean time , Then we need to convert this Greenwich mean time into Beijing time , I found a transformation method on the Internet, and finally the transformation was successful .
For more original content, please pay attention to : China soft International HarmonyOS Technical team
Beginner to master 、 Skills to cases , Systematic sharing HarmonyOS Development technology , Welcome to contribute and subscribe , Let's move forward hand in hand to build Hongmeng ecology .
Want to know more about open source , Please visit :
边栏推荐
- Win11如何设置用户权限?Win11设置用户权限的方法
- 2023届春招实习-个人面试过程和面经分享
- Factory high-precision positioning management system, digital safety production management
- 综述 | 激光与视觉融合SLAM
- 软件测试的可持续发展,必须要学会敲代码?
- vim 从嫌弃到依赖(22)——自动补全
- 【LeetCode】43. 字符串相乘
- Zhang Chi Consulting: household appliance enterprises use Six Sigma projects to reduce customers' unreasonable return cases
- ABAP-调用Restful API
- [pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
猜你喜欢

【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)

Nuxt.js数据预取

AVL 平衡二叉搜索树

Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan

软件测试的可持续发展,必须要学会敲代码?

【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测

The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple

Lean Six Sigma project counseling: centralized counseling and point-to-point counseling

【目标跟踪】|STARK

【LeetCode】43. 字符串相乘
随机推荐
Detailed explanation of stm32adc analog / digital conversion
Please, stop painting star! This has nothing to do with patriotism!
Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
大龄测试/开发程序员该何去何从?是否会被时代抛弃?
Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
Qt+pcl Chapter 6 point cloud registration ICP Series 2
Qt+pcl Chapter 6 point cloud registration ICP series 3
Win11如何设置用户权限?Win11设置用户权限的方法
【一天学awk】函数与自定义函数
[video memory optimization] deep learning video memory optimization method
u本位合约和币本位合约有区别,u本位合约会爆仓吗
将ABAP On-Premises系统连接到中央检查系统以进行自定义代码迁移
Short Wei Lai grizzly, to "touch China" in the concept of stocks for a living?
关于用 ABAP 代码手动触发 SAP CRM organization Model 自动决定的研究
MySQL advanced 4
#夏日挑战赛# HarmonyOS canvas实现时钟
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
RT-Thread Env 工具介绍(学习笔记)
雷神科技冲刺北交所,拟募集资金5.4亿元
三星率先投产3nm芯片,上海应届硕士生可直接落户,南开成立芯片科学中心,今日更多大新闻在此...