function screen_to_canvas(p) { // should be called with coordinates obtained from MouseEvent.clientX/clientY * window.devicePixelRatio const xc = (p.x - offset.x) / zoom; const yc = (p.y - offset.y) / zoom; return {'x': xc, 'y': yc}; } function canvas_to_screen(state, p) { const xs = (p.x * zoom + offset.x) / window.devicePixelRatio; const ys = (p.y * zoom + offset.y) / window.devicePixelRatio; return {'x': xs, 'y': ys}; }