如何使用 webgl 将 ngraph.pixi 和 PIXI.js 的箭头绘制到 link?
How Paint the arrow to link by ngraph.pixi with PIXI.js with webgl?
我画了有向图,所以我画了箭头到直线,我是pixi.js和javascript的新手,我想学习它,你能帮忙吗我怎么给它画一个箭头?
这是 demo,我想将箭头添加到 link。
这是在 this class
中绘制 link 的代码
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);
}
这是完整的代码
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();
}
复制代码的link是here
非常感谢
据我所知 pixi.js 不支持开箱即用的箭头渲染。您必须手动绘制箭头。这个想法是使用原始向量运算来计算箭头翅膀应该在哪里。
如果用归一化向量(长度等于1的向量)操作,实现起来会更容易。一旦你有了图 link 的向量,你就知道了它的方向,然后你可以计算 link 上的偏移量,箭头的翅膀应该停在那里。一旦你有了偏移量,你所要做的就是采用一个正交向量并从你的原始向量步进到 left/right - 这些是你的箭头翅膀应该停止的点。
画个图就更容易理解了,但是我手头没有好笔和铅笔,所以这里是渲染箭头的代码:
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);
}
这会呈现漂亮的箭头:
我画了有向图,所以我画了箭头到直线,我是pixi.js和javascript的新手,我想学习它,你能帮忙吗我怎么给它画一个箭头?
这是 demo,我想将箭头添加到 link。
这是在 this class
中绘制 link 的代码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);
}
这是完整的代码
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();
}
复制代码的link是here
非常感谢
据我所知 pixi.js 不支持开箱即用的箭头渲染。您必须手动绘制箭头。这个想法是使用原始向量运算来计算箭头翅膀应该在哪里。
如果用归一化向量(长度等于1的向量)操作,实现起来会更容易。一旦你有了图 link 的向量,你就知道了它的方向,然后你可以计算 link 上的偏移量,箭头的翅膀应该停在那里。一旦你有了偏移量,你所要做的就是采用一个正交向量并从你的原始向量步进到 left/right - 这些是你的箭头翅膀应该停止的点。
画个图就更容易理解了,但是我手头没有好笔和铅笔,所以这里是渲染箭头的代码:
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);
}
这会呈现漂亮的箭头: