Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

338
Views
delete selected objects in fabric.js

My aim is to get three buttons in fabric.js: "Copy", "Paste", "Delete".

Button with name "Copy" should copy selected objects. Button with name "Paste" should paste copied objects. Button with name "Delete" should delete selected objects from canvas.

"Copy" and "paste" buttons helps to work with selected objectives. Buttons with copy and paste works well at that time remove button shows error:

How should I solve my problem?

function Copy()
{
    canvas.getActiveObject().clone(function(cloned)
  {
        _clipboard = cloned;
    });
}
  function Delete() 
  {
    canvas.getActiveObject().remove();
  }
  
        function Paste() {
            
            _clipboard.clone(function(clonedObj) {
                canvas.discardActiveObject();
                clonedObj.set({
                    left: clonedObj.left + 10,
                    top: clonedObj.top + 10,
                    evented: true,
                });
                if (clonedObj.type === 'activeSelection') {
                    
                    clonedObj.canvas = canvas;
                    clonedObj.forEachObject(function(obj) {
                        canvas.add(obj);
                    });
                    clonedObj.setCoords();
                } else {
                    canvas.add(clonedObj);
                }
                _clipboard.top += 10;
                _clipboard.left += 10;
                canvas.setActiveObject(clonedObj);
                canvas.requestRenderAll();
            });
        }

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

var rect = new fabric.Rect({
    left: 100,
    top: 50,
    fill: '#D81B60',
    width: 100,
    height: 100,
    strokeWidth: 2,
    stroke: "#880E4F",
    rx: 10,
    ry: 10,
    angle: 45,
    hasControls: true
});

canvas.add(rect);
<div>

    <button type="button" onclick="Copy()">copy</button>
    <button type="button" onclick="Paste()">paste</button>
    <button type="button" onclick="Delete()">delete</button>

  </div>
  <div style="display:flex;flex-direction:row;">
    <div>
      <canvas id="c" width="1300" height="1300"></canvas>
    </div>

  </div>
  
  <script src="https://unpkg.com/fabric@4.6.0/dist/fabric.js"></script>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The error is clear you are getting:
remove is not a function

You should be using canvas.remove(object) here is a working sample:

function Delete() {
  var active = canvas.getActiveObject()
  if (active) {
    canvas.remove(active)
    if (active.type == "activeSelection") {
      active.getObjects().forEach(x => canvas.remove(x))
      canvas.discardActiveObject().renderAll()
    }
  }
}

var canvas = this.__canvas = new fabric.Canvas('c');
var rect = new fabric.Rect({
  left: 100,
  top: 50,
  fill: '#D81B60',
  width: 100,
  height: 100,
  strokeWidth: 2,
  stroke: "#880E4F",
  rx: 10,
  ry: 10,
  angle: 45,
  hasControls: true
});
canvas.add(rect);

var rect2 = new fabric.Rect({
  left: 160,
  top: 10,
  fill: '#D81B60',
  width: 60,
  height: 60,
  strokeWidth: 2,
  stroke: "#880E4F",
  hasControls: true
});
canvas.add(rect2);
<div>
  <button type="button" onclick="Delete()">delete</button>
</div>
<div style="display:flex;flex-direction:row;">
  <div>
    <canvas id="c" width="1300" height="1300"></canvas>
  </div>
</div>

<script src="https://unpkg.com/fabric@4.6.0/dist/fabric.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!