@@ -55,7 +55,6 @@ const ImageMapper = props => {
55
55
ctx . current . strokeStyle = strokeColor ;
56
56
ctx . current . strokeRect ( left , top , right - left , bot - top ) ;
57
57
ctx . current . fillRect ( left , top , right - left , bot - top ) ;
58
- ctx . current . fillStyle = props . fillColor ;
59
58
} ;
60
59
61
60
const drawCircle = ( coords , fillColor , lineWidth , strokeColor ) => {
@@ -67,7 +66,6 @@ const ImageMapper = props => {
67
66
ctx . current . closePath ( ) ;
68
67
ctx . current . stroke ( ) ;
69
68
ctx . current . fill ( ) ;
70
- ctx . current . fillStyle = props . fillColor ;
71
69
} ;
72
70
73
71
const drawPoly = ( coords , fillColor , lineWidth , strokeColor ) => {
@@ -84,7 +82,6 @@ const ImageMapper = props => {
84
82
ctx . current . closePath ( ) ;
85
83
ctx . current . stroke ( ) ;
86
84
ctx . current . fill ( ) ;
87
- ctx . current . fillStyle = props . fillColor ;
88
85
} ;
89
86
90
87
const getDimensions = type => {
@@ -94,18 +91,21 @@ const ImageMapper = props => {
94
91
return props [ type ] ;
95
92
} ;
96
93
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
+
98
98
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 ;
103
103
}
104
104
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 ;
109
109
}
110
110
return false ;
111
111
} ;
@@ -116,6 +116,14 @@ const ImageMapper = props => {
116
116
const imageWidth = getValues ( 'width' , imgWidth ) ;
117
117
const imageHeight = getValues ( 'height' , imgHeight ) ;
118
118
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
+
119
127
canvas . current . width = imageWidth ;
120
128
canvas . current . height = imageHeight ;
121
129
container . current . style . width = `${ imageWidth } px` ;
@@ -143,7 +151,7 @@ const ImageMapper = props => {
143
151
callingFn (
144
152
shape ,
145
153
event . target . getAttribute ( 'coords' ) . split ( ',' ) ,
146
- area . fillColor ,
154
+ area . fillColor || props . fillColor ,
147
155
area . lineWidth || props . lineWidth ,
148
156
area . strokeColor || props . strokeColor
149
157
) ;
@@ -162,16 +170,16 @@ const ImageMapper = props => {
162
170
} ;
163
171
164
172
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
+ }
165
181
if ( props . onClick ) {
166
182
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
- }
175
183
props . onClick ( area , index , event ) ;
176
184
}
177
185
} ;
@@ -348,19 +356,23 @@ ImageMapper.propTypes = {
348
356
349
357
export default React . memo ( ImageMapper , ( prevProps , nextProps ) => {
350
358
const watchedProps = [
359
+ 'src' ,
351
360
'active' ,
352
- 'fillColor ' ,
361
+ 'width ' ,
353
362
'height' ,
354
363
'imgWidth' ,
355
- 'lineWidth' ,
356
- 'map' ,
357
- 'src' ,
364
+ 'fillColor' ,
358
365
'strokeColor' ,
359
- 'width' ,
366
+ 'lineWidth' ,
367
+ 'natural' ,
368
+ 'areaKeyName' ,
369
+ 'stayHighlighted' ,
370
+ 'parentWidth' ,
371
+ 'responsive' ,
360
372
...nextProps . rerenderProps ,
361
373
] ;
362
374
363
375
const propChanged = watchedProps . some ( prop => prevProps [ prop ] !== nextProps [ prop ] ) ;
364
376
365
- return isEqual ( prevProps . map , nextProps . map ) || ! propChanged ;
377
+ return isEqual ( prevProps . map , nextProps . map ) && ! propChanged ;
366
378
} ) ;
0 commit comments