当前位置:网站首页>Fabric. JS zoom canvas
Fabric. JS zoom canvas
2022-07-02 14:27:00 【Director of Moral Education Department】
Brief introduction
give the thumbs-up + Focus on + Collection = Learned to
<br>
Use canvas Projects developed , The need for scrolling the canvas should not be a minority , Like maps .
Fabric.js It also provides the function of scaling the canvas , This article mainly explains several methods for setting the canvas size .
<br>
<br>
Do it
Check the documentation before you start .
I put the documents related to this article here
mouse:wheel: Wheel eventgetZoom: Get the current zoom level of the canvassetZoom: Set canvas zoom levelzoomToPoint: Set the canvas zoom scale and zoom origin
among setZoom and zoomToPoint The application scenarios are different .
<br>
start
Before using zoom , First initialize the canvas .
I will also set a background image on the canvas , Convenient for observation .

<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 canvas = new fabric.Canvas('canvasBox') // Add background // The first 1 Parameters : Picture path // The first 2 Parameters : Callback function , Returning to the function will return the image object fabric.Image.fromURL('../../images/bg.jpg', img => { canvas.setBackgroundImage(img) canvas.renderAll() })</script><br>
Zoom canvas ( Take the upper left corner as the origin )
Zoom the canvas with the upper left corner as the origin , Recommended getZoom and setZoom Combine .
getZoom You can get the current zoom level of the canvas , use setZoom Set a new zoom level .
So I added... On the page 2 Button , A zoom in , A zoom out .

<div> <button onclick="setzoom(1)"> Zoom in </button> <button onclick="setzoom(-1)"> narrow </button></div><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> canvas = new fabric.Canvas('canvasBox') fabric.Image.fromURL('../../images/bg.jpg', img => { canvas.setBackgroundImage(img) canvas.renderAll() }) // Set canvas zoom level function setzoom(val) { let zoom = canvas.getZoom() // Get the current zoom level of the canvas zoom += val canvas.setZoom(zoom) // Set canvas zoom level }</script>Zoom level when zooming in Add 1, Zoom level when zooming out reduce 1.
<br>
Zoom canvas ( Take the mouse pointer as the origin )

<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> canvas = new fabric.Canvas('canvasBox') fabric.Image.fromURL('../../images/bg.jpg', img => { canvas.setBackgroundImage(img) canvas.renderAll() }) // Listen for mouse wheel zoom Events canvas.on('mouse:wheel', opt => { const delta = opt.e.deltaY // Roller , Rolling up is -100, Rolling down is 100 let zoom = this.canvas.getZoom() // Gets the current zoom value of the canvas zoom *= 0.999 ** delta if (zoom > 20) zoom = 20 // Limit the maximum zoom level if (zoom < 0.01) zoom = 0.01 // Limit the minimum zoom level // Zoom with the mouse position as the origin this.canvas.zoomToPoint( { // Key points x: opt.e.offsetX, y: opt.e.offsetY }, zoom // Pass in the modified zoom level ) })</script> Use mouse:wheel Monitor mouse wheel scrolling , If you scroll up ,deltaY The value of is 100, Down there is -100, So you can set a formula to control the zoom level when scrolling .
zoomToPoint It can be understood as setZoom Enhanced Edition , The first parameter is the origin coordinates , This example passes in the current coordinates of the mouse ; The second parameter is the zoom level .
<br>
<br>
Code warehouse
Fabric Scroll wheel zooms canvas
<br>
<br>
Recommended reading
《Fabric.js The use of superscripts and subscripts 》
《Fabric.js How to use the brush ?》
《Fabric.js Let users manually bold text 》
give the thumbs-up + Focus on + Collection = Learned to
边栏推荐
- php链表创建和遍历
- 关于Flink框架窗口(window)函数最全解析
- Method of creating linked server for cross server data access
- Simple introduction to ENSP
- 当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
- uni-app中使用computed解决了tab切换中data()值显示的异常
- P1908 reverse sequence pair
- Adhere to the foundation of 20 minutes go every day II
- Golang quickly generates model and queryset of database tables
- Do you know that there is an upper limit on the size of Oracle data files?
猜你喜欢

Daily learning 2

The use of TestNG, the testing framework (II): the use of TestNG XML

How kaggle uses utility script

Error: eacces: permission denied, access to "/usr/lib/node_modules"
![[Hongke technology sharing] how to test DNS server: DNS performance and response time test](/img/f4/d8c21d6c33985fd6d819cd44c22c72.png)
[Hongke technology sharing] how to test DNS server: DNS performance and response time test

<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持

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

Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute

kaggle如何使用utility script

uniapp自动化测试学习
随机推荐
Route (II)
c# 水晶报表打印
Essential elements of science fiction 3D scenes - City
The conference on the growth of all things was held in Hangzhou, and dangbei was selected into the top 100 list of future unicorns in China in 2022
2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
NLA自然语言分析实现数据分析零门槛
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
Chapter 9: xshell free version installation
[Hongke technology sharing] how to test DNS server: DNS performance and response time test
Golang 快速生成数据库表的 model 和 queryset
全屋Wi-Fi:一个谁也解决不好的痛点?
How kaggle uses utility script
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
PyQt5_ Qscrollarea content is saved as a picture
测试框架TestNG的使用(二):testNG xml的使用
[development environment] Dell computer system reinstallation (download Dell OS recovery tool | use Dell OS recovery tool to make USB flash disk system | install system)
NLA自然语言分析,让数据分析更智能
Simple introduction to ENSP
一般来讲,如果频繁出现inconsistent tab and space的报错
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选