-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
119 lines (105 loc) · 3.04 KB
/
main.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var Voronoi = require('voronoi')
const urlParams = new URLSearchParams(window.location.search);
var layouts = ['cose', 'cose', 'circle', 'concentric', 'random', 'random']
var layout = urlParams.get('layout') || randItem(layouts)
var n = urlParams.get('n') || 10
var maxx = 100
var points = RandPoints(n, maxx, maxx, 0, 0)
var bbox = { xl: 0, xr: maxx, yt: 0, yb: maxx };
const voronoi = new Voronoi();
var diagram = voronoi.compute(points, bbox)
console.log(diagram)
var vas = diagram.edges.map(edge => {
return {
data: {
id: edge.va.x + edge.va.y + "",
x: edge.va.x,
y: edge.va.y,
}
}
});
var vbs = diagram.edges.map(edge => {
return {
data: {
id: edge.vb.x + edge.vb.y + "",
x: edge.vb.x,
y: edge.vb.y,
}
}
});
var vertices = [...new Set(vas.concat(vbs))]
console.log(vertices)
var edges = diagram.edges.map(edge => {
return {
data: {
id: edge.va.x + edge.va.y + edge.vb.x + edge.vb.y,
source: edge.va.x + edge.va.y + "",
target: edge.vb.x + edge.vb.y + ""
}
}
})
console.log(edges)
var container = document.getElementById('cy')
var cy = cytoscape({
container,
elements: {
nodes: vertices,
edges: edges
},
layout: {
name: layout,
},
});
var colorsPalletes = [
["#22577A", "#38A3A5", "#57CC99", "#80ED99"],
["#264653", "#2A9D8F", "#E9C46A", "#F4A261", "#E76F51"],
["#03045e", "#023e8a", "#0077b6", "#0096c7", "#00b4d8","#48cae4","#90e0ef","#ade8f4", "#caf0f8"]
]
var colors = randItem(colorsPalletes)
for (const vertex of vertices) {
var color = randItem(colors)
cy.style().selector(select(vertex.data.id)).style({
'background-fill': 'radial-gradient',
'background-gradient-stop-colors': color + " " + shadeColor(color, -10) + " " + shadeColor(color, -50),
// 'background-gradient-stop-positions': '0% 95%'
}).update()
}
cy.style().selector("edge").style({
'line-fill': 'linear-gradient',
'line-gradient-stop-colors': edge =>
{
return getColor(edge.data().source) + " " + getColor(edge.data().target)
}
}).update()
console.log(cy.style().json())
cy.on('dragfreeon', 'node', e => {
if (isWin())
{
alert("you won!!")
}
})
function isWin()
{
var edges = cy.edges()
for (const edge1 of edges) {
var a1 = cy.nodes("[id = \"" + edge1.data().source.replace('.', '\.') + "\"]").renderedPosition()
var b1 = cy.nodes("[id = \"" + edge1.data().target.replace('.', '\.') + "\"]").renderedPosition()
for (const edge2 of edges) {
if (edge1 == edge2) continue
var a2 = cy.nodes("[id = \"" + edge2.data().source.replace('.', '\.') + "\"]").renderedPosition()
var b2 = cy.nodes("[id = \"" + edge2.data().target.replace('.', '\.') + "\"]").renderedPosition()
if (intersect(a1.x, a1.y, b1.x, b1.y, a2.x, a2.y, b2.x, b2.y) != false)
{
return false
}
}
}
return true
}
function getColor(nodeId)
{
var nodecolors = cy.$(select(nodeId)).style('background-gradient-stop-colors').split(' ')
var lastColor = nodecolors[1]
console.log(lastColor)
return lastColor
}