当前位置:网站首页>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笔记——QTableWidget 之 指定某列排序
- 【Rust】Rust 语言基础 | 学习语言都应该快速得出印象
- MySQL密码正确但是启动报错Unable to create initial connections of pool.Access denied for user ‘root‘@‘localhost
- 力扣解法汇总676-实现一个魔法字典
- Reentrant function
- 在ubuntu中使用pypyodbc无法连接sql server
- C语言结构体初始化的四种方法
- 力扣解法汇总1200-最小绝对差
- 一线互联网P7面试题总结---补充中
- 按结构体某一元素排序的小程序(fishing_3)
猜你喜欢
可重入函数
化繁为简,聊一聊复制状态机系统架构抽象
QT笔记——eventFilter事件过滤器
女嘉賓報名
DistSQL 深度解析:打造动态化的分布式数据库
MySQL修改密码不成功(无效)的解决办法
Lesson 12 MySQL high availability component MHA
Geowebcache publishes ArcGIS slice data
C # entry series (XXVII) -- Brief Analysis of LINQ
"Review of software engineering in Wuhan University of technology" Chapter 6 | coding specification
随机推荐
A survey of network defense decision-making methods based on attack defense game
力扣解法汇总522-最长特殊序列 II
力扣解法汇总515-在每个树行中找最大值
QT笔记—— VS一个项目生成多个exe文件
Rocky基础练习题-shell脚本-1
力扣解法汇总513-找树左下角的值
力扣解法汇总648-单词替换
qt5.12 + vs2019 无法定位程序输入点 于动态链接库
QT笔记——网络通信 之 QUdpSocket
QT筆記—— QTableWidget 之 拖拽行數 和 移動
QT笔记——QTableWidget 之 指定某列排序
Internet Download Manager2022试用版(简称 IDM)
腾讯持久化框架MMKV原理探究
C语言中continue的理解(fishing_1)
SQL设计教学管理库
"Review of software engineering in Wuhan University of technology" Chapter III software requirements
「武汉理工大学 软件工程复习」第一章 | 软件工程概述
基于流谱理论的SSL/TLS协议攻击检测方法
ES6 assignment deconstruction
QT笔记——QTableWidget表格生成树,QTreeWidget树节点生成表格内容