html2canvas-pro 2.4.4 → 2.4.5

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.
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.spreadShadowPath = exports.shadowRadius = void 0;
4
+ const bezier_curve_1 = require("../bezier-curve");
5
+ const vector_1 = require("../vector");
6
+ // CSS Backgrounds 3, 6.1.1: growing a small corner needs the cubic adjustment
7
+ // to approach a square continuously as the original radius approaches zero.
8
+ const shadowRadius = (radius, spread) => {
9
+ const delta = spread > radius ? spread * (1 + Math.pow(radius / spread - 1, 3)) : spread;
10
+ return Math.max(0, radius + delta);
11
+ };
12
+ exports.shadowRadius = shadowRadius;
13
+ // Input is the four clockwise border/padding corner segments from BoundCurves.
14
+ // Translation alone preserves the old radii and turns a spread circle into a
15
+ // rounded square. Resize each quarter ellipse around its box corner instead.
16
+ const spreadShadowPath = (paths, spread) => {
17
+ if (spread === 0)
18
+ return paths;
19
+ const corners = paths.map((point, index) => {
20
+ if (!(0, bezier_curve_1.isBezierCurve)(point))
21
+ return { x: point.x, y: point.y, rx: 0, ry: 0 };
22
+ return {
23
+ x: index % 2 === 0 ? point.start.x : point.end.x,
24
+ y: index % 2 === 0 ? point.end.y : point.start.y,
25
+ rx: Math.abs(point.end.x - point.start.x),
26
+ ry: Math.abs(point.end.y - point.start.y)
27
+ };
28
+ });
29
+ const width = corners[1].x - corners[0].x + 2 * spread;
30
+ const height = corners[3].y - corners[0].y + 2 * spread;
31
+ if (width <= 0 || height <= 0)
32
+ return [];
33
+ const radii = corners.map((c) => ({ x: (0, exports.shadowRadius)(c.rx, spread), y: (0, exports.shadowRadius)(c.ry, spread) }));
34
+ const ratio = (size, sum) => (sum > 0 ? size / sum : 1);
35
+ const factor = Math.min(1, ratio(width, radii[0].x + radii[1].x), ratio(width, radii[3].x + radii[2].x), ratio(height, radii[0].y + radii[3].y), ratio(height, radii[1].y + radii[2].y));
36
+ return paths.map((point, index) => {
37
+ const corner = corners[index];
38
+ const x = corner.x + (index === 1 || index === 2 ? spread : -spread);
39
+ const y = corner.y + (index >= 2 ? spread : -spread);
40
+ if (!(0, bezier_curve_1.isBezierCurve)(point))
41
+ return new vector_1.Vector(x, y);
42
+ const rx = radii[index].x * factor;
43
+ const ry = radii[index].y * factor;
44
+ if (!rx || !ry)
45
+ return new vector_1.Vector(x, y);
46
+ const sx = rx / corner.rx;
47
+ const sy = ry / corner.ry;
48
+ const resize = (v) => new vector_1.Vector(x + (v.x - corner.x) * sx, y + (v.y - corner.y) * sy);
49
+ return new bezier_curve_1.BezierCurve(resize(point.start), resize(point.startControl), resize(point.endControl), resize(point.end));
50
+ });
51
+ };
52
+ exports.spreadShadowPath = spreadShadowPath;
@@ -0,0 +1,194 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.paintBoxShadow = void 0;
4
+ const color_utilities_1 = require("../../css/types/color-utilities");
5
+ const constants_1 = require("../../core/constants");
6
+ const bound_curves_1 = require("../bound-curves");
7
+ const box_sizing_1 = require("../box-sizing");
8
+ const path_1 = require("../path");
9
+ const canvas_path_1 = require("./canvas-path");
10
+ const filter_surface_1 = require("./filter-surface");
11
+ const surface_bounds_1 = require("./surface-bounds");
12
+ const box_shadow_geometry_1 = require("./box-shadow-geometry");
13
+ const box_shadow_transform_1 = require("./box-shadow-transform");
14
+ // Separate closed subpaths plus even-odd filling avoid reversing only the order
15
+ // of corner Beziers, which does not reverse the curves themselves.
16
+ const complement = (ctx, rect, hole) => {
17
+ ctx.beginPath();
18
+ ctx.rect(rect.left, rect.top, rect.width, rect.height);
19
+ if (hole.length) {
20
+ (0, canvas_path_1.formatCanvasPath)(ctx, hole);
21
+ ctx.closePath();
22
+ }
23
+ };
24
+ const insetHole = (paint, shadow) => {
25
+ const spread = shadow.spread.number;
26
+ const box = (0, box_sizing_1.paddingBox)(paint.container);
27
+ if (box.width - 2 * spread <= 0 || box.height - 2 * spread <= 0)
28
+ return [];
29
+ return (0, box_shadow_geometry_1.spreadShadowPath)((0, bound_curves_1.calculatePaddingBoxPath)(paint.curves), -spread);
30
+ };
31
+ // Rasterize in local coordinates, blur once, and then let the destination CTM
32
+ // transform the complete shadow. This also preserves anisotropic/sheared blur.
33
+ const paintShadowSurface = async (ctx, paint, shadow, options, budget, space) => {
34
+ const padding = (0, box_sizing_1.paddingBox)(paint.container);
35
+ const blur = shadow.blur.number / 2;
36
+ const margin = Math.ceil(3 * blur) + 2;
37
+ const spread = shadow.spread.number;
38
+ const area = shadow.inset
39
+ ? padding
40
+ : paint.container.bounds.add(shadow.offsetX.number - spread, shadow.offsetY.number - spread, 2 * spread, 2 * spread);
41
+ const bounds = (0, surface_bounds_1.cropSurface)(area.add(-margin, -margin, 2 * margin, 2 * margin), space.viewport, margin, space.scale);
42
+ if (!bounds.width || !bounds.height)
43
+ return true;
44
+ const width = Math.ceil(bounds.width * space.scale);
45
+ const height = Math.ceil(bounds.height * space.scale);
46
+ const reserved = (0, surface_bounds_1.reserveSurface)(budget, width, height);
47
+ if (!reserved)
48
+ return false;
49
+ let source;
50
+ let filtered;
51
+ try {
52
+ source = ctx.canvas.ownerDocument.createElement('canvas');
53
+ source.width = width;
54
+ source.height = height;
55
+ const sourceCtx = source.getContext('2d');
56
+ if (!sourceCtx)
57
+ return false;
58
+ sourceCtx.scale(space.scale, space.scale);
59
+ sourceCtx.translate(-bounds.left, -bounds.top);
60
+ const silhouette = shadow.inset
61
+ ? insetHole(paint, shadow)
62
+ : (0, box_shadow_geometry_1.spreadShadowPath)((0, bound_curves_1.calculateBorderBoxPath)(paint.curves), spread);
63
+ const path = (0, path_1.transformPath)(silhouette, shadow.offsetX.number, shadow.offsetY.number, 0, 0);
64
+ if (shadow.inset)
65
+ complement(sourceCtx, bounds, path);
66
+ else
67
+ (0, canvas_path_1.createCanvasPath)(sourceCtx, path);
68
+ sourceCtx.fillStyle = (0, color_utilities_1.asString)(shadow.color);
69
+ sourceCtx.fill(shadow.inset ? 'evenodd' : 'nonzero');
70
+ filtered = await (0, filter_surface_1.renderFilterSurface)(source, { blur }, 1, space.scale, options.signal);
71
+ ctx.save();
72
+ try {
73
+ if (shadow.inset) {
74
+ (0, canvas_path_1.createCanvasPath)(ctx, (0, bound_curves_1.calculatePaddingBoxPath)(paint.curves));
75
+ ctx.clip();
76
+ }
77
+ else {
78
+ complement(ctx, space.viewport, (0, bound_curves_1.calculateBorderBoxPath)(paint.curves));
79
+ ctx.clip('evenodd');
80
+ }
81
+ ctx.drawImage(filtered, bounds.left, bounds.top, filtered.width / space.scale, filtered.height / space.scale);
82
+ }
83
+ finally {
84
+ ctx.restore();
85
+ }
86
+ return true;
87
+ }
88
+ catch (error) {
89
+ if (!(error instanceof filter_surface_1.FilterSurfaceError))
90
+ throw error;
91
+ return false;
92
+ }
93
+ finally {
94
+ if (filtered && filtered !== source)
95
+ (0, filter_surface_1.releaseSurface)(filtered);
96
+ if (source)
97
+ (0, filter_surface_1.releaseSurface)(source);
98
+ budget.pixels -= reserved;
99
+ }
100
+ };
101
+ const paintBoxShadow = async (ctx, paint, shadow, options, budget) => {
102
+ if (options.signal?.aborted)
103
+ throw new DOMException('The operation was aborted.', 'AbortError');
104
+ if ((0, color_utilities_1.isTransparent)(shadow.color))
105
+ return;
106
+ const spread = shadow.spread.number;
107
+ if (!shadow.inset &&
108
+ (paint.container.bounds.width + 2 * spread <= 0 || paint.container.bounds.height + 2 * spread <= 0))
109
+ return;
110
+ // getTransform includes capture scale, CSS transforms and their origins.
111
+ // Without it the CTM is unknown: approximate it with the capture transform
112
+ // and only ever move the mask relative to the real CTM (see below).
113
+ const knownTransform = typeof ctx.getTransform === 'function';
114
+ const matrix = knownTransform
115
+ ? ctx.getTransform()
116
+ : {
117
+ a: options.scale,
118
+ b: 0,
119
+ c: 0,
120
+ d: options.scale,
121
+ e: -options.x * options.scale,
122
+ f: -options.y * options.scale
123
+ };
124
+ const space = (0, box_shadow_transform_1.shadowSpace)(matrix, ctx.canvas.width, ctx.canvas.height);
125
+ // Singular transforms have zero painted area; never attempt to invert them.
126
+ if (!space)
127
+ return;
128
+ const { viewport } = space;
129
+ if (shadow.blur.number > 0 &&
130
+ (shadow.inset || !space.uniform) &&
131
+ (await paintShadowSurface(ctx, paint, shadow, options, budget, space)))
132
+ return;
133
+ const padding = (0, box_sizing_1.paddingBox)(paint.container);
134
+ ctx.save();
135
+ try {
136
+ if (shadow.inset && shadow.blur.number === 0) {
137
+ (0, canvas_path_1.createCanvasPath)(ctx, (0, bound_curves_1.calculatePaddingBoxPath)(paint.curves));
138
+ ctx.clip();
139
+ complement(ctx, padding, (0, path_1.transformPath)(insetHole(paint, shadow), shadow.offsetX.number, shadow.offsetY.number, 0, 0));
140
+ ctx.fillStyle = (0, color_utilities_1.asString)(shadow.color);
141
+ ctx.fill('evenodd');
142
+ return;
143
+ }
144
+ const margin = Math.ceil(shadow.blur.number * 2 +
145
+ Math.abs(spread) +
146
+ Math.abs(shadow.offsetX.number) +
147
+ Math.abs(shadow.offsetY.number)) + 2;
148
+ // Displace in OUTPUT pixels, not along a CSS axis. A rotated or reflected
149
+ // local x axis need not point off the left edge of the output bitmap.
150
+ const sourceBounds = shadow.inset
151
+ ? padding.add(-margin, -margin, 2 * margin, 2 * margin)
152
+ : paint.container.bounds.add(-spread, -spread, 2 * spread, 2 * spread);
153
+ const { a, b, c, d, e, f } = matrix;
154
+ const right = sourceBounds.left + sourceBounds.width;
155
+ const bottom = sourceBounds.top + sourceBounds.height;
156
+ const maxX = Math.max(a * sourceBounds.left + c * sourceBounds.top + e, a * right + c * sourceBounds.top + e, a * right + c * bottom + e, a * sourceBounds.left + c * bottom + e);
157
+ const displacement = Math.max(constants_1.SHADOW_MASK_OFFSET, maxX + margin * space.scale + 2);
158
+ if (shadow.inset) {
159
+ (0, canvas_path_1.createCanvasPath)(ctx, (0, bound_curves_1.calculatePaddingBoxPath)(paint.curves));
160
+ ctx.clip();
161
+ }
162
+ else {
163
+ complement(ctx, viewport, (0, bound_curves_1.calculateBorderBoxPath)(paint.curves));
164
+ ctx.clip('evenodd');
165
+ }
166
+ // Current paths retain their device coordinates across transform changes.
167
+ // setTransform with an approximate matrix would discard CSS transforms
168
+ // on the real CTM, so the legacy path translates relative to it instead.
169
+ if (knownTransform)
170
+ ctx.setTransform(a, b, c, d, e - displacement, f);
171
+ else
172
+ ctx.translate(-displacement / a, 0);
173
+ if (shadow.inset)
174
+ complement(ctx, sourceBounds, insetHole(paint, shadow));
175
+ else
176
+ (0, canvas_path_1.createCanvasPath)(ctx, (0, box_shadow_geometry_1.spreadShadowPath)((0, bound_curves_1.calculateBorderBoxPath)(paint.curves), spread));
177
+ if (knownTransform)
178
+ ctx.setTransform(a, b, c, d, e, f);
179
+ else
180
+ ctx.translate(displacement / a, 0);
181
+ ctx.shadowOffsetX = displacement + a * shadow.offsetX.number + c * shadow.offsetY.number;
182
+ ctx.shadowOffsetY = b * shadow.offsetX.number + d * shadow.offsetY.number;
183
+ // Allocation/filter-failure fallback remains bounded-memory. Its blur is
184
+ // isotropic under nonuniform transforms, but positioning stays correct.
185
+ ctx.shadowBlur = shadow.blur.number * space.scale;
186
+ ctx.shadowColor = (0, color_utilities_1.asString)(shadow.color);
187
+ ctx.fillStyle = 'rgb(0, 0, 0)';
188
+ ctx.fill(shadow.inset ? 'evenodd' : 'nonzero');
189
+ }
190
+ finally {
191
+ ctx.restore();
192
+ }
193
+ };
194
+ exports.paintBoxShadow = paintBoxShadow;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shadowSpace = void 0;
4
+ const bounds_1 = require("../../css/layout/bounds");
5
+ // Map the output bitmap back into the current user space. Capture x/y are not
6
+ // in this space once CSS transforms (including ancestor transforms) are active.
7
+ // An AABB of all four inverse-transformed corners is conservative under rotation.
8
+ const shadowSpace = (matrix, width, height) => {
9
+ const { a, b, c, d, e, f } = matrix;
10
+ if (![a, b, c, d, e, f, width, height].every(Number.isFinite))
11
+ return null;
12
+ const determinant = a * d - b * c;
13
+ if (!Number.isFinite(determinant) || determinant === 0)
14
+ return null;
15
+ const inversePoint = (x, y) => ({
16
+ x: (d * (x - e) - c * (y - f)) / determinant,
17
+ y: (-b * (x - e) + a * (y - f)) / determinant
18
+ });
19
+ const corners = [inversePoint(0, 0), inversePoint(width, 0), inversePoint(width, height), inversePoint(0, height)];
20
+ if (!corners.every((point) => Number.isFinite(point.x) && Number.isFinite(point.y)))
21
+ return null;
22
+ const left = Math.min(...corners.map((point) => point.x));
23
+ const top = Math.min(...corners.map((point) => point.y));
24
+ const right = Math.max(...corners.map((point) => point.x));
25
+ const bottom = Math.max(...corners.map((point) => point.y));
26
+ // Largest singular value: enough local raster resolution for either output
27
+ // axis. A scalar Canvas shadow blur is exact only for similarity transforms.
28
+ const xx = a * a + b * b;
29
+ const yy = c * c + d * d;
30
+ const xy = a * c + b * d;
31
+ const scale = Math.sqrt((xx + yy + Math.hypot(xx - yy, 2 * xy)) / 2);
32
+ if (!Number.isFinite(scale) || scale <= 0)
33
+ return null;
34
+ const tolerance = 1e-10 * Math.max(xx, yy);
35
+ return {
36
+ matrix,
37
+ viewport: new bounds_1.Bounds(left, top, right - left, bottom - top),
38
+ scale,
39
+ uniform: Math.abs(xx - yy) <= tolerance && Math.abs(xy) <= tolerance
40
+ };
41
+ };
42
+ exports.shadowSpace = shadowSpace;
@@ -21,7 +21,7 @@ const effects_renderer_1 = require("./effects-renderer");
21
21
  const canvas_path_1 = require("./canvas-path");
