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?
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
add a comment |
up vote
0
down vote
favorite
Why does the following rect and lines not match up?
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
why you are zooming the canvascanvas.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 becausestrokeWidth
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Why does the following rect and lines not match up?
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
Why does the following rect and lines not match up?
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
fabricjs
asked Nov 8 at 11:50
Andre Helberg
3991316
3991316
why you are zooming the canvascanvas.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 becausestrokeWidth
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
add a comment |
why you are zooming the canvascanvas.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 becausestrokeWidth
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
add a comment |
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>
add a comment |
up vote
0
down vote
There is no problem:
[http://jsfiddle.net/odwg3nsq/]
You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.
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
add a comment |
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.
Good guess though :) made me double check the docs
– Andre Helberg
Nov 8 at 15:10
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
edited Nov 8 at 14:43
answered Nov 8 at 14:31
Durga
9,46021030
9,46021030
add a comment |
add a comment |
up vote
0
down vote
There is no problem:
[http://jsfiddle.net/odwg3nsq/]
You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.
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
add a comment |
up vote
0
down vote
There is no problem:
[http://jsfiddle.net/odwg3nsq/]
You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.
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
add a comment |
up vote
0
down vote
up vote
0
down vote
There is no problem:
[http://jsfiddle.net/odwg3nsq/]
You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.
There is no problem:
[http://jsfiddle.net/odwg3nsq/]
You could set the padding/margin of the outer container, but in the canvas everything is ok with the rectangle.
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
add a comment |
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
add a comment |
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.
Good guess though :) made me double check the docs
– Andre Helberg
Nov 8 at 15:10
add a comment |
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.
Good guess though :) made me double check the docs
– Andre Helberg
Nov 8 at 15:10
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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