Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fractional offset of inline SVG box shifts pixel grid #14

Open
subzey opened this issue Jul 21, 2015 · 2 comments
Open

Fractional offset of inline SVG box shifts pixel grid #14

subzey opened this issue Jul 21, 2015 · 2 comments

Comments

@subzey
Copy link

subzey commented Jul 21, 2015

Affects: IE 9 to Edge (ex-Spartan), Firefox

While the top-left corner of <svg> bounding box coordinates are rounded, the svg contents are rendered as if there was no rounding.

Demo: https://jsfiddle.net/subzey/8a3wa5nv/1/embedded/result/
Notice how grid becomes blurry when offset is fractional. At the same time the border is rendered sharply all the time.

It's relatively easy to avoid fractional absolute coordinates when setting via JS. But the fractional part may appear if anything in the document flow before the svg element had fractional size, for example, inline-block with text.

Maybe the described behavior is spec compliant, but it's hard to reach pixel-perfect rendering.

@tshinnic
Copy link

Only some browsers happen to position SVG boxes on pixel boundaries, that is true. What I have done is:

  • surround every <svg> with a <div>
  • check if <svg> left/top is a fractional pixel value
  • insert <div> padding or <svg> margin to move to unit values
  • monitor resize events to redo the padding/margins as needed

That means that if you happen to be on a browser that automatically aligns SVG boxes on pixel boundaries the code does nothing (yay)

Of course that means the <div> must be sized at least one px larger than the <svg> size to allow for the <svg> to shift around to unit pixel values.

Inside the <svg> it's SVG's world. Outside the <svg> it's the DOM world. At the boundary between them you hear Rod Serling's voice intoning the "you are in the Twilight Zone"!

@subzey
Copy link
Author

subzey commented Oct 29, 2015

@tshinnic I did something similar to solve this issue, but instead of wrapping in <div> I insert and empty inline-block span earlier in the layout flow and set its margin-right to value in range -0.5 to +0.5 px.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants