You can use the properties of the image element to determine the scaling factor to apply to the overlay items.
The intrinsic (unscaled) dimensions of an image are:
const unscaledWidth = img.naturalWidth,
unscaledHeight = img.naturalHeight;
The dimensions of the image as drawn are:
const scaledWidth = img.offsetWidth,
scaledHeight = img.offsetHeight;
The scaling factors are thus:
const scaleX = scaledWidth / unscaledWidth,
scaleY = scaledHeight / unscaledHeight;
Ref:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…