【问题标题】:Is it possible to force deselection of all object onclick?是否可以强制取消选择所有对象 onclick?
【发布时间】:2017-11-16 02:33:28
【问题描述】:

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

目前打印我正在使用以下内容:

// 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>

来自打印对话:

提前感谢您的帮助!

【问题讨论】:

    标签: javascript html canvas fabricjs


    【解决方案1】:

    你可以使用

    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>

    【讨论】:

    • 我如何使用您通过我的点击按钮提供的内容?我尝试将功能添加到我没有运气的功能中
    猜你喜欢
    • 2011-07-09
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多