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

边栏推荐
- Sword finger offer 14- I. cut rope
- 96% of the collected traffic is prevented by bubble mart of cloud hosting
- Global and Chinese market of cell scrapers 2022-2028: Research Report on technology, participants, trends, market size and share
- Keepalived set the master not to recapture the VIP after fault recovery (it is invalid to solve nopreempt)
- 13. Time conversion function
- Question d: Haffman coding
- Crawler practice website image batch download
- The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult
- Node write API
- Node solves cross domain problems
猜你喜欢

Résumé: entropie, énergie libre, symétrie et dynamique dans le cerveau

C # learning notes: structure of CS documents

A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China

1day vulnerability pushback skills practice (3)

Buuctf QR code

Problems and solutions of several concurrent scenarios of redis

No clue about the data analysis report? After reading this introduction of smartbi, you will understand!

The "message withdrawal" of a push message push, one click traceless message withdrawal makes the operation no longer difficult

I stepped on a foundation pit today

Advanced learning of MySQL -- Application -- storage engine
随机推荐
String: LV1 eat hot pot
MySQL query
Code Execution Vulnerability - no alphanumeric rce create_ function()
Keep an IT training diary 054- opening and closing
Remote work guide
Global and Chinese market of contour projectors 2022-2028: Research Report on technology, participants, trends, market size and share
Résumé des outils communs et des points techniques de l'examen PMP
Imperial cms7.5 imitation "D9 download station" software application download website source code
Redis transaction
Crawler practice website image batch download
The difference between int (1) and int (10)
[software implementation series] software implementation interview questions with SQL joint query diagram
Question d: Haffman coding
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
Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
Kiss number + close contact problem
Network communication basic kit -- IPv4 socket structure
C language black Technology: Archimedes spiral! Novel, interesting, advanced~
Network byte order
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