KineticJS和捏和变焦

时间:2013-06-05 02:06:39

标签: javascript html5 canvas kineticjs

我希望达到此处列出的效果:http://www.html5canvastutorials.com/labs/html5-canvas-multi-touch-scale-stage-with-kineticjs/

但在Layer

中的KineticJS

我已经让它工作到一定程度(使用与链接中基本相同的代码),但似乎Layer在0,0原点附近缩放,我看不到有关更改变换原点的任何doco? / p>

如何有效地捏合和缩放Layer以使其围绕中心点缩放?

1 个答案:

答案 0 :(得分:2)

我为这种行为制作了一个插件:https://github.com/eduplus/pinchlayer

现在对于最近的更改可能有点过时,但是layerTouchMove函数中的逻辑很可能仍然是合理的。这里:

var touch1 = event.touches[0];
var touch2 = event.touches[1];  

if (touch1 && touch2) {
 self.setDraggable(false);
 if (self.trans != undefined) { self.trans.stop(); }
    if (self.startDistance === undefined) {
        self.startDistance = self.getDistance(touch1, touch2);
        self.touchPosition.x = (touch1.clientX + touch2.clientX) / 2;
        self.touchPosition.y = (touch1.clientY + touch2.clientY) / 2;
        self.layerPosition.x = (Math.abs(self.getX()) + self.touchPosition.x) / self.startScale;
        self.layerPosition.y = (Math.abs(self.getY()) + self.touchPosition.y) / self.startScale;
    }
    else {
        var dist = self.getDistance(touch1, touch2);
        var scale = (dist / self.startDistance) * self.startScale;
        if (scale < self.minScale) { scale = self.minScale; }
        if (scale > self.maxScale) { scale = self.maxScale; }
        self.setScale(scale, scale);
        var x = (self.layerPosition.x * scale) - self.touchPosition.x;
        var y = (self.layerPosition.y * scale) - self.touchPosition.y;
        var pos = self.checkBounds({ x: -x, y: -y });
        self.setPosition(pos.x, pos.y);
        self.draw();
     }

基本上它会记录起点和距离(夹点的长度),然后相应地缩放和设置图层位置。希望这会有所帮助。