Skip to content

Commit 3107e21

Browse files
committed
[stories] Adds a new story
That new story showcases how to render local images in various ways with @sigma/node-image. It addresses issue #1440.
1 parent 721ac7e commit 3107e21

File tree

8 files changed

+169
-1
lines changed

8 files changed

+169
-1
lines changed
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { NodeImageProgram, NodePictogramProgram } from "@sigma/node-image";
2+
import Graph from "graphology";
3+
import Sigma from "sigma";
4+
5+
import { onStoryDown } from "../utils";
6+
// Gives a URL usable at runtime
7+
import PNG_IMAGE from "./images/dice.png";
8+
// Gives a URL usable at runtime
9+
import JPG_IMAGE from "./images/flower.jpg";
10+
// Gives a URL usable at runtime
11+
import SVG_ICON from "./images/icon-0.svg";
12+
// Gives the string content of the SVG image
13+
import RAW_SVG_ICON from "./images/icon-1.svg?raw";
14+
15+
const STRING_SVG_ICON = `<svg
16+
fill="#000000"
17+
stroke-width="0"
18+
viewBox="0 0 320 512"
19+
height="200px"
20+
width="200px"
21+
xmlns="http://www.w3.org/2000/svg"
22+
>
23+
<path
24+
d="M142.9 96c-21.5 0-42.2 8.5-57.4 23.8L54.6 150.6c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L40.2 74.5C67.5 47.3 104.4 32 142.9 32C223 32 288 97 288 177.1c0 38.5-15.3 75.4-42.5 102.6L109.3 416H288c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-12.9 0-24.6-7.8-29.6-19.8s-2.2-25.7 6.9-34.9L200.2 234.5c15.2-15.2 23.8-35.9 23.8-57.4c0-44.8-36.3-81.1-81.1-81.1z"
25+
></path>
26+
</svg>`;
27+
28+
const BASE_64_IMAGE =
29+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURB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Nz8bWk1qMxjGTOMThZ3kvgLI5AzFfo379UAAAAASUVORK5CYII=";
30+
31+
function svgToDataURI(svg: string): string {
32+
const blob = new Blob([svg], { type: "image/svg+xml" });
33+
return URL.createObjectURL(blob);
34+
}
35+
36+
export default () => {
37+
const container = document.getElementById("sigma-container") as HTMLElement;
38+
39+
const graph = new Graph();
40+
41+
graph.addNode("a", {
42+
x: 0,
43+
y: 0,
44+
size: 20,
45+
label: "A",
46+
color: "red",
47+
image: PNG_IMAGE,
48+
type: "image",
49+
});
50+
graph.addNode("b", {
51+
x: 1,
52+
y: -1,
53+
size: 40,
54+
label: "B",
55+
color: "red",
56+
image: JPG_IMAGE,
57+
type: "image",
58+
});
59+
graph.addNode("c", {
60+
x: 3,
61+
y: -2,
62+
size: 20,
63+
label: "C",
64+
color: "red",
65+
image: SVG_ICON,
66+
type: "pictogram",
67+
});
68+
graph.addNode("d", {
69+
x: 1,
70+
y: -3,
71+
size: 20,
72+
label: "D",
73+
color: "blue",
74+
image: svgToDataURI(RAW_SVG_ICON),
75+
type: "pictogram",
76+
});
77+
graph.addNode("e", {
78+
x: 3,
79+
y: -4,
80+
size: 40,
81+
label: "E",
82+
color: "blue",
83+
image: svgToDataURI(STRING_SVG_ICON),
84+
type: "pictogram",
85+
});
86+
graph.addNode("f", {
87+
x: 4,
88+
y: -5,
89+
size: 20,
90+
label: "F",
91+
color: "blue",
92+
image: BASE_64_IMAGE,
93+
type: "image",
94+
});
95+
96+
graph.addEdge("a", "b", { size: 10 });
97+
graph.addEdge("b", "c", { size: 10 });
98+
graph.addEdge("b", "d", { size: 10 });
99+
graph.addEdge("c", "b", { size: 10 });
100+
graph.addEdge("c", "e", { size: 10 });
101+
graph.addEdge("d", "c", { size: 10 });
102+
graph.addEdge("d", "e", { size: 10 });
103+
graph.addEdge("e", "d", { size: 10 });
104+
graph.addEdge("f", "e", { size: 10 });
105+
106+
const renderer = new Sigma(graph, container, {
107+
nodeProgramClasses: {
108+
image: NodeImageProgram,
109+
pictogram: NodePictogramProgram,
110+
},
111+
});
112+
113+
onStoryDown(() => {
114+
renderer.kill();
115+
});
116+
};

packages/storybook/stories/node-image/stories.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { Meta, StoryObj } from "@storybook/web-components";
22

33
import template from "./index.html?raw";
4+
import localImagesPlay from "./local-images";
5+
import localImagesSource from "./local-images?raw";
46
import nodeImagesPlay from "./node-images";
57
import nodeImagesSource from "./node-images?raw";
68
import nodePictogramsPlay from "./node-pictograms";
@@ -54,6 +56,18 @@ export const nodePictogramsWithBackground: Story = {
5456
},
5557
};
5658

59+
export const localImages: Story = {
60+
name: "Displaying local images",
61+
render: () => template,
62+
play: localImagesPlay,
63+
args: {},
64+
parameters: {
65+
storySource: {
66+
source: localImagesSource,
67+
},
68+
},
69+
};
70+
5771
export const optionsShowcase: Story = {
5872
name: "Options showcase",
5973
render: () => template,

packages/storybook/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "../../tsconfig.base.json",
3-
"include": ["./types/raw.d.ts", ".storybook/*.tsx", "stories"]
3+
"include": ["./types", ".storybook/*.tsx", "stories"]
44
}

packages/storybook/types/images.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
declare module "*.jpg" {
2+
const value: string;
3+
export default value;
4+
}
5+
6+
declare module "*.png" {
7+
const value: string;
8+
export default value;
9+
}
10+
11+
declare module "*.svg" {
12+
const value: string;
13+
export default value;
14+
}

0 commit comments

Comments
 (0)