Changeset 35776


Ignore:
Timestamp:
2021-12-09T10:52:12+13:00 (2 years ago)
Author:
cstephen
Message:

Fix magnifier logic

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/model-interfaces-dev/atea/ocr/src/components/OcrImageDisplay.vue

    r35775 r35776  
    3333            }
    3434
    35             this.$refs.magnifier.style.background = "url('" + newImage.src + "')";
    36             this.$refs.magnifier.style.backgroundSize = (newImage.width * this.magnifierZoom) + "px " +
    37                 (newImage.height * this.magnifierZoom) + "px";
     35            /** @type {HTMLDivElement} */
     36            const magnifier = this.$refs.magnifier;
     37            const magWidth = newImage.width * this.magnifierZoom;
     38            const magHeight = newImage.height * this.magnifierZoom;
     39
     40            magnifier.style.backgroundImage = `url('${newImage.src}')`;
     41            magnifier.style.backgroundSize = `${magWidth}px ${magHeight}px`;
    3842
    3943            this.magnifierBounds = newImage.getBoundingClientRect();
     
    5559            const magXCenter = magnifier.offsetWidth / 2;
    5660            const magYCenter = magnifier.offsetHeight / 2;
    57             let { x, y } = getRelativeCursorPos(e);
     61            let { x, y } = this.getRelativeCursorPos(e, magContainerBounds);
    5862
    5963            // Calculate our screen-space bounds
     
    8185            magnifier.style.top = (y - magYCenter) + "px";
    8286
    83             const imageX = x - relImageLeft - magXCenter;
    84             const imageY = y - relImageTop - magYCenter;
    85 
    86             magnifier.style.backgroundPosition = "-" + (imageX * this.magnifierZoom) + "px -" + (imageY * this.magnifierZoom) + "px";
    87 
    88             /**
    89              * Gets the position of the cursor relative to the magnifier's container.
    90              * @param {MouseEvent} e The mouse event.
    91              */
    92             function getRelativeCursorPos(e) {
    93                 let x = e.pageX - magContainerBounds.x;
    94                 let y = e.pageY - magContainerBounds.y;
    95 
    96                 // Consider any page scrolling
    97                 x = x - window.scrollX;
    98                 y = y - window.scrollY;
    99 
    100                 return {
    101                     x: x,
    102                     y: y
    103                 };
    104             }
     87            // Calculate the offset for the magnified image
     88            const imageX = x - relImageLeft - (magXCenter / this.magnifierZoom);
     89            const imageY = y - relImageTop - (magYCenter / this.magnifierZoom);
     90
     91            // Convert the offset to valid CSS values
     92            const actualX = imageX < 0
     93                ? "0"
     94                : `-${(imageX * this.magnifierZoom)}px`
     95
     96            const actualY = imageY < 0
     97                ? "0"
     98                : `-${(imageY * this.magnifierZoom)}px`
     99
     100            magnifier.style.backgroundPosition = `${actualX} ${actualY}`
     101        },
     102        /**
     103         * Gets the position of the cursor relative to a container.
     104         * @param {MouseEvent} e The mouse event.
     105         * @param {DOMRect} bounds The bounding container.
     106         * @returns {{x: Number, y: Number}} The relative X and Y coordinate of the cursor.
     107         */
     108        getRelativeCursorPos(e, bounds) {
     109            let x = e.pageX - bounds.x;
     110            let y = e.pageY - bounds.y;
     111
     112            // Consider any page scrolling
     113            x = x - window.scrollX;
     114            y = y - window.scrollY;
     115
     116            return {
     117                x: x,
     118                y: y
     119            };
    105120        }
    106121    },
     
    210225.magnifier {
    211226    position: absolute;
    212     width: 150px;
    213     height: 150px;
     227    width: 175px;
     228    height: 175px;
    214229
    215230    background-repeat: no-repeat;
    216231    border: 2px solid #000;
    217     // cursor: none;
     232    cursor: none;
     233    border-radius: 40%;
    218234}
    219235</style>
Note: See TracChangeset for help on using the changeset viewer.