Skip to content

Commit

Permalink
fix: respect image bounds when scaling iamge from viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
micahg committed Aug 25, 2024
1 parent b2e1eab commit fa53643
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/mui/src/utils/contentworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,37 @@ function calculateViewport(
containerWidth: number,
containerHeight: number,
) {
// screen w/h
const [cw, ch] = [containerWidth, containerHeight];

// vp = rotated screen w/h
[_vp.width, _vp.height] = rotatedWidthAndHeight(-angle, cw, ch);

// multiply viewport by zoom factor WHICH CAN LEAD TO IMAGE SIZES GREATER THAN ACTUAL IMAGE SIZE
[_img.width, _img.height] = [zoom * _vp.width, zoom * _vp.height];
if (_img.width > backgroundImage.width) {
// img (scaled viewport) greater than actual image, so shrink it down and adjust the viewport to fit it
_img.width = backgroundImage.width;
_vp.width = Math.round((_vp.height * _img.width) / _img.height);
} else if (_img.height > backgroundImage.height) {
// one side of the displayed image region fits into our viewport
_img.height = backgroundImage.height;
_vp.height = Math.round((_vp.width * _img.height) / _img.width);
} else if (_img.y < 0) {
// remember, our "image" dimensions are based on our viewport and zoom so if we're off the page, just slide and we'll still fit
_img.y = 0;
} else if (_img.x < 0) {
// remember, our "image" dimensions are based on our viewport and zoom so if we're off the page, just slide and we'll still fit
_img.x = 0;
} else if (_img.x + _img.width > backgroundImage.width) {
_img.x = backgroundImage.width - _img.width;
} else if (_img.y + _img.height > backgroundImage.height) {
_img.y = backgroundImage.height - _img.height;
}
console.log(`MICAH vp is ${JSON.stringify(_vp)}`);
console.log(`MICAH img is ${JSON.stringify(_img)}`);
console.log(`MICAH bg ${backgroundImage.width},${backgroundImage.height}`);
return;
}

/**
Expand Down

0 comments on commit fa53643

Please sign in to comment.