@logicflow/extension 2.1.10 → 2.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -110,10 +110,14 @@ export declare class Snapshot {
110
110
  */
111
111
  private getClassRules;
112
112
  /**
113
- * 根据浏览器类型获取Canvas尺寸限制
114
- * @returns 包含最大主维度和次维度的对象
113
+ * 根据浏览器对 canvas 的大小限制,计算等比缩放比例
114
+ * - 参考 MDN 最大的画布尺寸:
115
+ * https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/canvas#最大的画布尺寸
116
+ *
117
+ * @param width
118
+ * @param height
115
119
  */
116
- private getCanvasDimensionsByBrowser;
120
+ private getCanvasScaleRatio;
117
121
  /**
118
122
  * 将 svg 转化为 canvas
119
123
  * @param svg - svg 元素
@@ -345,24 +345,28 @@ var Snapshot = /** @class */ (function () {
345
345
  return rules;
346
346
  };
347
347
  /**
348
- * 根据浏览器类型获取Canvas尺寸限制
349
- * @returns 包含最大主维度和次维度的对象
348
+ * 根据浏览器对 canvas 的大小限制,计算等比缩放比例
349
+ * - 参考 MDN 最大的画布尺寸:
350
+ * https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/canvas#最大的画布尺寸
351
+ *
352
+ * @param width
353
+ * @param height
350
354
  */
351
- Snapshot.prototype.getCanvasDimensionsByBrowser = function () {
352
- var userAgent = navigator.userAgent;
353
- // 默认值
354
- var maxCanvasDimension = 65535;
355
- var otherMaxCanvasDimension = 4096;
356
- if (userAgent.indexOf('Chrome') !== -1 ||
357
- userAgent.indexOf('Edge') !== -1) {
358
- maxCanvasDimension = 65535;
359
- otherMaxCanvasDimension = 4096;
355
+ Snapshot.prototype.getCanvasScaleRatio = function (width, height) {
356
+ /* 单边最大像素 */
357
+ var maxCanvasSide = 32767;
358
+ /* 最大像素面积 */
359
+ var maxCanvasArea = 268435456;
360
+ var area = width * height;
361
+ if (width <= maxCanvasSide &&
362
+ height <= maxCanvasSide &&
363
+ area <= maxCanvasArea) {
364
+ return 1;
360
365
  }
361
- else if (userAgent.indexOf('Firefox') !== -1) {
362
- maxCanvasDimension = 32767;
363
- otherMaxCanvasDimension = 3814;
364
- }
365
- return { maxCanvasDimension: maxCanvasDimension, otherMaxCanvasDimension: otherMaxCanvasDimension };
366
+ var widthScale = maxCanvasSide / width;
367
+ var heightScale = maxCanvasSide / height;
368
+ var areaScale = maxCanvasArea / area;
369
+ return Math.min(widthScale, heightScale, areaScale);
366
370
  };
367
371
  /**
368
372
  * 将 svg 转化为 canvas
@@ -372,14 +376,14 @@ var Snapshot = /** @class */ (function () {
372
376
  */
373
377
  Snapshot.prototype.getCanvasData = function (svg, toImageOptions) {
374
378
  return __awaiter(this, void 0, void 0, function () {
375
- var width, height, backgroundColor, _a, padding, copy, dpr, base, bbox, layoutCanvas, layout, offsetX, offsetY, graphModel, transformModel, SCALE_X, SCALE_Y, TRANSLATE_X, TRANSLATE_Y, safetyFactor, actualWidth, actualHeight, factorWidth, factorHeight, bboxWidth, bboxHeight, canvas, safetyMargin, _b, maxCanvasDimension, otherMaxCanvasDimension, MAX_CANVAS_DIMENSION, OTHER_MAX_CANVAS_DIMENSION, targetWidth, targetHeight, scaleWidth, scaleHeight, ctx, img, style, foreignObject;
376
- return __generator(this, function (_c) {
377
- switch (_c.label) {
379
+ var width, height, backgroundColor, _a, padding, copy, dpr, base, bbox, layoutCanvas, layout, offsetX, offsetY, graphModel, transformModel, SCALE_X, SCALE_Y, TRANSLATE_X, TRANSLATE_Y, safetyFactor, actualWidth, actualHeight, factorWidth, factorHeight, bboxWidth, bboxHeight, canvas, safetyMargin, targetWidth, targetHeight, scaleRatio, ctx, img, style, foreignObject;
380
+ return __generator(this, function (_b) {
381
+ switch (_b.label) {
378
382
  case 0:
379
383
  width = toImageOptions.width, height = toImageOptions.height, backgroundColor = toImageOptions.backgroundColor, _a = toImageOptions.padding, padding = _a === void 0 ? 40 : _a;
380
384
  return [4 /*yield*/, this.cloneSvg(svg, false)];
381
385
  case 1:
382
- copy = _c.sent();
386
+ copy = _b.sent();
383
387
  dpr = window.devicePixelRatio || 1;
384
388
  if (dpr < 1) {
385
389
  // https://github.com/didi/LogicFlow/issues/1222
@@ -417,54 +421,29 @@ var Snapshot = /** @class */ (function () {
417
421
  canvas.style.height = "".concat(bboxHeight, "px");
418
422
  safetyMargin = toImageOptions.safetyMargin || 0 // 额外的安全边距
419
423
  ;
420
- _b = this.getCanvasDimensionsByBrowser(), maxCanvasDimension = _b.maxCanvasDimension, otherMaxCanvasDimension = _b.otherMaxCanvasDimension;
421
- MAX_CANVAS_DIMENSION = maxCanvasDimension;
422
- OTHER_MAX_CANVAS_DIMENSION = otherMaxCanvasDimension;
423
424
  targetWidth = bboxWidth * dpr;
424
425
  targetHeight = bboxHeight * dpr;
425
- scaleWidth = 1 //宽 缩放
426
- ;
427
- scaleHeight = 1 //高 缩放
428
- ;
429
- // 对宽和高分别进行缩放,如chrome,矩形单边最大宽度不超过65535,如宽超过65535,那么高不能超过4096,否则像素会超,也会显示不出。
430
- if (targetWidth > MAX_CANVAS_DIMENSION &&
431
- targetHeight > OTHER_MAX_CANVAS_DIMENSION) {
432
- scaleWidth = MAX_CANVAS_DIMENSION / targetWidth;
433
- scaleHeight = OTHER_MAX_CANVAS_DIMENSION / targetHeight;
434
- }
435
- else if (targetWidth > OTHER_MAX_CANVAS_DIMENSION &&
436
- targetHeight > MAX_CANVAS_DIMENSION) {
437
- scaleWidth = OTHER_MAX_CANVAS_DIMENSION / targetWidth;
438
- scaleHeight = MAX_CANVAS_DIMENSION / targetHeight;
439
- }
440
- else if (targetWidth > MAX_CANVAS_DIMENSION &&
441
- targetHeight < OTHER_MAX_CANVAS_DIMENSION) {
442
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetWidth;
443
- }
444
- else if (targetWidth < OTHER_MAX_CANVAS_DIMENSION &&
445
- targetHeight > MAX_CANVAS_DIMENSION) {
446
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetHeight;
447
- }
448
- if (scaleWidth < 1 || scaleHeight < 1) {
449
- targetWidth = Math.floor(targetWidth * scaleWidth);
450
- targetHeight = Math.floor(targetHeight * scaleHeight);
426
+ scaleRatio = this.getCanvasScaleRatio(targetWidth, targetHeight);
427
+ if (scaleRatio < 1) {
428
+ targetWidth = Math.floor(targetWidth * scaleRatio);
429
+ targetHeight = Math.floor(targetHeight * scaleRatio);
451
430
  }
452
431
  // 将导出区域移动到左上角,canvas 绘制的时候是从左上角开始绘制的
453
432
  // 在transform矩阵中加入padding值,确保左侧元素不会被截断
454
433
  // 对这个矩阵进行缩放,否则会导致截断
455
434
  ;
456
435
  copy.lastChild.style.transform =
457
- "matrix(".concat(scaleWidth, ", 0, 0, ").concat(scaleHeight, ", ").concat((-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleWidth +
436
+ "matrix(".concat(scaleRatio, ", 0, 0, ").concat(scaleRatio, ", ").concat((-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleRatio +
458
437
  padding +
459
438
  factorWidth / 2 +
460
- safetyMargin, ", ").concat((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleHeight + padding + factorHeight / 2 + safetyMargin, ")");
439
+ safetyMargin, ", ").concat((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleRatio + padding + factorHeight / 2 + safetyMargin, ")");
461
440
  canvas.width = targetWidth + (padding + safetyMargin) * 2 * dpr;
462
441
  canvas.height = targetHeight + (padding + safetyMargin) * 2 * dpr;
463
442
  ctx = canvas.getContext('2d');
464
443
  if (ctx) {
465
444
  // 清空canvas
466
445
  ctx.clearRect(0, 0, canvas.width, canvas.height);
467
- ctx.scale(dpr * scaleWidth, dpr * scaleHeight);
446
+ ctx.scale(dpr, dpr);
468
447
  // 如果有背景色,设置流程图导出的背景色
469
448
  if (backgroundColor) {
470
449
  ctx.fillStyle = backgroundColor;
@@ -110,10 +110,14 @@ export declare class Snapshot {
110
110
  */
111
111
  private getClassRules;
112
112
  /**
113
- * 根据浏览器类型获取Canvas尺寸限制
114
- * @returns 包含最大主维度和次维度的对象
113
+ * 根据浏览器对 canvas 的大小限制,计算等比缩放比例
114
+ * - 参考 MDN 最大的画布尺寸:
115
+ * https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/canvas#最大的画布尺寸
116
+ *
117
+ * @param width
118
+ * @param height
115
119
  */
116
- private getCanvasDimensionsByBrowser;
120
+ private getCanvasScaleRatio;
117
121
  /**
118
122
  * 将 svg 转化为 canvas
119
123
  * @param svg - svg 元素
@@ -348,24 +348,28 @@ var Snapshot = /** @class */ (function () {
348
348
  return rules;
349
349
  };
350
350
  /**
351
- * 根据浏览器类型获取Canvas尺寸限制
352
- * @returns 包含最大主维度和次维度的对象
351
+ * 根据浏览器对 canvas 的大小限制,计算等比缩放比例
352
+ * - 参考 MDN 最大的画布尺寸:
353
+ * https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/canvas#最大的画布尺寸
354
+ *
355
+ * @param width
356
+ * @param height
353
357
  */
354
- Snapshot.prototype.getCanvasDimensionsByBrowser = function () {
355
- var userAgent = navigator.userAgent;
356
- // 默认值
357
- var maxCanvasDimension = 65535;
358
- var otherMaxCanvasDimension = 4096;
359
- if (userAgent.indexOf('Chrome') !== -1 ||
360
- userAgent.indexOf('Edge') !== -1) {
361
- maxCanvasDimension = 65535;
362
- otherMaxCanvasDimension = 4096;
358
+ Snapshot.prototype.getCanvasScaleRatio = function (width, height) {
359
+ /* 单边最大像素 */
360
+ var maxCanvasSide = 32767;
361
+ /* 最大像素面积 */
362
+ var maxCanvasArea = 268435456;
363
+ var area = width * height;
364
+ if (width <= maxCanvasSide &&
365
+ height <= maxCanvasSide &&
366
+ area <= maxCanvasArea) {
367
+ return 1;
363
368
  }
364
- else if (userAgent.indexOf('Firefox') !== -1) {
365
- maxCanvasDimension = 32767;
366
- otherMaxCanvasDimension = 3814;
367
- }
368
- return { maxCanvasDimension: maxCanvasDimension, otherMaxCanvasDimension: otherMaxCanvasDimension };
369
+ var widthScale = maxCanvasSide / width;
370
+ var heightScale = maxCanvasSide / height;
371
+ var areaScale = maxCanvasArea / area;
372
+ return Math.min(widthScale, heightScale, areaScale);
369
373
  };
370
374
  /**
371
375
  * 将 svg 转化为 canvas
@@ -375,14 +379,14 @@ var Snapshot = /** @class */ (function () {
375
379
  */
376
380
  Snapshot.prototype.getCanvasData = function (svg, toImageOptions) {
377
381
  return __awaiter(this, void 0, void 0, function () {
378
- var width, height, backgroundColor, _a, padding, copy, dpr, base, bbox, layoutCanvas, layout, offsetX, offsetY, graphModel, transformModel, SCALE_X, SCALE_Y, TRANSLATE_X, TRANSLATE_Y, safetyFactor, actualWidth, actualHeight, factorWidth, factorHeight, bboxWidth, bboxHeight, canvas, safetyMargin, _b, maxCanvasDimension, otherMaxCanvasDimension, MAX_CANVAS_DIMENSION, OTHER_MAX_CANVAS_DIMENSION, targetWidth, targetHeight, scaleWidth, scaleHeight, ctx, img, style, foreignObject;
379
- return __generator(this, function (_c) {
380
- switch (_c.label) {
382
+ var width, height, backgroundColor, _a, padding, copy, dpr, base, bbox, layoutCanvas, layout, offsetX, offsetY, graphModel, transformModel, SCALE_X, SCALE_Y, TRANSLATE_X, TRANSLATE_Y, safetyFactor, actualWidth, actualHeight, factorWidth, factorHeight, bboxWidth, bboxHeight, canvas, safetyMargin, targetWidth, targetHeight, scaleRatio, ctx, img, style, foreignObject;
383
+ return __generator(this, function (_b) {
384
+ switch (_b.label) {
381
385
  case 0:
382
386
  width = toImageOptions.width, height = toImageOptions.height, backgroundColor = toImageOptions.backgroundColor, _a = toImageOptions.padding, padding = _a === void 0 ? 40 : _a;
383
387
  return [4 /*yield*/, this.cloneSvg(svg, false)];
384
388
  case 1:
385
- copy = _c.sent();
389
+ copy = _b.sent();
386
390
  dpr = window.devicePixelRatio || 1;
387
391
  if (dpr < 1) {
388
392
  // https://github.com/didi/LogicFlow/issues/1222
@@ -420,54 +424,29 @@ var Snapshot = /** @class */ (function () {
420
424
  canvas.style.height = "".concat(bboxHeight, "px");
421
425
  safetyMargin = toImageOptions.safetyMargin || 0 // 额外的安全边距
422
426
  ;
423
- _b = this.getCanvasDimensionsByBrowser(), maxCanvasDimension = _b.maxCanvasDimension, otherMaxCanvasDimension = _b.otherMaxCanvasDimension;
424
- MAX_CANVAS_DIMENSION = maxCanvasDimension;
425
- OTHER_MAX_CANVAS_DIMENSION = otherMaxCanvasDimension;
426
427
  targetWidth = bboxWidth * dpr;
427
428
  targetHeight = bboxHeight * dpr;
428
- scaleWidth = 1 //宽 缩放
429
- ;
430
- scaleHeight = 1 //高 缩放
431
- ;
432
- // 对宽和高分别进行缩放,如chrome,矩形单边最大宽度不超过65535,如宽超过65535,那么高不能超过4096,否则像素会超,也会显示不出。
433
- if (targetWidth > MAX_CANVAS_DIMENSION &&
434
- targetHeight > OTHER_MAX_CANVAS_DIMENSION) {
435
- scaleWidth = MAX_CANVAS_DIMENSION / targetWidth;
436
- scaleHeight = OTHER_MAX_CANVAS_DIMENSION / targetHeight;
437
- }
438
- else if (targetWidth > OTHER_MAX_CANVAS_DIMENSION &&
439
- targetHeight > MAX_CANVAS_DIMENSION) {
440
- scaleWidth = OTHER_MAX_CANVAS_DIMENSION / targetWidth;
441
- scaleHeight = MAX_CANVAS_DIMENSION / targetHeight;
442
- }
443
- else if (targetWidth > MAX_CANVAS_DIMENSION &&
444
- targetHeight < OTHER_MAX_CANVAS_DIMENSION) {
445
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetWidth;
446
- }
447
- else if (targetWidth < OTHER_MAX_CANVAS_DIMENSION &&
448
- targetHeight > MAX_CANVAS_DIMENSION) {
449
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetHeight;
450
- }
451
- if (scaleWidth < 1 || scaleHeight < 1) {
452
- targetWidth = Math.floor(targetWidth * scaleWidth);
453
- targetHeight = Math.floor(targetHeight * scaleHeight);
429
+ scaleRatio = this.getCanvasScaleRatio(targetWidth, targetHeight);
430
+ if (scaleRatio < 1) {
431
+ targetWidth = Math.floor(targetWidth * scaleRatio);
432
+ targetHeight = Math.floor(targetHeight * scaleRatio);
454
433
  }
455
434
  // 将导出区域移动到左上角,canvas 绘制的时候是从左上角开始绘制的
456
435
  // 在transform矩阵中加入padding值,确保左侧元素不会被截断
457
436
  // 对这个矩阵进行缩放,否则会导致截断
458
437
  ;
459
438
  copy.lastChild.style.transform =
460
- "matrix(".concat(scaleWidth, ", 0, 0, ").concat(scaleHeight, ", ").concat((-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleWidth +
439
+ "matrix(".concat(scaleRatio, ", 0, 0, ").concat(scaleRatio, ", ").concat((-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleRatio +
461
440
  padding +
462
441
  factorWidth / 2 +
463
- safetyMargin, ", ").concat((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleHeight + padding + factorHeight / 2 + safetyMargin, ")");
442
+ safetyMargin, ", ").concat((-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleRatio + padding + factorHeight / 2 + safetyMargin, ")");
464
443
  canvas.width = targetWidth + (padding + safetyMargin) * 2 * dpr;
465
444
  canvas.height = targetHeight + (padding + safetyMargin) * 2 * dpr;
466
445
  ctx = canvas.getContext('2d');
467
446
  if (ctx) {
468
447
  // 清空canvas
469
448
  ctx.clearRect(0, 0, canvas.width, canvas.height);
470
- ctx.scale(dpr * scaleWidth, dpr * scaleHeight);
449
+ ctx.scale(dpr, dpr);
471
450
  // 如果有背景色,设置流程图导出的背景色
472
451
  if (backgroundColor) {
473
452
  ctx.fillStyle = backgroundColor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logicflow/extension",
3
- "version": "2.1.10",
3
+ "version": "2.1.11",
4
4
  "description": "LogicFlow Extensions",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -20,8 +20,8 @@
20
20
  "author": "Logicflow-Team",
21
21
  "license": "Apache-2.0",
22
22
  "peerDependencies": {
23
- "@logicflow/vue-node-registry": "1.1.9",
24
- "@logicflow/core": "2.1.8"
23
+ "@logicflow/vue-node-registry": "1.1.10",
24
+ "@logicflow/core": "2.1.9"
25
25
  },
26
26
  "dependencies": {
27
27
  "@antv/hierarchy": "^0.6.11",
@@ -32,8 +32,8 @@
32
32
  "preact": "^10.17.1",
33
33
  "rangy": "^1.3.1",
34
34
  "vanilla-picker": "^2.12.3",
35
- "@logicflow/core": "2.1.8",
36
- "@logicflow/vue-node-registry": "1.1.9"
35
+ "@logicflow/core": "2.1.9",
36
+ "@logicflow/vue-node-registry": "1.1.10"
37
37
  },
38
38
  "devDependencies": {
39
39
  "less": "^4.1.1",
@@ -315,31 +315,34 @@ export class Snapshot {
315
315
  }
316
316
 
317
317
  /**
318
- * 根据浏览器类型获取Canvas尺寸限制
319
- * @returns 包含最大主维度和次维度的对象
318
+ * 根据浏览器对 canvas 的大小限制,计算等比缩放比例
319
+ * - 参考 MDN 最大的画布尺寸:
320
+ * https://developer.mozilla.org/zh-CN/docs/Web/HTML/Reference/Elements/canvas#最大的画布尺寸
321
+ *
322
+ * @param width
323
+ * @param height
320
324
  */
321
- private getCanvasDimensionsByBrowser(): {
322
- maxCanvasDimension: number
323
- otherMaxCanvasDimension: number
324
- } {
325
- const userAgent = navigator.userAgent
325
+ private getCanvasScaleRatio(width: number, height: number): number {
326
+ /* 单边最大像素 */
327
+ const maxCanvasSide = 32767
328
+ /* 最大像素面积 */
329
+ const maxCanvasArea = 268435456
326
330
 
327
- // 默认值
328
- let maxCanvasDimension = 65535
329
- let otherMaxCanvasDimension = 4096
331
+ const area = width * height
330
332
 
331
333
  if (
332
- userAgent.indexOf('Chrome') !== -1 ||
333
- userAgent.indexOf('Edge') !== -1
334
+ width <= maxCanvasSide &&
335
+ height <= maxCanvasSide &&
336
+ area <= maxCanvasArea
334
337
  ) {
335
- maxCanvasDimension = 65535
336
- otherMaxCanvasDimension = 4096
337
- } else if (userAgent.indexOf('Firefox') !== -1) {
338
- maxCanvasDimension = 32767
339
- otherMaxCanvasDimension = 3814
338
+ return 1
340
339
  }
341
340
 
342
- return { maxCanvasDimension, otherMaxCanvasDimension }
341
+ const widthScale = maxCanvasSide / width
342
+ const heightScale = maxCanvasSide / height
343
+ const areaScale = maxCanvasArea / area
344
+
345
+ return Math.min(widthScale, heightScale, areaScale)
343
346
  }
344
347
 
345
348
  /**
@@ -406,62 +409,33 @@ export class Snapshot {
406
409
  // 为宽画布添加额外的安全边距,确保不会裁剪
407
410
  const safetyMargin = toImageOptions.safetyMargin || 0 // 额外的安全边距
408
411
 
409
- // 获取当前浏览器类型,不同浏览器对canvas的限制不同
410
- const { maxCanvasDimension, otherMaxCanvasDimension } =
411
- this.getCanvasDimensionsByBrowser()
412
- const MAX_CANVAS_DIMENSION = maxCanvasDimension
413
- const OTHER_MAX_CANVAS_DIMENSION = otherMaxCanvasDimension
414
-
415
412
  let targetWidth = bboxWidth * dpr
416
413
  let targetHeight = bboxHeight * dpr
417
- let scaleWidth = 1 //宽 缩放
418
- let scaleHeight = 1 //高 缩放
419
- // 对宽和高分别进行缩放,如chrome,矩形单边最大宽度不超过65535,如宽超过65535,那么高不能超过4096,否则像素会超,也会显示不出。
420
- if (
421
- targetWidth > MAX_CANVAS_DIMENSION &&
422
- targetHeight > OTHER_MAX_CANVAS_DIMENSION
423
- ) {
424
- scaleWidth = MAX_CANVAS_DIMENSION / targetWidth
425
- scaleHeight = OTHER_MAX_CANVAS_DIMENSION / targetHeight
426
- } else if (
427
- targetWidth > OTHER_MAX_CANVAS_DIMENSION &&
428
- targetHeight > MAX_CANVAS_DIMENSION
429
- ) {
430
- scaleWidth = OTHER_MAX_CANVAS_DIMENSION / targetWidth
431
- scaleHeight = MAX_CANVAS_DIMENSION / targetHeight
432
- } else if (
433
- targetWidth > MAX_CANVAS_DIMENSION &&
434
- targetHeight < OTHER_MAX_CANVAS_DIMENSION
435
- ) {
436
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetWidth
437
- } else if (
438
- targetWidth < OTHER_MAX_CANVAS_DIMENSION &&
439
- targetHeight > MAX_CANVAS_DIMENSION
440
- ) {
441
- scaleHeight = scaleWidth = MAX_CANVAS_DIMENSION / targetHeight
442
- }
443
414
 
444
- if (scaleWidth < 1 || scaleHeight < 1) {
445
- targetWidth = Math.floor(targetWidth * scaleWidth)
446
- targetHeight = Math.floor(targetHeight * scaleHeight)
415
+ // 超出 canvas 大小限制时,进行等比缩放
416
+ const scaleRatio = this.getCanvasScaleRatio(targetWidth, targetHeight)
417
+ if (scaleRatio < 1) {
418
+ targetWidth = Math.floor(targetWidth * scaleRatio)
419
+ targetHeight = Math.floor(targetHeight * scaleRatio)
447
420
  }
421
+
448
422
  // 将导出区域移动到左上角,canvas 绘制的时候是从左上角开始绘制的
449
423
  // 在transform矩阵中加入padding值,确保左侧元素不会被截断
450
424
  // 对这个矩阵进行缩放,否则会导致截断
451
425
  ;(copy.lastChild as SVGElement).style.transform =
452
- `matrix(${scaleWidth}, 0, 0, ${scaleHeight}, ${
453
- (-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleWidth +
426
+ `matrix(${scaleRatio}, 0, 0, ${scaleRatio}, ${
427
+ (-offsetX + TRANSLATE_X) * (1 / SCALE_X) * scaleRatio +
454
428
  padding +
455
429
  factorWidth / 2 +
456
430
  safetyMargin
457
- }, ${(-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleHeight + padding + factorHeight / 2 + safetyMargin})`
431
+ }, ${(-offsetY + TRANSLATE_Y) * (1 / SCALE_Y) * scaleRatio + padding + factorHeight / 2 + safetyMargin})`
458
432
  canvas.width = targetWidth + (padding + safetyMargin) * 2 * dpr
459
433
  canvas.height = targetHeight + (padding + safetyMargin) * 2 * dpr
460
434
  const ctx = canvas.getContext('2d')
461
435
  if (ctx) {
462
436
  // 清空canvas
463
437
  ctx.clearRect(0, 0, canvas.width, canvas.height)
464
- ctx.scale(dpr * scaleWidth, dpr * scaleHeight)
438
+ ctx.scale(dpr, dpr)
465
439
  // 如果有背景色,设置流程图导出的背景色
466
440
  if (backgroundColor) {
467
441
  ctx.fillStyle = backgroundColor