简体   繁体   English

如何绘制箭头以通过 ngraph.pixi 与 PIXI.js 与 webgl 链接?

[英]How Paint the arrow to link by ngraph.pixi with PIXI.js with webgl?

I so paint the direct graph, so I do paint the arrow to the line, I'm new with the pixi.js and the javascript and I want to learn it, can you help me how I can paint an arrow to it?我画了直接图,所以我画了箭头到直线,我是 pixi.js 和 javascript 的新手,我想学习它,你能帮我画一个箭头吗?

This is a demo and I want to add the arrow to the link.这是一个演示,我想在链接中添加箭头。

This is the code for paint the link, inside this class这是绘制链接的代码,在这个 class里面

module.exports = function (link, ctx) {
  ctx.lineStyle(link.width, 0x333333, 1);
  ctx.moveTo(link.from.x, link.from.y);
  ctx.lineTo(link.to.x, link.to.y);
}

This is the complete code这是完整的代码

module.exports.main = function () {
  var graph = require('ngraph.generators').balancedBinTree(5);
  var createPixiGraphics = require('../');

  var setting = {
    rendererOptions: {
      backgroundColor: 0xFFFFFF,
      antialias: true,
    }
  }

  var pixiGraphics = createPixiGraphics(graph, setting);
  pixiGraphics.createLinkUI(require('./lib/createLinkUI'));
  pixiGraphics.renderLink(require('./lib/renderLink'));
  pixiGraphics.createNodeUI(require('./lib/createNodeUI'));
  pixiGraphics.renderNode(require('./lib/renderNode'));
  var layout = pixiGraphics.layout;

  // just make sure first node does not move:
  layout.pinNode(graph.getNode(1), true);

  // begin animation loop:
  pixiGraphics.run();
}

The link for reproducing the code is here重现代码的链接在这里

Thanks a lot非常感谢

As far as I know pixi.js doesn't support rendering of the arrows out of the box.据我所知,pixi.js 不支持开箱即用的箭头渲染。 You'd have to manually draw the arrows.您必须手动绘制箭头。 The idea is to use primitive vector operations to calculate where arrow wings should be.这个想法是使用原始向量运算来计算箭头的翅膀应该在哪里。

It is easier to implement if you operate with normalized vectors (vectors whose length is equal to 1).如果您使用归一化向量(长度等于 1 的向量)进行操作,则更容易实现。 Once you have a vector for a graph link, you know its direction, you can then count offset on the link, where arrow wings should stop.一旦你有了一个图形链接的向量,你就知道它的方向,然后你可以计算链接上的偏移量,箭头翅膀应该停止的地方。 Once you have offset, all you have to do is take an orthogonal vector and step to the left/right from your original vector - these are the points where your arrow wings should stop.一旦你有了偏移,你所要做的就是取一个正交向量并从你的原始向量向左/向右步进——这些是你的箭头翅膀应该停止的点。

It would be much easier to understand if you draw a picture, but I don't have good pen and pencil at the moment, so here is the code that renders the arrow:如果你画一张图会更容易理解,但我目前没有好的钢笔和铅笔,所以这里是呈现箭头的代码:

function defaultLinkRenderer(link) {
  graphics.lineStyle(1, 0xcccccc, 1);
  graphics.moveTo(link.from.x, link.from.y);
  graphics.lineTo(link.to.x, link.to.y);

  // first, let's compute normalized vector for our link:
  let dx = link.to.x - link.from.x;
  let dy = link.to.y - link.from.y;
  let l = Math.sqrt(dx * dx + dy * dy);

  if (l === 0) return; // if length is 0 - can't render arrows

  // This is our normal vector. It describes direction of the graph
  // link, and has length == 1:
  let nx = dx/l; let ny = dy/l;

  // Now let's draw the arrow:
  let arrowLength = 6;       // Length of the arrow
  let arrowWingsLength = 2;  // How far arrow wings are from the link?

  // This is where arrow should end. We do `(l - NODE_WIDTH)` to
  // make sure it ends before the node UI element.
  let ex = link.from.x + nx * (l - NODE_WIDTH);
  let ey = link.from.y + ny * (l - NODE_WIDTH);

  // Offset on the graph link, where arrow wings should be
  let sx = link.from.x + nx * (l - NODE_WIDTH - arrowLength);
  let sy = link.from.y + ny * (l - NODE_WIDTH - arrowLength);

  // orthogonal vector to the link vector is easy to compute:
  let topX = -ny;
  let topY = nx;

  // Let's draw the arrow:
  graphics.moveTo(ex, ey);
  graphics.lineTo(sx + topX * arrowWingsLength, sy + topY * arrowWingsLength);
  graphics.moveTo(ex, ey);
  graphics.lineTo(sx - topX * arrowWingsLength, sy - topY * arrowWingsLength);
}

And that renders nice looking arrows:这会呈现出漂亮的箭头:

最后结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM