Skip to content

Commit 8b0033f

Browse files
committed
fix(extension): 修复导出插件safetyMargin和safetyFactor导致的快照导出后不居中对齐问题 & 修改safetyFactor为1 safetyMargin为0
1 parent b3c57ce commit 8b0033f

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

  • packages/extension/src/tools/snapshot

packages/extension/src/tools/snapshot/index.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -389,29 +389,31 @@ export class Snapshot {
389389
// 计算实际宽高,考虑缩放因素
390390
// 在宽画布情况下,getBoundingClientRect可能无法获取到所有元素的边界
391391
// 因此我们添加一个安全系数来确保能够容纳所有元素
392-
const safetyFactor = toImageOptions.safetyFactor || 1.1 // 安全系数,增加10%的空间
393-
const actualWidth = (bbox.width / SCALE_X) * safetyFactor
394-
const actualHeight = (bbox.height / SCALE_Y) * safetyFactor
392+
const safetyFactor = toImageOptions.safetyFactor || 1 // 安全系数,增加10%的空间
393+
const actualWidth = bbox.width / SCALE_X
394+
const actualHeight = bbox.height / SCALE_Y
395+
const factorWidth = actualWidth * (safetyFactor - 1)
396+
const factorHeight = actualHeight * (safetyFactor - 1)
395397

396398
// 包含所有元素的最小宽高,确保足够大以容纳所有元素
397-
const bboxWidth = Math.ceil(actualWidth)
398-
const bboxHeight = Math.ceil(actualHeight)
399+
const bboxWidth = Math.ceil(actualWidth + factorWidth)
400+
const bboxHeight = Math.ceil(actualHeight + factorHeight)
399401
const canvas = document.createElement('canvas')
400402
canvas.style.width = `${bboxWidth}px`
401403
canvas.style.height = `${bboxHeight}px`
402404

403405
// 宽高值 默认加padding 40,保证图形不会紧贴着下载图片
404406
// 为宽画布添加额外的安全边距,确保不会裁剪
405-
const safetyMargin = toImageOptions.safetyMargin || 40 // 额外的安全边距
407+
const safetyMargin = toImageOptions.safetyMargin || 0 // 额外的安全边距
406408

407409
// 获取当前浏览器类型,不同浏览器对canvas的限制不同
408410
const { maxCanvasDimension, otherMaxCanvasDimension } =
409411
this.getCanvasDimensionsByBrowser()
410412
const MAX_CANVAS_DIMENSION = maxCanvasDimension
411413
const OTHER_MAX_CANVAS_DIMENSION = otherMaxCanvasDimension
412414

413-
let targetWidth = bboxWidth * dpr + padding * 2 + safetyMargin
414-
let targetHeight = bboxHeight * dpr + padding * 2 + safetyMargin
415+
let targetWidth = bboxWidth * dpr
416+
let targetHeight = bboxHeight * dpr
415417
let scaleWidth = 1 //宽 缩放
416418
let scaleHeight = 1 //高 缩放
417419
// 对宽和高分别进行缩放,如chrome,矩形单边最大宽度不超过65535,如宽超过65535,那么高不能超过4096,否则像素会超,也会显示不出。
@@ -448,12 +450,13 @@ export class Snapshot {
448450
// 对这个矩阵进行缩放,否则会导致截断
449451
;(copy.lastChild as SVGElement).style.transform =
450452
`matrix(${scaleWidth}, 0, 0, ${scaleHeight}, ${
451-
(-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleWidth + padding / dpr
452-
}, ${
453-
(-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleHeight + padding / dpr
454-
})`
455-
canvas.width = targetWidth
456-
canvas.height = targetHeight
453+
(-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleWidth +
454+
padding +
455+
factorWidth / 2 +
456+
safetyMargin
457+
}, ${(-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleHeight + padding + factorHeight / 2 + safetyMargin})`
458+
canvas.width = targetWidth + (padding + safetyMargin) * 2 * dpr
459+
canvas.height = targetHeight + (padding + safetyMargin) * 2 * dpr
457460
const ctx = canvas.getContext('2d')
458461
if (ctx) {
459462
// 清空canvas
@@ -678,12 +681,6 @@ export class Snapshot {
678681
fileType?: string,
679682
toImageOptions?: ToImageOptions,
680683
): Promise<SnapshotResponse> {
681-
console.log(
682-
'getSnapshotBase64---------------',
683-
backgroundColor,
684-
fileType,
685-
toImageOptions,
686-
)
687684
return await this.withExportPreparation(
688685
() => this._getSnapshotBase64(backgroundColor, fileType, toImageOptions),
689686
toImageOptions,

0 commit comments

Comments
 (0)