当前位置:网站首页>Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
2022-07-02 05:17:00 【Director of Moral Education Department】
Brief introduction
I list. 3 Plant in Fabric.js
in Change pictures Methods .
It also includes Replace the pictures in the group The operation of .
<br>
<br>
Environment and version
Chrome Browser version :96.0.4664.45
Fabric.js edition :4.6.0
I developed it in a native environment , It also provides a copy of Vue3
Code developed in the environment ( There is a link at the end of the article ).
<br>
<br>
Hands-On Activities
Next there is 3 A case , Used 2 A picture Agumon.png
and Bhikkhu.png
, The picture is in iconfont Found on the website .
If you need to use the picture of this case , It can be obtained in the warehouse provided at the end of the text .
scene 1: Change the of picture elements src
If you add... To the canvas Image
object , Then you can use Image.setSrc
Set new picture , And then use Canvas.renderAll
Refresh the canvas .
<style> canvas { border: 1px solid #ccc; }</style><button onclick="change()"> Modify picture </button><canvas width="300" height="300" id="canvas"></canvas><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.min.js"></script><script> // Instantiation canvas canvas = new fabric.Canvas('canvas') // Create a picture object fabric.Image.fromURL('../../images/Agumon.png', oImg => { // Add picture objects to canvas in canvas.add(oImg) }) // Change picture event function change() { // Get the picture object . Because in this case , There is only one element in the canvas , use getObjects() The first element of the obtained array is the image const img = canvas.getObjects()[0] // Use setSrc Method to change the picture , The second parameter is the callback function , Refresh in the callback function canvas that will do img.setSrc('../../images/Bhikkhu.png', () => { canvas.renderAll() }) }</script>
The above scenario is the simplest .
If there are multiple figures and pictures on the canvas , You may need to add some custom attributes when creating images .
Use fabric.getObjects().find()
Just search .
find()
Is the original method of array .
<br>
<br>
scene 2: Modify the pictures in the group ( No cache )
Creating groups is cached by default , If there is a cache, use Canvas.renderAll()
Method does not update the image .
So when you create a group, you should declare Don't cache : Group.objectCaching
.
<style> canvas { border: 1px solid #ccc; }</style><button onclick="change()"> Modify picture </button><canvas width="300" height="300" id="canvas"></canvas><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.min.js"></script><script> // Instantiation canvas canvas = new fabric.Canvas('canvas') // Create a picture object fabric.Image.fromURL('../../images/Agumon.png', oImg => { // Text const text = new fabric.Text(' Groups without cache ', { fontSize: 14, top: 50 }) // Create group const group = new fabric.Group([oImg, text], { objectCaching: false // Don't cache !!! }) // Add group to canvas in canvas.add(group) }) // Change picture event function change() { // Get group const group = canvas.getObjects()[0] // Get photo const img = group.getObjects().find(item => { // adopt isType Judge picture elements , Because there are 2 Elements ( A picture , A text ) return item.isType('image') }) // Find pictures , Then replace img.setSrc('../../images/Bhikkhu.png', () => { // After changing the picture , refresh canvas canvas.renderAll() }) }</script>
This kind of situation , Focus on creating groups , To declare objectCaching: false
.
<br>
<br>
scene 3: Modify the pictures in the group ( With cache )
If Group (Group) Set cache , It needs to be replaced again Group (Group) Pictures in .
Here's what I did :
- Look for picture objects , And save it to a variable ;
- Delete picture objects in the group ( Use
Group.removeWithUpdate
); - Update the of the picture object
src
Point to ( UseImage.setSrc
); - Put the pictures into groups ( Use
Group.addWithUpdate
); - Re render the canvas ( Use
Canvas.renderAll
);
<br>
<style> canvas { border: 1px solid #ccc; }</style><button onclick="change()"> Modify picture </button><canvas width="300" height="300" id="canvas"></canvas><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.min.js"></script><script> // Instantiation canvas canvas = new fabric.Canvas('canvas') // Create a picture object fabric.Image.fromURL('../../images/Agumon.png', oImg => { // Text const text = new fabric.Text(' Groups without cache ', { fontSize: 14, top: 50 }) // Create group const group = new fabric.Group([oImg, text]) // Add group to canvas in canvas.add(group) }) // Change picture event function change() { // Get group const group = canvas.getObjects()[0] // 【1】 Look for picture objects , And save it to a variable const img = group.getObjects().find(item => { // adopt isType Judge picture elements , Because there are 2 Elements ( A picture , A text ) return item.isType('image') }) // 【2】 Delete picture objects in the group group.removeWithUpdate(img) // 【3】 Update the of the picture object `src` Point to img.setSrc('../../images/Bhikkhu.png', () => { // 【4】 Put the pictures into groups group.addWithUpdate(img) // 【5】 Re render the canvas canvas.renderAll() }) }</script>
According to this example, the goal can be achieved , But I always feel uncomfortable .
If you have better ideas to share , Discuss learning together .
<br>
If you also need to change the picture in your project , But not above 3 In two scenarios , You can leave a message , I'll try to solve .
<br>
<br>
Code warehouse
Native way to achieve Change picture
stay Vue3 Use in Fabric Realization Change picture
<br>
<br>
More recommendations
《Fabric.js From entry to mastery 》
《Fabric.js Achieve gradient (Gradient) effect , Including radial gradient radial》
《Fabric.js Customize the right-click menu 》
give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- js中的Map(含leetcode例题)
- 案例分享|智慧化的西部机场
- Essence and physical meaning of convolution (deep and brief understanding)
- C case of communication between server and client based on mqttnet
- Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
- [quick view opencv] familiar with CV matrix operation with image splicing examples (3)
- Ansible installation and use
- Leetcode18题 【四数之和】递归解法
- ubuntu20.04安装mysql8
- Go GC garbage collection notes (three color mark)
猜你喜欢
摆正元素(带过渡动画)
Gee dataset: chirps pentad high resolution global grid rainfall dataset
Here comes the chicken soup! Keep this quick guide for data analysts
指针使用详解
运维工作的“本手、妙手、俗手”
[opencv] image binarization
Johnson–Lindenstrauss Lemma(2)
CubeMx DMA笔记
Disable access to external entities in XML parsing
LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
随机推荐
Preparation for writing SAP ui5 applications using typescript
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
Gee series: Unit 1 Introduction to Google Earth engine
从数组中找出和为目标的下标
7.1 simulation summary
Draw a wave chart_ Digital IC
Global and Chinese market of commercial fish tanks 2022-2028: Research Report on technology, participants, trends, market size and share
ubuntu20.04安装mysql8
Global and Chinese market of pressure gauges 2022-2028: Research Report on technology, participants, trends, market size and share
[high speed bus] Introduction to jesd204b
黑馬筆記---Set系列集合
数据的储存
Line by line explanation of yolox source code of anchor free series network (7) -- obj in head_ loss、Cls_ Loss and reg_ Calculation and reverse transmission of loss I
Find the subscript with and as the target from the array
国产全中文-自动化测试软件Apifox
7.1模擬賽總結
Nodejs (03) -- custom module
Collectors. Groupingby sort
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]