Primarily,
You have a small error if you have not decided that this is so for any reason:
To authorize a scene, simply create the scene as follows:
var stage = new Kinetic.Stage({ container: 'container', width: window.innerWidth, height: window.innerHeight });
To center an object, you must calculate its width and height compared to the scene and place it accordingly.
var rect = new Kinetic.Rect({ x: stage.getWidth()/4, y: stage.getHeight()/4, width: stage.getWidth() / 2, height: stage.getHeight() / 2, fill: 'green', stroke: 'black', strokeWidth: 4 });
It was simple because your rectangle is half the width and half the height of the scene.
A more complex solution that takes into account different sizes looks something like this:
x: (stage.getWidth()/2)-(rect.getWidth()/2) // similar for height y: (stage.getHeight()/2)-(rect.getHeight()/2) // similar for height
source share