@@ -62,7 +62,7 @@ const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanva
62
62
return { action : '' , x : x , y : y , width : width , height : height } ;
63
63
} ;
64
64
65
- function drawRect ( rect : Rect , ctx : CanvasRenderingContext2D , scaleFactor : number ) : void {
65
+ function drawRect ( rect : Rect , ctx : CanvasRenderingContext2D ) : void {
66
66
// creates a shadow around
67
67
ctx . shadowColor = 'rgba(0, 0, 0, 0.7)' ;
68
68
ctx . shadowBlur = 50 ; // Amount of blur for the shadow
@@ -71,14 +71,14 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, scaleFactor: number
71
71
case 'highlight' :
72
72
// draws a rectangle first so that the shadow is visible before clearing
73
73
ctx . fillStyle = 'rgb(0, 0, 0)' ;
74
- ctx . fillRect ( rect . x * scaleFactor , rect . y * scaleFactor , rect . width * scaleFactor , rect . height * scaleFactor ) ;
74
+ ctx . fillRect ( rect . x , rect . y , rect . width , rect . height ) ;
75
75
76
- ctx . clearRect ( rect . x * scaleFactor , rect . y * scaleFactor , rect . width * scaleFactor , rect . height * scaleFactor ) ;
76
+ ctx . clearRect ( rect . x , rect . y , rect . width , rect . height ) ;
77
77
78
78
break ;
79
79
case 'hide' :
80
80
ctx . fillStyle = 'rgb(0, 0, 0)' ;
81
- ctx . fillRect ( rect . x * scaleFactor , rect . y * scaleFactor , rect . width * scaleFactor , rect . height * scaleFactor ) ;
81
+ ctx . fillRect ( rect . x , rect . y , rect . width , rect . height ) ;
82
82
83
83
break ;
84
84
default :
@@ -87,12 +87,7 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, scaleFactor: number
87
87
88
88
ctx . strokeStyle = '#ff0000' ;
89
89
ctx . lineWidth = 2 ;
90
- ctx . strokeRect (
91
- ( rect . x + 1 ) * scaleFactor ,
92
- ( rect . y + 1 ) * scaleFactor ,
93
- ( rect . width - 2 ) * scaleFactor ,
94
- ( rect . height - 2 ) * scaleFactor ,
95
- ) ;
90
+ ctx . strokeRect ( rect . x + 1 , rect . y + 1 , rect . width - 2 , rect . height - 2 ) ;
96
91
}
97
92
98
93
function resizeCanvas ( canvas : HTMLCanvasElement , imageDimensions : Rect ) : void {
@@ -128,68 +123,60 @@ export function ScreenshotEditorFactoryv2({
128
123
const rectDivRef = hooks . useRef < HTMLDivElement > ( null ) ;
129
124
const [ imageSource , setImageSource ] = hooks . useState < HTMLCanvasElement | null > ( null ) ;
130
125
const [ isShown , setIsShown ] = hooks . useState < boolean > ( true ) ;
126
+ const [ scaleFactor , setScaleFactor ] = hooks . useState < number > ( 1 ) ;
131
127
132
128
const resize = hooks . useCallback ( ( ) : void => {
133
129
const screenshotCanvas = screenshotRef . current ;
134
- if ( ! screenshotCanvas ) {
135
- throw new Error ( 'Could not get canvas' ) ;
136
- }
137
-
138
130
const graywashCanvas = graywashRef . current ;
139
- if ( ! graywashCanvas ) {
140
- return ;
141
- }
142
-
143
- if ( ! imageSource ) {
144
- return ;
145
- }
146
131
const measurementDiv = measurementRef . current ;
147
- if ( ! measurementDiv ) {
132
+ if ( ! screenshotCanvas || ! graywashCanvas || ! imageSource || ! measurementDiv ) {
148
133
return ;
149
134
}
135
+
150
136
const imageDimensions = getContainedSize ( measurementDiv , imageSource ) ;
137
+
151
138
resizeCanvas ( screenshotCanvas , imageDimensions ) ;
139
+ const scale = screenshotCanvas . width / graywashCanvas . width ;
152
140
resizeCanvas ( graywashCanvas , imageDimensions ) ;
153
- const rectDiv = rectDivRef . current ;
154
- if ( ! rectDiv ) {
155
- return ;
156
- }
157
- rectDiv . style . width = `${ imageDimensions . width } px` ;
158
- rectDiv . style . height = `${ imageDimensions . height } px` ;
159
141
160
- drawScene ( ) ;
161
- } , [ imageSource ] ) ;
162
-
163
- hooks . useLayoutEffect ( ( ) => {
164
- const screenshotCanvas = screenshotRef . current ;
165
- if ( ! screenshotCanvas ) {
166
- throw new Error ( 'Could not get canvas' ) ;
167
- }
168
-
169
- const graywashCanvas = graywashRef . current ;
170
- if ( ! graywashCanvas ) {
171
- return ;
142
+ if ( scale !== 1 && scale !== scaleFactor ) {
143
+ const scaledCommands = drawCommands . map ( rect => {
144
+ return {
145
+ ... rect ,
146
+ x : rect . x * scaleFactor ,
147
+ y : rect . y * scaleFactor ,
148
+ width : rect . width * scaleFactor ,
149
+ height : rect . height * scaleFactor ,
150
+ } ;
151
+ } ) ;
152
+
153
+ setDrawCommands ( scaledCommands ) ;
172
154
}
155
+ setScaleFactor ( scale ) ;
173
156
174
- if ( ! imageSource ) {
175
- return ;
176
- }
177
- const measurementDiv = measurementRef . current ;
178
- if ( ! measurementDiv ) {
157
+ const screenshotContext = screenshotCanvas . getContext ( '2d' , { alpha : false } ) ;
158
+ if ( ! screenshotContext ) {
179
159
return ;
180
160
}
181
161
182
- resize ( ) ;
183
- const imageDimensions = getContainedSize ( measurementDiv , imageSource ) ;
184
-
185
- const screenshotContext = screenshotCanvas . getContext ( '2d' , { alpha : false } ) ;
162
+ screenshotContext . drawImage ( imageSource , 0 , 0 , imageDimensions . width , imageDimensions . height ) ;
163
+ drawScene ( ) ;
186
164
187
- if ( ! screenshotContext ) {
165
+ const rectDiv = rectDivRef . current ;
166
+ if ( ! rectDiv ) {
188
167
return ;
189
168
}
169
+ rectDiv . style . width = `${ imageDimensions . width } px` ;
170
+ rectDiv . style . height = `${ imageDimensions . height } px` ;
171
+ } , [ imageSource , isShown , drawCommands ] ) ;
190
172
191
- screenshotContext . drawImage ( imageSource , 0 , 0 , imageDimensions . width , imageDimensions . height ) ;
192
- } , [ imageSource , isShown ] ) ;
173
+ hooks . useEffect ( ( ) => {
174
+ WINDOW . addEventListener ( 'resize' , resize ) ;
175
+
176
+ return ( ) => {
177
+ WINDOW . removeEventListener ( 'resize' , resize ) ;
178
+ } ;
179
+ } , [ resize ] ) ;
193
180
194
181
hooks . useEffect ( ( ) => {
195
182
const graywashCanvas = graywashRef . current ;
@@ -206,41 +193,14 @@ export function ScreenshotEditorFactoryv2({
206
193
} , [ currentRect ] ) ;
207
194
208
195
function drawBuffer ( ) : void {
209
- if ( ! imageBuffer ) {
210
- return ;
211
- }
212
-
213
196
const ctx = imageBuffer . getContext ( '2d' , { alpha : false } ) ;
214
- if ( ! ctx ) {
215
- return ;
216
- }
217
- if ( ! imageSource ) {
197
+ const graywashCanvas = graywashRef . current ;
198
+ if ( ! imageBuffer || ! ctx || ! imageSource || ! graywashCanvas ) {
218
199
return ;
219
200
}
220
201
221
202
ctx . drawImage ( imageSource , 0 , 0 ) ;
222
-
223
- const grayWashBufferBig = DOCUMENT . createElement ( 'canvas' ) ;
224
- grayWashBufferBig . width = imageBuffer . width ;
225
- grayWashBufferBig . height = imageBuffer . height ;
226
-
227
- const grayCtx = grayWashBufferBig . getContext ( '2d' ) ;
228
- if ( ! grayCtx ) {
229
- return ;
230
- }
231
-
232
- // applies the graywash if there's any boxes drawn
233
- if ( drawCommands . length || currentRect ) {
234
- grayCtx . fillStyle = 'rgba(0, 0, 0, 0.25)' ;
235
- grayCtx . fillRect ( 0 , 0 , imageBuffer . width , imageBuffer . height ) ;
236
- }
237
-
238
- const scale = imageBuffer . width / measurementRef . current ! . clientWidth ;
239
-
240
- for ( const rect of drawCommands ) {
241
- drawRect ( rect , grayCtx , scale ) ;
242
- }
243
- ctx . drawImage ( grayWashBufferBig , 0 , 0 ) ;
203
+ ctx . drawImage ( graywashCanvas , 0 , 0 , imageBuffer . width , imageBuffer . height ) ;
244
204
}
245
205
246
206
function drawScene ( ) : void {
@@ -262,12 +222,12 @@ export function ScreenshotEditorFactoryv2({
262
222
ctx . fillRect ( 0 , 0 , graywashCanvas . width , graywashCanvas . height ) ;
263
223
}
264
224
265
- for ( const rect of drawCommands ) {
266
- drawRect ( rect , ctx , 1 ) ;
267
- }
225
+ drawCommands . forEach ( rect => {
226
+ drawRect ( rect , ctx ) ;
227
+ } ) ;
268
228
269
229
if ( currentRect ) {
270
- drawRect ( currentRect , ctx , 1 ) ;
230
+ drawRect ( currentRect , ctx ) ;
271
231
setCurrentRect ( undefined ) ;
272
232
}
273
233
}
0 commit comments