canvas - 使用fabricjs和angularjs进行缩放缩放

时间:2014-09-09 08:56:36

标签: javascript html5 angularjs canvas fabricjs

我正在尝试使用 FabricJs 在画布上进行缩放缩放。但我不知道。

我还搜索了谷歌的一些代码。但不行。任何人都不会使用fabric编写用于缩放选项的文章。

如果你对此有任何想法,请。请让我知道.......

我试过这个

...

canvas.on('touch:gesture', function (e) {
                isGestureEvent = true;
                // /* Just use the scale of the touch event to determine zoom in or zoom out. */
                var lPinchScale = e.self.scale;
                var type = e.e.type;
                if (type == "touchstart") {

                    //console.log("touchstart");
                    aGestureTouchStartTouches = e.e.touches;
                    aGestureScale.push(lPinchScale);
                    //console.log("scale:" + lPinchScale);

                } else if (type == "touchmove") {
                    console.log("touchmove");


                    //var modCheck = 5;
                    //if(aGestureScale.length % modCheck == 0) {

                    // canvasScale is current scale
                    // lPinchScale is what we want to set it to, relative to what it is now
                    // ie if canvasScale == 2.7
                    // var newScale = canvasScale * lPinchScale
                    // newScale is where we want canvasScale to end up
                    // 

                    // Get the last pinch scale;
                    var lastScale = aGestureScale[aGestureScale.length - 1];

                    // if scaleDiff negative then pinch in (zoom out), positive it is pinch out (zoom in)
                    var scaleDiff = lPinchScale - lastScale;
                    if (scaleDiff < 0) {

                        scaleDiff = lastScale - lPinchScale;
                        scaleDiff = 1 + scaleDiff;
                        fnZoomOutByFactor(scaleDiff);
                    }
                    else {

                        scaleDiff = 1 + scaleDiff;
                        fnZoomInByFactor(scaleDiff);
                    }

                    //console.log("scale:" + lPinchScale);
                    //}

                    // add new scale
                    aGestureScale.push(lPinchScale);

                } else {
                    console.log("gesture that is not move or start")
                }
            });

...

就像示例代码一样。我不能把我的所有代码。 bcs这是非常大的代码..请为我提供一些样品..

谢谢!

我已下载最新版本:https://github.com/kangax/fabric.js/tags

但不行。 :(

2 个答案:

答案 0 :(得分:4)

我改变了一点,它的工作

var canvas = new fabric.CanvasWithViewport('canvas');
canvas.on('touch:gesture',function(event){
    isGestureEvent = true;      
    var lPinchScale = event.self.scale;  
    var scaleDiff = (lPinchScale -1)/10 + 1;  // Slow down zoom speed    
    canvas.setZoom(canvas.viewport.zoom*scaleDiff);   

});

答案 1 :(得分:1)

您需要使用手势模块进行自定义构建:http://fabricjs.com/build/

使用此功能时,应触发手势事件。