2013-03-18 97 views
1

我试图围绕使用KineticJS的点旋转线条。但是线条始终围绕原点旋转。我已经尝试使用偏移量,但它也没有工作。KineticJS:围绕点旋转线条

黑点通常可以定位。我希望灰线围绕黑点在0和360之间的角度旋转。

line.setPoints([{ 
    x: stage.getWidth()/2, 
    y: stage.getHeight()/2 
}, { 
    x: stage.getWidth()/2, 
    y: stage.getHeight()/2 - 30 
}]); 

line.transitionTo({ 
    rotation: angle, 
    duration: 1, 
    easing: 'ease-out' 
}); 

任何想法?

我做了一个小提琴:http://jsfiddle.net/QuT4d/

回答

1

http://jsfiddle.net/QuT4d/1/

所以你需要设置从一个位置获得去为您的生产线,并设置相对于位置的点:

line = new Kinetic.Line({ 
     x: stage.getWidth()/2, // <--- right here 
     y: stage.getHeight()/2, // <--- and right here 
     points: [{ 
      x: 0, // <--- if you start at 0, you start at the above x: which is the center 
      y: 0 // <--- if you start at 0, you start at the above y: which is the center 
     }, { 
      x: 0, 
      y: 0- 30 
     }], 
     stroke: 'gray', 
     strokeWidth: 3 
    }); 

它并不是说它没有工作,你是在设置位置,然后设置点,这是真的吸引你的对象离开屏幕。如果您将转换时间设置为较大的数字并且看到它慢慢移出屏幕,则可以看到发生这种情况。

您也不需要再次设置点或重置位置。

+0

这对我有用,谢谢!我再次设置位置,因为通常情况下位置是可编辑的,我只是将这部分留在示例小提琴中。 – dersvenhesse 2013-03-18 15:35:49

+0

常见的错误是忘记了职位是相对的。本可以做到绝对定位,但这可能会给你未来更多的工作。 – SoluableNonagon 2013-03-18 20:03:01