Демо-компонент
Здесь есть полная версия демо-компонента - https://github.com/calvinvette/ngx-cytoscape/blob/master/cytodemo/cytodemo.component.ts
с получением ссылки на объект cy.
Вы можете получить ссылку с помощью cy
Объект с помощью @ViewChild
в вашем компоненте, и вы можете получить доступ к объекту cy
из этого ссылочного компонента в функции ngAfterViewInit
.
@ViewChild(CytoscapeComponent)
private cytograph: CytoscapeComponent;
ngAfterViewInit() {
if (this.cytograph.cy) {
const cyLayer = this.cytograph.cy.cyCanvas();
const cnv: HTMLCanvasElement = cyLayer.getCanvas();
const ctx: CanvasRenderingContext2D = cnv.getContext("2d");
// ...
this.cytograph.cy.on("render cyCanvas.resize", function(evt, src) {
// "this" is now "cy" inside this callback function
cyLayer.resetTransform(ctx);
cyLayer.clear(ctx);
ctx.fillStyle = "#ff00ff";
//ctx.fillRect(0, 0, 100, 100); // Top left corner
cyLayer.setTransform(ctx);
const width = cnv.width;
const height = cnv.height;
const data = Array(width * height);
// Draw model elements
this.nodes().forEach(function(node) {
const pos = node.position();
// Do something with canvas at or around the node's position
ctx.fillRect(pos.x - 25, pos.y - 25, 50, 50); // At node position (bisection point of 50x50 rectangle)
});
});
}
// ...
}