Why do lines and rects not match up in Fabricjs











up vote
0
down vote

favorite












Why does the following rect and lines not match up?
off-center rect



The lines are 1px apart, 5px long, starting from (0,0)



The rect is top: 0, left: 0, width: 1, height: 1



Running fiddle



Am I missing something with lines?










share|improve this question






















  • why you are zooming the canvas canvas.setZoom(50);? is there any requirement? check grid example.
    – Durga
    Nov 8 at 13:33












  • @Durga I've had discrepancies with how fonts are rendered, I've narrowed it down this issue. I'm zooming in to show the issue, we won't see half a pixel at 0 zoom.
    – Andre Helberg
    Nov 8 at 14:21










  • Its because strokeWidth of rect object, make it zero, it will work as expected. jsfiddle
    – Durga
    Nov 8 at 14:24












  • @Durga That did it! Please make an answer so I can mark it as correct
    – Andre Helberg
    Nov 8 at 14:26















up vote
0
down vote

favorite












Why does the following rect and lines not match up?
off-center rect



The lines are 1px apart, 5px long, starting from (0,0)



The rect is top: 0, left: 0, width: 1, height: 1



Running fiddle



Am I missing something with lines?










share|improve this question






















  • why you are zooming the canvas canvas.setZoom(50);? is there any requirement? check grid example.
    – Durga
    Nov 8 at 13:33












  • @Durga I've had discrepancies with how fonts are rendered, I've narrowed it down this issue. I'm zooming in to show the issue, we won't see half a pixel at 0 zoom.
    – Andre Helberg
    Nov 8 at 14:21










  • Its because strokeWidth of rect object, make it zero, it will work as expected. jsfiddle
    – Durga
    Nov 8 at 14:24












  • @Durga That did it! Please make an answer so I can mark it as correct
    – Andre Helberg
    Nov 8 at 14:26













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Why does the following rect and lines not match up?
off-center rect



The lines are 1px apart, 5px long, starting from (0,0)



The rect is top: 0, left: 0, width: 1, height: 1



Running fiddle



Am I missing something with lines?










share|improve this question













Why does the following rect and lines not match up?
off-center rect



The lines are 1px apart, 5px long, starting from (0,0)



The rect is top: 0, left: 0, width: 1, height: 1



Running fiddle



Am I missing something with lines?







fabricjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 11:50









Andre Helberg

3991316




3991316












  • why you are zooming the canvas canvas.setZoom(50);? is there any requirement? check grid example.
    – Durga
    Nov 8 at 13:33












  • @Durga I've had discrepancies with how fonts are rendered, I've narrowed it down this issue. I'm zooming in to show the issue, we won't see half a pixel at 0 zoom.
    – Andre Helberg
    Nov 8 at 14:21










  • Its because strokeWidth of rect object, make it zero, it will work as expected. jsfiddle
    – Durga
    Nov 8 at 14:24












  • @Durga That did it! Please make an answer so I can mark it as correct
    – Andre Helberg
    Nov 8 at 14:26


















  • why you are zooming the canvas canvas.setZoom(50);? is there any requirement? check grid example.
    – Durga
    Nov 8 at 13:33












  • @Durga I've had discrepancies with how fonts are rendered, I've narrowed it down this issue. I'm zooming in to show the issue, we won't see half a pixel at 0 zoom.
    – Andre Helberg
    Nov 8 at 14:21










  • Its because strokeWidth of rect object, make it zero, it will work as expected. jsfiddle
    – Durga
    Nov 8 at 14:24












  • @Durga That did it! Please make an answer so I can mark it as correct
    – Andre Helberg
    Nov 8 at 14:26
















why you are zooming the canvas canvas.setZoom(50);? is there any requirement? check grid example.
– Durga
Nov 8 at 13:33






why you are zooming the canvas canvas.setZoom(50);? is there any requirement? check grid example.
– Durga
Nov 8 at 13:33














@Durga I've had discrepancies with how fonts are rendered, I've narrowed it down this issue. I'm zooming in to show the issue, we won't see half a pixel at 0 zoom.
– Andre Helberg
Nov 8 at 14:21




@Durga I've had discrepancies with how fonts are rendered, I've narrowed it down this issue. I'm zooming in to show the issue, we won't see half a pixel at 0 zoom.
– Andre Helberg
Nov 8 at 14:21












Its because strokeWidth of rect object, make it zero, it will work as expected. jsfiddle
– Durga
Nov 8 at 14:24






Its because strokeWidth of rect object, make it zero, it will work as expected. jsfiddle
– Durga
Nov 8 at 14:24














