It looks like you're new here. If you want to get involved, click one of these buttons!
local tempscale = self.zoom self.canvaslayer:setScale(tempscale) self:addEventListener(Event.MOUSE_WHEEL, function(e) -- e.wheel = 120 if self.canvaslayer:hitTestPoint(e.x, e.y) then -- local posx, posy = self.canvaslayer:getPosition() local posx, posy = e.x, e.y -- local posx, posy = self.canvaslayer:globalToLocal(e.x, e.y) -- self.canvaslayer:setAnchorPosition(e.x, e.y) self.canvaslayer:set("anchorX", posx / tempscale) self.canvaslayer:set("anchorY", posy / tempscale) end ... end |
Comments
EDIT: looks like a matrix could nicely do the job!? EDIT2: no it doesn't
EDIT3: for references
https://github.com/OneLoneCoder/Javidx9/blob/master/ConsoleGameEngine/SmallerProjects/OneLoneCoder_PanAndZoom.cpp
https://github.com/benbotto/zoom-on-a-point/blob/bcd30da0b41fdd0bdd5bda25b418b42417177040/world.js#L23
https://stackoverflow.com/questions/30002361/image-zoom-centered-on-mouse-position
Viva Gideros
Assuming I understood what you want to achieve, you can do it with matrices:
1) Take the sprite transform matrix
2) translate it so that the new center point is the point where your want to scale
3) scale the matrix
4) translate it back
5) apply it again to the sprite
Or you can just set scale and compute new position: New Position=(Old Position - ScalePoint * Scale), or something along those lines, I didn't check the maths
Likes: MoKaLux
Or to be more gideros specific, imagine you have a pixel somewhere on screen, then you point on it with your mouse, then the pixel should zoom (mouse wheel) and remains at the same position.
The code I tried:
I will of course post what I can hopefully figure out
the zooming code is something like that, perhaps it helps:
EDIT: in my code there is a parent sprite (self) and i'm scaling its child boardContainer. in your code perhaps you do not need the parent, it can be the stage itself. i use the dummy parent sprite to be able to easily rescale/reposition everything on the screen when the window is resized.
Likes: MoKaLux
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Fragmenter - animated loop machine and IKONOMIKON - the memory game
I jumping into it right now, thanks a bunch guys, really appreciated
In your wheel event code, I would compute offset like this:
Likes: MoKaLux
EDIT: for references:
var img = document.createElement("img");
img.src ="https://st.motortrend.com/uploads/sites/5/2018/11/2019-Mazda3-hatch-profile.jpg";
var canvas = window._canvas = new fabric.Canvas("imageCanvas");
$(img).on('load',function(){
image = new fabric.Image(img, {
centeredRotation: true,
centeredScaling: true,
top: 0,
left: 0
});
canvas.add(image);
});
canvas.on("mouse:move", function(event) {
currentMouseY = Math.round(event.e.y - canvas._offset.top);
currentMouseX = Math.round(event.e.x - canvas._offset.left);
});
function zoom(delta, target) {
var factor = 0.8;
if (delta < 0) {
factor = 1/factor;
}
// Zoom into the image.
image.setScaleX(image.getScaleX() * factor);
image.setScaleY(image.getScaleY() * factor);
// Calculate displacement of zooming position.
var dx = (currentMouseX - image.getLeft()) * (factor - 1),
dy = (currentMouseY - image.getTop()) * (factor - 1);
// Compensate for displacement.
image.setLeft(image.getLeft() - dx);
image.setTop(image.getTop() - dy);
canvas.renderAll();
}
$(canvas.wrapperEl).on("mousewheel", function (event) {
var target = canvas.findTarget(event);
if (target) {
var delta = event.originalEvent.wheelDelta / 120;
zoom(-delta, target);
};
event.preventDefault() && false;
});
https://jsfiddle.net/fgLmyxw4/
This one should be easy, isn't it mokalux ?
I will test it more, polish it and straight to gideros wiki (keszegh, do I have your permission?).
Viva Gideros Forum
Likes: keszegh
Likes: MoKaLux
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: MoKaLux
Fragmenter - animated loop machine and IKONOMIKON - the memory game
I am still looking into it, give me some little extra time. I am posting it just in case!
Viva Gideros
PS: I don't know if it was useful/good practice to move local variables viewportx, viewporty outside the MOUSE_WHEEL event function .
Viva Gideros, Viva Gideros Forum
https://wiki.gideros.rocks/index.php/Examples#ZOOM_SPRITE_TO_MOUSE_POSITION_.40hgy29.2C_.40keszegh
Likes: pie
Likes: MoKaLux, pie
Fragmenter - animated loop machine and IKONOMIKON - the memory game
While Qt is working fine, win32 doesn't zoom to mouse cursor
I don't know what could be the issue , any ideas please?
Thank you
EDIT: it seems to be a translate canvas miscalculation?
EDIT2: maybe I need to take into account the window position?
in my code i've actually changed the scale computation to an exponential one instead of linear, as it feels much more natural:
Likes: MoKaLux
Fragmenter - animated loop machine and IKONOMIKON - the memory game