使用KineticJs缩放和移动绘图

时间:2013-08-21 16:57:55

标签: javascript html5 html5-canvas kineticjs

我无法使用KineticJs正确缩放绘图。我有一个由多个Kinetic绘图对象组成的图形,这些图形都保存在一个组中。我试图做的是调整图纸的长度,如果新的长度超过某个阈值,那么图纸将被重新调整。

到目前为止,它运作良好。重新缩放正如预期的那样工作,但是当我再次使绘图居中时,我遇到了问题。正在计算绘图的原点,我试图使用

移动它
    drawingGroup.move(newOrigin.x, newOrigin.y)

初始原点约为200px。当绘图开始重新缩放时,此值为0,但随着图像继续缩小,它开始从屏幕左侧掉落。

我还没有找到一个很好的例子来调整绘图的长度,然后缩放,调整大小并使新绘图居中。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

即使小组变宽,如何缩小小组以适应舞台

此图显示了起始的“蓝色”组 - 未加宽且未缩放。

enter image description here

此图显示蓝色组在超出舞台后缩小并​​缩小。

enter image description here

以下是如何在没有“浮动”的情况下缩小您的群组:

  • 撤消任何先前的组偏移(按任何先前的缩放系数)。
  • 计算新的比例因子(例如:缩小比例)。
  • 保存新的缩放系数,以便在将来的任何缩放中使用。
  • 根据新的缩放系数计算新的组偏移量。
  • 将组的偏移量设置为新缩放的偏移量。
  • 重绘图层。

您需要向drawingGroup添加与缩放相关的几个属性:

// the current SCALED offset
drawingGroup.offsetX=0;
drawingGroup.offsetY=0;

// the point from which all scaling will occur
drawingGroup.scalePtX=0;
drawingGroup.scalePtY=0;

// the amount of current scaling applied to the group
// ==1.00 is the beginning un-scaled group
// >1.00 scales the group larger
// <1.00 scales the group smaller
drawingGroup.scaleFactor=1.00;

然后将此方法添加到drawingGroup以实际进行缩放:

drawingGroup.scaleBy=function(scaleChange){

    // undo previous offset
    this.offsetX += this.scalePtX/this.scaleFactor;
    this.offsetY += this.scalePtY/this.scaleFactor;

    // calc new scaling factor
    var newScaleFactor = this.scaleFactor*(1+scaleChange);

    // create new offset
    this.offsetX -= this.scalePtX/newScaleFactor;
    this.offsetY -= this.scalePtY/newScaleFactor;

    // set new scale factor
    this.scaleFactor=newScaleFactor;

    // do the new offset
    this.setOffset(this.offsetX,this.offsetY);

    // do the new scale
    this.setScale(this.scaleFactor);

    layer.draw();

};

这是一个测试代码,它既可以扩展组,也可以自动缩小它以适应舞台:

此测试代码还会在缩小后重新定位组。

    // widen by 20px
    // if wider than stage, scale down by 15%
    $("#wider").click(function(){
        drawingGroup.setWidth(drawingGroup.getWidth()+20);
        rect.setWidth(drawingGroup.getWidth());
        var width=drawingGroup.getWidth()*drawingGroup.scaleFactor;
        if(drawingGroup.getX()+width>stageWidth){
            drawingGroup.scaleBy(-0.15);
            recenter();
        }
        setStatus();
        layer.draw();
    });

    function recenter(){
        var w=drawingGroup.getWidth()*drawingGroup.scaleFactor;
        var h=drawingGroup.getHeight()*drawingGroup.scaleFactor;
        drawingGroup.setX(stage.getWidth()/2-w/2);
        drawingGroup.setY(stage.getHeight()/2-h/2);
    }

请注意,setX()/ setY()可以正常用于组。

请务必乘以scalingFactor计算缩放组宽度/高度。

这是完整的代码和小提琴:http://jsfiddle.net/m1erickson/UyDbW/

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"></script>

<style>
body{ padding:15px; }
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
  width:300px;
  height:300px;
}
</style>        
<script>
$(function(){

    var stage = new Kinetic.Stage({
      container: 'container',
      width: 300,
      height: 300
    });
    var layer = new Kinetic.Layer();
    stage.add(layer);

    // objects used in jquery events
    var drawingGroup;
    var rect,circle;
    var stageWidth=stage.getWidth();
    var stageHeight=stage.getHeight();


    drawingGroup=new Kinetic.Group({
        x:stageWidth/2-50,
        y:stageHeight/2-50,
        width:100,
        height:100,
    });
    drawingGroup.offsetX=0;
    drawingGroup.offsetY=0;
    drawingGroup.scalePtX=0;
    drawingGroup.scalePtY=0;
    drawingGroup.scaleFactor=1.00;
    drawingGroup.scaleBy=function(scaleChange){

        // undo previous offset
        this.offsetX += this.scalePtX/this.scaleFactor;
        this.offsetY += this.scalePtY/this.scaleFactor;

        // calc new scaling factor
        var newScaleFactor = this.scaleFactor*(1+scaleChange);

        // create new offset
        this.offsetX -= this.scalePtX/newScaleFactor;
        this.offsetY -= this.scalePtY/newScaleFactor;

        // set new scale factor
        this.scaleFactor=newScaleFactor;

        // do the new offset
        this.setOffset(this.offsetX,this.offsetY);

        // do the new scale
        this.setScale(this.scaleFactor);

        layer.draw();

    };
    layer.add(drawingGroup);

    var rect=new Kinetic.Rect({
        x:0,
        y:0,
        width:100,
        height:100,
        stroke:"lightgray",
        fill:"skyblue"
    });
    drawingGroup.add(rect);

    var circle=new Kinetic.Circle({
        x:50,
        y:50,
        radius:20,
        fill:"blue"
    });
    drawingGroup.add(circle);

    var status=new Kinetic.Text({
        x:15,
        y:15,
        text:"width=100, scale=1.00",
        fontSize:18,
        fill:"red"
    });
    layer.add(status);

    setStatus();

    layer.draw();

    function setStatus(){
        var width=drawingGroup.getWidth();
        var scale=Math.round(drawingGroup.scaleFactor*100);
        status.setText("Width: "+width+", Scale: "+scale+"%");
    }


    // widen by 20px
    // if wider than stage, scale down by 15%
    $("#wider").click(function(){
        drawingGroup.setWidth(drawingGroup.getWidth()+20);
        rect.setWidth(drawingGroup.getWidth());
        var width=drawingGroup.getWidth()*drawingGroup.scaleFactor;
        if(drawingGroup.getX()+width>stageWidth){
            drawingGroup.scaleBy(-0.15);
            recenter();
        }
        setStatus();
        layer.draw();
    });

    function recenter(){
        var w=drawingGroup.getWidth()*drawingGroup.scaleFactor;
        var h=drawingGroup.getHeight()*drawingGroup.scaleFactor;
        drawingGroup.setX(stage.getWidth()/2-w/2);
        drawingGroup.setY(stage.getHeight()/2-h/2);
    }


}); // end $(function(){});

</script>       
</head>

<body>
    <p>Blue group will scale if extending past stage</p>
    <button id="wider">Wider</button>
    <div id="container"></div>
</body>
</html>