Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image render area #1696

Merged
merged 6 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions ext/js/display/structured-content-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class StructuredContentGenerator {

if (typeof border === 'string') { imageContainer.style.border = border; }
if (typeof borderRadius === 'string') { imageContainer.style.borderRadius = borderRadius; }
imageContainer.style.width = `${usedWidth}em`;
if (typeof title === 'string') {
imageContainer.title = title;
}
Expand All @@ -124,17 +125,13 @@ export class StructuredContentGenerator {
const image = this._contentManager instanceof DisplayContentManager ?
/** @type {HTMLCanvasElement} */ (this._createElement('canvas', 'gloss-image')) :
/** @type {HTMLImageElement} */ (this._createElement('img', 'gloss-image'));
if (sizeUnits === 'em' && (hasPreferredWidth || hasPreferredHeight)) {
const emSize = 14; // We could Number.parseFloat(getComputedStyle(document.documentElement).fontSize); here for more accuracy but it would cause a layout and be extremely slow; possible improvement would be to calculate and cache the value
const scaleFactor = 2 * window.devicePixelRatio;
image.style.width = `${usedWidth}em`;
image.style.height = `${usedWidth * invAspectRatio}em`;
image.width = usedWidth * emSize * scaleFactor;
} else {
image.width = usedWidth;
}
image.width = width;
image.height = image.width * invAspectRatio;

// Anki will not render images correctly without specifying to use 100% width and height
image.style.width = '100%';
image.style.height = '100%';

imageContainer.appendChild(image);

if (this._contentManager instanceof DisplayContentManager) {
Expand Down
Loading
Loading