22
22
  const object_fit_1 = require("../object-fit");
23
23
  const content_renderer_1 = require("./content-renderer");
24
- const constants_1 = require("../../core/constants");
24
+ const box_shadow_painter_1 = require("./box-shadow-painter");
25
25
  const writing_mode_1 = require("../../css/property-descriptors/writing-mode");
26
26
  const font_utils_1 = require("./font-utils");
27
27
  const text_1 = require("../../css/layout/text");
@@ -399,30 +399,7 @@ class CanvasRenderer {
399
399
  await this.renderSurfaceBoxShadow(paint, shadow);
400
400
  continue;
401
401
  }
402
- this.ctx.save();
403
- const borderBoxArea = (0, bound_curves_1.calculateBorderBoxPath)(paint.curves);
404
- const maskOffset = shadow.inset ? 0 : constants_1.SHADOW_MASK_OFFSET;
405
- const shadowPaintingArea = (0, path_1.transformPath)(borderBoxArea, -maskOffset + (shadow.inset ? 1 : -1) * shadow.spread.number, (shadow.inset ? 1 : -1) * shadow.spread.number, shadow.spread.number * (shadow.inset ? -2 : 2), shadow.spread.number * (shadow.inset ? -2 : 2));
406
- if (shadow.inset) {
407
- this.path(borderBoxArea);
408
- this.ctx.clip();
409
- this.mask(shadowPaintingArea);
410
- }
411
- else {
412
- this.mask(borderBoxArea);
413
- this.ctx.clip();
414
- this.path(shadowPaintingArea);
415
- }
416
- // Canvas shadow metrics ignore the transform. Surface sources
417
- // need capture-pixel offsets, including the displaced mask.
418
- const shadowScale = this.surfaceRoot ? this.options.scale : 1;
419
- this.ctx.shadowOffsetX = (shadow.offsetX.number + maskOffset) * shadowScale;
420
- this.ctx.shadowOffsetY = shadow.offsetY.number * shadowScale;
421
- this.ctx.shadowColor = (0, color_utilities_1.asString)(shadow.color);
422
- this.ctx.shadowBlur = shadow.blur.number * shadowScale;
423
- this.ctx.fillStyle = shadow.inset ? (0, color_utilities_1.asString)(shadow.color) : 'rgba(0,0,0,1)';
424
- this.ctx.fill();
425
- this.ctx.restore();
402
+ await (0, box_shadow_painter_1.paintBoxShadow)(this.ctx, paint, shadow, this.options, this.surfaceBudget);
426
403
  }
