<!DOCTYPE html> <html> <head> <script src="https://unpkg.com/[email protected]/konva.min.js"></script> <meta charset="utf-8" /> <title>Konva Drag and Drop Stress Test with 10,000 Shapes Demo</title> <style> body { margin: 0; padding: 0; overflow: hidden; background-color: #f0f0f0; } </style> </head> <body> <div id="container"></div> <script> var width = window.innerWidth; var height = window.innerHeight;
function addCircle(layer) { var color = colors[colorIndex++]; if (colorIndex >= colors.length) { colorIndex = 0; }
var randX = Math.random() * stage.width(); var randY = Math.random() * stage.height(); var circle = new Konva.Circle({ x: randX, y: randY, radius: 6, fill: color });
layer.add(circle); }
var stage = new Konva.Stage({ container: 'container', width: width, height: height });
var dragLayer = new Konva.Layer(); var colors = [ 'red', 'orange', 'yellow', 'green', 'blue', 'cyan', 'purple' ]; var colorIndex = 0;
var layersArr = []; /* * create 10 layers each containing 1000 shapes to create * 10,000 shapes. This greatly improves performance because * only 1,000 shapes will have to be drawn at a time when a * circle is removed from a layer rather than all 10,000 shapes. * Keep in mind that having too many layers can also slow down performance. * I found that using 10 layers each made up of 1,000 shapes performs better * than 20 layers with 500 shapes or 5 layers with 2,000 shapes */ for (var i = 0; i < 10; i++) { var layer = new Konva.Layer(); layersArr.push(layer);
for (var n = 0; n < 1000; n++) { addCircle(layer); }
stage.add(layer); } stage.add(dragLayer);
stage.on('mousedown', function(evt) { var circle = evt.target; var layer = circle.getLayer();
circle.moveTo(dragLayer); layer.draw(); circle.startDrag(); }); </script> </body> </html>
|