Skip to content

Instantly share code, notes, and snippets.

@rvighne
Last active January 4, 2016 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvighne/8677360 to your computer and use it in GitHub Desktop.
Save rvighne/8677360 to your computer and use it in GitHub Desktop.
Most browsers do not make it easy to draw perfect 1-pixel-wide lines; usually they are two pixels wide and weaker than the color you want. This little hack fixes that and some image smoothing problems.
function pixelPerfect(ctx) {
ctx.translate(0.5, 0.5); // so pixels with odd coordinates snap to nearest physical pixel
ctx.imageSmoothingEnabled = false; // may need polyfill for safari
}
// Call pixelPerfect with your canvas context (obtained by getContext) as argument, and happy whole-pixel drawing!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment