5 ms·
Dude!! Freaking awesome!! Thanks a bunch!! That's exactly what I was looking for! If you have some sort of ko-fi account or whatever I'll buy you a coffee!
by Trufa 5y ago
Dude!! Freaking awesome!! Thanks a bunch!! That's exactly what I was looking for! If you have some sort of ko-fi account or whatever I'll buy you a coffee!
- simonsarris 5y agoHaha well, you could always subscribe to my newsletter for a month. You might get a kick out of it anyway. I'm glad to have solved your mystery!
- Trufa 5y agoDone!!
- etimberg 5y agoOne thing to keep in mind with the window.devicePixelRatio is that it is not a static value as it can change over time. Some common reasons it could change are if the user drags the browser window from a low density screen to a high density one. Additionally, it will change if the user zooms the page in and out.
- leeoniya 5y agoyou can use matchMedia to detect devicePixelRatio changes and re-sync: https://github.com/leeoniya/uPlot/blob/190134aa844cfa2a0c052e72501240efc6031ced/src/dom.js#L20-L33 https://github.com/leeoniya/uPlot/blob/190134aa844cfa2a0c052... everything stays crisp even as you browser-zoom. e.g. https://leeoniya.github.io/uPlot/demos/area-fill.html https://leeoniya.github.io/uPlot/demos/area-fill.html
- moron4hire 5y agoZooming also fires the Window's `resize` event, which will also catch a user rotating their smartphone/tablet, or, erh, resizing the window on desktop.
- leeoniya 5y agoresize and orientationchange don't change devicePixelRatio though, so they're somewhat different concerns.
- moron4hire 5y agoNo, it's the same concern. The concern is not "how do I keep track of just devicePixelRatio?" It's "how do I fill this window with a canvas at native resolution?" Resize handles all of that. Device changes orientation? Resize fires. User zooms on content (this changing devicePixelRatio)? Resize fires. Anything that changes the page's clientWidth/clientHeight causes the Resize event to fire. You need only the one event.
- deleted 5y ago[deleted]
- deleted 5y ago[deleted]
- Dylan16807 5y ago> The concern is not "how do I keep track of just devicePixelRatio?" It's "how do I fill this window with a canvas at native resolution?" Not everyone's canvas is filling the window.
- moron4hire 5y agoAll the ways in which you will care about how to buffer dimensions of your canvas to avoid aliasing artifacts can be handled with just the window.onresize event, whether your canvas is fixed or proportionally sized. https://codepen.io/SeanMcBeth/pen/QWOoLNX https://codepen.io/SeanMcBeth/pen/QWOoLNX Throw a check in there to make sure the native dimensions actually did change so you don't needlessly redraw. Debounce it with setTimeout if you're super worried about people waggling their windows around or whatever.
- inDigiNeous 5y agoYeah I was struggling with this one a while, seems there is no way to know the actual devicePixelRatio of the display inside a browser, as the browser zoom level directly affects that. Or does anyone here happen to know a way ?
- moron4hire 5y agoDevicePixelRatio and clientWidth/clientHeight change together. You need only the Resize event on the Canvas. Your canvas.clientWidth * window.devicePixelRatio will always be the native width of the canvas.