427
404
  }
428
405
  // Render border-image if present (replaces traditional borders per CSS spec)
@@ -0,0 +1,3 @@
1
+ import { Path } from '../path';
2
+ export declare const shadowRadius: (radius: number, spread: number) => number;
3
+ export declare const spreadShadowPath: (paths: Path[], spread: number) => Path[];
@@ -0,0 +1,13 @@
1
+ import { BoxShadow } from '../../css/property-descriptors/box-shadow';
2
+ import { ElementPaint } from '../stacking-context';
3
+ import { SurfaceBudget } from './surface-bounds';
4
+ interface ShadowOptions {
5
+ x: number;
6
+ y: number;
7
+ width: number;
8
+ height: number;
9
+ scale: number;
10
+ signal?: AbortSignal;
11
+ }
12
+ export declare const paintBoxShadow: (ctx: CanvasRenderingContext2D, paint: ElementPaint, shadow: BoxShadow[number], options: ShadowOptions, budget: SurfaceBudget) => Promise<void>;
13
+ export {};
@@ -0,0 +1,16 @@
1
+ import { Bounds } from '../../css/layout/bounds';
2
+ export interface ShadowTransform {
3
+ a: number;
4
+ b: number;
5
+ c: number;
6
+ d: number;
7
+ e: number;
8
+ f: number;
9
+ }
10
+ export interface ShadowSpace {
11
+ matrix: ShadowTransform;
12
+ viewport: Bounds;
13
+ scale: number;
14
+ uniform: boolean;
15
+ }
16
+ export declare const shadowSpace: (matrix: ShadowTransform, width: number, height: number) => ShadowSpace | null;
package/package.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "default": "./dist/html2canvas-pro.esm.js"
20
20
  }
21
21
  },
22
- "version": "2.4.4",
22
+ "version": "2.4.5",
23
23
  "author": {
24
24
  "name": "yorickshan",
25
25
  "email": "yorickshan@gmail.com",