@@ -83,6 +83,10 @@ new Vue({
8383 canvas . addEventListener ( 'mousemove' , this . onMouseMove ) ;
8484 canvas . addEventListener ( 'click' , this . onMouseClick ) ;
8585 canvas . addEventListener ( 'mouseleave' , this . onMouseLeave ) ;
86+
87+ // 设置Canvas的尺寸和分辨率
88+ this . setupCanvasResolution ( '#screenshotCanvas' ) ;
89+ this . setupCanvasResolution ( '#hierarchyCanvas' ) ;
8690 } ,
8791 methods : {
8892 initPlatform ( ) {
@@ -240,31 +244,40 @@ new Vue({
240244 this . renderScreenshot ( cachedScreenshot ) ;
241245 }
242246 } ,
247+
248+ // 解决在高分辨率屏幕上,Canvas绘制的内容可能会显得模糊。这是因为Canvas的默认分辨率与屏幕的物理像素密度不匹配
249+ setupCanvasResolution ( selector ) {
250+ const canvas = this . $el . querySelector ( selector ) ;
251+ const dpr = window . devicePixelRatio || 1 ;
252+ const rect = canvas . getBoundingClientRect ( ) ;
253+ canvas . width = rect . width * dpr ;
254+ canvas . height = rect . height * dpr ;
255+ const ctx = canvas . getContext ( '2d' ) ;
256+ ctx . scale ( dpr , dpr ) ;
257+ } ,
243258 renderScreenshot ( base64Data ) {
244- const img = new Image ( ) ;
245- img . src = `data:image/png;base64,${ base64Data } ` ;
246- img . onload = ( ) => {
247- const canvas = this . $el . querySelector ( '#screenshotCanvas' ) ;
248- const ctx = canvas . getContext ( '2d' ) ;
259+ const img = new Image ( ) ;
260+ img . src = `data:image/png;base64,${ base64Data } ` ;
261+ img . onload = ( ) => {
262+ const canvas = this . $el . querySelector ( '#screenshotCanvas' ) ;
263+ const ctx = canvas . getContext ( '2d' ) ;
249264
250- const { clientWidth : canvasWidth , clientHeight : canvasHeight } = canvas ;
251- canvas . width = canvasWidth ;
252- canvas . height = canvasHeight ;
265+ const { clientWidth : canvasWidth , clientHeight : canvasHeight } = canvas ;
253266
254- const { width : imgWidth , height : imgHeight } = img ;
255- const scale = Math . min ( canvasWidth / imgWidth , canvasHeight / imgHeight ) ;
256- const x = ( canvasWidth - imgWidth * scale ) / 2 ;
257- const y = ( canvasHeight - imgHeight * scale ) / 2 ;
267+ this . setupCanvasResolution ( '#screenshotCanvas' ) ;
258268
259- this . screenshotTransform = { scale, offsetX : x , offsetY : y } ;
269+ const { width : imgWidth , height : imgHeight } = img ;
270+ const scale = Math . min ( canvasWidth / imgWidth , canvasHeight / imgHeight ) ;
271+ const x = ( canvasWidth - imgWidth * scale ) / 2 ;
272+ const y = ( canvasHeight - imgHeight * scale ) / 2 ;
260273
261- ctx . clearRect ( 0 , 0 , canvasWidth , canvasHeight ) ;
262- ctx . drawImage ( img , x , y , imgWidth * scale , imgHeight * scale ) ;
274+ this . screenshotTransform = { scale, offsetX : x , offsetY : y } ;
263275
264- const hierarchyCanvas = this . $el . querySelector ( '#hierarchyCanvas' ) ;
265- hierarchyCanvas . width = canvasWidth ;
266- hierarchyCanvas . height = canvasHeight ;
267- } ;
276+ ctx . clearRect ( 0 , 0 , canvasWidth , canvasHeight ) ;
277+ ctx . drawImage ( img , x , y , imgWidth * scale , imgHeight * scale ) ;
278+
279+ this . setupCanvasResolution ( '#hierarchyCanvas' ) ;
280+ } ;
268281 } ,
269282 findSmallestNode ( node , mouseX , mouseY , scale , offsetX , offsetY ) {
270283 let smallestNode = null ;
0 commit comments