There are many ways to generate small preview. Konva doesn’t provide any methods to do this automatically. But we can use Konva methods to generate preview area manually.
We will show two options - cloning and using images. In large applications it is better to generate preview from the state of the app.
Clone nodes from the main stage.
So we can just clone the stage or the layer and update its internal nodes from the state of the main canvas area.
Also it will make sense to simplify shapes on the preview. Like hide texts, remove strokes and shadows, etc.
Instructions: try to drag a circle. See how the preview is updating. Double click to add a new shape.
const layer = new Konva.Layer(); stage.add(layer);
// generate random shapes for (var i = 0; i < 10; i++) { const shape = new Konva.Circle({ x: Math.random() * stage.width(), y: Math.random() * stage.height(), radius: Math.random() * 30 + 5, fill: Konva.Util.getRandomColor(), draggable: true, // each shape MUSH have uniq name // so we can easily update the preview clone by name name: 'shape-' + i }); layer.add(shape); }
// clone original layer, and disable all events on it // we will use "let" here, because we can redefine layer later let previewLayer = layer.clone({ hitGraphEnabled: false }); previewStage.add(previewLayer);
functionupdatePreview(){ // we just need to update ALL nodes in the preview layer.children.forEach(shape => { // find cloned node const clone = previewLayer.findOne('.' + shape.name()); // update its position from the original clone.position(shape.position()); }); previewLayer.batchDraw(); }
stage.on('dragmove', updatePreview);
// add new shapes on double click or double tap stage.on('dblclick dbltap', () => { const shape = new Konva.Circle({ x: stage.getPointerPosition().x, y: stage.getPointerPosition().y, radius: Math.random() * 30 + 5, fill: Konva.Util.getRandomColor(), draggable: true, // each shape MUSH have uniq name // so we can easily update the preview clone by name name: 'shape-' + layer.children.length }); layer.add(shape); layer.batchDraw();
// remove all layer previewLayer.destroy(); // generate new one previewLayer = layer.clone({ hitGraphEnabled: false }); previewStage.add(previewLayer); }); </script> </body> </html>
Use image preview
Or we can export the stage to an image and use it as a preview.
For performance reasons we are not updating the preview on every dragmove events.
const layer = new Konva.Layer(); stage.add(layer);
// generate random shapes for (var i = 0; i < 10; i++) { const shape = new Konva.Circle({ x: Math.random() * stage.width(), y: Math.random() * stage.height(), radius: Math.random() * 30 + 5, fill: Konva.Util.getRandomColor(), draggable: true, // each shape MUSH have uniq name // so we can easily update the preview clone by name name: 'shape-' + i }); layer.add(shape); }
// update preview only on dragend for performance stage.on('dragend', updatePreview);
// add new shapes on double click or double tap stage.on('dblclick dbltap', () => { const shape = new Konva.Circle({ x: stage.getPointerPosition().x, y: stage.getPointerPosition().y, radius: Math.random() * 30 + 5, fill: Konva.Util.getRandomColor(), draggable: true, // each shape MUSH have uniq name // so we can easily update the preview clone by name name: 'shape-' + layer.children.length }); layer.add(shape); layer.draw();
updatePreview(); });
// show initial preview updatePreview(); </script> </body> </html>