Skip to content

Commit 068bc65

Browse files
fix(visualization): migrate to html2canvas-pro and fix screenshot transparency
Replace html2canvas with html2canvas-pro and fix transparent background using backgroundColor: null. Add test to verify transparent backgrounds.
1 parent ab1e846 commit 068bc65

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

visualization/angular.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
],
4242
"styles": ["app/tailwind.css", "app/app.scss", "material-icons/iconfont/material-icons.css"],
4343
"scripts": [],
44-
"allowedCommonJsDependencies": ["ajv", "color-convert", "html2canvas", "ignore", "md5", "qrcode", "three-orbit-controls"]
44+
"allowedCommonJsDependencies": ["ajv", "color-convert", "html2canvas-pro", "ignore", "md5", "qrcode", "three-orbit-controls"]
4545
},
4646
"configurations": {
4747
"production": {

visualization/app/codeCharta/ui/screenshotButton/screenshotButton.component.spec.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ jest.mock("../../../../app/codeCharta/util/clipboard/clipboardWriter", () => {
1919
}
2020
})
2121

22-
jest.mock("html2canvas", () => {
22+
const mockHtml2Canvas = jest.fn()
23+
24+
jest.mock("html2canvas-pro", () => {
2325
return {
2426
__esModule: true,
25-
default: jest.fn().mockImplementation(() => document.createElement("canvas"))
27+
default: (...args: unknown[]) => mockHtml2Canvas(...args)
2628
}
2729
})
2830

2931
describe("screenshotButtonComponent", () => {
3032
beforeEach(() => {
33+
mockHtml2Canvas.mockImplementation(() => document.createElement("canvas"))
34+
3135
TestBed.configureTestingModule({
3236
imports: [ScreenshotButtonComponent],
3337
providers: [
@@ -131,6 +135,23 @@ describe("screenshotButtonComponent", () => {
131135
expect(isScreenshotButtonDisabled(container)).toBe(true)
132136
})
133137
})
138+
139+
it("should use null backgroundColor to create transparent screenshots for better clarity", async () => {
140+
// Arrange
141+
const { fixture } = await render(ScreenshotButtonComponent)
142+
jest.spyOn(CanvasRenderingContext2D.prototype, "getImageData").mockImplementation(() => createMockImageData().imageData)
143+
144+
// Act
145+
await fixture.componentInstance.makeScreenshotToFile()
146+
147+
// Assert
148+
expect(mockHtml2Canvas).toHaveBeenCalledWith(
149+
expect.anything(),
150+
expect.objectContaining({
151+
backgroundColor: null
152+
})
153+
)
154+
})
134155
})
135156

136157
function isScreenshotButtonDisabled(container: Element) {

visualization/app/codeCharta/ui/screenshotButton/screenshotButton.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit } from "@angular/core"
22
import { State, Store } from "@ngrx/store"
33
import hotkeys from "hotkeys-js"
4-
import html2canvas from "html2canvas"
4+
import html2canvas from "html2canvas-pro"
55
import { Color, WebGLRenderer } from "three"
66
import { checkWriteToClipboardAllowed, setToClipboard } from "../../../../app/codeCharta/util/clipboard/clipboardWriter"
77
import { CcState } from "../../codeCharta.model"
@@ -121,7 +121,7 @@ export class ScreenshotButtonComponent implements OnInit {
121121

122122
const canvas = await html2canvas(document.querySelector("body"), {
123123
removeContainer: true,
124-
backgroundColor: "#00",
124+
backgroundColor: null,
125125
scrollY: -offsetMenuBar,
126126
height: bodyHeight - offsetMenuBar,
127127
ignoreElements(element) {

visualization/package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

visualization/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"fflate": "^0.8.2",
8686
"font-awesome": "^4.7.0",
8787
"hotkeys-js": "^3.8.2",
88-
"html2canvas": "^1.4.1",
88+
"html2canvas-pro": "^1.5.13",
8989
"idb": "^8.0.0",
9090
"ignore": "^7.0.5",
9191
"klona": "^2.0.4",

0 commit comments

Comments
 (0)