@Durga That did it! Please make an answer so I can mark it as correct
– Andre Helberg
Nov 8 at 14:26




@Durga That did it! Please make an answer so I can mark it as correct
– Andre Helberg
Nov 8 at 14:26












3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










The padding you got because of strokeWidth of the Rect object, make it zero, it will work as expected. jsfiddle



DEMO






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

var gridLine = function(at, stroke, canvas, length) {
var horizontal = [0, at, length, at];
var vertical = [at, 0, at, length];

let hLine = new fabric.Line(horizontal, {
strokeWidth: 0.02,
stroke,
});
let vLine = new fabric.Line(vertical, {
strokeWidth: 0.02,
stroke,
});
canvas.add(hLine);
canvas.add(vLine);
}

var showGrid = function(fabric) {
var count = 5;
var separationPx = 1;
for (var i = 0; i < count; i++) {
gridLine(i * separationPx, "silver", fabric, count);
}
}

var rect = new fabric.Rect({
top: 0,
left: 0,
width: 1,
height: 1,
fill: "red",
strokeWidth: 0
});

showGrid(canvas);
canvas.add(rect);

canvas.setZoom(50);

<script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.4/fabric.js"></script>
<canvas id="c" width="300" height="300"></canvas>








share|improve this answer






























    up vote
    0
    down vote













    There is no problem:



    [http://jsfiddle.net/odwg3nsq/]


    enter image description here



    You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.






    share|improve this answer























    • I think I'm missing something with your answer. What does your image illustrate? There is a problem, there's a half a pixel difference between the origin for the lines and the origin for the square.
      – Andre Helberg
      Nov 8 at 14:19


















    up vote
    0
    down vote













    I am a beginner to Fabricjs so there might be an easier answer. To me it looks like the "padding" (?) is what causes the problem. Look at the control borders when you select the rectangle, they do align but are bigger than your rectangle is.



    I could fix this by setting the rect to (-0.5, -0.5, 1, 1), however I think this is not the best workaround and you should probably try to adjust the padding of your rect.



    Hope it helps in a way.



    Cheers!



    Edit: It's not the padding it's the strokeWidth as Durga pointed out in the comments to the question.






    share|improve this answer























    • Good guess though :) made me double check the docs
      – Andre Helberg
      Nov 8 at 15:10











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207160%2fwhy-do-lines-and-rects-not-match-up-in-fabricjs%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    The padding you got because of strokeWidth of the Rect object, make it zero, it will work as expected. jsfiddle



    DEMO






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

    var gridLine = function(at, stroke, canvas, length) {
    var horizontal = [0, at, length, at];
    var vertical = [at, 0, at, length];

    let hLine = new fabric.Line(horizontal, {
    strokeWidth: 0.02,
    stroke,
    });
    let vLine = new fabric.Line(vertical, {
    strokeWidth: 0.02,
    stroke,
    });
    canvas.add(hLine);
    canvas.add(vLine);
    }

    var showGrid = function(fabric) {
    var count = 5;
    var separationPx = 1;
    for (var i = 0; i < count; i++) {
    gridLine(i * separationPx, "silver", fabric, count);
    }
    }

    var rect = new fabric.Rect({
    top: 0,
    left: 0,
    width: 1,
    height: 1,
    fill: "red",
    strokeWidth: 0
    });

    showGrid(canvas);
    canvas.add(rect);

    canvas.setZoom(50);

    <script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.4/fabric.js"></script>
    <canvas id="c" width="300" height="300"></canvas>








    share|improve this answer



























      up vote
      1
      down vote



      accepted










      The padding you got because of strokeWidth of the Rect object, make it zero, it will work as expected. jsfiddle



      DEMO






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

      var gridLine = function(at, stroke, canvas, length) {
      var horizontal = [0, at, length, at];
      var vertical = [at, 0, at, length];

      let hLine = new fabric.Line(horizontal, {
      strokeWidth: 0.02,
      stroke,
      });
      let vLine = new fabric.Line(vertical, {
      strokeWidth: 0.02,
      stroke,
      });
      canvas.add(hLine);
      canvas.add(vLine);
      }

      var showGrid = function(fabric) {
      var count = 5;
      var separationPx = 1;
      for (var i = 0; i < count; i++) {
      gridLine(i * separationPx, "silver", fabric, count);
      }
      }

      var rect = new fabric.Rect({
      top: 0,
      left: 0,
      width: 1,
      height: 1,
      fill: "red",
      strokeWidth: 0
      });

      showGrid(canvas);
      canvas.add(rect);

      canvas.setZoom(50);

      <script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.4/fabric.js"></script>
      <canvas id="c" width="300" height="300"></canvas>








      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        The padding you got because of strokeWidth of the Rect object, make it zero, it will work as expected. jsfiddle



        DEMO






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

        var gridLine = function(at, stroke, canvas, length) {
        var horizontal = [0, at, length, at];
        var vertical = [at, 0, at, length];

        let hLine = new fabric.Line(horizontal, {
        strokeWidth: 0.02,
        stroke,
        });
        let vLine = new fabric.Line(vertical, {
        strokeWidth: 0.02,
        stroke,
        });
        canvas.add(hLine);
        canvas.add(vLine);
        }

        var showGrid = function(fabric) {
        var count = 5;
        var separationPx = 1;
        for (var i = 0; i < count; i++) {
        gridLine(i * separationPx, "silver", fabric, count);
        }
        }

        var rect = new fabric.Rect({
        top: 0,
        left: 0,
        width: 1,
        height: 1,
        fill: "red",
        strokeWidth: 0
        });

        showGrid(canvas);
        canvas.add(rect);

        canvas.setZoom(50);

        <script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.4/fabric.js"></script>
        <canvas id="c" width="300" height="300"></canvas>








        share|improve this answer














        The padding you got because of strokeWidth of the Rect object, make it zero, it will work as expected. jsfiddle



        DEMO






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

        var gridLine = function(at, stroke, canvas, length) {
        var horizontal = [0, at, length, at];
        var vertical = [at, 0, at, length];

        let hLine = new fabric.Line(horizontal, {
        strokeWidth: 0.02,
        stroke,
        });
        let vLine = new fabric.Line(vertical, {
        strokeWidth: 0.02,
        stroke,
        });
        canvas.add(hLine);
        canvas.add(vLine);
        }

        var showGrid = function(fabric) {
        var count = 5;
        var separationPx = 1;
        for (var i = 0; i < count; i++) {
        gridLine(i * separationPx, "silver", fabric, count);
        }
        }

        var rect = new fabric.Rect({
        top: 0,
        left: 0,
        width: 1,
        height: 1,
        fill: "red",
        strokeWidth: 0
        });

        showGrid(canvas);
        canvas.add(rect);

        canvas.setZoom(50);

        <script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.4/fabric.js"></script>
        <canvas id="c" width="300" height="300"></canvas>








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

        var gridLine = function(at, stroke, canvas, length) {
        var horizontal = [0, at, length, at];
        var vertical = [at, 0, at, length];

        let hLine = new fabric.Line(horizontal, {
        strokeWidth: 0.02,
        stroke,
        });
        let vLine = new fabric.Line(vertical, {
        strokeWidth: 0.02,
        stroke,
        });
        canvas.add(hLine);
        canvas.add(vLine);
        }

        var showGrid = function(fabric) {
        var count = 5;
        var separationPx = 1;
        for (var i = 0; i < count; i++) {
        gridLine(i * separationPx, "silver", fabric, count);
        }
        }

        var rect = new fabric.Rect({
        top: 0,
        left: 0,
        width: 1,
        height: 1,
        fill: "red",
        strokeWidth: 0
        });

        showGrid(canvas);
        canvas.add(rect);

        canvas.setZoom(50);

        <script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.4/fabric.js"></script>
        <canvas id="c" width="300" height="300"></canvas>





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

        var gridLine = function(at, stroke, canvas, length) {
        var horizontal = [0, at, length, at];
        var vertical = [at, 0, at, length];

        let hLine = new fabric.Line(horizontal, {
        strokeWidth: 0.02,
        stroke,
        });
        let vLine = new fabric.Line(vertical, {
        strokeWidth: 0.02,
        stroke,
        });
        canvas.add(hLine);
        canvas.add(vLine);
        }

        var showGrid = function(fabric) {
        var count = 5;
        var separationPx = 1;
        for (var i = 0; i < count; i++) {
        gridLine(i * separationPx, "silver", fabric, count);
        }
        }

        var rect = new fabric.Rect({
        top: 0,
        left: 0,
        width: 1,
        height: 1,
        fill: "red",
        strokeWidth: 0
        });

        showGrid(canvas);
        canvas.add(rect);

        canvas.setZoom(50);

        <script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.3.4/fabric.js"></script>
        <canvas id="c" width="300" height="300"></canvas>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 14:43

























        answered Nov 8 at 14:31









        Durga

        9,46021030




        9,46021030
























            up vote
            0
            down vote













            There is no problem:



            [http://jsfiddle.net/odwg3nsq/]


            enter image description here



            You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.






            share|improve this answer























            • I think I'm missing something with your answer. What does your image illustrate? There is a problem, there's a half a pixel difference between the origin for the lines and the origin for the square.
              – Andre Helberg
              Nov 8 at 14:19















            up vote
            0
            down vote













            There is no problem:



            [http://jsfiddle.net/odwg3nsq/]


            enter image description here



            You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.






            share|improve this answer























            • I think I'm missing something with your answer. What does your image illustrate? There is a problem, there's a half a pixel difference between the origin for the lines and the origin for the square.
              – Andre Helberg
              Nov 8 at 14:19













            up vote
            0
            down vote










            up vote
            0
            down vote









            There is no problem:



            [http://jsfiddle.net/odwg3nsq/]


            enter image description here



            You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.






            share|improve this answer














            There is no problem:



            [http://jsfiddle.net/odwg3nsq/]


            enter image description here



            You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 13:50

























            answered Nov 8 at 13:44









            Cookinski

            715




            715












            • I think I'm missing something with your answer. What does your image illustrate? There is a problem, there's a half a pixel difference between the origin for the lines and the origin for the square.
              – Andre Helberg
              Nov 8 at 14:19


















            • I think I'm missing something with your answer. What does your image illustrate? There is a problem, there's a half a pixel difference between the origin for the lines and the origin for the square.
              – Andre Helberg
              Nov 8 at 14:19
















            I think I'm missing something with your answer. What does your image illustrate? There is a problem, there's a half a pixel difference between the origin for the lines and the origin for the square.
            – Andre Helberg
            Nov 8 at 14:19




            I think I'm missing something with your answer. What does your image illustrate? There is a problem, there's a half a pixel difference between the origin for the lines and the origin for the square.
            – Andre Helberg
            Nov 8 at 14:19










            up vote
            0
            down vote













            I am a beginner to Fabricjs so there might be an easier answer. To me it looks like the "padding" (?) is what causes the problem. Look at the control borders when you select the rectangle, they do align but are bigger than your rectangle is.



            I could fix this by setting the rect to (-0.5, -0.5, 1, 1), however I think this is not the best workaround and you should probably try to adjust the padding of your rect.



            Hope it helps in a way.



            Cheers!



            Edit: It's not the padding it's the strokeWidth as Durga pointed out in the comments to the question.






            share|improve this answer























            • Good guess though :) made me double check the docs
              – Andre Helberg
              Nov 8 at 15:10















            up vote
            0
            down vote













            I am a beginner to Fabricjs so there might be an easier answer. To me it looks like the "padding" (?) is what causes the problem. Look at the control borders when you select the rectangle, they do align but are bigger than your rectangle is.



            I could fix this by setting the rect to (-0.5, -0.5, 1, 1), however I think this is not the best workaround and you should probably try to adjust the padding of your rect.



            Hope it helps in a way.



            Cheers!



            Edit: It's not the padding it's the strokeWidth as Durga pointed out in the comments to the question.






            share|improve this answer























            • Good guess though :) made me double check the docs
              – Andre Helberg
              Nov 8 at 15:10













            up vote
            0
            down vote










            up vote
            0
            down vote









            I am a beginner to Fabricjs so there might be an easier answer. To me it looks like the "padding" (?) is what causes the problem. Look at the control borders when you select the rectangle, they do align but are bigger than your rectangle is.



            I could fix this by setting the rect to (-0.5, -0.5, 1, 1), however I think this is not the best workaround and you should probably try to adjust the padding of your rect.



            Hope it helps in a way.



            Cheers!



            Edit: It's not the padding it's the strokeWidth as Durga pointed out in the comments to the question.






            share|improve this answer














            I am a beginner to Fabricjs so there might be an easier answer. To me it looks like the "padding" (?) is what causes the problem. Look at the control borders when you select the rectangle, they do align but are bigger than your rectangle is.



            I could fix this by setting the rect to (-0.5, -0.5, 1, 1), however I think this is not the best workaround and you should probably try to adjust the padding of your rect.



            Hope it helps in a way.



            Cheers!



            Edit: It's not the padding it's the strokeWidth as Durga pointed out in the comments to the question.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 14:28

























            answered Nov 8 at 13:30









            Tobias O.

            12




            12












            • Good guess though :) made me double check the docs
              – Andre Helberg
              Nov 8 at 15:10


















            • Good guess though :) made me double check the docs
              – Andre Helberg
              Nov 8 at 15:10
















            Good guess though :) made me double check the docs
            – Andre Helberg
            Nov 8 at 15:10




            Good guess though :) made me double check the docs
            – Andre Helberg
            Nov 8 at 15:10


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207160%2fwhy-do-lines-and-rects-not-match-up-in-fabricjs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)