forked from sgratzl/cytoscape.js-layers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotations.ts
92 lines (83 loc) · 2.59 KB
/
annotations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* eslint-disable @typescript-eslint/no-namespace */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
namespace Annotations {
declare const cytoscape: typeof import('cytoscape');
declare const CytoscapeLayers: typeof import('../build');
const cy = cytoscape({
container: document.getElementById('app'),
elements: fetch('./grid-data.json').then((r) => r.json()),
layout: {
name: 'grid',
},
});
const layers = CytoscapeLayers.layers(cy);
function renderBar(ctx: CanvasRenderingContext2D, value: number, y: number, w: number, h: number) {
ctx.fillRect(0, y, w * value, h);
ctx.strokeRect(0, y, w, h);
}
function renderHistogram(ctx: CanvasRenderingContext2D, bins: number[], y: number, w: number, h: number) {
const binWidth = w / bins.length;
for (let i = 0; i < bins.length; i++) {
const vi = bins[i];
ctx.fillRect(i * binWidth, y + h * (1 - vi), binWidth, h * vi);
}
ctx.strokeRect(0, y, w, h);
}
cy.on('ready', () => {
layers.renderPerNode(
layers.nodeLayer.insertAfter('canvas'),
(ctx, node, bb) => {
const barValue: number = node.scratch('value') ?? Math.random();
node.scratch('value', barValue);
const histogram: number[] = node.scratch('bins') ?? [
Math.random(),
Math.random(),
Math.random(),
Math.random(),
Math.random(),
];
node.scratch('bins', histogram);
ctx.fillStyle = 'white';
ctx.fillRect(0, bb.h, bb.w, 20);
ctx.strokeStyle = 'black';
ctx.fillStyle = 'red';
renderBar(ctx, barValue, bb.h, bb.w, 5);
ctx.fillStyle = 'green';
renderHistogram(ctx, histogram, bb.h + 5, bb.w, 15);
},
{
position: 'top-left',
checkBounds: true,
}
);
});
console.log(layers);
document.getElementById('png')?.addEventListener('click', () => {
layers
.png({
output: 'blob-promise',
ignoreUnsupportedLayerOrder: true,
bg: 'red',
})
.then((r) => {
const url = URL.createObjectURL(r);
const a = document.getElementById('url') as HTMLAnchorElement;
a.href = url;
a.click();
});
});
document.getElementById('png2')?.addEventListener('click', () => {
layers
.png({
output: 'blob-promise',
ignoreUnsupportedLayerOrder: true,
full: true,
})
.then((r) => {
const url = URL.createObjectURL(r);
const a = document.getElementById('url') as HTMLAnchorElement;
a.href = url;
a.click();
});
});
}