Skip to content

Commit

Permalink
fix screenshot on appliancePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AderanFeng committed Aug 1, 2024
1 parent fb4886c commit de018d4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"@netless/app-slide": "0.2.60",
"@netless/appliance-plugin": "^1.0.3",
"@netless/appliance-plugin": "^1.0.4",
"@netless/combine-player": "^1.1.1",
"@netless/cursor-tool": "^0.1.1",
"@netless/iframe-bridge": "2.1.9",
Expand Down
36 changes: 22 additions & 14 deletions src/bridge/Displayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function setBackgroundColor(r: number, g: number, b: number, a?: number) {
}
window.setBackgroundColor = setBackgroundColor;

function screenshot(scenePath: string, fn: (scenePath: string, div: HTMLElement, width: number, height: number) => void, responseCallback: any) {
function screenshot(scenePath: string, fn: (scenePath: string, div: HTMLElement, width: number, height: number) => void | Promise<void>, responseCallback: any) {
const div = document.createElement("div");
div.setAttribute("class", "shadow");
const whiteboard = document.getElementById(whiteboardContainerId);
Expand All @@ -58,18 +58,20 @@ function screenshot(scenePath: string, fn: (scenePath: string, div: HTMLElement,
div.style.background = color;
}
document.body.appendChild(div);
fn(scenePath, div, div.clientWidth, div.clientHeight);
html2canvas(div, {
useCORS: true, onclone: async function (div: Document): Promise<void> {
const images = Array.from(div.getElementsByTagName("image"));
for (const i of images) {
const image = i as SVGImageElement;
const url = image.href.baseVal;
// https://github.com/niklasvh/html2canvas/issues/2104
const dataUri = await urlContentToDataUri(url);
image.href.baseVal = dataUri as string;
Promise.resolve(fn(scenePath, div, div.clientWidth, div.clientHeight)).then(() => {
return html2canvas(div, {
useCORS: true,
onclone: async function (div: Document): Promise<void> {
const images = Array.from(div.getElementsByTagName("image"));
for (const i of images) {
const image = i as SVGImageElement;
const url = image.href.baseVal;
// https://github.com/niklasvh/html2canvas/issues/2104
const dataUri = await urlContentToDataUri(url);
image.href.baseVal = dataUri as string;
}
}
}
});
}).then(canvas => {
const data = canvas.toDataURL();
document.body.removeChild(div);
Expand All @@ -81,11 +83,17 @@ export class AsyncDisplayerBridge {
constructor(readonly aDisplayer: Displayer) { }

sceneSnapshot = (scenePath: string, responseCallback: any) => {
screenshot(scenePath, this.aDisplayer.fillSceneSnapshot.bind(this.aDisplayer), responseCallback);
const f = window.appliancePlugin ?
(this.aDisplayer as any).fillSceneSnapshotAsync.bind(this.aDisplayer) :
this.aDisplayer.fillSceneSnapshot.bind(this.aDisplayer);
screenshot(scenePath, f, responseCallback);
};

scenePreview = (scenePath: string, responseCallback: any) => {
screenshot(scenePath, this.aDisplayer.scenePreview.bind(this.aDisplayer), responseCallback);
const f = window.appliancePlugin ?
(this.aDisplayer as any).scenePreviewAsync.bind(this.aDisplayer) :
this.aDisplayer.scenePreview.bind(this.aDisplayer);
screenshot(scenePath,f, responseCallback);
};
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1140,10 +1140,10 @@
resolved "https://registry.yarnpkg.com/@netless/app-slide/-/app-slide-0.2.60.tgz#8a4ab6087172ddbba93616777cc8310fd5c9b538"
integrity sha512-YhB0vzCVBQD31UpJ8XftnLrGEJrpDWrWWpzpYhBvOzb1RGKpDejZAkBLhbgAoIYR6Xjjwh2aWOdZOAqIrX+8uQ==

"@netless/appliance-plugin@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@netless/appliance-plugin/-/appliance-plugin-1.0.3.tgz#98d8029cd2a69bb98a69b8ae2ecd91c4579d73e0"
integrity sha512-0JeJbJq71/JwgAKxo0wURIMSvBgfxy621Er/cwZOHpM/d2063jObcwm/MaYo7fqgKTUnGt/tsb+xyyWliughhQ==
"@netless/appliance-plugin@^1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@netless/appliance-plugin/-/appliance-plugin-1.0.4.tgz#54d7c29c0da60d5a49e7733b618aab46d075f16a"
integrity sha512-iXLW/ArF/gJ71sX7y8Sz2OJfBHFusEQBeCihHCx/aL913PNGOzpIai3hSuFVoVPqTzlxaJEC2qhoRwoKGzeR9Q==
dependencies:
eventemitter2 "^6.4.9"
lineclip "^1.1.5"
Expand Down

0 comments on commit de018d4

Please sign in to comment.