leeoniya / uPlot

📈 A small, fast chart for time series, lines, areas, ohlc & bars
MIT License
8.48k stars 370 forks source link

valToPos() returns wrong values when browser is zoomed in #917

Closed DreiDe closed 3 months ago

DreiDe commented 3 months ago

When using valToPos() inside the drawClear hook, and plotting e.g. a line with u.ctx.lineTo the resulting line is offset to the left and to the top when the browser window is zoomed in. The plot itself however is drawn correctly. Is there some internal logic for handling this?

leeoniya commented 3 months ago

by default valToPos() works in css/logical pixels that you would use for cursor/dom positioning. if you want this properly scaled to canvas pixels you have to call it with true for the third arg:

https://github.com/leeoniya/uPlot/blob/4b9b92788627724c13667228fab411ac9f638f4a/dist/uPlot.d.ts#L116-L117

DreiDe commented 3 months ago

@leeoniya Thanks for the quick support. This makes totally sense and fixed the problem. I wasn't aware that CSS pixels and Canvas pixels deviate when the browser window is scaled. I really appreciate your help :relieved: