javascript - Fabric.js 调整对象大小无法正常工作
问题描述
我无法调整对象的大小我正在使用两个输入,从中获得新的宽度和高度,并将该输入值用作新高度,并且用于活动对象和对象容器正在调整大小,但内容不是。
(function() {
var canvas = this.__canvas = new fabric.Canvas('canvas');
var imgURL = 'http://i.imgur.com/8rmMZI3.jpg';
// create a rectangle with a fill and a different color stroke
var pugImg = new Image();
pugImg.onload = function (img) {
var pug = new fabric.Image(pugImg, {
width: 500,
height: 500,
left: 50,
top: 70,
scaleX: .25,
scaleY: .25
});
canvas.add(pug);
};
pugImg.src = imgURL;
canvas.renderAll();
$('#objSetterWidth, #objSetterHeight').bind('input', function() {
o = canvas.getActiveObject();
w = $('#objSetterWidth').val();
h = $('#objSetterHeight').val();
o.width = w / o.scaleX
o.height = h / o.scaleY
o.setCoords();
canvas.renderAll();
});
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group col-md-6">
<input type="number" class="form-control form-control-sm" placeholder="Width" id="objSetterWidth">
</div>
<div class="form-group col-md-6">
<input type="number" class="form-control form-control-sm" placeholder="Height" id="objSetterHeight">
</div>
<canvas id="canvas" width="800" height="600"></canvas>
解决方案
这就是width
/height
要做的 - 他们不会调整原始图像的大小,而是调整容器的大小,有效地“裁剪”原始图像。您要做的是让fabric 根据图像的原始尺寸设置width
andheight
并缩放对象 - 这样您将影响图像容器和图像本身。您也不需要width
传入height
构造Image
函数。如果您想立即添加图像并缩放到特定大小,请使用scaleToWidth()
或scaleToHeight()
。
无论如何,这是通过输入调整图像大小的代码:
(function() {
var canvas = this.__canvas = new fabric.Canvas('canvas');
var imgURL = 'http://i.imgur.com/8rmMZI3.jpg';
// create a rectangle with a fill and a different color stroke
var pugImg = new Image();
pugImg.onload = function (img) {
var pug = new fabric.Image(pugImg, {
//width: 500, //- you don't need this unless you want to 'crop'
//height: 500, //- you don't need this unless you want to 'crop'
left: 50,
top: 70,
scaleX: .25,
scaleY: .25
});
canvas.add(pug);
canvas.renderAll();
};
pugImg.src = imgURL;
$('#objSetterWidth, #objSetterHeight').bind('input', function() {
var o = canvas.getActiveObject();
// if no object selected, do nothing
if (!o) {
return
}
var w = $('#objSetterWidth').val();
var h = $('#objSetterHeight').val();
// get the original image dimensions
var originalW = o.getOriginalSize().width;
var originalH = o.getOriginalSize().height;
// if w input is empty, don't scale width
if (w) {
o.set('scaleX', w / originalW);
}
// if h input is empty, don't scale height
if (h) {
o.set('scaleY', h / originalH);
}
o.setCoords();
canvas.renderAll();
});
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group col-md-6">
<input type="number" class="form-control form-control-sm" placeholder="Width" id="objSetterWidth">
</div>
<div class="form-group col-md-6">
<input type="number" class="form-control form-control-sm" placeholder="Height" id="objSetterHeight">
</div>
<canvas id="canvas" width="800" height="600"></canvas>
推荐阅读
- python - getattr() 方法如何处理这个 HackerRank 案例?
- c - 程序对于相对较小的数字运行良好,但对于非常大的数字会变得混乱
- python - 如何缩小精灵的命中框范围?
- php - 如何使用 php 中的 API 为 Google 课堂项目创建课程作业
- android - flutter run m1 with plugins failed pod and android emulator doesn't build
- algorithm - 二维数组动态规划的矩阵求幂
- javascript - 我不能在 React JS ClassName 中使用“$”美元符号
- flutter - 您好,我想在屏幕上显示我的代码底部的填充部分,延迟 10 秒。我该怎么做?
- android - 如何在 Android 的 Jetpack Compose 的 LazyColumn 中从 Firebase 存储加载图像?
- python - 用于检测键盘敲击的 Python 模块