Skip to content

Commit 93c2b50

Browse files
perf: optimize CustomMarkerClass draw method
* Replacing offsetHeight / Width with transformX, Y alternative * The zillow way, using only transforms for positioning.
1 parent 447491b commit 93c2b50

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/utils/index.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,52 +63,51 @@ export function createCustomMarkerClass(api: typeof google.maps): ICustomMarkerC
6363

6464
if (point) {
6565
this.element.style.position = "absolute";
66-
const height = this.element.offsetHeight;
67-
const width = this.element.offsetWidth;
68-
let x: number, y: number;
66+
let transformX: string, transformY: string;
67+
6968
switch (this.opts.anchorPoint) {
7069
case "TOP_CENTER":
71-
x = point.x - width / 2;
72-
y = point.y;
70+
transformX = "-50%";
71+
transformY = "-100%";
7372
break;
7473
case "BOTTOM_CENTER":
75-
x = point.x - width / 2;
76-
y = point.y - height;
74+
transformX = "-50%";
75+
transformY = "0";
7776
break;
7877
case "LEFT_CENTER":
79-
x = point.x;
80-
y = point.y - height / 2;
78+
transformX = "-100%";
79+
transformY = "-50%";
8180
break;
8281
case "RIGHT_CENTER":
83-
x = point.x - width;
84-
y = point.y - height / 2;
82+
transformX = "0";
83+
transformY = "-50%";
8584
break;
8685
case "TOP_LEFT":
87-
x = point.x;
88-
y = point.y;
86+
transformX = "-100%";
87+
transformY = "-100%";
8988
break;
9089
case "TOP_RIGHT":
91-
x = point.x - width;
92-
y = point.y;
90+
transformX = "0";
91+
transformY = "-100%";
9392
break;
9493
case "BOTTOM_LEFT":
95-
x = point.x;
96-
y = point.y - height;
94+
transformX = "-100%";
95+
transformY = "0";
9796
break;
9897
case "BOTTOM_RIGHT":
99-
x = point.x - width;
100-
y = point.y - height;
98+
transformX = "0";
99+
transformY = "0";
101100
break;
102101
default:
103102
// "center"
104-
x = point.x - width / 2;
105-
y = point.y - height / 2;
103+
transformX = "-50%";
104+
transformY = "-50%";
106105
}
107106

108-
this.element.style.left = x + "px";
109-
this.element.style.top = y + "px";
107+
const xPos = point.x + (this.opts.offsetX || 0) + "px";
108+
const yPos = point.y + (this.opts.offsetY || 0) + "px";
110109
// eslint-disable-next-line prettier/prettier
111-
this.element.style.transform = `translateX(${this.opts.offsetX || 0}px) translateY(${this.opts.offsetY || 0}px)`;
110+
this.element.style.transform = `translateX(${transformX}) translateX(${xPos}) translateY(${transformY}) translateY(${yPos})`;
112111

113112
if (this.opts.zIndex) {
114113
this.element.style.zIndex = this.opts.zIndex.toString();

0 commit comments

Comments
 (0)