I want to zoom in & out an image in blazor on asp.net.
As I use for Google Maps, I want to move the image position by zooming and dragging the image with the mouse wheel.(I want to use an image file, not a Google map.)
Is there a way to zoom in, zoom out and drag specific images in blazor?
Note:
This component enables you to zoom by pressing
shift
whilemouse wheel
up (zoom out) ormouse wheel
down (zoom in) and move the image by pressingmouse button 1
down while moving. (it's more panning than dragging it)Restriction in Blazor: (at the time of writing this)
OffsetX
andOffsetY
within the html element as described here and also here, so the moving of the image has to be done using CSS only.@onscroll:stopPropagation
,@onwheel:stopPropagation
,@onmousewheel:stopPropagation
and/or@onscroll:preventDefault
,@onwheel:preventDefault
,@onmousewheel:preventDefault
set on the parentmainImageContainer
element. The screen will still scroll left and right if the content is wider than the viewable page.Solution:
The Zooming part is pretty straight forward, all you need to do is set the
transform:scale(n);
property in the@onmousewheel
event.The moving of the image is a bit more complex because there is no reference to where the mouse pointer is in relation to the image or element boundaries. (OffsetX and OffsetY)
The only thing we can determine is if a mouse button is pressed and then calculate what direction the mouse is moving in
left
,right
,up
ordown
.The idea is then to move the position of element with the image by setting the
top
andleft
CSS values as percentages.This component code:
This is the usage:
and this is the result:
I only tested this in chrome on a desktop computer without touch input.