- #WHAT DOES ZOOM MATH 500 DO HOW TO#
- #WHAT DOES ZOOM MATH 500 DO UPDATE#
- #WHAT DOES ZOOM MATH 500 DO CODE#
- #WHAT DOES ZOOM MATH 500 DO DOWNLOAD#
translate(-image.width / 2, -image.height / 2) // 1 translate(image.width / 2, image.height / 2) // 3 If instead you would like to zoom the image about the center, then a solution is as follows: (1) translate the image such that its center is at (0, 0) (2) scale the image by x and y factors (3) translate the image back. So, if you have an image and scale it by a factor of 2, the bottom-right point will double in both the x and y directions (using the convention that is the top-left of the image).
When a matrix is scaled, the scale is at point (0, 0). That is, it works well if your image is already transformed. This is a trivial linear algebra problem, and the method I present works well with pan, zoom, skew, etc. I like Tatarize's answer, but I'll provide an alternative. It's using CSS3 Transforms but you should be able to use the same calculations for your Canvas.
#WHAT DOES ZOOM MATH 500 DO DOWNLOAD#
I've managed to implement a version that just uses scale and translate on their own, check it out here Wait for the images to download then use your mouse wheel to zoom, also supports panning by dragging the image around.
#WHAT DOES ZOOM MATH 500 DO UPDATE#
Update 2: Just noticed I'm using transform-origin together with translate. You will of course need to adapt it to use the canvas scale and translate methods. css('-moz-transform-origin', xImage + 'px ' + yImage + 'px') determine the location on the screen at the new scale YImage = yImage + ((yScreen - yLast) / scale)
XImage = xImage + ((xScreen - xLast) / scale) find current location on the image at the current scale Var yScreen = e.pageY - $(this).offset().top Var xScreen = e.pageX - $(this).offset().left $("#mosaicContainer").mousewheel(function(e, delta) Var yImage = 0 // last y location on the image Var xImage = 0 // last x location on the image Var yLast = 0 // last y location on the screen Var xLast = 0 // last x location on the screen
#WHAT DOES ZOOM MATH 500 DO CODE#
Update: Meh! I'll just post the code here rather than get you to follow a link: $(document).ready(function() Whether you use CSS3 transforms, or canvas' own scale and translate methods is upto you, but check the above link for the calculations. Using CSS3 transform allows you to zoom in on anything, just make sure the container DIV is set to overflow: hidden to stop the zoomed edges spilling out of the sides. Make sure you set the CSS3 transform-origin to 0, 0 (-moz-transform-origin: 0 0). css('transform', 'scale(1) translate(0px, 0px)') css('-webkit-transform', 'scale(1) translate(0px, 0px)') using jQuery: $('div.canvasContainer > canvas') I didn't realise there was scale and translate methods on Canvas context, you can achieve the same thing using CSS3 eg. When I get it working I'll post code here, but check out above link for the mouse-zoom-to-point part.
#WHAT DOES ZOOM MATH 500 DO HOW TO#
I've got the zoom to mouse cursor bit working fine, still trying to figure out how to allow the user to drag the canvas around like you can do in Google Maps. I'm in the middle of building a jQuery plugin that does this (Google Maps style zoom using CSS3 Transforms). I asked a similar question on Stackoverflow but got no response, but posted in DocType (StackOverflow for HTML/CSS) and got a response. This is actually a very difficult problem (mathematically), and I'm working on the same thing almost. The remaining code then needs to apply the scaling and translate to the draw context so it's origin coincides with the canvas corner. Originy -= mousey/(scale*zoom) - mousey/scale originx -= mousex/(scale*zoom) - mousex/scale Therefore we need to shift the origin (coordinates of the corner) to account for this. Originally the mouse is at a distance mouse/scale from the corner, we want the point under the mouse to remain in the same place after the zoom, but this is at mouse/new_scale away from the corner. The key, as pointed out, is to compute the axis position such that the zoom point (mouse pointer) remains in the same place after the zoom. Normalize mouse wheel movement to +1 or -1 to avoid unusual jumps. Schedule the redraw for the next display refresh.Ĭonst mousex = event.clientX - canvas.offsetLeft Ĭonst mousey = event.clientY - canvas.offsetTop Const canvas = document.getElementById("canvas") Ĭontext.fillRect(originx, originy, width/scale, height/scale)