当前位置:网站首页>Fabric. JS centered element
Fabric. JS centered element
2022-07-25 00:58:00 【51CTO】
give the thumbs-up + Focus on + Comment on = Learned to
In the use of Fabric.js When developing , You may need to center the element .
This article summarizes Fabric.js Common methods of centering elements , These include :
- Window based horizontal centering
- Canvas based horizontal centering
- Horizontal center with animation
- Window based vertical centering
- Canvas based vertical centering
- Vertical center with animation
- Horizontal and vertical centering at the same time ( It is also based on windows or canvas )
besides , It also sums up Center the specified element at the canvas level and The element itself calls the centered method .
Reading this article requires you to have some Fabric.js Basics , If you don't understand Fabric.js What is it? , You can read 《Fabric.js From entry to expansion 》
Create basic projects
For the convenience of demonstration , When I initialize the canvas :
- Add a background image , The size of the background image is as large as the initialized canvas .
- Add a rectangle , Then the object to be centered is it .
- Add mouse wheel to zoom canvas while scrolling ( Easy to demonstrate Based on windows and Canvas based The difference between ).
- Add mouse drag canvas translation position ( Easy to demonstrate Based on windows and Canvas based The difference between ).

There's a bit more code , but The zoom level of the canvas can be modified when the scroll wheel is scrolling and Drag the canvas In fact, some of them are unnecessary , This code is mainly written for the convenience of demonstration .
All the elements referred to in the following examples are rect , Because this example takes rect Explain . You need to adjust according to the objects to be operated in the actual project .
Horizontal center
Center the specified element horizontally .
Based on windows

It says 2 Medium method , Method 1 Is to manipulate the specified object with the canvas ; Method 2 It is the element that adjusts its position according to the window .
Let me explain what is... Directly from the above figure Center the elements horizontally according to the window
Zoom

Moving the canvas

After zooming and moving the canvas ,canvas.viewportCenterObjectH and rect.viewportCenterH It will still be centered horizontally according to the standard of the window .
Canvas based

Zoom

Moving the canvas

You can talk to Based on windows Compare the effect of .
With animation effect

The effect with animation needs to be called in the canvas fxCenterObjectH Method . The center with animation effect is centered according to the canvas , Not windows !
Vertical center
The usage of vertical center is similar to that of horizontal center , Just a change api. The horizontal center is “H” , For vertical centering “V”.
Based on windows

Canvas based
With animation

level + vertical Center at the same time
Fabric.js It also provides the function of horizontal and vertical centering at the same time .
Based on windows

Canvas based

With animation
For the time being, we haven't found any animation effect in the vertical and horizontal center at the same time api, So you can try calling fxCenterObjectH and fxCenterObjectV

Code warehouse
Recommended reading
《Fabric.js The use of superscripts and subscripts 》
《Fabric.js How to use the brush ?》
give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- 7.15 - daily question - 408
- Chapter III kernel development
- MySQL series | log module
- Uxdb resets the password without knowing the plaintext password
- Tencent low code platform is officially open source! You can drag and drop and generate mobile phone projects and PC projects! Get private benefits
- A string "0" was actually the culprit of the collapse of station b
- 494. Target sum · depth first search · knapsack problem
- Daily question 1 · 1260. Two dimensional network migration · simulation
- The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
- Interview questions
猜你喜欢

BGP machine room and BGP

The font changes with the change of the form

Invitation letter | "people, finance, tax" digital empowerment, vigorously promote retail enterprises to achieve "doubling" of economies of scale

Principle of data proxy
![[mindspore ascend] [running error] graph_ In mode, run the network to report an error](/img/81/9e96182be149aef221bccb63e1ce96.jpg)
[mindspore ascend] [running error] graph_ In mode, run the network to report an error

Vscode installation and configuration

Cloud native observability tracking technology in the eyes of Baidu engineers

Divide 300000 bonus! Deeperec CTR model performance optimization Tianchi challenge is coming

Listing of China graphite: the market value is nearly HK $1.2 billion, achieving a zero breakthrough in the listing of Hegang private enterprises

Which automation tools can double the operation efficiency of e-commerce?
随机推荐
Educational events
What is the root password of MySQL initial installation
What is the function of transdata operator and whether it can optimize performance
The number of palindromes in question 9 of C language deduction. Two pointer array traversal method
7.16 - daily question - 408
A string "0" was actually the culprit of the collapse of station b
7.24 party notice
Soft test --- fundamentals of programming language (Part 2)
Several states of the process
Brush questions of binary tree (5)
MySQL的最左前缀原则
进程的几种状态
Promtool Check
C language force buckle the flipped number of question 7. Violence Act
Some of my understanding about anti shake and throttling
7.14 - daily question - 408
Principle of data proxy
Automated test series selenium three kinds of waiting for detailed explanation
Dynamic kubernetes cluster capacity expansion of airbnb
2012.4.13 360 written examination summary