Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is why I wish KineticJS supported multiple names. This method would be much cleaner if we were able to use something like:</p> <p><code>get('.anchor')</code></p> <p>But since we need to name the anchors because there will be multiple instances of each anchor, we cannot assign them each an ID.</p> <p>So this is how I did it:</p> <p>I had to add a background rectangle with id <strong><code>bg</code></strong> so that the layer was clickable:</p> <pre><code>var bg = new Kinetic.Rect({ width: stage.getWidth(), height: stage.getHeight(), x: 0, y: 0, id: 'bg' }); layer.add(bg); </code></pre> <p>Now the layer can be clicked and we can select the targetNode:</p> <pre><code>layer.on('mousedown', function (e) { var node = e.targetNode; select(node); }); function select(node) { deselect(); if (node.parent.nodeType = 'Kinetic.Group') { var children = node.parent.children; for (i = 1; i &lt; children.length; i++) { if (children[i].getName() == 'topLeft' || children[i].getName() == 'topRight' || children[i].getName() == 'bottomRight' || children[i].getName() == 'bottomLeft') { children[i].show(); } } } } function deselect() { var children = layer.children; for (i = 1; i &lt; children.length; i++) { var grandChildren = children[i].children; if (grandChildren) { for (j = 1; j &lt; grandChildren.length; j++) { if (grandChildren[j].getName() == 'topLeft' || grandChildren[j].getName() == 'topRight' || grandChildren[j].getName() == 'bottomRight' || grandChildren[j].getName() == 'bottomLeft') { grandChildren[j].hide(); layer.draw(); } } } } } </code></pre> <p>On select, we deselect everything and then we find the anchors IF the node we clicked on is a group (containing a shape and the anchors). We don't want to select any anchors if we click on the <strong><code>bg</code></strong></p> <p>Here's the <a href="http://jsfiddle.net/projeqht/kLnxK/" rel="nofollow">jsFiddle</a> </p> <p>Also note: I hid the anchors once they were added to the group, that's why no anchors are shown on init.</p> <p><strong>Edit:</strong> <code>i=1</code> and <code>j=1</code> because the image is the first child inside the group, and the anchors follow.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload