I am writing an application where I need to zoom using the vertex coordinates in Vertex shader and am also able to achieve the same but the zoom doesn't look correct because if we point our mouse on a certain x,y position it should take that as center and it is only happening for me for the very first time when I zoom and if I try to zoom by moving the mouse to another x,y coordinate afterwards, I get incorrect result. Am I doing anything wrong?
Steps to check :
Open Fiddle and browse for any image from your PC.
Zoom using Mousewheel on some X,Y coordinate and and later move your mouse and try zooming on another x,y coordinate.
Error : It is not zooming properly if we move the mouse and zoom again.
Here's the fiddle and code:
`
var prevX, prevY, zoomCounter = 0;
var zoom = function (e) {
var x = e.offsetX, y = e.offsetY;
var w = img.width, h = img.height;
var leftPart = (x - AR.x)/AR.renderableWidth;
var topPart = (y - AR.y)/AR.renderableHeight;
var rightPart = 1.0 - leftPart;
var bottomPart = 1.0 - topPart;
if (e.wheelDelta > 0) {
zoomCounter++;
} else {
zoomCounter--;
}
if (zoomCounter < 0) {
zoomCounter = 0;
return;
}
/*if (prevX && (prevX !== x || prevY !== y)) {
console.warn('Coords changed');
leftPart = (x - prevX - AR.x)/AR.renderableWidth;
topPart = (y - prevY - AR.y)/AR.renderableHeight;
rightPart = 1.0 - leftPart;
bottomPart = 1.0 - topPart;
}*/
if (e.wheelDelta > 0) {
x1 -= 0.10 * w * leftPart;
x2 += 0.10 * w * rightPart;
y1 -= 0.10 * h * topPart;
y2 += 0.10 * h * bottomPart;
} else {
x1 += 0.10 * w * leftPart;
x2 -= 0.10 * w * rightPart;
y1 += 0.10 * h * topPart;
y2 -= 0.10 * h * bottomPart;
}
updateCanvas();
prevX = x;
prevY = y;
};
`