Skip to content

Commit 4ab0fd4

Browse files
committed
highlight vertex on hover
1 parent d9dec4b commit 4ab0fd4

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/graph.js

+8
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@ function init(){
2323
document.addEventListener(eventName, onDragEvents, false)
2424
});
2525
window.addEventListener('graph_edge', onGraphEdge, false);
26+
window.addEventListener('graph_mouse', onGraphVertex, false);
2627

2728
fetch('./graphs/GraphSON_blueprints.json')
2829
.then(response => response.json())
2930
.then(json => import_graph(json))
3031

3132
}
3233

34+
function onGraphVertex(e){
35+
if(e.detail.type == 'hover'){
36+
console.log(`graph> hover on ${e.detail.id} , ${e.detail.start}`);
37+
utils.send('graph_vertex',{type:'highlight',id:e.detail.id,start:e.detail.start});
38+
}
39+
}
40+
3341
function import_vertex(vertex){
3442
let res = vertex;
3543
res.label = (typeof(vertex.label) != "undefined")?vertex.label:vertex.name;

src/physics_matter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ function edge_add(params){
234234
stiffness: 0.01,
235235
damping: 0.05
236236
});
237-
Matter.World.addConstraint(engine.world,constraint);
237+
//Matter.World.addConstraint(engine.world,constraint);
238238
}
239239
}
240240

src/view_svg.js

+40-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* - graph (clear)
66
*/
77

8+
import * as utils from "./../src/utils.js";
9+
810
let draw;
911
let width = 100;
1012
let height = 50;
@@ -21,18 +23,39 @@ function init(element){
2123
function vertex_add(id,label){
2224
console.log(`view_svg> added node '${label}'`);
2325
let group = draw.group().id('g_'+id);
24-
let text = draw.text(label).id('t_'+id);
26+
let text = draw.text(label).id('t_'+id).css('pointer-events', 'none');
2527
let vert = draw
2628
.rect(width,height)
2729
.id('vert_'+id)
28-
.attr({ fill: '#00af06' });
30+
.attr({ fill: '#00af06' })
31+
.on([ 'click', 'mouseover',
32+
'mouseleave','contextmenu',
33+
'touchstart','touchend'], onMouseVertex);
2934
vert.center(0,0);
3035
group.add(vert);
3136
text.center(0,0);
3237
group.add(text);
3338
group.center(0,0);
3439
}
3540

41+
42+
function onMouseVertex(e){
43+
//console.log(`${e.type} on ${e.target.id}`);
44+
if(['contextmenu', 'click'].includes(e.type)){
45+
e.preventDefault();
46+
e.stopPropagation();
47+
}
48+
if(['mouseover','touchstart'].includes(e.type)){
49+
const id = e.target.id.substr(5,event.target.id.length);
50+
utils.send('graph_mouse',{'type':'hover',id:id,start:true});
51+
}
52+
if(['mouseleave','touchend'].includes(e.type)){
53+
const id = e.target.id.substr(5,event.target.id.length);
54+
utils.send('graph_mouse',{'type':'hover',id:id,start:false});
55+
}
56+
return false;
57+
}
58+
3659
function vertex_readd(id, label){
3760
//let group = SVG('#g_'+id);
3861
var groupd_svg = document.getElementById('g_'+id);
@@ -105,6 +128,18 @@ function graph_clear(){
105128
}
106129
}
107130

131+
function vertex_highlight(id,start){
132+
let vertex = SVG('#vert_'+id);
133+
if(start){
134+
vertex.css('fill','#b00f06');
135+
}
136+
else{
137+
vertex.css('fill','#228855');
138+
}
139+
console.log(`svg> highlight , ${start}`);
140+
141+
}
142+
108143
function onViewVertex(e){
109144
const d = e.detail;
110145
if(d.type == 'add_before_edge'){
@@ -116,6 +151,9 @@ function onViewVertex(e){
116151
else if(e.detail.type == "add_after_edge"){
117152
vertex_readd(d.id,d.label);
118153
}
154+
else if(e.detail.type == "highlight"){
155+
vertex_highlight(d.id,d.start);
156+
}
119157
}
120158

121159
function onViewEdge(e){

0 commit comments

Comments
 (0)