当前位置:网站首页>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 :
边栏推荐
- 大龄测试/开发程序员该何去何从?是否会被时代抛弃?
- Équipe tensflow: Nous ne sommes pas abandonnés
- RT-Thread Env 工具介绍(学习笔记)
- MySQL高级篇4
- The latest NLP game practice summary!
- STM32F1与STM32CubeIDE编程实例-PWM驱动蜂鸣器生产旋律
- The last picture is seamlessly connected with the first picture in the swiper rotation picture
- ABAP-调用Restful API
- TensorFlow团队:我们没被抛弃
- 【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
猜你喜欢
【LeetCode】43. String multiplication
Zhang Chi's class: several types and differences of Six Sigma data
智慧党建: 穿越时空的信仰 | 7·1 献礼
Please, stop painting star! This has nothing to do with patriotism!
For the sustainable development of software testing, we must learn to knock code?
一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
STM32F411 SPI2输出错误,PB15无脉冲调试记录【最后发现PB15与PB14短路】
#夏日挑战赛# HarmonyOS canvas实现时钟
【目标跟踪】|STARK
Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
随机推荐
Reading notes of top performance version 2 (V) -- file system monitoring
ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
Is JPMorgan futures safe to open an account? What is the account opening method of JPMorgan futures company?
HR面试:最常见的面试问题和技巧性答复
C#/VB.NET 合并PDF文档
Day 3 of rhcsa study
【LeetCode】43. 字符串相乘
关于用 ABAP 代码手动触发 SAP CRM organization Model 自动决定的研究
如何写出好代码 - 防御式编程指南
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
雷神科技冲刺北交所,拟募集资金5.4亿元
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
#夏日挑战赛# HarmonyOS canvas实现时钟
"Qt+pcl Chapter 6" point cloud registration ICP Series 6
2022 Moonriver全球黑客松优胜项目名单
Wechat applet 02 - Implementation of rotation map and picture click jump
MySQL advanced 4
What time do you get off work?!!!
Équipe tensflow: Nous ne sommes pas abandonnés