当前位置:网站首页>Fabric.js 缩放画布
Fabric.js 缩放画布
2022-07-02 11:08:00 【德育处主任】
本文简介
点赞 + 关注 + 收藏 = 学会了
<br>
使用 canvas 开发的项目,滚轮缩放画布的需求应该不算少数,比如地图。
Fabric.js 也提供了缩放画布的功能,本文主要讲解设置画布大小的几种方法。
<br>
<br>
动手实现
在动手前先查查文档。
我把和本文相关的文档放在这
其中 setZoom 和 zoomToPoint 的应用场景不同。
<br>
起步
在使用缩放功能之前,先初始化一下画布。
我还会在画布上设置一个背景图,便于观察。

<canvas id="canvasBox" width="600" height="600" style="border: 1px solid #ccc;"></canvas><!-- 引入 Fabric.js --><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/521/fabric.js"></script><script> // 初始化画布 canvas = new fabric.Canvas('canvasBox') // 添加背景图 // 第1个参数:图片路径 // 第2个参数:回调函数,回到函数里会返回图片对象 fabric.Image.fromURL('../../images/bg.jpg', img => { canvas.setBackgroundImage(img) canvas.renderAll() })</script><br>
缩放画布(以左上角为原点)
以左上角为原点进行缩放画布,推荐使用 getZoom 和 setZoom 组合。
getZoom 可以获取画布当前缩放级别,用 setZoom 设置一个新的缩放级别。
所以我在页面上再加2个按钮,一个放大,一个缩小。

<div> <button onclick="setzoom(1)">放大</button> <button onclick="setzoom(-1)">缩小</button></div><canvas id="canvasBox" width="600" height="600" style="border: 1px solid #ccc;"></canvas><!-- 引入 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() }) // 设置画布缩放级别 function setzoom(val) { let zoom = canvas.getZoom() // 获取画布当前缩放级别 zoom += val canvas.setZoom(zoom) // 设置画布缩放级别 }</script>放大时缩放级别加1,缩小时缩放级别减1。
<br>
缩放画布(以鼠标指针为原点)

<canvas id="canvasBox" width="600" height="600" style="border: 1px solid #ccc;"></canvas><!-- 引入 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() }) // 监听鼠标滚轮缩放事件 canvas.on('mouse:wheel', opt => { const delta = opt.e.deltaY // 滚轮,向上滚一下是 -100,向下滚一下是 100 let zoom = this.canvas.getZoom() // 获取画布当前缩放值 zoom *= 0.999 ** delta if (zoom > 20) zoom = 20 // 限制最大缩放级别 if (zoom < 0.01) zoom = 0.01 // 限制最小缩放级别 // 以鼠标所在位置为原点缩放 this.canvas.zoomToPoint( { // 关键点 x: opt.e.offsetX, y: opt.e.offsetY }, zoom // 传入修改后的缩放级别 ) })</script>使用 mouse:wheel 监听鼠标滚轮滚动,如果向上滚动,deltaY 的值是100,向下就是 -100,所以可以自己设置一条公式来控制滚动时的缩放级别。
zoomToPoint 可以理解为 setZoom 的增强版,第一个参数是原点坐标,本例传入鼠标当前所在的坐标;第二个参数是缩放级别。
<br>
<br>
代码仓库
<br>
<br>
推荐阅读
点赞 + 关注 + 收藏 = 学会了
边栏推荐
- Daily learning 3
- freemarker的使用
- Adhere to the foundation of 20 minutes go every day II
- MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)
- QT - make a simple calculator - realize four operations
- Solving the longest subsequence with linear DP -- three questions
- selenium 元素定位方法
- Use of freemaker
- The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
- 默认插槽,具名插槽,作用域插槽
猜你喜欢

Daily learning 3

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

MySQL 45 lecture - learning from the actual battle of geek time MySQL 45 Lecture Notes - 04 | easy to understand index (Part 1)

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

Quick analysis: easy to share the Internet

Will your sleep service dream of the extra bookinfo on the service network
![[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)](/img/de/7d70f513577e93f1bde1969935a29e.jpg)
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)

YOLOv3&YOLOv5输出结果说明

Use of swagger

Design and implementation of car query system based on php+mysql
随机推荐
docker mysql
NLA自然语言分析实现数据分析零门槛
Codeforces Round #803 (Div. 2)(A~D)
Some interview suggestions for Android programmers "suggestions collection"
抓包工具fiddler学习
c# 水晶报表打印
数据湖(十一):Iceberg表数据组织与查询
每天坚持20分钟go的基础二
php链表创建和遍历
篇9:XShell免费版安装
Thymeleaf dependency
Simple introduction to ENSP
Whole house Wi Fi: a pain point that no one can solve?
Methods of software testing
Multi rotor aircraft control using PID and LQR controllers
Go operation redis
Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
693. Travel sequencing (map + topology)
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
Route (II)