当前位置:网站首页>Fabric. Usage of JS eraser (including recovery function)
Fabric. Usage of JS eraser (including recovery function)
2022-07-02 14:27:00 【Director of Moral Education Department】
Brief introduction
give the thumbs-up + Focus on + Collection = Learned to
<br>
In this paper, Fabric.js Eraser function of .

Fabric.js The basic package of does not include the eraser module , If your project needs an eraser , Use the customized version Fabric.js .
<br>
This article needs to have Fabric.js Basic knowledge of .
If you don't know what Fabric.js , I suggest you like it 《Fabric.js From entry to defiance 》.
At the same time, it is best to understand the usage of basic brushes 《Fabric.js Basic brush usage BaseBrush》.
This article USES Fabric 5.2 edition .
<br>
<br>
Knock on the code
This article USES the Original three piece set Development by . There will also be an eraser included npm Download mode .
<br>
customized Fabric.js
Basic version of the Fabric.js Eraser function is not included , If your project needs to use the eraser function , Need to FabricJS builder Customize in .
CDN

Choose Erasing , Then slide to the bottom of the page , Download the development version or compressed version according to your project needs

Above is CDN How to do it , stay <script> In the label , Use src Just quote .
<br>
npm
npm Someone also packed an eraser Fabric.js package .
You can use the command to download to your project
npm i fabric-with-erasing<br>
It should be noted that ,fabric-with-erasing It's in the basic version fabric Eraser function is added in , Use fabric-with-erasing There is no need to download Fabric .
In writing this article ,fabric-with-erasing Used in Fabric The version is 5.2 .
console.log(fabric.version)<br>
code

The functions to be realized in this example :
- You can change the canvas mode ( Frame selection 、 Clean )
- The Royal Blue Square cannot be wiped
- The wiped place can be restored
<br>
<!-- Button to modify canvas mode --><div style="margin-bottom: 10px;"> <button id="select" type="button" onclick="changeAction('select')">select</button> <button id="erase" type="button" onclick="changeAction('erase')">erase</button> <button id="erase" type="button" onclick="changeAction('undoErasing')">undo erasing</button></div><!-- canvas --><canvas id="c" width="400" height="400" style="border: 1px solid #ccc;"></canvas><!-- Introduce customized fabric --><script src="./fabric.js"></script><script> // Initialize canvas const canvas = this.__canvas = new fabric.Canvas('c') // Add graphics to the canvas ( This example adds 2 A square ) canvas.add( // The first square ( royal blue ) new fabric.Rect({ top: 50, left: 50, width: 50, height: 50, fill: "#4b5cc4", opacity: 0.8, erasable: false // It is not allowed to wipe }), // The second square ( Pink ) new fabric.Rect({ top: 50, left: 150, width: 50, height: 50, fill: "#f47983", opacity: 0.8 }) ) // Modify the sketchpad behavior mode function changeAction(mode) { switch (mode) { case "select": canvas.isDrawingMode = false // Painting is not allowed ( Return to normal box selection mode ) break case "erase": canvas.isDrawingMode = true // Enter painting mode canvas.freeDrawingBrush = new fabric.EraserBrush(canvas) // Use an eraser brush canvas.freeDrawingBrush.width = 10 // Set the brush thickness to 10 break case 'undoErasing': canvas.isDrawingMode = true canvas.freeDrawingBrush = new fabric.EraserBrush(canvas) canvas.freeDrawingBrush.width = 10 canvas.freeDrawingBrush.inverted = true // Restore the wiped place default: break } }</script><br>
- Use an eraser , First, you need to
isDrawingModeSet totrue. new fabric.EraserBrushYou need to pass in the canvas itself , The object when initializing the canvasconst canvas = this.__canvas = new fabric.Canvas('c').- take
canvas.freeDrawingBrush.invertedSet totrueYou can restore the wiped place .
<br>
<br>
Code warehouse
<br>
<br>
Recommended reading
| article | brief introduction |
|---|---|
| 《Fabric.js Basic brush usage BaseBrush》 | Before reading this article, I strongly recommend that you first understand the usage of basic brushes , Because the eraser is actually a brush |
| 《Fabric.js Draw circles freely 》 | take “ Frame selection ” The action is transformed into a free drawing circle |
| 《Fabric.js 3 individual api Set canvas width and height 》 | Width and height settings are not available only during initialization , In this paper, 3 There are three ways to set the canvas width and height , Make it easier for your canvas to adapt to different usage scenarios |
| 《Fabric.js Change the picture 3 Methods ( Including changing pictures in the Group , And the existence of cache )》 | If your project needs to dynamically change the pictures on the canvas , Then I'll summarize it for you 3 Medium method |
| 《Fabric.js Righting element 4 Methods ( With transition animation )》 | One click to place the elements that have been rotated by you |
| 《Fabric.js Upload local image to canvas background 》 | In addition to setting the canvas background during initialization , I also do the function of uploading background locally , Let the canvas modify the background image at runtime |
| 《 stay Vue3 Use in Fabric.js Achieve gradient (Gradient) effect , Including radial gradient radial》 | The official introductory tutorial also has only linear gradients , So that some articles say Fabric.js Only linear gradients are supported . But in fact Radial Gradient I fully support |
| 《Fabric.js From entry to defiance 》 | Fabric.js Getting started , Can deal with simple business after learning |
| 《Fabric.js Right-click menu 》 | Fabric.js There is no right-click event yet , If you want to realize the function of right-click menu , You can directly copy the code of this article ~ |
give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- 默认插槽,具名插槽,作用域插槽
- Openharmony notes --------- (4)
- 抓包工具fiddler学习
- Federated Search: all requirements in search
- Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
- Daily learning 2
- 当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
- In 2021, the global TCB adapter revenue was about $93 million, and it is expected to reach $315.5 million in 2028
- MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
- Data consistency between redis and database
猜你喜欢

Method of creating linked server for cross server data access

什么是 eRDMA?丨科普漫画图解

2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验

Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)

Everyone believes that the one-stop credit platform makes the credit scenario "useful"

uniapp自动化测试学习

HMS core machine learning service helps zaful users to shop conveniently

Mysql5.7 installation super easy tutorial

QT new project_ MyNotepad++

SystemServer进程
随机推荐
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
Default slot, named slot, scope slot
go操作redis
删除元素(带过渡动画)
QT how to set fixed size
Fabric.js 手动加粗文本iText
Multi rotor aircraft control using PID and LQR controllers
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
每日学习3
Thymeleaf dependency
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
给Android程序员的一些面试建议「建议收藏」
什么是 eRDMA?丨科普漫画图解
The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
P1042 [NOIP2003 普及组] 乒乓球
Fabric.js 自由绘制圆形
你知道Oracle的数据文件大小有上限么?
Openharmony notes --------- (4)