Fabric.js: canvas.clear() makes the canvas background transparent

Created on 18 Oct 2016  Â·  12Comments  Â·  Source: fabricjs/fabric.js

Version

1.6.5+

Test Case

https://jsfiddle.net/6z5vay2e/

Steps to reproduce

Clear the canvas using canvas.clear()

Expected Behavior

canvas.clear() does not affect the background color

Actual Behavior

canvas.clear() removes the canvas background color

Most helpful comment

concat is not needed, since long getObjects() returns always a new array.

concat() with no arguments is a quick way to return a copy of an array, similarly to slice(0).

All 12 comments

It's possible to set the background colour and render the canvas after it has been cleared, I'm just wondering why this is happening now. This wasn't present in earlier versions.

Hello,

This is normal behavior : http://fabricjs.com/docs/fabric.Canvas.html#clear

Clears all contexts (background, main, top) of an instance

I prefer that it keeps the background...
I wondering too

Well, i did not write that function in first place.
One user some weeks ago came here and complained the opposite.
Then i read the jsdoc part and it said that cleared everything, and i just did it.

So in my opinion, clear is ok like it is.
On canvas we objects, background, backgroundColor, overlya, overlaycolor.
Everyone of them can be cleared setting a property, so either clear does everything or it does not have any reason to exist.

I m open to discussion anyway.

I would definitely say if canvas.clear() clears all properties, then there needs to be another function, like canvas.clearObjects(), to handle the far-more-typical use case of simply removing all objects from the canvas.

does not canvas.remove(canvas.getObjects()) work?

On Oct 18, 2016 18:40, "stephenkillingsworth" [email protected]
wrote:

I would definitely say if canvas.clear() clears all properties, then there
needs to be another function, like canvas.clearObjects(), to handle the
far-more-typical use case of simply removing all objects from the canvas.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/kangax/fabric.js/issues/3356#issuecomment-254566285,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABI4QGnF3S6PKdD7xWBJ9dWiZ83Ka1DZks5q1PaBgaJpZM4KZ80H
.

no, it does not, and you cannot do

canvas.forEachObject(function(o){
  o.remove();
})

is really a minor annoyance i have to say.

In my mind I would think the clear() method would delete all objects and not the canvas properties. There is some subjectivity here, but I understand that is by design and so is not a bug.

Can we have a parameter for this without removing the background ? Like in previous 1.5 version ? http://jsfiddle.net/InferOn/fx05t621/2/

Well to remove all objects and nothing else you can still do canvas.remove(...canvas.getObjects().concat()) or canvas.remove.apply(canvas, canvas.getObjects().concat()) too many flags or options are complicated.

Well to remove all objects and nothing else you can still do canvas.remove(...canvas.getObjects().concat()) or canvas.remove.apply(canvas, canvas.getObjects().concat())

thanks for the snippet, can we add this to the docs of canvas.remove() ?

or add a "trivial convenience function", like stephenkillingsworth suggested,
"like canvas.clearObjects()" or like canvas.removeAll()

also, why do we need concat() ?
why cant we just say canvas.remove(...canvas.getObjects()) ?
concat makes no difference in my case

also we should set canvas.renderOnAddRemove = false for this, no?

concat is not needed, since long getObjects() returns always a new array.

concat() with no arguments is a quick way to return a copy of an array, similarly to slice(0).

why not just create a new canvas and initialize with original properties

Was this page helpful?
0 / 5 - 0 ratings

Related issues

raichu picture raichu  Â·  4Comments

bhaskardas9475 picture bhaskardas9475  Â·  4Comments

f987002856 picture f987002856  Â·  3Comments

medialwerk picture medialwerk  Â·  5Comments

eugene-g13 picture eugene-g13  Â·  3Comments