当前位置:网站首页>Straighten elements (with transition animation)
Straighten elements (with transition animation)
2022-07-02 05:18:00 【Director of Moral Education Department】
Brief introduction
This time we're going to talk about 4 individual “ straighten ” Element method , This is the scene I met at work .
I don't know straighten
Use “ straighten ” This word is not translated correctly , That's what I'm going to call it anyway !
straighten
: According to the distance , Rotate the element from the current angle to 0、90、180、270 Equal angle .
<br>
【 The definition of Baidu Encyclopedia 】
straighten, English words , transitive verb 、 intransitive verb , As a transitive verb, it means “ rectify ; send … correct ; send … Straight ; send … to become better ”, As an intransitive verb, it means “ Straighten up ; to become better ”.
<br>
English class representative's reminder ~
<br>
The effect is shown below
<br>
<br>
Version Description
Fabric.js edition :4.6.0
<br>
<br>
relevant API
Fabric.js
There are several API complete Straightening operation :
canvas.straightenObject(object)
object.straighten()
canvas.fxStraightenObject(object)
object.fxStraighten()
<br>
It should be noted that , Use object.straighten
and object.fxStraighten
After straightening the elements , The picture will not be updated automatically ( But in fact, it has been put right ), Need to cooperate with canvas.renderAll()
Wait to refresh the canvas API Use it together .
There seems to be no special explanation on this official document .
<br>
above 4 individual API in , belt fx
Is a transition animation effect .
<br>
<br>
Sample code
In the next code , Used Element object
I defined it in public variables .
If you need to get the specified element dynamically , You can use canvas.getObjects()
、getActiveObject()
Other methods , It depends on your business scenario .
These methods are not the focus of this article , So now let's return to the original purpose .
<br>
Method 1:canvas.straightenObject
canvas.straightenObject
This method allows us to pass in 1 Parameters . This parameter is the element object you need to straighten (fabric.Object
).
<br>
I use one. triangle For example . The logic is written in the code comments .
<button onclick="straightenObject()"> Straighten the elements </button><canvas id="canvasBox" width="600" height="600"></canvas><!-- introduce fabric.js --><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.min.js"></script><script>let canvas = null // Canvas object let triangle = null // Triangle object window.onload = function() { // Initialize canvas canvas = new fabric.Canvas('canvasBox', { width: 400, height: 400 }) // triangle triangle = new fabric.Triangle({ top: 100, left: 100, width: 80, // Bottom edge length height: 100, // Distance from bottom edge to diagonal fill: 'blue', angle: 30 // It's spinning 30 degree }) // Add rectangle to canvas canvas.add(triangle)}// straighten function straightenObject() { // Pass in the triangle object canvas.straightenObject(triangle)}</script>
<br>
Method 2:object.straighten
Use object.straighten
Can also achieve canvas.straightenObject
The effect of .
but object.straighten
Does not automatically refresh the canvas , Need to use canvas.renderAll()
And other methods .
<!-- Omitted code --><script>// straighten function straightenObject() { // Triangle object calls straighten Method , So as to straighten itself triangle.straighten() // Straighten out , The canvas needs to be refreshed canvas.renderAll()}</script>
<br>
Method 3:canvas.fxStraightenObject
The first two methods have no transition effect .
Next, try the transition effect API:canvas.fxStraightenObject
.
and canvas.straightenObject
equally ,canvas.fxStraightenObject
You also need to pass in a Object parameters (fabric.Object
) .
<button onclick="fxStraightenObject()"> Straighten the elements ( Transition animations )</button><canvas id="canvasBox" width="600" height="600"></canvas><!-- introduce fabric.js --><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.min.js"></script><script>let canvas = null // Canvas object let triangle = null // Triangle object window.onload = function() { // Initialize canvas canvas = new fabric.Canvas('canvasBox', { width: 400, height: 400 }) // triangle triangle = new fabric.Triangle({ top: 100, left: 100, width: 80, // Bottom edge length height: 100, // Distance from bottom edge to diagonal fill: 'blue', angle: 30 // It's spinning 30 degree }) // Add rectangle to canvas canvas.add(triangle)}// straighten function fxStraightenObject() { // Pass in the triangle object canvas.fxStraightenObject(triangle)}</script>
canvas.fxStraightenObject
There is no need to use additional methods to refresh the canvas .
<br>
Method 4:object.fxStraighten
Use object.fxStraighten
It can also realize the straightening function with transition animation .
but object.fxStraighten
A little special , It can also be introduced into 1 Parameters , This parameter is an object , There are 2 Function fields .
onComplete
: Callback function after animationonChange
: Callback function during animation execution
Use as follows
<!-- Omitted code --><script>// straighten function fxStraightenObject() { // Pass in the triangle object triangle.fxStraighten({ onChange() { canvas.renderAll() }, onComplete() { canvas.renderAll() } })}</script>
<br>
My thoughts
- If the number of elements to be straightened is small , have access to
canvas.straightenObject
orcanvas.fxStraightenObject
. - If there are many elements , Use
object.straighten
Put all the elements in place , Then do it againcanvas.renderAll()
, Refresh once . - If there are many elements , Use
object.fxStraighten
when , If you don't need to do anything in the callback function , have access torequestAnimationFrame
The method of collective refresh .
<br>
The above first 3 Point code
<button onclick="fxStraightenObject()"> Straighten the elements ( Transition animations )</button><canvas id="canvasBox" width="600" height="600"></canvas> <script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.js"></script><script>let canvas = null // Canvas object let triangle = null // Triangle object let rect = null // Rectangular objects window.onload = function() { // Use Elements id Create a canvas , At this point, you can box select... On the canvas canvas = new fabric.Canvas('canvasBox', { width: 400, height: 400 }) // triangle triangle = new fabric.Triangle({ top: 100, left: 100, width: 80, // Bottom edge length height: 100, // Distance from bottom edge to diagonal fill: 'blue', angle: 30 }) // rectangular rect = new fabric.Rect({ top: 70, left: 200, width: 100, // wide height: 30, // high fill: 'orange', angle: -40 }) // Add rectangle to canvas canvas.add(triangle, rect) renderAll()}// Constantly refresh the canvas function renderAll() { canvas.renderAll() requestAnimationFrame(renderAll)}// straighten ( With transition animation )function fxStraightenObject() { triangle.fxStraighten() // Equilateral triangle rect.fxStraighten() // Straighten the rectangle }</script>
This way of writing is just to provide a way of thinking , Real development needs to be flexibly adjusted according to your business .
<br>
<br>
Code warehouse
- Native way to achieve Straighten the elements ( With transition animation )
- stay Vue3 Use in Fabric Realization Straighten the elements ( With transition animation )
<br>
<br>
Recommended reading
- 《Fabric.js From entry to expansion 》
- 《Fabric.js 3 individual api Set canvas width and height 》
- 《Fabric.js Customize the right-click menu 》
give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- [opencv] image binarization
- Thread pool batch processing data
- Ubuntu 20.04 installing mysql8
- 画波形图_数字IC
- Dark horse notes -- map set system
- Gee dataset: chirps pentad high resolution global grid rainfall dataset
- 黑马笔记---Set系列集合
- Set the default style of scroll bar Google browser
- LeetCode 1175. 质数排列(质数判断+组合数学)
- 黑马笔记---Map集合体系
猜你喜欢
6.网络-基础
Video multiple effects production, fade in effect and border background are added at the same time
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
7.TCP的十一种状态集
Mysql基础---查询(1天学会mysql基础)
Cubemx DMA notes
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Differential identities (help find mean, variance, and other moments)
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
Gee: analyze the change of spatial centroid of remote sensing image [centroid acquisition analysis]
随机推荐
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
操作符详解
The El cascader echo only selects the questions that are not displayed
Case sharing | intelligent Western Airport
Gee series: unit 7 remote sensing image classification using GEE [random forest classification]
Disable access to external entities in XML parsing
Gee series: Unit 1 Introduction to Google Earth engine
Gee: remote sensing image composite and mosaic
No logic is executed after the El form is validated successfully
延时队列两种实现方式
数据的储存
Exercise notes 13 (effective letter ectopic words)
A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
6.网络-基础
Fabric.js 激活输入框
el-cascader回显只选中不显示的问题
函数栈帧的创建和销毁
Set the default style of scroll bar Google browser
Operator details
Video cover image setting, put cover images into multiple videos in the simplest way