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

边栏推荐
- Advanced learning of MySQL -- Application -- storage engine
- Setting methods, usage methods and common usage scenarios of environment variables in postman
- Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL
- Hospital network planning and design document based on GLBP protocol + application form + task statement + opening report + interim examination + literature review + PPT + weekly progress + network to
- MySQL query
- 96% of the collected traffic is prevented by bubble mart of cloud hosting
- 3D game modeling is in full swing. Are you still confused about the future?
- 14. Process time
- The "two-way link" of pushing messages helps app quickly realize two-way communication capability
- Basé sur... Netcore Development blog Project Starblog - (14) Implementation of theme switching function
猜你喜欢

I stepped on a foundation pit today

WordPress collection WordPress hang up collection plug-in

Database concept and installation

Buuctf QR code

Advanced learning of MySQL -- Application -- storage engine

Network communication basic kit -- IPv4 socket structure

Solve the problem that the tabbar navigation at the bottom of vantui does not correspond to the page (window.loading.hash)

Jenkins continuous integration environment construction V (Jenkins common construction triggers)

Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex

Unity controls the selection of the previous and next characters
随机推荐
Bugku Zhi, you have to stop him
POSTECH | option compatible reward reverse reinforcement learning
Baijia forum the founding of the Eastern Han Dynasty
Site favorites
The "two-way link" of pushing messages helps app quickly realize two-way communication capability
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
Fudan released its first review paper on the construction and application of multimodal knowledge atlas, comprehensively describing the existing mmkg technology system and progress
The first spring of the new year | a full set of property management application templates are presented, and Bi construction is "out of the box"
The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
[software implementation series] software implementation interview questions with SQL joint query diagram
Talking about custom conditions and handling errors in MySQL Foundation
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.
Global and Chinese markets of advanced X-ray inspection system (Axi) in PCB 2022-2028: Research Report on technology, participants, trends, market size and share
What are the conditions for the opening of Tiktok live broadcast preview?
I stepped on a foundation pit today
Remote work guide
Pagoda SSL can't be accessed? 443 port occupied? resolvent
Optimization theory: definition of convex function + generalized convex function
15. System limitations and options
Have you entered the workplace since the first 00???