gpt4 book ai didi

javascript - 是否可以强制取消选择所有对象 onclick?

转载 作者:行者123 更新时间:2023-11-28 17:43:44 25 4
gpt4 key购买 nike

我的问题是,当我去打印fabricjs Canvas 时,有时我会忘记取消选择一个对象,并且当它进入打印对话框时它会留下控件。有没有办法清除单击时选定的任何对象?

当前打印我使用以下内容:

// Print
function printCanvas() {
var dataUrl = document.getElementById('c').toDataURL(); //attempt to save base64 string to server using this var
var windowContent = '<!DOCTYPE html>';
windowContent += '<html>'
windowContent += '<head><title>Print canvas</title></head>';
windowContent += '<body>'
windowContent += '<img src="' + dataUrl + '" onload=window.print();window.close();>';
windowContent += '</body>';
windowContent += '</html>';
var printWin = window.open('', '', 'width=340,height=260');
printWin.document.open();
printWin.document.write(windowContent);
}
<小时/>
<button onclick="printCanvas()">Print</button>

来自打印对话框:

enter image description here

感谢您提前提供的任何帮助!

最佳答案

你可以使用

canvas.discardActiveObject();
canvas.renderAll();

(function() {
var canvas = this.__canvas = new fabric.Canvas('canvas');

// create a rectangle with a fill and a different color stroke
var rect = new fabric.Rect({
left: 50,
top: 50,
width: 50,
height: 50,
fill: 'rgba(255,127,39,1)',
stroke: 'rgba(34,177,76,1)',
strokeWidth: 5
});
canvas.add(rect);
canvas.renderAll();
$('#deselect').click(function(){
canvas.discardActiveObject();
canvas.renderAll();
});
})();
<canvas id="canvas" width="150" height="150"></canvas>
<button id="deselect">deselect</button>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js"></script>

关于javascript - 是否可以强制取消选择所有对象 onclick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47314150/

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