Giter Site home page Giter Site logo

Comments (1)

shengbeiniao avatar shengbeiniao commented on June 23, 2024

已解决代码如下

  function transform(point){
      return [point.x,point.y]
    }
    G6.registerEdge('customEdge',{
      draw(item){
        console.log(item);
        let source=item.source.getAnchorPoints();
        let target=item.target.getAnchorPoints();
        console.log(source,target);
        const group = item.getGraphicGroup();
        let p1=transform(source[0]);
        let p4=transform(target[0]);
        let p2=[p1[0],p1[1]-20];
        let p3=[p4[0],p2[1]];
        console.log(p1,p2,p3,p4);
        group.addShape('polyline', {
          attrs: {
            points: [p1,p2,p3,p4],
            stroke: 'red'
          }
        });
        return group;
      }
    })

    G6.registerNode('customNode', {
      anchor: {
        type: 'circle',
        points: [
          [0.5, 0],
          [1, 0.5],
          [0.5, 1],
          [0, 0.5]
        ]
      },
      draw(item) {
        let x = 0,
          y = 0,
          width = 100,
          height = 100,
          r = 6;
        const group = item.getGraphicGroup();
        group.addShape('image', {
          attrs: {
            x: x + 5,
            y: x + 5,
            width: width - 10,
            height: width - 10,
            img: './ecs.svg'
          }
        });
        //激活状态
        if (item.model.status === 'active') {
          group.addShape('rect', {
              attrs: {
                x: x,
                y: y,
                width: width,
                height: width,
                stroke: 'black'
              }
            }),
            //上
            group.addShape('circle', {
              attrs: {
                x: (width - r) / 2,
                y: y,
                r: r,
                fill: '#91d5ff'
              }
            });
          //右
          group.addShape('circle', {
            attrs: {
              x: width,
              y: (height - r) / 2,
              r: r,
              fill: '#91d5ff'
            }
          });
          //下
          group.addShape('circle', {
            attrs: {
              x: (width - r) / 2,
              y: height,
              r: r,
              fill: '#91d5ff'
            }
          });
          //左
          group.addShape('circle', {
            attrs: {
              x: x,
              y: (height - y) / 2,
              r: r,
              fill: '#91d5ff'
            }
          });
        }
        return group;
      }
    });

    const graph = new G6.Graph({
      container: 'app',
      fitView: 'cc',
      width: window.innerWidth,
      height: window.innerHeight
    });

    graph.on('node:click', function (target) {
      //点击node,改变model
      graph.update(target.item, {
        status: 'active'
      })
    });

    graph.add('node', {
      id: 'a',
      x: 100,
      y: 100,
      shape: 'customNode'
    });

    graph.add('node', {
      id: 'b',
      x: 300,
      y: 300,
      shape: 'customNode',
      stroke: 'blue'
    });

    graph.add('edge', {
      source: 'a',
      target: 'b',
      size: 3,
      shape:'customEdge'
    });

    //处理拖动
    let node = 0;
    let dx = 0;
    let dy = 0;

    graph.on('node:dragstart', function (ev) {
      let item = ev.item;
      let model = item.getModel();
      node = item;
      dx = model.x - ev.x;
      dy = model.y - ev.y;
    });

    graph.on('node:drag', function (ev) {
      node && graph.update(node, {
        x: ev.x + dx,
        y: ev.y + dy
      });
    });

image

from g6-editor.

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.