当前位置:网站首页>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
边栏推荐
- Mysql5.7 installation super easy tutorial
- 万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
- 测试框架TestNG的使用(二):testNG xml的使用
- 软件测试的方法
- Selenium, element operation and browser operation methods
- Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
- Basic knowledge of QT original code
- Fabric.js 上划线、中划线(删除线)、下划线
- In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
- docker mysql
猜你喜欢
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
HMS core machine learning service helps zaful users to shop conveniently
Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
Whole house Wi Fi: a pain point that no one can solve?
In 2021, the global revenue of structural bolts was about $796.4 million, and it is expected to reach $1097.6 million in 2028
uniapp自动化测试学习
Launcher启动过程
Codeforces Round #803 (Div. 2)(A~D)
Codeforces Round #803 (Div. 2)(A~D)
Use of UIC in QT
随机推荐
BeanUtils -- shallow copy -- example / principle
MQ教程 | Exchange(交换机)
Codeforces Round #803 (Div. 2)(A~D)
P1908 逆序对
Quarkus learning IV - project development to deployment
Yyds dry goods inventory software encryption lock function
测试框架TestNG的使用(二):testNG xml的使用
freemarker的使用
Basic knowledge of QT original code
Essential elements of science fiction 3D scenes - City
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
删除元素(带过渡动画)
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
Story point vs. Human Sky
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
Simple introduction to ENSP
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
Federated Search: all requirements in search
How many knowledge points can a callable interface have?
路由(二)