当前位置:网站首页>Fabric.js 3个api设置画布宽高
Fabric.js 3个api设置画布宽高
2022-07-02 05:08:00 【德育处主任】
本文简介
使用 Fabric.js 创建画布时,可以在参数里配置画布的宽高。
除了初始化时设置之外,Fabric.js 还提供了其他 api 供后期修改画布宽高。
本文列举了 Fabric.js 的3个 api 设置画布宽高。
这3个 api 虽然简单,但在实际开发中可能很重要。比如监听浏览器窗口缩放,动态调整画布的宽高。

如果想入门 Fabric.js 可以看 《Fabric.js从入门到膨胀》
<br>
<br>
环境说明
Chrome浏览器版本:96.0.4664.45
Fabric.js版本:4.6.0
我是在原生环境下开发的,同时也提供了一份 Vue3 环境下开发的代码(文末有链接)。
<br>
<br>
实际操作
本例使用了3个 api :
canvas.setWidth:设置画布宽度canvas.setHeight:设置画布高度Canvas.setDimensions:设置画布大小
<style> .btn-x { margin-bottom: 10px; } #canvasBox { border: 1px solid #ccc; }</style><div class="btn-x"> <button onclick="setWidth(200)">宽度200px</button> <button onclick="setHeight(300)">高度300px</button> <button onclick="setDimensions(600, 400)">一键设置宽高:宽度600px,高度400px</button></div><canvas id="canvasBox" width="600" height="600"></canvas><script src="https://cdn.bootcdn.net/ajax/libs/fabric.js/460/fabric.min.js"></script><script> let canvas = null // 设置画布宽度 function setWidth(width) { canvas.setWidth(width) } // 设置画布高度 function setHeight(height) { canvas.setHeight(height) } // 一键设置宽高 function setDimensions(width, height) { canvas.setDimensions({ width, height }) } window.onload = function() { // 使用 元素id 创建画布,此时可以在画布上框选 canvas = new fabric.Canvas('canvasBox', { width: 100, height: 100 }) }</script><br>
<br>
仓库及推荐阅读
仓库
<br>
推荐阅读
边栏推荐
- 数据库问题汇总
- Application d'un robot intelligent dans le domaine de l'agroécologie
- Pyflink writes MySQL examples with JDBC
- Feign realizes file uploading and downloading
- 删除排序数组中的重复项go语言实现
- Typescript function details
- Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
- List of common bugs in software testing
- Leetcode basic programming: array
- 06 装饰(Decorator)模式
猜你喜欢

Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel

Hcip day 17

C# 基于MQTTNet的服务端与客户端通信案例

Video cover image setting, put cover images into multiple videos in the simplest way

Lay the foundation for children's programming to become a basic discipline

Orthogonal test method and function diagram method for test case design

Lm09 Fisher inverse transform inversion mesh strategy

06 装饰(Decorator)模式

How to configure PostgreSQL 12.9 to allow remote connections

Fabric.js IText 手动设置斜体
随机推荐
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
Ruby replaces gem Alibaba image
Latest: the list of universities and disciplines for the second round of "double first-class" construction was announced
【ClickHouse】How to create index for Map Type Column or one key of it?
leetcode两数相加go实现
農業生態領域智能機器人的應用
运维工作的“本手、妙手、俗手”
LeetCode 1175. 质数排列(质数判断+组合数学)
7.1 Résumé du concours de simulation
Cultivate primary and secondary school students' love for educational robots
List of common bugs in software testing
Use of Baidu map
JS interview collection test question 1
js面试收藏试题1
[common error] the DDR type of FPGA device is selected incorrectly
Analyzing the hands-on building tutorial in children's programming
Creation and destruction of function stack frames
Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
Splice characters in {{}}