当前位置:网站首页>Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
Constantly changing harmonyos custom JS components during the Spring Festival - Smart Koi
2022-07-04 03:01:00 【Harmonyos technology community】
author : Bao Yuedong
brief introduction
Today I will show you how to use pure JS Code Canvas Draw a swimming Koi
Let's take a look at the renderings first
Realize the idea
Split
First look at the design drawings , Our little fish by the head 、 fin 、 The body 、 Arthropod 、 The tail consists of five parts .
The implementation of each part is given below api
The head is a solid circle , Can pass canvas.arc()
The fin is composed of a straight line and a curve , use path, Curves can use Bezier curves canvas.quadraticCurveTo().
The body is made up of two straight lines and curves , Bezier curve is also selected , Through the control point, you can control the small fish fat and thin
Arthropod 1 It consists of two solid circles and a trapezoid . The trapezoid can be seen as four closed line segments .
Arthropod 2 By a trapezoid and a circle , The scheme is the same as above
The tail consists of two triangles , It is also a closed segment
Parameters
Datum dimension parameters
First, we set a benchmark size parameter to control the size of the small fish , Here we choose the radius of the fish head R
Other parameters are as follows :
The overall size of the fish is as follows :
It is worth noting that , The length of the canvas is not equal to the length of the fish , Because the center of gravity of the small fish is not located in the center of the fish's length , It's the center of the little fish , But the little fish turns around it , Therefore, the radius of our canvas needs to be expanded into the length from the center of the small fish body to the tail of the small fish , The size of the entire canvas =4.19R*4.19R, as follows
Datum rotation parameter
In order to make our little fish turn around , Here we need a rotation angle , We choose fishMainAngle As a protagonist .
Implementation steps
Customize a Canvas The components of
Hold the context relevant to the drawing
Here we delay 200ms, stay js In order to get hml Defined fishcanvas Components , to context And hold . Then perform onDraw Drawing
Define the drawing method
The following is a step-by-step description
Clear the canvas
We need to erase the canvas before each painting , Otherwise, it will pollute the subsequent drawing
Draw the small fish head
Draw the small fish head , You need the coordinates of the center of the small fish head , Small fish head radius . The center coordinate of the small fish head can be determined by the center of gravity coordinate middlePoint、 distance 、 The current angle is calculated .
Note that the canvas coordinate axis is in the upper left corner ,x Shaft to the right ,y Axis down
Draw small fish eyes , The small fish eye is realized by two ellipses , Use canvas.ellipse() draw
Draw small fins
utilize startPoint,controlPoint,endPoint Draw Bessel's closure path
Draw the arthropod
The limbs are round 、 Trapezoidal structure , First draw a circle, then talk about trapezoidal drawing
Draw a small fishtail size triangle
Draw the body of a small fish
First, get the four vertices of the small fish , Get two control points on the left and right , Draw a closed path
The swing of a small fish
swing
By constantly changing the rotation angle of the fish fishAngle To simulate the swing of a small fish , Timers can be used here , More convenient can be used Animator
here startAnimation Method, we create a Animator, And designate option,option The parameters in are described as follows
After creating the animation, we specify onframe Callback ,currentValue Is the animation change factor ,currentValue Affect the angle of small fish . It's over currentValue, We call onDraw() Redraw
The effect is as follows :
️ Although the swing of small fish is realized here , But it feels weird . Daily small fish swing , Small fishtail , The amplitude and frequency of arthropods are higher than that of small fish , Only in this way can it not appear dull . Range we add one to the small fishtail and arthropod currentValue Correlation product to expand the amplitude .
How to update animation , The frequency of each part of the small fish is different , Here is the frequency conversion of small fish
frequency conversion
Scheme 1 : For different frequency parts, create one Animator, Every Animator Manage the frequency of the same part and draw . The interaction is complicated
Option two : Use the periodicity of sine function .sin(nx) The frequency is sin(x) Of n times , If the angle formula of small fish is similar to sin(nx) relevant , Then we set different n Value to achieve frequency inconsistency , For example, the angle of the head =sin(2x), The angle of the tail =sin(3x), Then the frequency of the tail is that of the head 1.5 times .
Above fishAngle The frequency of 1.2 times , The frequency of arthropods has increased 1.5 times .sin,cos Angle variation range , That is to say currentValue Of end-begin, Must be 360 An integral multiple of a degree , In this way, we can guarantee sin,cos The periodicity of .
We set up begin=0, The following requirements currentValue Of end value
The following is the frequency conversion option
call
summary
Through the exercise of this project , We customize Canvas,JS Animation , Have a deeper understanding of trigonometric functions
Here is only the realization of small fish swing and frequency conversion , Later, when there is time, we will click the screen to realize the swimming of small fish 、 Turning when swimming 、 Steering frequency conversion
Source code address
For more original content, please pay attention to : Shenkaihong 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 Hongmeng , Please visit :
51CTO Hongmeng technology community jointly built with Huawei officials
https://harmonyos.51cto.com/#bkwz
边栏推荐
- Dans la recherche de l'intelligence humaine ai, Meta a misé sur l'apprentissage auto - supervisé
- 7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
- 60 year old people buy medical insurance and recommend a better product
- STM32 key content
- PMP 考試常見工具與技術點總結
- Problems and solutions of several concurrent scenarios of redis
- The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
- Site favorites
- Question d: Haffman coding
- Talking about custom conditions and handling errors in MySQL Foundation
猜你喜欢
Pagoda SSL can't be accessed? 443 port occupied? resolvent
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
WordPress collection WordPress hang up collection plug-in
SQL injection (1) -- determine whether there are SQL injection vulnerabilities
VRRP+BFD
Lichuang EDA learning notes 14: PCB board canvas settings
LV1 previous life archives
On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
Node write API
[Yugong series] February 2022 attack and defense world advanced question misc-84 (MySQL)
随机推荐
Short math guide for latex by Michael downs
[development team follows] API specification
Li Chuang EDA learning notes 13: electrical network for drawing schematic diagram
Leetcode 110 balanced binary tree
Global and Chinese market for travel wheelchairs 2022-2028: Research Report on technology, participants, trends, market size and share
On Valentine's day, I code a programmer's exclusive Bing Dwen Dwen (including the source code for free)
I stepped on a foundation pit today
2022 registration examination for safety production management personnel of fireworks and firecracker production units and examination skills for safety production management personnel of fireworks an
Question C: Huffman tree
Base d'apprentissage de la machine: sélection de fonctionnalités avec lasso
false sharing
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
1day vulnerability pushback skills practice (3)
[Yugong series] February 2022 attack and defense world advanced question misc-84 (MySQL)
The requests module uses
Rhcsa day 2
Love and self-discipline and strive to live a core life
60 year old people buy medical insurance and recommend a better product
Talking about custom conditions and handling errors in MySQL Foundation
Mysql-15 aggregate function