当前位置:网站首页>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
边栏推荐
- Director of Shanghai Bureau of culture and Tourism: safety is the lifeline of culture and tourism, and we are seizing the new track of yuancosmos
- ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
- [mindspore] [xception model] script statement is suspected to be wrong
- Listing of China graphite: the market value is nearly HK $1.2 billion, achieving a zero breakthrough in the listing of Hegang private enterprises
- BisinessCardGen
- Measurement and Multisim Simulation of volt ampere characteristics of circuit components (engineering documents attached)
- GUI basic application
- Detailed usage of iperf
- Batchinsert avoid inserting duplicate data ignor
- Example analysis of enum data type in MySQL
猜你喜欢

Harbor installation

Vegetable greenhouses turned into smart factories! Baidu AI Cloud helps Shouguang, Shandong build a new benchmark for smart agriculture

Wireshark packet capturing and rapid packet location skills

Cloud native observability tracking technology in the eyes of Baidu engineers

===、==、Object. Is basic package type

Vscode installation and configuration

Director of Shanghai Bureau of culture and Tourism: safety is the lifeline of culture and tourism, and we are seizing the new track of yuancosmos

Detailed usage of iperf

BGP machine room and BGP

BGP机房和BGP
随机推荐
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
BisinessCardGen
The leftmost prefix principle of MySQL
How to use measurement data to drive the improvement of code review
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
Financial RPA robot enables enterprises to open a new era of intelligence
第四章 驱动子系统开发
自动化测试系列-Selenium三种等待详解
Big talk · book sharing | Haas Internet of things device cloud integrated development framework
Why does [mindspore ascend] [custom operator] repeatedly assign values to one tensor affect another tensor?
Volley7 – networkdispatcher obtains data from the network [easy to understand]
asp rs.open sql,conn,3,1中3,1代表什么?
How to create an index
Add the two numbers in the linked list of the second question of C language. Ergodic method
Yolov7:oserror: [winerror 1455] the page file is too small to complete the final solution of the operation
[FAQ of waiting insurance] can the waiting insurance evaluation organization help with the waiting insurance rectification?
Esp32 OLED lvgl displays common Chinese characters
494. Target sum · depth first search · knapsack problem
Implementing DDD based on ABP -- domain logic and application logic
Ggplot2 visual faceting, visual faceted ridge plot with facet_wrap, and customize the background color of the faceted icon title box