<body> <divid="stage-parent"> <divid="container"></div> </div> <script> // let's think our scene virtual size will be 1000x1000px // but the real size will be different to fit user's page var sceneWidth = 1000; var sceneHeight = 1000;
var stage = new Konva.Stage({ container: 'container', // first just set set as is width: sceneWidth, height: sceneHeight, });
var layer = new Konva.Layer(); stage.add(layer);
// add circle into center var circle = new Konva.Circle({ radius: 50, fill: 'red', x: stage.width() / 2, y: stage.height() / 2, }); layer.add(circle);
// rectangle in bottom right of the stage var rect = new Konva.Rect({ fill: 'green', x: stage.width() - 100, y: stage.height() - 100, width: 100, height: 100, }); layer.add(rect);
functionfitStageIntoParentContainer(){ var container = document.querySelector('#stage-parent');
// now we need to fit stage into parent container var containerWidth = container.offsetWidth;
// but we also make the full scene visible // so we need to scale all objects on canvas var scale = containerWidth / sceneWidth;
fitStageIntoParentContainer(); // adapt the stage on any window resize window.addEventListener('resize', fitStageIntoParentContainer); </script> </body> </html>