Skip to content

Commit

Permalink
fix missing image tags referencing SVG images
Browse files Browse the repository at this point in the history
 - call screen.renderFrame() instead of canvg.render() and finish loading sub document when loading the image
 - add stripInternal flag to TypeScript compiler in order not to expose internal Screen members
  • Loading branch information
HackbrettXXX committed Oct 16, 2024
1 parent e40080a commit 7aceae6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
32 changes: 16 additions & 16 deletions src/Document/ImageElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
RenderingContext2D
} from '../types';
import BoundingBox from '../BoundingBox';
import Canvg from '../Canvg';
import Document from './Document';
import RenderedElement from './RenderedElement';

Expand All @@ -14,6 +15,7 @@ export default class ImageElement extends RenderedElement {
loadingPromise: Promise<void>;
protected readonly isSvg: boolean;
protected image: CanvasImageSource | string;
private subDocument: Canvg | null = null;

constructor(
document: Document,
Expand Down Expand Up @@ -75,6 +77,16 @@ export default class ImageElement extends RenderedElement {
}
}

const subDocument = this.document.canvg.forkString(
null, // we will set the rendering context later
this.image as string
);

subDocument.document.documentElement.parent = this;
this.subDocument = subDocument;

await subDocument.screen.finishLoading();

this.loaded = true;
}

Expand All @@ -100,23 +112,11 @@ export default class ImageElement extends RenderedElement {
ctx.translate(x, y);

if (this.isSvg) {
const subDocument = document.canvg.forkString(
ctx,
this.image as string,
{
ignoreMouse: true,
ignoreAnimation: true,
ignoreDimensions: true,
ignoreClear: true,
offsetX: 0,
offsetY: 0,
scaleWidth: width,
scaleHeight: height
}
);
const subDocument = this.subDocument;
const screen = subDocument.screen;

subDocument.document.documentElement.parent = this;
void subDocument.render();
screen.ctx = ctx;
screen.renderFrame(subDocument.document.documentElement, true, true, width, height, 0, 0);
} else {
const image = this.image as CanvasImageSource;

Expand Down
16 changes: 13 additions & 3 deletions src/Screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default class Screen {
private intervalId: number = null;

constructor(
readonly ctx: RenderingContext2D,
public ctx: RenderingContext2D,
{
fetch = defaultFetch,
window = defaultWindow
Expand Down Expand Up @@ -162,7 +162,12 @@ export default class Screen {
return isReadyLock;
}

private async finishLoading(): Promise<void> {
/* eslint-disable jsdoc/check-tag-names */
/**
* @internal
*/
/* eslint-enable jsdoc/check-tag-names */
async finishLoading(): Promise<void> {
await Promise.all(this.loadingPromises.map(_ => _()));
this.loadingPromises = [];
}
Expand Down Expand Up @@ -432,7 +437,12 @@ export default class Screen {
return false;
}

private renderFrame(
/* eslint-disable jsdoc/check-tag-names */
/**
* @internal
*/
/* eslint-enable jsdoc/check-tag-names */
renderFrame(
element: Element,
ignoreDimensions: boolean,
ignoreClear: boolean,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"experimentalDecorators": true,
"emitDecoratorMetadata": false,
"inlineSourceMap": true,
"stripInternal": true,
"baseUrl": ".",
"lib": [
"esnext",
Expand Down

0 comments on commit 7aceae6

Please sign in to comment.