当前位置:网站首页>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

边栏推荐
- 7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
- Fudan released its first review paper on the construction and application of multimodal knowledge atlas, comprehensively describing the existing mmkg technology system and progress
- PMP 考試常見工具與技術點總結
- The difference between int (1) and int (10)
- Unity controls the selection of the previous and next characters
- Lichuang EDA learning notes 14: PCB board canvas settings
- The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
- Jenkins continuous integration environment construction V (Jenkins common construction triggers)
- Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
- 1day vulnerability pushback skills practice (3)
猜你喜欢

長文綜述:大腦中的熵、自由能、對稱性和動力學

中電資訊-信貸業務數字化轉型如何從星空到指尖?

What are the conditions for the opening of Tiktok live broadcast preview?

(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

WP collection plug-in free WordPress collection hang up plug-in

FRP intranet penetration

Database concept and installation

Ai aide à la recherche de plagiat dans le design artistique! L'équipe du professeur Liu Fang a été embauchée par ACM mm, une conférence multimédia de haut niveau.

false sharing
随机推荐
I stepped on a foundation pit today
Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
中電資訊-信貸業務數字化轉型如何從星空到指尖?
Global and Chinese market of thin film deposition systems 2022-2028: Research Report on technology, participants, trends, market size and share
Mysql to PostgreSQL real-time data synchronization practice sharing
Key knowledge of embedded driver
13. Time conversion function
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
查詢效率提昇10倍!3種優化方案,幫你解决MySQL深分頁問題
Safety tips - seat belt suddenly fails to pull? High speed police remind you how to use safety belts in a standardized way
[development team follows] API specification
Global and Chinese markets for electroencephalogram (EEG) devices 2022-2028: Research Report on technology, participants, trends, market size and share
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Redis transaction
[untitled]
15. System limitations and options
JS object definition
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
Rhcsa day 2