当前位置:网站首页>Fabric. JS centered element
Fabric. JS centered element
2022-07-22 18:28: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
边栏推荐
猜你喜欢
QT笔记——网络通信 之 QUdpSocket
Understanding of continue in C language (fishing_1)
QT笔记——QtXml
It took two hours to find the bug about scrollto scrolling the distance from offsettop to the top
"Review of software engineering in Wuhan University of technology" Chapter 2 | software process model
"Review of software engineering in Wuhan University of technology" Chapter 5 | software architecture
「武汉理工大学 软件工程复习」第六章 | 编码规范
Female guest registration
女嘉賓報名
云原生(十) | Kubernetes篇之Kubernetes简介
随机推荐
基于细粒度嵌入空间预留的密文域图像可逆信息隐藏方法
QT笔记——操作Execl
二、注解
QT notes - drag lines and movement of qtablewidget
flutter 线性布局让两个控件充满一行
Verilog for loop (1)
水博士
「武汉理工大学 软件工程复习」第三章 | 软件需求
QT笔记——QtXml
Fabric.js 居中元素
主动降噪耳机排行榜10强,主动降噪耳机十大品牌
互斥锁和信号量
2022-07-21:给定一个字符串str,和一个正数k, 你可以随意的划分str成多个子串, 目的是找到在某一种划分方案中,有尽可能多的回文子串,长度>=k,
Gbase8s database makes the current connection a dormant connection
QT笔记——QJson
es6赋值解构
Fabric. JS control element level
double类型不能进行精确计算的问题
About the external entity key operation spinbox control in stemwin (fishing_2)
Gbase8s database identity connection