2013-08-21 109 views
0

我無法使用KineticJs正確縮放繪圖。我有一個由許多Kinetic繪圖對象組成的繪圖,這些繪圖對象都保存在一個組中。我試圖做的是對圖形的長度進行調整,如果新的長度超過了某個閾值,那麼圖形將被重新調整。使用KineticJs縮放和移動繪圖

它到目前爲止工作良好。重新縮放如預期的那樣工作,但我在再次對圖形進行居中時遇到了問題。繪圖的來源正在計算中,我試圖將其移動使用

drawingGroup.move(newOrigin.x, newOrigin.y) 

最初的原點大概是200px。當繪圖開始重新縮放時,此值爲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(); 

}; 

這是測試代碼這兩個擴大組和自動縮小它,以適應階段:

這個測試代碼也recenters組後reducecal ING。

// 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> 
+0

這是接近我所期待的。不同之處在於每次繪圖組的長度擴展時,我都試圖在繪製圖層時重繪圖像。你知道這樣做的好方法嗎? – Tom

+0

我編輯了我的答案,以在組縮小後重新接收組。請注意,您可以在組上使用setX/setY。但是,getWidth/getHeight將報告未縮放的值,因此您必須通過drawingGroup的scaleFactor乘以該寬度/高度以獲取縮放的大小。 – markE

+0

我有這個問題的更新。我想要做的是調整繪圖的長度,並用每個增量重新縮放繪圖。我希望繪圖以y軸爲中心,但繪圖應該從舞臺左側設置爲20px。使用遞增/遞減時,我無法正確地使用縮放比例。有沒有辦法讓這個工作? – Tom