@@ -4825,8 +4825,8 @@ function startCameraPreview() {
48254825 let cachedDrawParams = null ;
48264826
48274827 function updateDrawParams ( ) {
4828- const canvasW = DRAW_CONFIG . canvasW ;
4829- const canvasH = DRAW_CONFIG . canvasH ;
4828+ const screenW = DRAW_CONFIG . screenW ;
4829+ const screenH = DRAW_CONFIG . screenH ;
48304830 const videoW = video . videoWidth ;
48314831 const videoH = video . videoHeight ;
48324832
@@ -4839,24 +4839,24 @@ function startCameraPreview() {
48394839 const effectiveVideoH = isRotated ? videoW : videoH ;
48404840
48414841 const videoRatio = effectiveVideoW / effectiveVideoH ;
4842- const canvasRatio = canvasW / canvasH ;
4842+ const screenRatio = screenW / screenH ;
48434843
48444844 let drawW , drawH ;
4845- if ( videoRatio > canvasRatio ) {
4846- drawW = canvasW ;
4847- drawH = canvasW / videoRatio ;
4845+ if ( videoRatio > screenRatio ) {
4846+ drawW = screenW ;
4847+ drawH = screenW / videoRatio ;
48484848 } else {
4849- drawH = canvasH ;
4850- drawW = canvasH * videoRatio ;
4849+ drawH = screenH ;
4850+ drawW = screenH * videoRatio ;
48514851 }
48524852
4853- const drawX = ( canvasW - drawW ) / 2 ;
4854- const drawY = ( canvasH - drawH ) / 2 ;
4853+ const drawX = ( screenW - drawW ) / 2 ;
4854+ const drawY = ( screenH - drawH ) / 2 ;
48554855
48564856 return {
4857- canvasW , canvasH , drawW, drawH, drawX, drawY,
4858- centerX : canvasW / 2 ,
4859- centerY : canvasH / 2 ,
4857+ screenW , screenH , drawW, drawH, drawX, drawY,
4858+ centerX : screenW / 2 ,
4859+ centerY : screenH / 2 ,
48604860 rotation,
48614861 isRotated
48624862 } ;
@@ -4888,13 +4888,17 @@ function startCameraPreview() {
48884888 }
48894889
48904890 if ( cachedDrawParams ) {
4891- const { canvasW , canvasH , drawW, drawH, drawX, drawY, centerX, centerY, rotation, isRotated } = cachedDrawParams ;
4891+ const { screenW , screenH , drawW, drawH, drawX, drawY, centerX, centerY, rotation, isRotated } = cachedDrawParams ;
48924892
4893- // 只清除需要更新的区域,减少绘制操作
4894- dom . imageCtx . clearRect ( drawX , drawY , drawW , drawH ) ;
4893+ // 清除整个图像层(因为 Canvas 尺寸可能比屏幕大)
4894+ dom . imageCtx . clearRect ( 0 , 0 , DRAW_CONFIG . canvasW , DRAW_CONFIG . canvasH ) ;
4895+
4896+ // 计算 Canvas 到屏幕的缩放比例
4897+ const scaleX = DRAW_CONFIG . canvasW / screenW ;
4898+ const scaleY = DRAW_CONFIG . canvasH / screenH ;
48954899
48964900 dom . imageCtx . save ( ) ;
4897- dom . imageCtx . translate ( centerX , centerY ) ;
4901+ dom . imageCtx . translate ( centerX * scaleX , centerY * scaleY ) ;
48984902
48994903 if ( state . isMirrored ) {
49004904 dom . imageCtx . scale ( - 1 , 1 ) ;
@@ -4904,10 +4908,11 @@ function startCameraPreview() {
49044908
49054909 dom . imageCtx . imageSmoothingQuality = DRAW_CONFIG . imageSmoothingQuality ;
49064910
4911+ // 绘制到 Canvas 上,需要缩放到 Canvas 尺寸
49074912 if ( isRotated ) {
4908- dom . imageCtx . drawImage ( video , - drawH / 2 , - drawW / 2 , drawH , drawW ) ;
4913+ dom . imageCtx . drawImage ( video , - drawH / 2 * scaleX , - drawW / 2 * scaleY , drawH * scaleX , drawW * scaleY ) ;
49094914 } else {
4910- dom . imageCtx . drawImage ( video , - drawW / 2 , - drawH / 2 , drawW , drawH ) ;
4915+ dom . imageCtx . drawImage ( video , - drawW / 2 * scaleX , - drawH / 2 * scaleY , drawW * scaleX , drawH * scaleY ) ;
49114916 }
49124917
49134918 dom . imageCtx . restore ( ) ;
@@ -5270,24 +5275,31 @@ function drawImageToCenter(img) {
52705275 clearImageLayer ( ) ;
52715276 hideNoCameraMessage ( ) ;
52725277
5273- const canvasW = DRAW_CONFIG . canvasW ;
5274- const canvasH = DRAW_CONFIG . canvasH ;
5278+ const screenW = DRAW_CONFIG . screenW ;
5279+ const screenH = DRAW_CONFIG . screenH ;
52755280
52765281 const imgRatio = img . width / img . height ;
5277- const canvasRatio = canvasW / canvasH ;
5282+ const screenRatio = screenW / screenH ;
52785283
52795284 let drawW , drawH , drawX , drawY ;
52805285
5281- if ( imgRatio > canvasRatio ) {
5282- drawW = canvasW ;
5283- drawH = canvasW / imgRatio ;
5286+ if ( imgRatio > screenRatio ) {
5287+ drawW = screenW ;
5288+ drawH = screenW / imgRatio ;
52845289 } else {
5285- drawH = canvasH ;
5286- drawW = canvasH * imgRatio ;
5290+ drawH = screenH ;
5291+ drawW = screenH * imgRatio ;
52875292 }
52885293
5289- drawX = ( canvasW - drawW ) / 2 ;
5290- drawY = ( canvasH - drawH ) / 2 ;
5294+ // 计算 Canvas 到屏幕的缩放比例
5295+ const scaleX = DRAW_CONFIG . canvasW / screenW ;
5296+ const scaleY = DRAW_CONFIG . canvasH / screenH ;
5297+
5298+ // 转换到 Canvas 坐标
5299+ drawX = drawX * scaleX ;
5300+ drawY = drawY * scaleY ;
5301+ drawW = drawW * scaleX ;
5302+ drawH = drawH * scaleY ;
52915303
52925304 dom . imageCtx . drawImage ( img , drawX , drawY , drawW , drawH ) ;
52935305}
0 commit comments