【问题标题】:kineticjs shape based on left and top coordinates基于左侧和顶部坐标的动力学形状
【发布时间】:2013-07-24 01:52:52
【问题描述】:

我有一个 kineticjs 阶段和一个定义了 Path 对象的层。我从来没有能够在我的画布上显示路径。由于某种原因,画布/舞台看起来不尊重父 div 的 x 和 y 坐标。解决方案是什么? 我还尝试设置舞台和路径对象的 x 和 y 坐标,但没有运气。 这是我正在尝试的方法

var stage = new Kinetic.Stage({
  container : id,
  width : 105,
  height : 165
});
var path = new Kinetic.Path({
  data : "M176.0,463.0 L228.0,462.0 228.0,452.0 275.0,452.0 275.0,330.0 A40.0,0.0 0.0 1,1 254.0,295.0 L171.0,326.0 170.0,463.0z",
  fill : 'rgba(100, 15, 56, 0.5)',
  scale : 2,
  x : 176,
  y: 295
});

var layer = new Kinetic.Layer();
layer.add(path);
stage.add(layer);

请注意,我的路径几何从 176,463 开始。尽管父 div 位于正确的位置,但形状永远不会在此 div 内绘制。有什么指点吗?

【问题讨论】:

    标签: kineticjs


    【解决方案1】:

    KineticJS 中的 X 和 Y 坐标是相对于舞台的,而不是父 div 的页面 XY。

    因此,你的路径图就在那里,它只是在后台哇-a-a-a-ay。

    如果您将路径设置为适合舞台的比例和 x/y,您可以看到这一点:

        var path = new Kinetic.Path({
          data : "M176.0,463.0 L228.0,462.0 228.0,452.0 275.0,452.0 275.0,330.0 A40.0,0.0 0.0 1,1 254.0,295.0 L171.0,326.0 170.0,463.0z",
          fill : 'rgba(100, 15, 56, 0.5)',
          scale : .25,
          x : 5,
          y: 5
        });
    

    这是代码和小提琴:http://jsfiddle.net/m1erickson/ZMDYy/

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Prototype</title>
        <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>
    #container{
      border:solid 1px #ccc;
      margin-top: 10px;
      width:105px;
      height:165px;
    }
    </style>        
    <script>
    $(function(){
    
        var stage = new Kinetic.Stage({
            container: 'container',
            width: 105,
            height: 165
        });
        var layer = new Kinetic.Layer();
        stage.add(layer);
    
        var path = new Kinetic.Path({
          data : "M176.0,463.0 L228.0,462.0 228.0,452.0 275.0,452.0 275.0,330.0 A40.0,0.0 0.0 1,1 254.0,295.0 L171.0,326.0 170.0,463.0z",
          fill : 'rgba(100, 15, 56, 0.5)',
          scale : .25,
          x : 5,
          y: 5
        });
        layer.add(path);
        layer.draw();
    
    
    
    }); // end $(function(){});
    
    </script>       
    </head>
    
    <body>
        <div id="container"></div>
    </body>
    </html>
    

    【讨论】:

    • 感谢马克的指导。我所要做的就是将比例设置为 1 并取反坐标以获得画布内的形状。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多