1

我有一个 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 内绘制。任何指针?

4

1 回答 1

0

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

因此,您的路径图就在那里,它只是在后台哇-aaa-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>
于 2013-07-23T17:27:07.290 回答