admin 管理员组

文章数量: 1086019

I am using EaselJS as an API for HTML5 canvas.

I noticed that the following code:

line.graphics.setStrokeStyle(1).beginStroke("black").moveTo(100,100).lineTo(200,200);
stage.addChild(line);

...produces following line:

I set the thickness to 1 - but the line is still fuzzy. If you zoom in with the snapshot, you can see it actually occupies 3 pixels. I believe I read somewhere canvas draws a point between two pixels, so that both pixels will be colored in fact. And you need to shift where you draw the point by half the pixel width so it falls on the entire pixel.

I need sharp image for my applications, please advise.

I am using EaselJS as an API for HTML5 canvas.

I noticed that the following code:

line.graphics.setStrokeStyle(1).beginStroke("black").moveTo(100,100).lineTo(200,200);
stage.addChild(line);

...produces following line:

I set the thickness to 1 - but the line is still fuzzy. If you zoom in with the snapshot, you can see it actually occupies 3 pixels. I believe I read somewhere canvas draws a point between two pixels, so that both pixels will be colored in fact. And you need to shift where you draw the point by half the pixel width so it falls on the entire pixel.

I need sharp image for my applications, please advise.

Share Improve this question edited Feb 19, 2014 at 13:53 VividD 10.5k8 gold badges66 silver badges112 bronze badges asked Jul 13, 2011 at 0:35 William ShamWilliam Sham 13.3k11 gold badges51 silver badges67 bronze badges 4
  • 1 Have you tried changing moveTo(100,100) to moveTo(100.5,100.5)? – kangax Commented Jul 13, 2011 at 2:14
  • Yes. It works! But doesn't Easel have a bulit in SnapToPixel property..so that'll make my math easier? Do you know of any code snippet for JS in general that'll do it throughout, so i dont have to do .5 for every coordinate I enter? – William Sham Commented Jul 13, 2011 at 20:24
  • 1 Well, you could have a helper for that. E.g.: moveTo(coerce(100), coerce(100)) or moveTo('100'.toCanvas(), '100'.toCanvas()) — both of which are kind of ugly (and confusing), of course :) I'm not familiar with EaselJS, and whether it has anything to work around this issue. I know that in my library (fabric.js, kangax.github./fabric.js/demos/kitchensink) there's also nothing to take care of this... – kangax Commented Jul 14, 2011 at 0:11
  • I've previously considered having Graphics support the snapToPixel property, but it would have fairly noticeable performance implications, is unnecessary for anything other than straight lines, and is fairly easily solved by offsetting your shape by 0.5. For example: myShape.regX = myShape.regY = 0.5; – gskinner Commented Feb 1, 2015 at 3:02
Add a ment  | 

1 Answer 1

Reset to default 9

EaselJS is just an abstraction for the canvas APIs - which draws all lines on the specified coordinates. The snapToPixel API is specifically for doing automatic rounding, but doesn't take into account the half-pixel issue you are describing.

The best practice approach is to put everything into a Container, and put the container at positive or negative (0.5,0.5) - which will adjust everything, and you can work in a normal coordinate space, rather than offsetting all your calculations.

本文标签: javascriptEaselJS line fuzzinessStack Overflow