我已经用下面的代码解决了我的问题
var maxWidth = 1000;
var maxHeight = 1000:
if(obj.currentHeight > maxHeight || obj.currentWidth > maxHeight){
return;
}
obj.setCoords();
// top-left corner
if(obj.getBoundingRect(true).top < 0 || obj.getBoundingRect(true).left < 0){
obj.top = Math.max(obj.top, obj.top-obj.getBoundingRect(true).top)+5;
obj.left = Math.max(obj.left, obj.left-obj.getBoundingRect(true).left)+5;
}
// bot-right corner
if(obj.getBoundingRect(true).top+obj.getBoundingRect(true).height > maxHeight || obj.getBoundingRect(true).left+obj.getBoundingRect(true).width > maxWidth){
obj.top = Math.min(obj.top, (maxHeight-5)-obj.getBoundingRect(true).height+obj.top-obj.getBoundingRect(true).top);
obj.left = Math.min(obj.left, (maxWidth-5)-obj.getBoundingRect(true).width+obj.left-obj.getBoundingRect(true).left);
}
canvas.on('mouse:down', function(e){
var evt = e.e
this.lastPosX = evt.clientX;
this.lastPosY = evt.clientY;
});
canvas.on('mouse:move', function (e) {
if (global_panning && e && e.e && global_mover) {
zoom = canvas.getZoom();
if(zoom<0.4){
this.viewportTransform[4] = 200 - maxWidth * zoom /2;
this.viewportTransform[5] = 200 - maxHeight * zoom /2;
}else{
this.viewportTransform[4] += e.e.clientX - this.lastPosX;
this.viewportTransform[5] += e.e.clientY - this.lastPosY;
if (this.viewportTransform[4] >= 0) {
this.viewportTransform[4] = 0;
} else if (this.viewportTransform[4] < canvas.getWidth() - maxWidth * zoom) {
this.viewportTransform[4] = canvas.getWidth() - maxWidth * zoom;
}
if (this.viewportTransform[5] >= 0) {
this.viewportTransform[5] = 0;
} else if (this.viewportTransform[5] < canvas.getHeight() - maxHeight *zoom) {
this.viewportTransform[5] = canvas.getHeight() - maxHeight * zoom;
}
//console.log(this.viewportTransform);
}
this.requestRenderAll();
this.lastPosX = e.e.clientX;
this.lastPosY = e.e.clientY;
}
});
在 fabricJS 中,我使用了教程 5 中 Zoom 演示中的代码,根据我的需要对其进行自定义。在我的例子中,限制的大小由用户决定,所以我定义了一个变量,它收集该值,并替换我的 FabricJS 实例中的画布大小的值。
我希望这可以帮助你,在这个话题上需要帮助的人