Después de mucha lucha y buscando y tratando, utilizando la punta proporcionada por @Eric Rowell y el código publicado en el pliego de cuestionar Zoom in on a point (using scale and translate) finalmente me dieron el zoom dentro y fuera de un determinado punto trabajando usando KineticJS.
Aquí hay un DEMO que funciona.
Y aquí está el código:
var ui = {
stage: null,
scale: 1,
zoomFactor: 1.1,
origin: {
x: 0,
y: 0
},
zoom: function(event) {
event.preventDefault();
var evt = event.originalEvent,
mx = evt.clientX /* - canvas.offsetLeft */,
my = evt.clientY /* - canvas.offsetTop */,
wheel = evt.wheelDelta/120;
var zoom = (ui.zoomFactor - (evt.wheelDelta < 0 ? 0.2 : 0));
var newscale = ui.scale * zoom;
ui.origin.x = mx/ui.scale + ui.origin.x - mx/newscale;
ui.origin.y = my/ui.scale + ui.origin.y - my/newscale;
ui.stage.setOffset(ui.origin.x, ui.origin.y);
ui.stage.setScale(newscale);
ui.stage.draw();
ui.scale *= zoom;
}
};
$(function() {
var width = $(document).width() - 2,
height = $(document).height() - 5;
var stage = ui.stage = new Kinetic.Stage({
container: 'container',
width: width,
height: height
});
var layer = new Kinetic.Layer({
draggable: true
});
var rectX = stage.getWidth()/2 - 50;
var rectY = stage.getHeight()/2 - 25;
var box = new Kinetic.Circle({
x: 100,
y: 100,
radius: 50,
fill: '#00D200',
stroke: 'black',
strokeWidth: 2,
});
// add cursor styling
box.on('mouseover', function() {
document.body.style.cursor = 'pointer';
});
box.on('mouseout', function() {
document.body.style.cursor = 'default';
});
layer.add(box);
stage.add(layer);
$(stage.content).on('mousewheel', ui.zoom);
});
Gracias! Esto funciona. Lo siento por la respuesta tardía. – Skarbo
Oye, gracias, esto funciona bien, pero tengo muchas capas, así que no puedo configurar las capas como "arrastrables". por lo tanto, configuro la etapa arrastrable. Cuando muevo el escenario como arrastrando, no puedo acercarme al punto de zoom deseado. Tengo que recalcular algunas cosas como contar la etapa x y y pero no pude lograrlo. ¿Podrías ayudarme? – magirtopcu
@ user1645941 Por favor, comparta algunos ejemplos de trabajo del problema que está tratando (tal vez como [violín] (http://jsfiddle.net/)) y haré lo que pueda para ayudar. –