@@ -62,7 +62,7 @@ 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 , scaleFactor : number ) : void {
65+ function drawRect ( rect : Rect , ctx : CanvasRenderingContext2D ) : void {
6666 // creates a shadow around
6767 ctx . shadowColor = 'rgba(0, 0, 0, 0.7)' ;
6868 ctx . shadowBlur = 50 ; // Amount of blur for the shadow
@@ -71,14 +71,14 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, scaleFactor: number
7171 case 'highlight' :
7272 // draws a rectangle first so that the shadow is visible before clearing
7373 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 ) ;
7575
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 ) ;
7777
7878 break ;
7979 case 'hide' :
8080 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 ) ;
8282
8383 break ;
8484 default :
@@ -87,12 +87,7 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, scaleFactor: number
8787
8888 ctx . strokeStyle = '#ff0000' ;
8989 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 ) ;
9691}
9792
9893function resizeCanvas ( canvas : HTMLCanvasElement , imageDimensions : Rect ) : void {
@@ -128,68 +123,60 @@ export function ScreenshotEditorFactoryv2({
128123 const rectDivRef = hooks . useRef < HTMLDivElement > ( null ) ;
129124 const [ imageSource , setImageSource ] = hooks . useState < HTMLCanvasElement | null > ( null ) ;
130125 const [ isShown , setIsShown ] = hooks . useState < boolean > ( true ) ;
126+ const [ scaleFactor , setScaleFactor ] = hooks . useState < number > ( 1 ) ;
131127
132128 const resize = hooks . useCallback ( ( ) : void => {
133129 const screenshotCanvas = screenshotRef . current ;
134- if ( ! screenshotCanvas ) {
135- throw new Error ( 'Could not get canvas' ) ;
136- }
137-
138130 const graywashCanvas = graywashRef . current ;
139- if ( ! graywashCanvas ) {
140- return ;
141- }
142-
143- if ( ! imageSource ) {
144- return ;
145- }
146131 const measurementDiv = measurementRef . current ;
147- if ( ! measurementDiv ) {
132+ if ( ! screenshotCanvas || ! graywashCanvas || ! imageSource || ! measurementDiv ) {
148133 return ;
149134 }
135+
150136 const imageDimensions = getContainedSize ( measurementDiv , imageSource ) ;
137+
151138 resizeCanvas ( screenshotCanvas , imageDimensions ) ;
139+ const scale = screenshotCanvas . width / graywashCanvas . width ;
152140 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` ;
159141
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 ) ;
172154 }
155+ setScaleFactor ( scale ) ;
173156
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 ) {
179159 return ;
180160 }
181161
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 ( ) ;
186164
187- if ( ! screenshotContext ) {
165+ const rectDiv = rectDivRef . current ;
166+ if ( ! rectDiv ) {
188167 return ;
189168 }
169+ rectDiv . style . width = `${ imageDimensions . width } px` ;
170+ rectDiv . style . height = `${ imageDimensions . height } px` ;
171+ } , [ imageSource , isShown , drawCommands ] ) ;
190172
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 ] ) ;
193180
194181 hooks . useEffect ( ( ) => {
195182 const graywashCanvas = graywashRef . current ;
@@ -206,41 +193,14 @@ export function ScreenshotEditorFactoryv2({
206193 } , [ currentRect ] ) ;
207194
208195 function drawBuffer ( ) : void {
209- if ( ! imageBuffer ) {
210- return ;
211- }
212-
213196 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 ) {
218199 return ;
219200 }
220201
221202 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 ) ;
244204 }
245205
246206 function drawScene ( ) : void {
@@ -262,12 +222,12 @@ export function ScreenshotEditorFactoryv2({
262222 ctx . fillRect ( 0 , 0 , graywashCanvas . width , graywashCanvas . height ) ;
263223 }
264224
265- for ( const rect of drawCommands ) {
266- drawRect ( rect , ctx , 1 ) ;
267- }
225+ drawCommands . forEach ( rect => {
226+ drawRect ( rect , ctx ) ;
227+ } ) ;
268228
269229 if ( currentRect ) {
270- drawRect ( currentRect , ctx , 1 ) ;
230+ drawRect ( currentRect , ctx ) ;
271231 setCurrentRect ( undefined ) ;
272232 }
273233 }
0 commit comments