Pixel-perfect toolkit: provides support for both pixel-perfect applications (eg. pixel fonts) and mobile/high-DPI devices.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Shad Amethyst 7d12e8a412
🐛 Add round option to usePannable and set user-select in PixelPerfectTouch
1 year ago
src 🐛 Add round option to usePannable and set user-select in PixelPerfectTouch 1 year ago
test Add Pannable and attachPannable 1 year ago
.gitignore Supporting building as library with vite 1 year ago
.npmignore 🐛 Configure npm for publish, set typescript resolution to node16 1 year ago
LICENSE 🚚 Refactor code, add solid.js support 1 year ago
README.md 🎉 First commit 2 years ago
package-lock.json Fix horizontalLine being one pixel short, add canvas center support 1 year ago
package.json Fix horizontalLine being one pixel short, add canvas center support 1 year ago
pptk-old.js 🚚 Refactor code, add solid.js support 1 year ago
tsconfig.json 🐛 Configure npm for publish, set typescript resolution to node16 1 year ago
vite.config.ts Add downscale option 1 year ago

README.md

Pixel-Perfect ToolKit

This library aims to solve two problems in web-development, that are notoriously hard to both solve at once:

  • support for mobile devices (high-DPI, multi-touch, etc.)
  • pixel-perfect control, notably for pixel fonts

Limitations

Canvases must be contained

Because canvases must have an integer width and height, and because window.devicePixelRatio may be a floating-point value.

For instance, if dpr = 1.5 and the desired with is 81 CSS pixels, then the desired canvas width would be 81 * 1.5 = 121.5. Naively setting canvas.width = 121.5 results in the browser truncating it to 121, which only cover 121 / 1.5 ≈ 80.66 CSS pixels, resulting in blurred edges as the element has to be stretched horizontally by 0.4%.

Instead, we set canvas.width = Math.ceil(121.5) = 122, then set canvas.style.width to 122 / 1.5 ≈ 81.33px. This requires a parent "container" element to read the desired width from and to hide the overflowing 0.33 CSS pixel.

This is what the container element should have as style:

.container {
    /* Necessary properties */
    overflow: hidden;

    /* Optionally set a different background color to reduce flicker */
    background-color: /* ... */;

    /* Width and height should be set to anything besides `fit-content` */
    width: /* ... */;
    height: /* ... */;
}