Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Answer given above is correct. However there are 2 drawbacks of it.</p> <p>1: It does not have contains method without which, the custom node will not fire events like onMouseEnter or onClick etc.</p> <p>2: Images will flicker every time you move any node in graph or any time you pan the whole graph. This is because an image is loaded every time render function is called to re-plot the node. This also affects performance because of the same reason. A better option is to have images loaded once for all nodes having 'image' type and just re-plot them in the render function. This will help performance and stop images from flickering.</p> <p>We can have a function 'loadImages' which loads images in each node with 'type' as 'image'. Each node which is of type 'image' should also have a property called 'url' which is a url of the image to load.</p> <p>'Data' section of such node in json data would look something like below. </p> <pre><code>"data": { "$color": "#EBB056", "$type": "image", "$dim": 7, "$url":"magnify.png" // url of the image } </code></pre> <p>The loadImages function should be called right after the loadJson function i.e. right after loading json data.</p> <pre><code>// load JSON data. fd.loadJSON(json); //load images in node loadImages(); </code></pre> <p>Below is the implementation of loadImages function.</p> <pre><code>function loadImages(){ fd.graph.eachNode( function(node){ if( node.getData('type') == 'image' ){ var img = new Image(); img.addEventListener('load', function(){ node.setData('image',img); // store this image object in node }, false); img.src=node.getData('url'); } }); } </code></pre> <p>Finally, your custom node implementation would look something like below.</p> <pre><code>$jit.ForceDirected.Plot.NodeTypes.implement({ 'image': { 'render': function(node, canvas){ var ctx = canvas.getCtx(); var pos = node.pos.getc(true); if( node.getData('image') != 0 ){ var img = node.getData('image'); ctx.drawImage( img, pos.x-15, pos.y-15); } }, 'contains': function(node,pos){ var npos = node.pos.getc(true); dim = node.getData('dim'); return this.nodeHelper.circle.contains(npos, pos, dim); } } }); </code></pre>
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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