Your query has a complex solution—here's why:
The issue lies in the transformed state of your DOM container.
While your Kinetic objects behave as though they are in their original, non-transformed state.
The misinterpretation by your kinetic objects is due to the browser providing them with transformed mouse coordinates.
An easy resolution: Keep your DOM container untransformed and handle all rotations within KineticJS
A more challenging fix: Convert the rotated DOM mouse points into unrotated points for Kinetic usage.
Here's how you can approach this problem:
CSS transforms typically rotate around the element's center (50%, 50%), so identify the center of your Kinetic stage
var cx = stage.getWidth() / 2;
var cy = stage.getHeight() / 2;
If you have mouse coordinates in the transformed space (DOM), you must convert them to untransformed points (KineticJS)
var unrotatedPoint = unrotatedXY(cx, cy, mouseX, mouseY, cssDegreeRotation);
Below is the function that accomplishes this task:
function unrotatedXY(cx, cy, mouseX, mouseY, cssDegreeRotation) {
var dx = mouseX - cx;
var dy = mouseY - cy;
var r = Math.sqrt(dx * dx + dy * dy);
var cssRadianAngle = cssDegreeRotation * Math.PI / 180;
var rotatedAngle = Math.atan2(dy, dx);
var unrotatedAngle = rotatedAngle -= cssRadianAngle;
if (unrotatedAngle < 0) { unrotatedAngle += Math.PI * 2; }
unrotatedX = cx + r * Math.cos(unrotatedAngle);
unrotatedY = cy + r * Math.sin(unrotatedAngle);
return({ x: unrotatedX, y: unrotatedY });
}
The mouseX/mouseY values are obtained from the document, not KineticJS.
Thus, you need to listen to mouse events on the document (or container element), rather than directly in KineticJS.
$(document).mousemove(function(e){handleMouseMove(e);});
function handleMouseMove(e){
mouseX = parseInt(e.clientX - offsetX);
mouseY = parseInt(e.clientY - offsetY);
var unrotatedPoint = unrotatedXY(cx, cy, mouseX, mouseY, cssDegreeRotation);
// Now, you can perform hover actions against your Kinetic nodes using these converted mouse coordinates…
}
To reconnect with KineticJS, consider utilizing node.fire to trigger events with custom event objects containing the adjusted mouse coordinates.