Javascript 在react-p5中添加缩放功能

Javascript 在react-p5中添加缩放功能,javascript,reactjs,html5-canvas,Javascript,Reactjs,Html5 Canvas,我使用react-p5来显示一些数据,如下所示 节点的结构如下所示- nodes: [ { id:1, label: "WEB", x: 200, y: 150, bgcolor: "#459bd7", radius: 80, }, { id:2, label: "DATABASE", x: 400,

我使用react-p5来显示一些数据,如下所示

节点的结构如下所示-

nodes: [
    {
      id:1,
      label: "WEB",
      x: 200,
      y: 150,
      bgcolor: "#459bd7",
      radius: 80,
    },
    {
      id:2,
      label: "DATABASE",
      x: 400,
      y: 300,
      bgcolor: "#f5d608",
      radius: 100,
    }
]
links: [
    {
      source: 1,
      destination: 2,
      strokeWeight: 1,
    },
    {
      source: 1,
      destination: 3,
      strokeWeight: 5,
    }
  ]
let draw = (p5) => {
    p5.background("#ffffff")

    links.forEach(link=>{
      for(var i=0;i<nodes.length;i++){
        for(var j=0;j<nodes.length;j++){
          if(i!==j && link.source===nodes[i].id && link.destination===nodes[j].id){
            p5.stroke('#9e9e9e')
            p5.strokeWeight(link.strokeWeight);
            p5.line(nodes[i].x,nodes[i].y,nodes[j].x,nodes[j].y)
            break
          }
        }
      }
    })

    for(var i=0;i<nodes.length;i++){
      p5.stroke(nodes[i].bgcolor)
      p5.fill(nodes[i].bgcolor)
      p5.circle(nodes[i].x, nodes[i].y, nodes[i].radius);

      p5.textFont('Helvetica');
      p5.textSize(16);
      p5.strokeWeight(0);
      p5.fill(255)
      p5.textAlign(p5.CENTER);
      p5.text(nodes[i].label , nodes[i].x, nodes[i].y)
    }
}
链接的结构如下所示-

nodes: [
    {
      id:1,
      label: "WEB",
      x: 200,
      y: 150,
      bgcolor: "#459bd7",
      radius: 80,
    },
    {
      id:2,
      label: "DATABASE",
      x: 400,
      y: 300,
      bgcolor: "#f5d608",
      radius: 100,
    }
]
links: [
    {
      source: 1,
      destination: 2,
      strokeWeight: 1,
    },
    {
      source: 1,
      destination: 3,
      strokeWeight: 5,
    }
  ]
let draw = (p5) => {
    p5.background("#ffffff")

    links.forEach(link=>{
      for(var i=0;i<nodes.length;i++){
        for(var j=0;j<nodes.length;j++){
          if(i!==j && link.source===nodes[i].id && link.destination===nodes[j].id){
            p5.stroke('#9e9e9e')
            p5.strokeWeight(link.strokeWeight);
            p5.line(nodes[i].x,nodes[i].y,nodes[j].x,nodes[j].y)
            break
          }
        }
      }
    })

    for(var i=0;i<nodes.length;i++){
      p5.stroke(nodes[i].bgcolor)
      p5.fill(nodes[i].bgcolor)
      p5.circle(nodes[i].x, nodes[i].y, nodes[i].radius);

      p5.textFont('Helvetica');
      p5.textSize(16);
      p5.strokeWeight(0);
      p5.fill(255)
      p5.textAlign(p5.CENTER);
      p5.text(nodes[i].label , nodes[i].x, nodes[i].y)
    }
}
我的draw()函数如下所示-

nodes: [
    {
      id:1,
      label: "WEB",
      x: 200,
      y: 150,
      bgcolor: "#459bd7",
      radius: 80,
    },
    {
      id:2,
      label: "DATABASE",
      x: 400,
      y: 300,
      bgcolor: "#f5d608",
      radius: 100,
    }
]
links: [
    {
      source: 1,
      destination: 2,
      strokeWeight: 1,
    },
    {
      source: 1,
      destination: 3,
      strokeWeight: 5,
    }
  ]
let draw = (p5) => {
    p5.background("#ffffff")

    links.forEach(link=>{
      for(var i=0;i<nodes.length;i++){
        for(var j=0;j<nodes.length;j++){
          if(i!==j && link.source===nodes[i].id && link.destination===nodes[j].id){
            p5.stroke('#9e9e9e')
            p5.strokeWeight(link.strokeWeight);
            p5.line(nodes[i].x,nodes[i].y,nodes[j].x,nodes[j].y)
            break
          }
        }
      }
    })

    for(var i=0;i<nodes.length;i++){
      p5.stroke(nodes[i].bgcolor)
      p5.fill(nodes[i].bgcolor)
      p5.circle(nodes[i].x, nodes[i].y, nodes[i].radius);

      p5.textFont('Helvetica');
      p5.textSize(16);
      p5.strokeWeight(0);
      p5.fill(255)
      p5.textAlign(p5.CENTER);
      p5.text(nodes[i].label , nodes[i].x, nodes[i].y)
    }
}
let draw=(p5)=>{
p5.背景(“ffffff”)
links.forEach(link=>{
对于(var i=0;i