Skip to content

Commit bd5ec46

Browse files
committed
solved major bugs of Image Mapper
1 parent 1430b06 commit bd5ec46

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

src/lib/ImageMapper.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ const ImageMapper = props => {
5555
ctx.current.strokeStyle = strokeColor;
5656
ctx.current.strokeRect(left, top, right - left, bot - top);
5757
ctx.current.fillRect(left, top, right - left, bot - top);
58-
ctx.current.fillStyle = props.fillColor;
5958
};
6059

6160
const drawCircle = (coords, fillColor, lineWidth, strokeColor) => {
@@ -67,7 +66,6 @@ const ImageMapper = props => {
6766
ctx.current.closePath();
6867
ctx.current.stroke();
6968
ctx.current.fill();
70-
ctx.current.fillStyle = props.fillColor;
7169
};
7270

7371
const drawPoly = (coords, fillColor, lineWidth, strokeColor) => {
@@ -84,7 +82,6 @@ const ImageMapper = props => {
8482
ctx.current.closePath();
8583
ctx.current.stroke();
8684
ctx.current.fill();
87-
ctx.current.fillStyle = props.fillColor;
8885
};
8986

9087
const getDimensions = type => {
@@ -94,18 +91,21 @@ const ImageMapper = props => {
9491
return props[type];
9592
};
9693

97-
const getValues = (type, measure) => {
94+
const getValues = (type, measure, name = 'area') => {
95+
const responsiveWidth = props.parentWidth;
96+
const { naturalWidth, naturalHeight, clientWidth, clientHeight } = img.current;
97+
9898
if (type === 'width') {
99-
if (props.responsive) return props.parentWidth;
100-
if (props.natural) return img.current.naturalWidth;
101-
if (props.width) return measure;
102-
return img.current.clientWidth;
99+
if (props.responsive) return responsiveWidth;
100+
if (props.natural) return naturalWidth;
101+
if (props.width || name === 'image') return measure;
102+
return clientWidth;
103103
}
104104
if (type === 'height') {
105-
if (props.responsive) return img.current.clientHeight;
106-
if (props.natural) return img.current.naturalHeight;
107-
if (props.height) return measure;
108-
return img.current.clientHeight;
105+
if (props.responsive) return naturalHeight;
106+
if (props.natural) return naturalHeight;
107+
if (props.height || name === 'image') return measure;
108+
return clientHeight;
109109
}
110110
return false;
111111
};
@@ -116,6 +116,14 @@ const ImageMapper = props => {
116116
const imageWidth = getValues('width', imgWidth);
117117
const imageHeight = getValues('height', imgHeight);
118118

119+
if (props.width || props.responsive) {
120+
img.current.width = getValues('width', imgWidth, 'image');
121+
}
122+
123+
if (props.height || props.responsive) {
124+
img.current.height = getValues('height', imgHeight, 'image');
125+
}
126+
119127
canvas.current.width = imageWidth;
120128
canvas.current.height = imageHeight;
121129
container.current.style.width = `${imageWidth}px`;
@@ -143,7 +151,7 @@ const ImageMapper = props => {
143151
callingFn(
144152
shape,
145153
event.target.getAttribute('coords').split(','),
146-
area.fillColor,
154+
area.fillColor || props.fillColor,
147155
area.lineWidth || props.lineWidth,
148156
area.strokeColor || props.strokeColor
149157
);
@@ -162,16 +170,16 @@ const ImageMapper = props => {
162170
};
163171

164172
const click = (area, index, event) => {
173+
if (props.stayHighlighted) {
174+
const newArea = { ...area };
175+
newArea.preFillColor = area.fillColor || props.fillColor;
176+
const updatedAreas = storedMap.areas.map(cur =>
177+
cur[props.areaKeyName] === area[props.areaKeyName] ? newArea : cur
178+
);
179+
setMap(prev => ({ ...prev, areas: updatedAreas }));
180+
}
165181
if (props.onClick) {
166182
event.preventDefault();
167-
if (props.stayHighlighted) {
168-
const newArea = { ...area };
169-
newArea.preFillColor = area.fillColor;
170-
const updatedAreas = storedMap.areas.map(cur =>
171-
cur[props.areaKeyName] === area[props.areaKeyName] ? newArea : cur
172-
);
173-
setMap(prev => ({ ...prev, areas: updatedAreas }));
174-
}
175183
props.onClick(area, index, event);
176184
}
177185
};
@@ -348,19 +356,23 @@ ImageMapper.propTypes = {
348356

349357
export default React.memo(ImageMapper, (prevProps, nextProps) => {
350358
const watchedProps = [
359+
'src',
351360
'active',
352-
'fillColor',
361+
'width',
353362
'height',
354363
'imgWidth',
355-
'lineWidth',
356-
'map',
357-
'src',
364+
'fillColor',
358365
'strokeColor',
359-
'width',
366+
'lineWidth',
367+
'natural',
368+
'areaKeyName',
369+
'stayHighlighted',
370+
'parentWidth',
371+
'responsive',
360372
...nextProps.rerenderProps,
361373
];
362374

363375
const propChanged = watchedProps.some(prop => prevProps[prop] !== nextProps[prop]);
364376

365-
return isEqual(prevProps.map, nextProps.map) || !propChanged;
377+
return isEqual(prevProps.map, nextProps.map) && !propChanged;
366378
});

0 commit comments

Comments
 (0)