Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSR: tolerate floating point imprecision #220

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
}
},
"lint-staged": {
"*.{js,css,scss}": "prettier --write"
"*.{js,css,scss}": "prettier --write",
"src/**/*.tsx": "prettier --write",
"demo/**/*.tsx": "prettier --write"
}
}
16 changes: 12 additions & 4 deletions src/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1047,10 +1047,18 @@ export class Map extends Component<MapProps, MapReactState> {
const tileX = lng2tile(latLng[1], zoom)
const tileY = lat2tile(latLng[0], zoom)

return [
(tileX - tileCenterX) * 256.0 + width / 2 + (pixelDelta ? pixelDelta[0] : 0),
(tileY - tileCenterY) * 256.0 + height / 2 + (pixelDelta ? pixelDelta[1] : 0),
] as Point
const pixelX = (tileX - tileCenterX) * 256.0 + width / 2 + (pixelDelta ? pixelDelta[0] : 0)
const pixelY = (tileY - tileCenterY) * 256.0 + height / 2 + (pixelDelta ? pixelDelta[1] : 0)

// If SSR is used, this enclosing function must always return the same result for the same input.
// When the values are computed with e.g. Math.cos (Math.log, ...), the exact implementatiom is platform
// dependent and results can slightly vary.
// See https://stackoverflow.com/questions/26570626/math-log2-precision-has-changed-in-chrome
const roundMathImprecision = (pixel: number) => {
return Math.round(pixel * 1000) / 1000
}

return [roundMathImprecision(pixelX), roundMathImprecision(pixelY)]
}

calculateZoomCenter = (center: Point, coords: Point, oldZoom: number, newZoom: number): Point => {
Expand Down
24 changes: 16 additions & 8 deletions src/overlays/Marker.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React, { useState } from 'react'
import { PigeonProps } from '../types'
import { PigeonProps, Point } from '../types'

interface MarkerProps extends PigeonProps {
type CallbackArgs<P> = {
event: React.MouseEvent
anchor: Point
payload: P
}

interface MarkerProps<P> extends PigeonProps {
color?: string
payload?: any
payload?: P

width?: number
height?: number
Expand All @@ -16,13 +22,13 @@ interface MarkerProps extends PigeonProps {
children?: JSX.Element

// callbacks
onClick?: ({ event: HTMLMouseEvent, anchor: Point, payload: any }) => void
onContextMenu?: ({ event: HTMLMouseEvent, anchor: Point, payload: any }) => void
onMouseOver?: ({ event: HTMLMouseEvent, anchor: Point, payload: any }) => void
onMouseOut?: ({ event: HTMLMouseEvent, anchor: Point, payload: any }) => void
onClick?: (arg: CallbackArgs<P>) => void
onContextMenu?: (arg: CallbackArgs<P>) => void
onMouseOver?: (arg: CallbackArgs<P>) => void
onMouseOut?: (arg: CallbackArgs<P>) => void
}

export function Marker(props: MarkerProps): JSX.Element {
export function Marker<P = any>(props: MarkerProps<P>): JSX.Element {
const width =
typeof props.width !== 'undefined'
? props.width
Expand Down Expand Up @@ -54,6 +60,8 @@ export function Marker(props: MarkerProps): JSX.Element {
filter: hover ? 'drop-shadow(0 0 4px rgba(0, 0, 0, .3))' : '',
pointerEvents: 'none',
cursor: 'pointer',
width: width,
height: height,
...(props.style || {}),
}}
className={props.className ? `${props.className} pigeon-click-block` : 'pigeon-click-block'}
Expand Down