@@ -62,7 +62,12 @@ 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 ) : void {
65
+ function drawRect ( rect : Rect , ctx : CanvasRenderingContext2D , scale : number = 1 ) : void {
66
+ const scaledX = rect . x * scale ;
67
+ const scaledY = rect . y * scale ;
68
+ const scaledWidth = rect . width * scale ;
69
+ const scaledHeight = rect . height * scale ;
70
+
66
71
// creates a shadow around
67
72
ctx . shadowColor = 'rgba(0, 0, 0, 0.7)' ;
68
73
ctx . shadowBlur = 50 ; // Amount of blur for the shadow
@@ -71,14 +76,14 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D): void {
71
76
case 'highlight' :
72
77
// draws a rectangle first so that the shadow is visible before clearing
73
78
ctx . fillStyle = 'rgb(0, 0, 0)' ;
74
- ctx . fillRect ( rect . x , rect . y , rect . width , rect . height ) ;
79
+ ctx . fillRect ( scaledX , scaledY , scaledWidth , scaledHeight ) ;
75
80
76
- ctx . clearRect ( rect . x , rect . y , rect . width , rect . height ) ;
81
+ ctx . clearRect ( scaledX , scaledY , scaledWidth , scaledHeight ) ;
77
82
78
83
break ;
79
84
case 'hide' :
80
85
ctx . fillStyle = 'rgb(0, 0, 0)' ;
81
- ctx . fillRect ( rect . x , rect . y , rect . width , rect . height ) ;
86
+ ctx . fillRect ( scaledX , scaledY , scaledWidth , scaledHeight ) ;
82
87
83
88
break ;
84
89
default :
@@ -87,7 +92,7 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D): void {
87
92
88
93
ctx . strokeStyle = '#ff0000' ;
89
94
ctx . lineWidth = 2 ;
90
- ctx . strokeRect ( rect . x + 1 , rect . y + 1 , rect . width - 2 , rect . height - 2 ) ;
95
+ ctx . strokeRect ( scaledX + 1 , scaledY + 1 , scaledWidth - 2 , scaledHeight - 2 ) ;
91
96
}
92
97
93
98
function resizeCanvas ( canvas : HTMLCanvasElement , imageDimensions : Rect ) : void {
@@ -137,22 +142,8 @@ export function ScreenshotEditorFactoryv2({
137
142
138
143
resizeCanvas ( screenshotCanvas , imageDimensions ) ;
139
144
const scale = screenshotCanvas . width / graywashCanvas . width ;
140
- resizeCanvas ( graywashCanvas , imageDimensions ) ;
141
-
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 ) ;
154
- }
155
145
setScaleFactor ( scale ) ;
146
+ resizeCanvas ( graywashCanvas , imageDimensions ) ;
156
147
157
148
const screenshotContext = screenshotCanvas . getContext ( '2d' , { alpha : false } ) ;
158
149
if ( ! screenshotContext ) {
@@ -192,15 +183,50 @@ export function ScreenshotEditorFactoryv2({
192
183
}
193
184
} , [ currentRect ] ) ;
194
185
186
+ hooks . useEffect ( ( ) => {
187
+ const scaledCommands = drawCommands . map ( rect => {
188
+ return {
189
+ action : rect . action ,
190
+ x : rect . x * scaleFactor ,
191
+ y : rect . y * scaleFactor ,
192
+ width : rect . width * scaleFactor ,
193
+ height : rect . height * scaleFactor ,
194
+ } ;
195
+ } ) ;
196
+
197
+ setDrawCommands ( scaledCommands ) ;
198
+ } , [ scaleFactor ] ) ;
199
+
195
200
function drawBuffer ( ) : void {
196
201
const ctx = imageBuffer . getContext ( '2d' , { alpha : false } ) ;
197
- const graywashCanvas = graywashRef . current ;
198
- if ( ! imageBuffer || ! ctx || ! imageSource || ! graywashCanvas ) {
202
+ const measurementDiv = measurementRef . current ;
203
+ if ( ! imageBuffer || ! ctx || ! imageSource || ! measurementDiv ) {
199
204
return ;
200
205
}
201
206
202
207
ctx . drawImage ( imageSource , 0 , 0 ) ;
203
- ctx . drawImage ( graywashCanvas , 0 , 0 , imageBuffer . width , imageBuffer . height ) ;
208
+
209
+ const grayWashBufferBig = DOCUMENT . createElement ( 'canvas' ) ;
210
+ grayWashBufferBig . width = imageBuffer . width ;
211
+ grayWashBufferBig . height = imageBuffer . height ;
212
+
213
+ const grayCtx = grayWashBufferBig . getContext ( '2d' ) ;
214
+ if ( ! grayCtx ) {
215
+ return ;
216
+ }
217
+
218
+ // applies the graywash if there's any boxes drawn
219
+ if ( drawCommands . length || currentRect ) {
220
+ grayCtx . fillStyle = 'rgba(0, 0, 0, 0.25)' ;
221
+ grayCtx . fillRect ( 0 , 0 , imageBuffer . width , imageBuffer . height ) ;
222
+ }
223
+
224
+ const scale = imageBuffer . width / measurementDiv . clientWidth ;
225
+
226
+ drawCommands . forEach ( rect => {
227
+ drawRect ( rect , grayCtx , scale ) ;
228
+ } ) ;
229
+ ctx . drawImage ( grayWashBufferBig , 0 , 0 ) ;
204
230
}
205
231
206
232
function drawScene ( ) : void {
0 commit comments