Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://unpkg.com/konva@8/konva.min.js"></script>

<p id='info'>Hit info</info>
<div id="container" class='container'>

 



              
            
!

CSS

              
                body {
  margin: 10;
  padding: 10;
  overflow: hidden;
  background-color: #f0f0f0;
}
.info { min-height: 18px; margin: 5px 20px; }
.container {
  border: 1px solid black;
  width: 400px;
  height: 200px;
}
.buttons {
  margin:  20px 0 0 20px; 
  max-width: 400px;
}

#whiteboard-container {
    width: 300px;
    height: 300px;
    position: relative;
    overflow: hidden;
}
 
              
            
!

JS

              
                // All groups have their position as (0,0) unless changed.

// Set up a stage
let stage = new Konva.Stage({
    container: "container",
    width: 400,
    height: 200
  }),
  // add a layer to draw on
  layer1 = new Konva.Layer(),
 
  star = new Konva.Star({
    innerRadius: 20,
    outerRadius: 45,
    fill: "red",
    stroke: "black",
    strokeWidth: 5,
    numPoints: 5,
    x: 60,
    y: 60,
    draggable: true,
    shadowOffset: { x: 5, y: 5 },
    shadowColor: "black",
    shadowBlur: 5,
    shadowOpacity: 0.5,
    shadowForStrokeEnabled: false
  })
  zoomStep = 0.1;
 

// Stick 40 stars into each stage
for (var n = 0; n < 50; n++) {
  let clone1 = star.clone({
    x: Math.random() * stage.width() * (1 / stage.scaleX()),
    y: Math.random() * stage.height() * (1 / stage.scaleY()),
    id: 'shape' + n
  });
  layer1.add(clone1);
  clone1.cache(); 
}


// Add the layer to the stage and shapes to layer
stage.add(layer1); 

let scale1 = 1;
let center = {x: stage.width()/2, y: stage.height()/2};
 




 function getAllHits(pt){

    const layers = stage.children,
        len = layers.length,
        end = len - 1,
        list = [];

    for (let n = end; n >= 0; n--) {
        for (let shape of layers[n].children){  
            if (isPointInRectangle(pt.x, pt.y, shape.getClientRect()) ){
                // only handle suspected hits 
                if (shape.intersects(pt)){ 
                    // we now have a confirmed hit!
                    list.push(shape);
                }
            }
        }
    }
   return list;
}
 
// rectangle objects: {x:, y:, width:, height: }
// var rect={x:10, y:15, width:25, height:20}
// Return true if the x,y point is inside the rectangle

function isPointInRectangle(x,y,rect){
    return(x>rect.x && x<rect.x+rect.width && y>rect.y && y<rect.y+rect.height);
}

var timesPerSecond = 10; // how many times to fire the event per second
var wait = false;
stage.on('mousemove', function (event) {
    // don't handle events when one has just occurred
    if (!wait) {
        // fire the event
      const pt = stage.getPointerPosition();
        const hitList = getAllHits(pt);
        $('#info').html('Point (' +  pt.x + ', ' +  pt.y + ') Hit count ' + hitList.length);
        // stop any further events
        wait = true;
        // after a fraction of a second, allow events again
        setTimeout(function () {
            wait = false;
        }, 1000 / timesPerSecond);
    } 
});
              
            
!
999px

Console