Drawing a rectangle with 1px border - graphics.setStrokeStyle(1) doesn't work correctly

4,383 views
Skip to first unread message

Usual Suspects

unread,
Oct 19, 2012, 9:10:42 AM10/19/12
to eas...@googlegroups.com
Hi guys,

I'm using the following code piece to draw a rectangle with exactly 1px black border. Unfortunately, the result has a border thicker than 1px and not completely black but dark gray.

var border new createjs.Shape();
border.graphics.beginStroke("#000");
border.graphics.setStrokeStyle(1);
border.snapToPixel true;
border.graphics.drawRect(00450309);
border.stageWidth 200;
border.stageHeight 150;

Please see the jsFiddle example;

Grant Skinner

unread,
Oct 19, 2012, 10:52:52 AM10/19/12
to eas...@googlegroups.com
Canvas draws strokes "around" the pixel position you specify, so to get a nice crisp 1 px line you have to draw it on a half pixel position. A little confusing, but it's just the way Canvas works.

This will draw a crisp 1px black border:

var border new createjs.Shape();
border.graphics.beginStroke("#000");
border.graphics.setStrokeStyle(1);
border.snapToPixel true;
border.graphics.drawRect(00450309);
border.stageWidth 200 - 0.5;
border.stageHeight 150 - 0.5;


Usual Suspects

unread,
Oct 19, 2012, 12:30:43 PM10/19/12
to eas...@googlegroups.com
Grant,

This was interesting to know. Thanks for sharing.
Reply all
Reply to author
Forward
0 new messages