mirror of
https://akkoma.dev/AkkomaGang/akkoma-fe.git
synced 2024-11-09 10:13:42 +00:00
Aligning canvas to image
This commit is contained in:
parent
345934c2f3
commit
3947aafeba
1 changed files with 6 additions and 2 deletions
|
@ -144,10 +144,14 @@ const StillImage = {
|
||||||
|
|
||||||
// Use requestAnimationFrame to schedule the scaling to the next frame
|
// Use requestAnimationFrame to schedule the scaling to the next frame
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
|
// Compute scaling ratio between the natural dimensions of the image and its display dimensions
|
||||||
|
const scalingRatioWidth = parentElement.clientWidth / image.naturalWidth;
|
||||||
|
const scalingRatioHeight = parentElement.clientHeight / image.naturalHeight;
|
||||||
|
|
||||||
// Adjust for high-DPI displays
|
// Adjust for high-DPI displays
|
||||||
const ratio = window.devicePixelRatio || 1;
|
const ratio = window.devicePixelRatio || 1;
|
||||||
canvas.width = parentElement.clientWidth * ratio;
|
canvas.width = image.naturalWidth * scalingRatioWidth * ratio;
|
||||||
canvas.height = parentElement.clientHeight * ratio;
|
canvas.height = image.naturalHeight * scalingRatioHeight * ratio;
|
||||||
canvas.style.width = `${parentElement.clientWidth}px`;
|
canvas.style.width = `${parentElement.clientWidth}px`;
|
||||||
canvas.style.height = `${parentElement.clientHeight}px`;
|
canvas.style.height = `${parentElement.clientHeight}px`;
|
||||||
context.scale(ratio, ratio);
|
context.scale(ratio, ratio);
|
||||||
|
|
Loading…
Reference in a new issue