Skip to content

Download Image Example

This example demonstrates how to export the current flow as an image using Angular features and the html-to-image library.


import '@angular/compiler';
import { Component, ElementRef, viewChild, type Signal } from '@angular/core';
import {
initializeModel,
NgDiagramBackgroundComponent,
NgDiagramComponent,
NgDiagramModelService,
provideNgDiagram,
type NgDiagramConfig,
} from 'ng-diagram';
import { GenerateImageService } from './generate-image.service';
import { NavBarComponent } from './nav-bar/nav-bar.component';
@Component({
imports: [NgDiagramComponent, NavBarComponent, NgDiagramBackgroundComponent],
template: `
<div class="not-content diagram">
<ng-diagram #ngDiagram [model]="model" [config]="config">
<ng-diagram-background />
</ng-diagram>
<div class="nav-bar">
<nav-bar [diagramRef]="diagramElementRef()"></nav-bar>
</div>
</div>
`,
styleUrl: './diagram.component.scss',
providers: [GenerateImageService, NgDiagramModelService, provideNgDiagram()],
})
export class DiagramComponent {
diagramElementRef: Signal<ElementRef<HTMLElement> | undefined> = viewChild(
'ngDiagram',
{ read: ElementRef }
);
config = {
zoom: {
max: 3,
zoomToFit: {
onInit: true,
},
},
} satisfies NgDiagramConfig;
model = initializeModel({
nodes: [
{
id: 'MAIN ROOT',
position: { x: 500, y: 50 },
data: {},
size: { width: 140, height: 50 },
},
{
id: 'child1',
position: { x: 250, y: 100 },
data: {},
size: { width: 120, height: 44 },
},
{
id: 'child2',
position: { x: 250, y: 0 },
data: {},
size: { width: 120, height: 44 },
},
{
id: 'child3',
position: { x: 750, y: 0 },
data: {},
size: { width: 120, height: 44 },
},
{
id: 'leaf1',
position: { x: 0, y: 150 },
data: {},
size: { width: 100, height: 38 },
},
{
id: 'leaf2',
position: { x: 0, y: 50 },
data: {},
size: { width: 100, height: 38 },
},
{
id: 'leaf3',
position: { x: 1000, y: 50 },
data: {},
size: { width: 100, height: 38 },
},
{
id: 'leaf4',
position: { x: 1000, y: -50 },
data: {},
size: { width: 100, height: 38 },
},
{
id: 'leaf5',
position: { x: 1000, y: -150 },
data: {},
size: { width: 100, height: 38 },
},
{
id: 'leaf6',
position: { x: 1000, y: 150 },
data: {},
size: { width: 100, height: 38 },
},
],
edges: [
{
id: 'e1',
data: {},
source: 'MAIN ROOT',
target: 'child1',
sourcePort: 'port-left',
targetPort: 'port-right',
},
{
id: 'e2',
data: {},
source: 'MAIN ROOT',
target: 'child2',
sourcePort: 'port-left',
targetPort: 'port-right',
},
{
id: 'e3',
data: {},
source: 'MAIN ROOT',
target: 'child3',
sourcePort: 'port-right',
targetPort: 'port-left',
},
{
id: 'e4',
data: {},
source: 'child1',
target: 'leaf1',
sourcePort: 'port-left',
targetPort: 'port-right',
},
{
id: 'e5',
data: {},
source: 'child1',
target: 'leaf2',
sourcePort: 'port-left',
targetPort: 'port-right',
},
{
id: 'e6',
data: {},
source: 'child3',
target: 'leaf3',
sourcePort: 'port-right',
targetPort: 'port-left',
},
{
id: 'e7',
data: {},
source: 'child3',
target: 'leaf4',
sourcePort: 'port-right',
targetPort: 'port-left',
},
{
id: 'e8',
data: {},
source: 'child3',
target: 'leaf5',
sourcePort: 'port-right',
targetPort: 'port-left',
},
{
id: 'e9',
data: {},
source: 'child3',
target: 'leaf6',
sourcePort: 'port-right',
targetPort: 'port-left',
},
],
});
}
  • Export: Downloads the flow as a PNG image.
  • Bounding Box Calculation: Ensures the exported image includes all nodes with proper margins.
  • Generate Image Service: Handles the logic for exporting the flow as an image, using the html-to-image library and bounding box calculation.

  • Helper Functions: Manage the download process and calculate the bounding box for the flow.

  • NavBar Component: Provides a button to trigger the download action. Calculates the bounding box for the entire flow and passes the flow element reference to the service.

  • Download: Exports the current flow as a PNG image using the bounding box and margin settings.