Git Product home page Git Product logo

Comments (5)

vasturiano avatar vasturiano commented on September 26, 2024

@cherno-alpha thanks for reaching out.

The two features are completely independent. To better understand the issue you're experiencing, it's easier if you could create a small example on https://codepen.io/ that reproduces the problem.

from force-graph.

cherno-alpha avatar cherno-alpha commented on September 26, 2024

I'm new to github and codepen, so a cautious heads up.

I used your Load JSON example and want to set images as nodes with nodeCanvasObject(). What do I need to write in the function?

Here is the Link to Codepen:
https://codepen.io/cherno-alpha/pen/oNamadd

from force-graph.

vasturiano avatar vasturiano commented on September 26, 2024

@cherno-alpha you just need to refer to this example to see how to write your canvas code to paint images:

.nodeCanvasObject(({ img, x, y }, ctx) => {
const size = 12;
ctx.drawImage(img, x - size / 2, y - size / 2, size, size);
})

from force-graph.

KyleDave avatar KyleDave commented on September 26, 2024

@cherno-alpha - this was exactly what I wanted to ask! The JSON fetch is what I need for a large family tree:
https://quickening.zapto.org/fg-network.html
But I want images on the nodes, with attached labels:
https://quickening.zapto.org/fg-images.html

I can see the network example creating a data object with methods that refer to "node object accessor", but I don't know how to change nodeCanvasObject to work with that data object. It just tells me img undefined:

`fetch('Tree/links.json').then(res => res.json()).then(data => {

  const Graph = ForceGraph()
  (document.getElementById('graph'))
  .nodeCanvasObject(({ img, x, y }, ctx) => {
    const sizeX = 200;
    const sizeY = 237;
    const url = img.currentSrc;
    const label = decodeURI(url.substring(url.lastIndexOf('/')+1).slice(0, -4));
    const textWidth = ctx.measureText(label).width;
    ctx.drawImage(img, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);
    ctx.font = "11px Arial";
    ctx.fillText( label, x - sizeX / 2 + ( sizeX / 2  - textWidth / 2), y + sizeY / 2 + 20);
  })

`
I'm guessing "{img, x, y}" is the node object...

from force-graph.

vasturiano avatar vasturiano commented on September 26, 2024

@KyleDave yes that is the node object. So, basically you first need to add your image objects to each of your nodes, under the img attribute. This example shows that:

const imgs = ['cat.jpg', 'dog.jpg', 'eagle.jpg', 'elephant.jpg', 'grasshopper.jpg', 'octopus.jpg', 'owl.jpg', 'panda.jpg', 'squirrel.jpg', 'tiger.jpg', 'whale.jpg']
.map(src => {
const img = new Image();
img.src = `./imgs/${src}`;
return img;
});
// Random connected graph
const gData = {
nodes: imgs.map((img, id) => ({ id, img })),

from force-graph.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.