Skip to content

Commit f42f371

Browse files
author
fanyang
committed
fix(extension): 删去snapshot无用代码和修复文档存在的错误
1 parent 7f696bc commit f42f371

File tree

3 files changed

+15
-27
lines changed

3 files changed

+15
-27
lines changed

packages/extension/src/tools/snapshot/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ getSnapshot(fileName?: string, toImageOptions?: ToImageOptions) : Promise<void>
8282
| width | number | - | | 自定义导出图片的宽度,不设置即可,设置可能会拉伸图形 |
8383
| height | numebr | - | | 自定义导出图片的宽度,不设置即可,设置可能会拉伸图形 |
8484
| backgroundColor | string | - | | 图片背景,不设置背景默认透明 |
85-
| quality | number | - | | 图片质量,在指定图片格式为 jpeg 或 webp 的情况下,可以从 0 到 1 的区间内选择图片的质量。如果超出取值范围,将会使用默认值 0.92。其他参数会被忽略。 |
85+
| quality | number | - | | 图片质量,在指定图片格式为 jpeg 或 webp 的情况下,可以从 0 到 1 的区间内选择图片的质量。如果超出取值范围,将会使用默认值 0.92。其他不合法参数会被忽略 |
8686
| padding | number | 40 | | 图片内边距: 元素内容所在区之外空白空间,不设置默认有40的内边距 |
87-
| partialElement | boolean | false | |开启局部渲染后,默认不会导出不在画布区域的元素,开启后,将会导出 |
87+
| partialElement | boolean | false | |开启局部渲染后,默认不会导出已经移出画布区域的元素,开启后,将会导出 |
8888

89-
注意, `svg`目前暂不支持`width``height``backgroundColor``padding` 属性。
89+
注意:
90+
- `svg`目前暂不支持`width``height``backgroundColor``padding` 属性。
91+
- 自定义宽高后,可能会拉伸图形,这时候`padding`也会被拉伸导致不准确。
9092

9193
### lf.getSnapshotBlob(...)
9294

@@ -112,7 +114,7 @@ console.log(blob)
112114
```tsx | pure
113115

114116
export type SnapshotResponse = {
115-
data: Blob | string
117+
data: Blob | string // Blob对象 或 Base64文本编码文本
116118
width: number // 图片宽度
117119
height: number // 图片高度
118120
}
@@ -121,7 +123,7 @@ export type SnapshotResponse = {
121123

122124
### lf.getSnapshotBase64(...)
123125

124-
获取`Base64文本编码`对象
126+
获取`Base64文本编码`文本
125127

126128
```ts
127129

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export class Snapshot {
250250
partialElement,
251251
} = toImageOptions
252252
// const copy = await this.cloneSvg(svg, toImageOptions)
253-
// TODO: 为什么用上面封装的cloneSvg代替下面的方式不行
253+
// TODO: question1:为什么用上面封装的cloneSvg代替下面的方式不行
254254
const partial = this.lf.graphModel.partial
255255
// 如何开启局部渲染,并要导出全部元素,需要临时关闭局部渲染
256256
partial && partialElement && (await this.lf.graphModel.setPartial(false))
@@ -335,6 +335,7 @@ export class Snapshot {
335335
ctx.clearRect(0, 0, canvas.width, canvas.height)
336336
}
337337
}
338+
// TODO: question1: 初步排查是css这块上移后不生效,但不知道为什么
338339
// 设置css样式
339340
const img = new Image()
340341
const style = document.createElement('style')
@@ -350,37 +351,29 @@ export class Snapshot {
350351
createImageBitmap(img, {
351352
resizeWidth:
352353
width && height
353-
? copyCanvas(canvas, width, height, padding, dpr).width
354+
? copyCanvas(canvas, width, height).width
354355
: canvas.width,
355356
resizeHeight:
356357
width && height
357-
? copyCanvas(canvas, width, height, padding, dpr).height
358+
? copyCanvas(canvas, width, height).height
358359
: canvas.height,
359360
}).then((imageBitmap) => {
360361
ctx?.drawImage(imageBitmap, padding / dpr, padding / dpr)
361362
resolve(
362-
width && height
363-
? copyCanvas(canvas, width, height, padding, dpr)
364-
: canvas,
363+
width && height ? copyCanvas(canvas, width, height) : canvas,
365364
)
366365
})
367366
} else {
368367
ctx?.drawImage(img, padding / dpr, padding / dpr)
369368
resolve(
370-
width && height
371-
? copyCanvas(canvas, width, height, padding, dpr)
372-
: canvas,
369+
width && height ? copyCanvas(canvas, width, height) : canvas,
373370
)
374371
}
375372
// 如果局部渲染本来是开启的,继续开启
376373
partial && this.lf.graphModel.setPartial(true)
377374
} catch (e) {
378375
ctx?.drawImage(img, padding / dpr, padding / dpr)
379-
resolve(
380-
width && height
381-
? copyCanvas(canvas, width, height, padding, dpr)
382-
: canvas,
383-
)
376+
resolve(width && height ? copyCanvas(canvas, width, height) : canvas)
384377
}
385378
}
386379

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,13 @@ export function copyCanvas(
142142
originCanvas: HTMLCanvasElement,
143143
targetWidth: number,
144144
targetHeight: number,
145-
padding: number,
146-
dpr: number,
147145
): HTMLCanvasElement {
148146
const newCanvas = document.createElement('canvas')
149147
newCanvas.width = targetWidth
150148
newCanvas.height = targetHeight
151-
152-
const scaleX = originCanvas.width / targetWidth
153-
const scaleY = originCanvas.height / targetHeight
154-
console.log(padding, dpr, scaleX, scaleY)
155-
156149
const newCtx = newCanvas.getContext('2d')
157150
if (newCtx) {
158-
// TODO: 自定义宽高时 padding异常
151+
// 注意: 自定义宽高时,可能会拉伸图形,这时候padding也会被拉伸导致不准确
159152
newCtx.drawImage(
160153
originCanvas,
161154
0,

0 commit comments

Comments
 (0)