Skip to content

Commit

Permalink
[handle_pixel] improve zoom code
Browse files Browse the repository at this point in the history
Uses <img> attributes instead of CSS, improving accuracy and reducing CLS
  • Loading branch information
discomrade authored and shish committed Sep 9, 2024
1 parent 59227b1 commit e0ac345
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
35 changes: 24 additions & 11 deletions ext/handle_pixel/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,40 @@ document.addEventListener('DOMContentLoaded', () => {

var img = $('.shm-main-image');

/* get dimensions for the image when zoomed out to fit the screen */
zoom_height = Math.min(window.innerHeight * 0.8, img.data('height'));
// check max width of parent element when the image isn't extending it
img.css('display', 'none');
zoom_width = Math.min(img.parent().width(), img.data('width'));
img.css('display', '');
// keep the image in ratio
if(zoom_width / zoom_height > img.data('width') / img.data('height')) {
zoom_width = img.data('width') * (zoom_height / img.data('height'))
} else {
zoom_height = img.data('height') * (zoom_width / img.data('width'))
}

if(zoom_type === "full") {
img.css('max-width', img.data('width') + 'px');
img.css('max-height', img.data('height') + 'px');
img.attr('width', img.data('width'));
img.attr('height', img.data('height'));
}
if(zoom_type === "width") {
img.css('max-width', '95%');
img.css('max-height', img.data('height') + 'px');
img.attr('width', zoom_width);
img.attr('height', img.data('height'));
}
if(zoom_type === "height") {
img.css('max-width', img.data('width') + 'px');
img.css('max-height', (window.innerHeight * 0.95) + 'px');
img.attr('width', img.data('width'));
img.attr('height', zoom_height);
}
if(zoom_type === "both") {
img.css('max-width', '95%');
img.css('max-height', (window.innerHeight * 0.95) + 'px');
img.attr('width', zoom_width);
img.attr('height', zoom_height);
}

const zoomed_height_diff = Math.round(window.innerHeight * 0.95 - img.height());
const zoomed_width_diff = Math.round(img.parent().width() * 0.95 - img.width());
const zoom_height_diff = Math.round(zoom_height - img.data('height'));
const zoom_width_diff = Math.round(zoom_width - img.data('width'));

if (zoomed_height_diff > 0 && zoomed_width_diff > 0) {
if (zoom_height_diff == 0 && zoom_width_diff == 0) {
img.css('cursor', '');
} else if (zoom_type == "full") {
img.css('cursor', 'zoom-out');
Expand Down
2 changes: 2 additions & 0 deletions ext/handle_pixel/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function display_image(Image $image): void
'class' => 'shm-main-image',
'id' => 'main_image',
'src' => $image->get_image_link(),
'width' => $image->width,
'height' => $image->height,
'data-width' => $image->width,
'data-height' => $image->height,
'data-mime' => $image->get_mime(),
Expand Down

0 comments on commit e0ac345

Please sign in to comment.