0

Sorry for my bad English. I want to learn the use of fabric.js.

For the basic commands I haven't had any problem. But I now have this problem.

I have two button in the index.html file :

<tr>
            <td><button id="caricarettangolo" onMouseDown="caricarettangolo(), caricatutto()" type="button"><img src="immagini/prodotti/rettangolo.svg" /></button></td></tr>
            <tr>
          <td><button type="button" onMouseDown="caricastella()"><img src="immagini/prodotti/stella.svg" /></button></td></tr>
        </tr>

and external js file with this code

    function caricacerchio() {
  var canvas = new fabric.Canvas('canvas');
  var circle = new fabric.Circle({
  radius: 20, fill: 'green', left: 100, top: 100
});
    canvas.getObjects();
    canvas.add(circle);
    canvas.selection = false; 
    canvas.renderAll();
    canvas.calcOffset(); 
    };

function caricarettangolo() {
    var canvas = new fabric.Canvas('canvas');
    var rect = new fabric.Rect({
    width: 50,
    height: 50,
    left: 50,
    top: 50,
    fill: 'rgb(255,0,0)'
});
    canvas.getObjects();
    canvas.add(rect);
    canvas.selection = true;
    canvas.renderAll();
    canvas.calcOffset(); 
    };

But I don't have the solution for seeing two elements.

I see the first element, next, if i push another button, this completely clears the canvas and adds the new element, but clears the first element. If I click in the canvas square, return the first created element. Why? As I can see when I have two different buttons two different element? Thanks.

1 Answer 1

1

You're rendering on a new canvas every time you press the button:

 var canvas = new fabric.Canvas('canvas');

You want to render the canvas outside of your functions

 window.canvas = new fabric.Canvas('canvas');

then use the buttons to add them.

You can see an example here: http://jsfiddle.net/33MME/2/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.