gpt4 book ai didi

javascript - 如何删除 fabric.js 中的绘图?

转载 作者:行者123 更新时间:2023-11-30 06:51:37 27 4
gpt4 key购买 nike

有一个带有对象的 Canvas 。需要在物体上绘图,还需要橡皮擦功能。查看 Canvas 屏幕截图: canvas where able drawing function

我看到 fabric.js 没有删除功能。有什么方法可以用橡皮擦擦掉画的吗?

谢谢

最佳答案

var toBase64Btn = document.getElementById("toBase64");
var canvas = document.getElementById("a");
var ctx = canvas.getContext("2d");
var drawing = false;
var drawingMode = "add";
var pencilWidth = 25;
var base64Str = "";
var fabCan = new fabric.Canvas("b");
var fillColor = "rgba(255,0,0,.5)";

var getMouseCoordinate = function (evt) {
return {
x : evt.pageX - this.offsetLeft,
y : evt.pageY - this.offsetTop
};
};

canvas.onmousedown = function (e) {
if (drawing) return false;
drawing = true;
var mouse = getMouseCoordinate.call(this, e);

ctx.globalCompositeOperation = drawingMode === "add" ? "xor" : "destination-out";
ctx.fillStyle = drawingMode === "add" ? fillColor : "rgba(0,0,0,1)";

ctx.beginPath();
ctx.arc(mouse.x, mouse.y, pencilWidth, 0, 2*Math.PI);
ctx.fill();
ctx.closePath();
};

canvas.onmousemove = function (e) {
if (!drawing) return false;
var mouse = getMouseCoordinate.call(this, e);
ctx.beginPath();
ctx.arc(mouse.x, mouse.y, pencilWidth, 0, 2*Math.PI);
ctx.fill();
ctx.closePath();
};

canvas.onmouseup = function (e) {
if (!drawing) return false;
drawing = false;
};

toBase64Btn.onclick = function () {
if (drawing) return false;

var img = document.createElement("img");

img.onload = function () {

var group = [];

ctx.clearRect(0,0,canvas.width, canvas.height);
fabCan.add(new fabric.Image(this));

fabCan.getObjects().forEach(function (obj) {
obj.globalCompositeOperation = "xor";
group.push(obj);
});

fabCan.clear().add(new fabric.Group(group)).renderAll();
};

img.src = canvas.toDataURL("image/png");
};

document.getElementById("erase").onchange = function () {
drawingMode = this.checked ? "delete" : "add";
console.warn(drawingMode);
};

fabCan.add(new fabric.Rect({
width: 100,
height: 100,
fill: fillColor
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.6.3/fabric.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Erase: <input id="erase" type="checkbox">
<button id="toBase64" type="button">addToAOILayer</button><br/>
<canvas id="a" width="400" height="300"></canvas>
<canvas id="b" width="400" height="300"></canvas>

我的代码在这里HTML

Erase: <input id="erase" type="checkbox">
<button id="toBase64" type="button">addToAOILayer</button><br/>
<canvas id="a" width="400" height="300"></canvas>
<canvas id="b" width="400" height="300"></canvas>

结构.js

var toBase64Btn = document.getElementById("toBase64");
var canvas = document.getElementById("a");
var ctx = canvas.getContext("2d");
var drawing = false;
var drawingMode = "add";
var pencilWidth = 25;
var base64Str = "";
var fabCan = new fabric.Canvas("b");
var fillColor = "rgba(255,0,0,.5)";

var getMouseCoordinate = function (evt) {
return {
x : evt.pageX - this.offsetLeft,
y : evt.pageY - this.offsetTop
};
};

canvas.onmousedown = function (e) {
if (drawing) return false;
drawing = true;
var mouse = getMouseCoordinate.call(this, e);

ctx.globalCompositeOperation = drawingMode === "add" ? "xor" : "destination-out";
ctx.fillStyle = drawingMode === "add" ? fillColor : "rgba(0,0,0,1)";

ctx.beginPath();
ctx.arc(mouse.x, mouse.y, pencilWidth, 0, 2*Math.PI);
ctx.fill();
ctx.closePath();
};

canvas.onmousemove = function (e) {
if (!drawing) return false;
var mouse = getMouseCoordinate.call(this, e);
ctx.beginPath();
ctx.arc(mouse.x, mouse.y, pencilWidth, 0, 2*Math.PI);
ctx.fill();
ctx.closePath();
};

canvas.onmouseup = function (e) {
if (!drawing) return false;
drawing = false;
};

toBase64Btn.onclick = function () {
if (drawing) return false;

var img = document.createElement("img");

img.onload = function () {

var group = [];

ctx.clearRect(0,0,canvas.width, canvas.height);
fabCan.add(new fabric.Image(this));

fabCan.getObjects().forEach(function (obj) {
obj.globalCompositeOperation = "xor";
group.push(obj);
});

fabCan.clear().add(new fabric.Group(group)).renderAll();
};

img.src = canvas.toDataURL("image/png");
};

document.getElementById("erase").onchange = function () {
drawingMode = this.checked ? "delete" : "add";
console.warn(drawingMode);
};

fabCan.add(new fabric.Rect({
width: 100,
height: 100,
fill: fillColor
}));

我觉得对你有帮助

关于javascript - 如何删除 fabric.js 中的绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42292994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com