-1
A
回答
1
Here you can find solution :
On this solution I had included corner size to avoid object placed out of canvas while dragging, scaling.
var canvas = window._canvas = new fabric.Canvas('c');
canvas.selection = false;
fabric.Object.prototype.set({
transparentCorners: false,
cornerColor: 'red',
cornerSize: 12,
padding: 0
});
text = new fabric.Text('Sample',{
top: canvas.height/2,
left: canvas.width/2,
fill: '#000000'
});
canvas.add(text);
canvas.setActiveObject(text);
canvas.observe('object:scaling', function (e) {
var obj = e.target;
if(obj.getHeight() > obj.canvas.height || obj.getWidth() > obj.canvas.width){
obj.setScaleY(obj.originalState.scaleY);
obj.setScaleX(obj.originalState.scaleX);
}
obj.setCoords();
if(obj.getBoundingRect().top - (obj.cornerSize/2) < 0 ||
obj.getBoundingRect().left - (obj.cornerSize/2) < 0) {
obj.top = Math.max(obj.top, obj.top-obj.getBoundingRect().top + (obj.cornerSize/2));
obj.left = Math.max(obj.left, obj.left-obj.getBoundingRect().left + (obj.cornerSize/2));
}
if(obj.getBoundingRect().top+obj.getBoundingRect().height + obj.cornerSize > obj.canvas.height || obj.getBoundingRect().left+obj.getBoundingRect().width + obj.cornerSize > obj.canvas.width) {
obj.top = Math.min(obj.top, obj.canvas.height-obj.getBoundingRect().height+obj.top-obj.getBoundingRect().top - obj.cornerSize/2);
obj.left = Math.min(obj.left, obj.canvas.width-obj.getBoundingRect().width+obj.left-obj.getBoundingRect().left - obj.cornerSize /2);
}
});
canvas.observe('object:moving', function (e) {
var obj = e.target;
if(obj.getHeight() > obj.canvas.height || obj.getWidth() > obj.canvas.width){
obj.setScaleY(obj.originalState.scaleY);
obj.setScaleX(obj.originalState.scaleX);
}
obj.setCoords();
if(obj.getBoundingRect().top - (obj.cornerSize/2) < 0 ||
obj.getBoundingRect().left - (obj.cornerSize/2) < 0) {
obj.top = Math.max(obj.top, obj.top-obj.getBoundingRect().top + (obj.cornerSize/2));
obj.left = Math.max(obj.left, obj.left-obj.getBoundingRect().left + (obj.cornerSize/2));
}
if(obj.getBoundingRect().top+obj.getBoundingRect().height + obj.cornerSize > obj.canvas.height || obj.getBoundingRect().left+obj.getBoundingRect().width + obj.cornerSize > obj.canvas.width) {
obj.top = Math.min(obj.top, obj.canvas.height-obj.getBoundingRect().height+obj.top-obj.getBoundingRect().top - obj.cornerSize/2);
obj.left = Math.min(obj.left, obj.canvas.width-obj.getBoundingRect().width+obj.left-obj.getBoundingRect().left - obj.cornerSize /2);
}
});
相关问题
- 1. 如何在画布上制作HTML5可拖动对象?
- 2. 限制对象在画布中的移动
- 3. HTML5画布:使对象仅在画布的特定区域中可拖动。
- 4. jqueryui如何设置可拖动对象的限制?
- 5. 动画对象的画布
- 6. 在FabricJS画布上拖放对象
- 7. 如何为'Raphael'画布设置动画?
- 8. canvas/raphael.js - 在画布上为对象设置动画
- 9. 可拖动的对象和画布在循环中
- 10. html5 - 拖动画布
- 11. 如何在画布上设置对象的光标?
- 12. 设置对象拖动极限Fabric.js
- 13. 缩放面料js画布及其所有对象
- 14. 将SVG对象从一个画布拖到另一个画布
- 15. 如何复制画布中的动画
- 16. 如何在布料js中将自由绘制模式设置为圆形?
- 17. 限制画布中绘制图像对象的区域HTML5
- 18. JavaScript拖动绘制的画布元素
- 19. 获取相对于画布拖动后的画布元素的位置
- 20. 如何链接P5.js设置并使用html画布绘制?
- 21. 设置平移/拖动Zoomable自定义相对布局的边界限制
- 22. 我的拖动对象不会出现在画布上?
- 23. HTML画布:如何设置边框限制?
- 24. 如何在另一个画布中包含拖放的画布拖放?
- 25. 没有内容限制的拖动画布
- 26. 获得画布对象的位置和移动到画布触摸位置在HTML 5/JS
- 27. 在Android画布上设置动画点
- 28. 如何在画布上绘制画布
- 29. 如何在可拖动的画布上制作矩形?
- 30. ASP.NET在画布中互动对象
感谢各位为我工作,但我问你getBoundingRect获得矩形画布的区域,但我剪辑区域是形状,因此可以在此代码中实现形状 – Rajiv
嗨拉吉夫,我可以有你的样品小提琴。 – Mullainathan