Question

I have been able to successfully draw a line using Kinetic JS, as follows:

var track129 = new Kinetic.Spline({
points: [
{x: 3, y: 400}, 
{x: 196, y: 400}, 
{x: 213, y: 395}, 
{x: 290, y: 345},
{x: 319, y: 324},
{x: 389, y: 253},
{x: 457, y: 184},
{x: 471, y: 173},
{x: 481, y: 173},
{x: 682, y: 173}, // this is where the blue track branches to the red track (129, 009).
{x: 708, y: 171},
{x: 740, y: 186},
{x: 773, y: 218},
{x: 799, y: 240},
{x: 822, y: 251},
{x: 845, y: 254},
{x: 866, y: 251},
{x: 894, y: 238},
{x: 934, y: 204}        
],
stroke: 'blue',
strokeWidth: 2,
lineCap: 'round',
tension: .2
});

layer.add(track129);

I then rotate the line using the following command:

track129.setRotationDeg(45);

The visual display updates. I then attempt to get the transformed points out of the rotated line like so:

var mySpleenPoints = track129.getPoints(); 

I end up getting the same array of points back as I entered. I've tried to get the offset and translation in an effort to see if I can derive the absolute coordinates of the rotated points but I've had no luck. Can anyone help me extract the actual translated values?

Was it helpful?

Solution

sorry, the rotation doesn't actually translate the point values, so you're getting correct output. If you want the translated values, you will have to write a function which calculates the coordinates. The good thing is that you have all the values you need to do that. Rotation degree, coordinates, and rotation around point.

I think this is the equation (for rotation around the origin)

 x' = x * cos(Theta) - y * sin(Theta);
 y' = x * sin(Theta) + y * cos(Theta); 

HTML5 Canvas: Calculating a x,y point when rotated

https://math.stackexchange.com/questions/123098/finding-the-position-of-a-point-after-rotation-why-is-my-result-incorrect

OTHER TIPS

That is a really irritating behavior. Anyway, thanks for the post. I am providing code for anyone who encounters this problem in the future.

function degToRad(angle) {
return (angle / 180) * Math.PI;
}

function rotateVector ( v, center, angle ) {
var theta = degToRad(angle);        
var s = Math.sin(theta);

var c = Math.cos(theta);

var nx = c * (v.x -center.x) + center.x - s * (v.y - center.y);
var ny = s * (v.x - center.x) + c * (v.y - center.y) + center.y;

v.x = nx;
v.y = ny;

return v;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top