@@ -62,7 +62,12 @@ const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanva
6262 return { action : '' , x : x , y : y , width : width , height : height } ;
6363} ;
6464
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+
6671 // creates a shadow around
6772 ctx . shadowColor = 'rgba(0, 0, 0, 0.7)' ;
6873 ctx . shadowBlur = 50 ; // Amount of blur for the shadow
@@ -71,14 +76,14 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D): void {
7176 case 'highlight' :
7277 // draws a rectangle first so that the shadow is visible before clearing
7378 ctx . fillStyle = 'rgb(0, 0, 0)' ;
74- ctx . fillRect ( rect . x , rect . y , rect . width , rect . height ) ;
79+ ctx . fillRect ( scaledX , scaledY , scaledWidth , scaledHeight ) ;
7580
76- ctx . clearRect ( rect . x , rect . y , rect . width , rect . height ) ;
81+ ctx . clearRect ( scaledX , scaledY , scaledWidth , scaledHeight ) ;
7782
7883 break ;
7984 case 'hide' :
8085 ctx . fillStyle = 'rgb(0, 0, 0)' ;
81- ctx . fillRect ( rect . x , rect . y , rect . width , rect . height ) ;
86+ ctx . fillRect ( scaledX , scaledY , scaledWidth , scaledHeight ) ;
8287
8388 break ;
8489 default :
@@ -87,7 +92,7 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D): void {
8792
8893 ctx . strokeStyle = '#ff0000' ;
8994 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 ) ;
9196}
9297
9398function resizeCanvas ( canvas : HTMLCanvasElement , imageDimensions : Rect ) : void {
@@ -137,22 +142,8 @@ export function ScreenshotEditorFactoryv2({
137142
138143 resizeCanvas ( screenshotCanvas , imageDimensions ) ;
139144 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- }
155145 setScaleFactor ( scale ) ;
146+ resizeCanvas ( graywashCanvas , imageDimensions ) ;
156147
157148 const screenshotContext = screenshotCanvas . getContext ( '2d' , { alpha : false } ) ;
158149 if ( ! screenshotContext ) {
@@ -192,15 +183,50 @@ export function ScreenshotEditorFactoryv2({
192183 }
193184 } , [ currentRect ] ) ;
194185
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+
195200 function drawBuffer ( ) : void {
196201 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 ) {
199204 return ;
200205 }
201206
202207 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 ) ;
204230 }
205231
206232 function drawScene ( ) : void {
0 commit comments