如何利用织物画布上的背景图像获取绘图路径中的图像
how to get the image in drawing path with background image in fabric canvas?
我想要套索裁剪织物帆布。现在我可以在画布上画路径。但我不知道如何切割路径与背景图像。我有办法保存剪切的图像吗?这是我的代码
const canvas = document.getElementById('c');
const ctx = canvas.getContext('2d');
const img = document.createElement('IMG');
const base64data = canvas.toDataURL("image/jpeg")
img.onload = function() {
const OwnCanv = new fabric.Canvas('c', {
isDrawingMode: true
});
canvas.width = img.width;
canvas.height = img.height;
const imgInstance = new fabric.Image(img, {
async: false,
left: 0,
top: 0,
lockMovementX: true,
lockMovementY: true
});
OwnCanv.add(imgInstance);
OwnCanv.freeDrawingBrush.color = "purple"
OwnCanv.freeDrawingBrush.width = 5
OwnCanv.on('path:created', function(option) {
const path = option.path;
OwnCanv.isDrawingMode = false;
OwnCanv.remove(imgInstance);
OwnCanv.remove(path);
OwnCanv.clipTo = function(ctx) {
path.render(ctx);
};
OwnCanv.add(imgInstance);
});
}
img.src = base64data
}
你可以这样做:
OwnCanv.on('path:created', function(option) {
var path = option.path;
imgInstance.clipTo = function(ctx) {
//save context state
ctx.save();
//reset context tranformation matrix
ctx.setTransform(1,0,0,1,0,0);
//render the path
path.render(ctx);
//restore context state
ctx.restore();
};
OwnCanv.renderAll();
});
请看这里:https://jsfiddle.net/f82omjsr/
相关文章:
- 在视频和图像背景之间切换
- 带有图像背景的简单倒数计时器
- 无法设置图像背景图像
- 使用jQuery的透明图像背景
- 加快图像背景的页面加载速度
- HTML5 画布 - 如何在图像背景上画一条线
- IE8(Javascript和CSS)中带有拉伸BG的渐变图像背景
- 需要有关所选图像背景颜色的帮助.并将背景去除到所选择的其他图像
- 透明图像背景html5画布
- 你能帮我最大化我的图像背景吗
- 实现基于浏览器宽度的大图像背景拉伸的最简单方法
- Javascript改变图像/背景图像不工作
- 使悬停时的图像背景变暗,不影响文字
- 如何在javascript中扩展画布图像's背景并转换为文件对象
- JQuery移动设置图像背景与CSS
- 向图像(背景图像)添加超链接
- 课程互动360视频/图像背景在html5引导
- 在jQM中,为页面设置全屏CSS图像背景的正确/正确/实际方法是什么?
- 链接上随机图像背景
- 在JavaScript上设置图像背景形状