当前位置:网站首页>Fabric. JS lock the background image, not affected by zooming and dragging
Fabric. JS lock the background image, not affected by zooming and dragging
2022-06-10 17:08:00 【Director of Moral Education Division】
If your project has canvas drag 、 Zoom and other functions , And you don't want the background image to be dragged or zoomed , Then read on .
This article mainly explains how to lock the background image , To lock the background image, you only need to set 1 Only attributes .
This article will add a wheel to zoom the canvas 、 Drag and drop the canvas and other functions to test “ Lock background image ” The effect of .
It should be clear that , No matter how you drag and zoom the canvas , The background is still .
Hands on coding
start
<canvas id="canvasBox" width="600" height="600" style="border: 1px solid #ccc;"></canvas>
<!-- introduce Fabric.js -->
<script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script>
<script>
// Initialize canvas
const canvas = new fabric.Canvas('canvasBox', {
backgroundVpt: false // Not affected by viewport transformations ( That is, whether you drag or zoom the canvas , The background image is not affected )
})
</script>
Copy code **backgroundVpt Set to false This is the key .** Set this , The background image will no longer move , Not affected by viewport changes .
Add background 、 Rectangles and circles
For the convenience of demonstration , I want to set up a background image and two graphic elements , Only graphic elements are modified when zooming , The background image is motionless .
// Omitted code
fabric.Image.fromURL('../../images/bg.jpg', img => {
canvas.setBackgroundImage(img)
canvas.renderAll()
})
// circular
circle = new fabric.Circle({
name: 'circle',
top: 60,
left: 60,
radius: 30, // The radius of the circle 30
fill: 'yellowgreen'
})
// rectangular
rect = new fabric.Rect({
name: 'rect',
top: 30, // From the top of the container 60px
left: 100, // To the left of the container 200px
fill: 'orange', // fill a Orange
width: 60, // Width 60px
height: 60 // Height 60px
})
// Add rectangle to canvas
canvas.add(circle, rect)
Copy code Execute after setting the background image canvas.renderAll() It will be re rendered , Otherwise, the picture looks useless .
Add wheel zoom
Use the scroll wheel to zoom the canvas , Need to monitor mouse:wheel .
// Omitted code
canvas.on('mouse:wheel', opt => {
const delta = opt.e.deltaY // Roller , Rolling up is -100, Rolling down is 100
let zoom = canvas.getZoom() // Gets the current zoom value of the canvas
zoom *= 0.999 ** delta
if (zoom > 20) zoom = 20
if (zoom < 0.01) zoom = 0.01
canvas.zoomToPoint(
{ // Key points
x: opt.e.offsetX,
y: opt.e.offsetY
},
zoom
)
opt.e.preventDefault()
opt.e.stopPropagation()
})
Copy code Add drag and drop canvas event
Finally, add the drag and drop canvas event . Drag and drop includes mouse clicking , Mouse movement , Release the mouse 3 Events :
- Click the mouse :
mouse:down - Mouse movement :
mouse:move - Loosen the mouse :
mouse:up
canvas.on('mouse:down', opt => { // Trigger when the mouse is pressed
let evt = opt.e
canvas.isDragging = true // isDragging Is custom
canvas.lastPosX = evt.clientX // lastPosX Is custom
canvas.lastPosY = evt.clientY // lastPosY Is custom
})
canvas.on('mouse:move', opt => { // Trigger when mouse moves
if (canvas.isDragging) {
let evt = opt.e
let vpt = canvas.viewportTransform // Focus view conversion
vpt[4] += evt.clientX - canvas.lastPosX
vpt[5] += evt.clientY - canvas.lastPosY
canvas.requestRenderAll()
canvas.lastPosX = evt.clientX
canvas.lastPosY = evt.clientY
}
})
canvas.on('mouse:up', opt => { // Trigger when the mouse is released
canvas.setViewportTransform(canvas.viewportTransform) // Set the viewport conversion for this canvas instance
canvas.isDragging = false
})
Copy code summary
The functions explained in this article are not difficult , Just say when initializing the canvas backgroundVpt Set to false that will do .
The application scenario I can think of for the time being is the background of repeated patterns , Secure it .
Source warehouse
边栏推荐
- ADB is not an internal or external command, nor is it a runnable program or batch file
- Basic use cases for jedis
- Embedded development: five challenges in wireless update using MCU
- Fiddler设置断点
- 新思科技在《 2022 年 Gartner 应用安全测试关键能力报告》中表现优异 在五个常见用例中荣获最高分
- 从零开始,如何拥有自己的博客网站【华为云至简致远】
- 【无标题】
- 隐形马尔可夫模型及其训练(一)
- Why do I need a thread pool? What is pooling technology?
- Print linked list from end to end (6)
猜你喜欢

【无标题】

PyTorch基础(一)-- Anaconda 和 PyTorch安装

This paper introduces three feature selection methods in machine learning

Web2 到 Web3,意识形态也需要发生改变

Pytorch Foundation (I) -- anaconda and pytorch installation

Swing visualization plug-in jformdesigner of idea

Fiddler配置
![[web security self-study] section 1 building of basic Web Environment](/img/f8/f2d13c2879cdbc03ad261c6569bc1d.jpg)
[web security self-study] section 1 building of basic Web Environment
PHP实现多张图片上传功能的示例代码

大山深处的孩子,正在看见更远的星空
随机推荐
创建 Visual Studio 离线安装包并安装
What is the 100th trillion digit of PI decimal point? Google gives the answer with Debian server
Full array of arrays
VBA judge whether the file exists and ask for the idea of file backup
You have a ml.net quick reference manual to check!
Simple site backup shell script
智慧景區視頻監控 5G智慧燈杆網關組網綜合杆
oss存储引出的相关内容
SOA架构/测试阶段接口描述语言转换方案
For more than 20 years, there are only Durex, Okamoto and jasbon in the condom market
Hidden Markov model and its training (1)
大山深处的孩子,正在看见更远的星空
Detailed steps for installing redis image in docker (easy to understand, suitable for novices to get started quickly)
VBA将字符串分割、存入数组、写入文件并保存
看先进科技如何改变人类生活
Docker安装Redis镜像详细步骤(简单易懂,适合新手快速上手)
How does Dao achieve decentralized governance?
How to own your own blog website from scratch [Huawei cloud to jianzhiyuan]
关于透视投影变换及相关应用的详细推导
Nat. Commun. | 用于加速发现抗生素抗性基因的知识整合和决策支持