How to find relative mouse position?

In some cases you may need to find position of a point relative to a node. For purpose we can use mathematical Konva.Transform methods.

In this demo we have deep nesting transformed nodes: moved stage, scaled layer, rotated group.
Now we want to add circles into the group on click. But how to find position of that circles?
We can’t use stage.getPointerPosition() directly because that is position relative to top-left corner of the stage.

The idea is simple. We just need to use inverted absolute transform.

Relative Pointer Position Demoview raw
<!DOCTYPE html>
<script src="[email protected]/konva.min.js"></script>
<meta charset="utf-8" />
<title>Konva Relative Pointer Position Demo</title>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;

<div id="container"></div>
var width = window.innerWidth;
var height = window.innerHeight;

var stage = new Konva.Stage({
container: 'container',
width: width,
height: height,
x: 20,
y: 50,

var layer = new Konva.Layer({
scaleX: 1.2,
scaleY: 0.8,
rotation: 5,

var group = new Konva.Group({
x: 30,
rotation: 10,
scaleX: 1.5,

var text = new Konva.Text({
text: 'Click on the canvas to draw a circle',
fontSize: 20,

// this function will return pointer position relative to the passed node
function getRelativePointerPosition(node) {
var transform = node.getAbsoluteTransform().copy();
// to detect relative position we need to invert transform

// get pointer (say mouse or touch) position
var pos = node.getStage().getPointerPosition();

// now we can find relative point
return transform.point(pos);

stage.on('click', function () {
var pos = getRelativePointerPosition(group);
var shape = new Konva.Circle({
x: pos.x,
y: pos.y,
fill: 'red',
radius: 20,

Enjoying Konva? Please consider to support the project.