Do you have an issue with a specific file(s)? No
Can you reproduce using one of our samples or online demos?No
Are you using the WebViewer server? Yes
Does the issue only happen on certain browsers? No
Is your issue related to a front-end framework? No
Is your issue related to annotations? No
Please give a brief summary of your issue:
I’m currently implementing a custom image stamp panel using the WebViewer API and handling the image drop via the drop event. However, I’m facing an issue where the image stamp does not get placed exactly where the mouse pointer is located during the drop. It often appears slightly offset or in a different area of the page.
Could you please guide me on how to ensure the stamp gets added precisely at the mouse cursor position during the drop event? If there’s a better way to calculate the page point or handle zoom/scroll offsets, your input would be greatly appreciated.
Hello, I’m AiRon, an automated tech support bot While you wait for one of our customer support representatives, I can help you with your question.
It seems you’re dealing with positioning issues when placing an image stamp at the mouse cursor location within WebViewer. This usually involves accounting for the viewport’s zoom and scroll positions to ensure the image is placed exactly where desired.
Some things you can try:
Ensure that your mouse position is correctly translated into page coordinates considering the current zoom and scroll offsets.
Use the documentViewer.getScrollViewElement() to get the scroll positions and add these to your mouse coordinates to adjust for the current scroll position.
Check the transformation applied by the display mode using displayMode.windowToPage() to ensure it accounts for the current zoom level and rotation.
Make sure to use the correct property for the page number from getSelectedPages as it should return an array of selected pages.
Here are some resources that might help you:
WebViewer.getDisplayMode: Learn how to get the display mode and use it for accurate coordinate conversion.
After some investigation, I was able to reproduce the issue on my end with the code provided. Rather than using the coordinates provided from the drop event, we recommend using the suggestion in the guide for getting the mouse location. When doing so, this greatly improved the accuracy of where the annotation was being placed.
Here’s my full code snippet I used for testing (slightly modified to use double click as I did not have the custom image stamp panel:
// EDIT: New helper function getMouseLocation, see guide for more details
const getMouseLocation = e => {
const scrollElement = documentViewer.getScrollViewElement();
const scrollLeft = scrollElement.scrollLeft || 0;
const scrollTop = scrollElement.scrollTop || 0;
return {
x: e.pageX + scrollLeft,
y: e.pageY + scrollTop
};
};
const res = await fetch('../../envs/testing/png-transparent-circle-spiral-point-pattern-spiral.png'); // replace with your image path
const imageBlob = await res.blob();
const reader = new FileReader();
reader.onloadend = async () => {
documentViewer.addEventListener('dblClick', async e => {
// refer to getMouseLocation implementation above
// use the getMouseLocation() coordinates
const windowCoordinates = getMouseLocation(e);
const base64data = reader.result;
const viewerElement = documentViewer.getViewerElement();
const viewerRect = viewerElement.getBoundingClientRect();
const x = e.clientX - viewerRect.left;
const y = e.clientY - viewerRect.top;
const displayMode = documentViewer.getDisplayModeManager().getDisplayMode();
const selectedPages = displayMode.getSelectedPages(windowCoordinates, windowCoordinates);
const pageNumber = selectedPages?.last;
const pagePoint = displayMode.windowToPage(windowCoordinates, pageNumber);
const stampAnnot = new Annotations.StampAnnotation();
stampAnnot.PageNumber = pageNumber;
stampAnnot.X = pagePoint.x;
stampAnnot.Y = pagePoint.y;
stampAnnot.Width = 200;
stampAnnot.Height = 150;
stampAnnot.Author = annotationManager.getCurrentUser();
stampAnnot.Rotation = documentViewer.getCompleteRotation(pageNumber) * 90;
await stampAnnot.setImageData(base64data);
annotationManager.addAnnotation(stampAnnot);
annotationManager.redrawAnnotation(stampAnnot);
});
}
reader.readAsDataURL(imageBlob);
Let us know if this code snippet works for you!
Best Regards,
Jacob Romano Carlsen
Web Development Support Engineer Apryse Software Inc.
I tried your code sample, but now there’s an issue — when the user drags and drops the stamp image, it gets placed slightly to the right of where the user clicked.
You mentioned attaching a video, however there may have been an issue with uploading the video to the forum post, as I am unable to see the attached video. Would you be able to resend the video file? Alternatively, you can upload it to a file sharing service such as Google Drive or Sharepoint and provide a share link for us to use.
Additionally, from your testing are your custom UI components that you are implementing adding an unexpected offset to the cursor event? If so, and the distance is consistent, we would recommend applying an offset in the opposite direction to mitigate this.
Let us know how you would like to proceed.
Best Regards,
Jacob Romano Carlsen
Web Development Support Engineer Apryse Software Inc.
Thank you for the information. It is a bit difficult as the mouse location disappears, but is the offset of the placement only horizontal? Is it possible to get an approximation on how far offset the annotation is from the drop point?
In the demo video, at the far left there appears to be a scroll bar. Is there a small panel to the left of your stamp panel which is roughly the size of the offset?