更新@juan.facorro的示範規模,而不是舞臺形狀
jsFiddle
var ui = {
stage: null,
box: null,
scale: 1,
zoomFactor: 1.1,
zoom: function(event) {
event.preventDefault();
var evt = event.originalEvent,
mx = evt.offsetX,
my = evt.offsetY,
wheel = evt.wheelDelta/120; //n or -n
var zoom = (ui.zoomFactor - (evt.wheelDelta < 0 ? 0.2 : 0));
var newscale = ui.scale * zoom;
var origin = ui.box.getPosition();
origin.x = mx - (mx - origin.x) * zoom;
origin.y = my - (my - origin.y) * zoom;
ui.box.setPosition(origin.x, origin.y);
ui.box.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();
var rectX = stage.getWidth()/2 - 50;
var rectY = stage.getHeight()/2 - 25;
var box = ui.box = new Kinetic.Circle({
x: 100,
y: 100,
radius: 50,
fill: '#00D200',
stroke: 'black',
strokeWidth: 2,
draggable: true
});
// 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);
});
謝謝!這工作。回覆晚了非常抱歉。 – Skarbo
嘿,謝謝,這工作正常,但我有很多層,所以我不能將圖層設置爲「可拖動」。因此我設置階段可拖動。當我拖動舞臺時,我無法縮放到所需的縮放點。我不得不重新計算一些數字來計算階段x和y,但我無法完成。你能幫我嗎? – magirtopcu
@ user1645941請分享您正在處理的問題的一些工作示例(也許作爲[小提琴](http://jsfiddle.net/)),我會盡我所能提供幫助。 –