Draw a polygon freely using Fabric.js

Asked

Viewed 546 times

2

I’m using this code to try to draw a polygon freely: http://jsfiddle.net/e5Xth/3/

canvas.on('mouse:move', function (options) {
        if (lines[0] !== null && drawingObject.type == "roof") {
            setStartingPoint(options);
            lines[lineCounter - 1].set({
                x2: x,
                y2: y
            });
            canvas.renderAll();
        }
    });

My problem is, when I use this code on my page, it doesn’t draw correctly. When I begin to draw the polygon the lines do not appear in the place where I asked them to draw but always from the upper left field. when I click to get the polygon it is drawn in the correct location.

  • Are you using the code identical to that fiddle? This snippet you posted here doesn’t have part of the starting point, which happens in mousedown.

  • Ana, can you explain better what you mean by "he doesn’t draw correctly"? Which browser is the problem? jsFiddle works well for me...

  • Yes, I am using this jsfiddle in its entirety (http://jsfiddle.net/9VDD6/10/). A few minor differences from the first one I posted, but I don’t think the ones that caused me the problem with my page. I’m working on windows, on Chrome. When I say that you do not draw correctly on the page I mean the following situation: when I click on the "press to draw" and start drawing the lines they are not drawn from the point I want. They all leave from the upper left corner.

  • The even more curious part is that when I click again on "press to draw" to get the Poligono it appears to me correctly in the place where theoretically the lines should have been drawn.

1 answer

1

How the script works

The script consists of an editor that allows you to draw and work with geometric shapes. To do this, click on the label "Pressto Draw! And to stop, "then click on a few points inside the drawing rectangle and again on the label above. The clicked points will be closed in a polygon filled with the color purple.

I added the explanation why it might not be intuitive at first glance.

The problem

From what I understand, the problem occurs when drawing a second form, because it seems to interfere in the first drawn. The same would occur with subsequent forms.

The cause

The cause is the infamous global variables (roofPoints, lines and lineCounter).

Since they are not redefined after drawing one of the forms, the following form will include the points of the previous form. (I did not analyze all the impacts that occurred).

The solution

I took the liberty of making one Fork fiddle and apply some very simple adjustments.

The main adjustment was to restart the variables mentioned at the beginning of the drawing in a way. See the excerpt:

$("#btnRoof").click(function () {
    if (drawingObject.type == "roof") {
        drawingObject.type = "";
        canvas.remove(lines[lineCounter - 1]);
        var roof = makeRoof(roofPoints);
        canvas.add(roof);
        canvas.renderAll();
    } else {
        drawingObject.type = "roof"; // roof type
        roofPoints = [];
        lines = [];
        lineCounter = 0;            
    }
});

Completion

Avoid working with global variables as much as possible to avoid falling into this type of problem. Always use the minimum scope for variables.

For example, look at the variable roof in modified fiddle. It is declared locally as there was no need for it to be global.

I know that at some point it is important to share the variables between the various events, but try to do this by encapsulating the variables in an object that can be easily restarted.

Anyway, my intention is not to criticize, because it is certainly possible to adjust the script without difficulties. Only if the idea is to include more features and make it more complex, encapsulation will make a huge difference so it doesn’t get to the point where any maintenance makes it stop working.

  • Good afternoon, I apologise but I did not express myself in the best way. The jsfiddle works correctly, but taking the code of this jsfiddle and using it on my page it doesn’t work right when I try to draw. When I first click on "press to draw" and start drawing the lines they are not drawn from the point I define them. they start to "be born" all from the upper left field and I still can’t figure it out. this jsfiddle http://jsfiddle.net/9VDD6/10/ fully adhere to the way I am implementing on my page.

  • @Ana Qual browser?

  • Chrome-windows. but honestly I haven’t tried it yet on another.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.