Joh*_*ohn 18 javascript canvas fabricjs
如何使用fabric.js在画布上加载的图像上实现裁剪工具?我在画布上加载了一个图像.现在我想实现裁剪工具,允许用户裁剪图像并在完成后将其重新加载到画布上.
Kir*_*rby 13
(这个答案是对汤姆答案中的小提琴的一个迭代.谢谢你,汤姆,让我在路上.)
您可以使用fabric.Object.clipTo()或fabric.Object.toDataURL()在Fabric.js中裁剪.该clipTo()方法保留原始图像并通过蒙版显示裁剪.该toDataURL()方法创建一个新图像.
我的完整示例使用了该clipTo()方法.我在最后显示了一小段代码,显示了该toDataURL()方法.
解决方案1
摘要
差异来自汤姆的回答
在汤姆的回答中,有一些我希望改变的小事.所以在我的例子中,差异是
裁剪框从左到右,从右到左工作(汤姆的作品从右到左)
您有多个机会绘制裁剪框(尝试在Tom的原因框中重新绘制裁剪框以跳转)
适用于Fabric.js v1.5.0
更少的代码.
代码
// set to the event when the user pressed the mouse button down
var mouseDown;
// only allow one crop. turn it off after that
var disabled = false;
var rectangle = new fabric.Rect({
fill: 'transparent',
stroke: '#ccc',
strokeDashArray: [2, 2],
visible: false
});
var container = document.getElementById('canvas').getBoundingClientRect();
var canvas = new fabric.Canvas('canvas');
canvas.add(rectangle);
var image;
fabric.util.loadImage("http://fabricjs.com/lib/pug.jpg", function(img) {
image = new fabric.Image(img);
image.selectable = false;
canvas.setWidth(image.getWidth());
canvas.setHeight(image.getHeight());
canvas.add(image);
canvas.centerObject(image);
canvas.renderAll();
});
// capture the event when the user clicks the mouse button down
canvas.on("mouse:down", function(event) {
if(!disabled) {
rectangle.width = 2;
rectangle.height = 2;
rectangle.left = event.e.pageX - container.left;
rectangle.top = event.e.pageY - container.top;
rectangle.visible = true;
mouseDown = event.e;
canvas.bringToFront(rectangle);
}
});
// draw the rectangle as the mouse is moved after a down click
canvas.on("mouse:move", function(event) {
if(mouseDown && !disabled) {
rectangle.width = event.e.pageX - mouseDown.pageX;
rectangle.height = event.e.pageY - mouseDown.pageY;
canvas.renderAll();
}
});
// when mouse click is released, end cropping mode
canvas.on("mouse:up", function() {
mouseDown = null;
});
$('#cropB').on('click', function() {
image.clipTo = function(ctx) {
// origin is the center of the image
var x = rectangle.left - image.getWidth() / 2;
var y = rectangle.top - image.getHeight() / 2;
ctx.rect(x, y, rectangle.width, rectangle.height);
};
image.selectable = true;
disabled = true;
rectangle.visible = false;
canvas.renderAll();
});Run Code Online (Sandbox Code Playgroud)
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js" type="text/javascript"></script>
</head>
<body>
<canvas style="border: 1px solid black" id="canvas"></canvas>
<button id=cropB>crop</button>
</body>Run Code Online (Sandbox Code Playgroud)
解决方案2
或者,clipTo()我们可以使用生成新图像,而不是像上面那样使用toDataURL().像这样的东西
$('#cropB').on('click', function() {
image.selectable = true;
disabled = true;
rectangle.visible = false;
var cropped = new Image();
cropped.src = canvas.toDataURL({
left: rectangle.left,
top: rectangle.top,
width: rectangle.width,
height: rectangle.height
});
cropped.onload = function() {
canvas.clear();
image = new fabric.Image(cropped);
image.left = rectangle.left;
image.top = rectangle.top;
image.setCoords();
canvas.add(image);
canvas.renderAll();
};
});
Run Code Online (Sandbox Code Playgroud)
Tom*_*Tom 12
综上所述
el.selectable = false对不起,让我解释一下.该ctx.rect会从对象的中心点裁剪图像.还scaleX应该考虑这个因素.
x = select_el.left - object.left;
y = select_el.top - object.top;
x *= 1 / scale;
y *= 1 / scale;
width = select_el.width * 1 / scale;
heigh = select_el.height * 1 / scale;
object.clipTo = function (ctx) {
ctx.rect (x, y, width, height);
}
Run Code Online (Sandbox Code Playgroud)
完整示例:http://jsfiddle.net/hellomaya/kNEaX/1/
并查看此http://jsfiddle.net/hellomaya/hzqrM/以生成选择框.以及Fabric事件的参考:https://github.com/kangax/fabric.js/wiki/Working-with-events
| 归档时间: |
|
| 查看次数: |
21376 次 |
| 最近记录: |