Skip to content

Commit cf7b585

Browse files
committed
multi filters
1 parent 9ed3fb1 commit cf7b585

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

libs/svg_utils.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,25 @@ class Svg{
105105
return html(parent,/*html*/`
106106
<filter id="${params.id}" width="200%" height="200%">
107107
<feDiffuseLighting in="SourceGraphic" result="light" light-color="white">
108-
<fePointLight x="-30" y="-10" z="20"></fePointLight>
108+
<fePointLight x="${params.lx}" y="${params.ly}" z="${params.lz}"></fePointLight>
109109
</feDiffuseLighting>
110110
<feComposite in="SourceGraphic" in2="light" operator="arithmetic" k1="0.8" k2="0.2" k3="0" k4="0"></feComposite>
111-
<feDropShadow dx="10" dy="5" stdDeviation="3"></feDropShadow>
111+
<feDropShadow dx="${params.dx}" dy="${params.dy}" stdDeviation="3"></feDropShadow>
112+
</filter>`)
113+
}
114+
filter_light(parent,params){
115+
return html(parent,/*html*/`
116+
<filter id="${params.id}" width="200%" height="200%">
117+
<feDiffuseLighting in="SourceGraphic" result="light" light-color="white">
118+
<fePointLight x="${params.lx}" y="${params.ly}" z="${params.lz}"></fePointLight>
119+
</feDiffuseLighting>
120+
<feComposite in="SourceGraphic" in2="light" operator="arithmetic" k1="0.8" k2="0.2" k3="0" k4="0"></feComposite>
121+
</filter>`)
122+
}
123+
filter_shadow(parent,params){
124+
return html(parent,/*html*/`
125+
<filter id="${params.id}" width="200%" height="200%">
126+
<feDropShadow dx="${params.dx}" dy="${params.dy}" stdDeviation="3"></feDropShadow>
112127
</filter>`)
113128
}
114129

src/mouse.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ function onMousePan(e){
4242
}else if(e.type == "mousedown"){
4343
if((e.buttons == 1) && is_vetex){
4444
state.dragging = true
45-
event("vertex_drag",{type:"start",id:e.target.id})
45+
state.id = e.target.id
46+
event("vertex_drag",{type:"start",id:state.id})
4647
}
4748
}else if(e.type == "mouseup"){
4849
if(state.dragging){
4950
state.dragging = false
50-
event("vertex_drag",{type:"end"})
51+
event("vertex_drag",{type:"end",id:state.id})
5152
}
5253
}
5354
state.coord.x = e.clientX;

src/render.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,41 @@ let utl = new Svg()
66
let g = null;
77
let svg = null;
88

9+
function set_filter_neighbors(vertex,filterName){
10+
if(filterName != ""){
11+
for(let [vid,v] of Object.entries(vertex.neighbors)){
12+
v.svg.setAttribute("filter",`url(#${filterName})`)
13+
}
14+
}
15+
}
16+
917
function onVertexHover(e){
1018
const vertex = g.vertices[e.detail.id]
1119
if(e.detail.type == "enter"){
12-
for(let [vid,v] of Object.entries(vertex.neighbors)){
13-
v.svg.setAttribute("filter","url(#f_light_shadow)")
14-
}
20+
vertex.svg.setAttribute("filter",`url(#f_light_shadow)`)
21+
set_filter_neighbors(vertex,"f_light_shadow")
1522
console.log(`hover enter: ${vertex.label}`)
1623
}else if(e.detail.type == "exit"){
17-
for(let [vid,v] of Object.entries(vertex.neighbors)){
18-
v.svg.setAttribute("filter","")
19-
}
24+
vertex.svg.setAttribute("filter",`url(#f_light)`)
25+
set_filter_neighbors(vertex,"f_light")
2026
console.log(`hover exit: ${vertex.label}`)
2127
}
2228
}
2329

30+
function onVertexDrag(e){
31+
const vertex = g.vertices[e.detail.id]
32+
if(e.detail.type == "start"){
33+
vertex.svg.setAttribute("filter",`url(#f_light_shadow_h)`)
34+
}else if(e.detail.type == "end"){
35+
vertex.svg.setAttribute("filter",`url(#f_light_shadow)`)
36+
}
37+
}
38+
2439
class Render{
2540
constructor(graph_data){
2641
g = graph_data
2742
window.addEventListener( 'vertex_hover', onVertexHover, false );
43+
window.addEventListener( 'vertex_drag', onVertexDrag, false );
2844
}
2945

3046
create(parent_div){
@@ -48,7 +64,11 @@ class Render{
4864

4965
draw(){
5066
clear(svg)
51-
utl.filter_light_shadow(svg,{id:"f_light_shadow"})
67+
utl.filter_light(svg,{id:"f_light",lx:-30,ly:-10,lz:20})
68+
utl.filter_light(svg,{id:"f_high_light",lx:-30,ly:-10,lz:100})
69+
utl.filter_light_shadow(svg,{id:"f_light_shadow_h",lx:-30,ly:-10,lz:20,dx:15,dy:8})
70+
utl.filter_light_shadow(svg,{id:"f_light_shadow",lx:-30,ly:-10,lz:20,dx:5,dy:2})
71+
const defaultFilter = "f_light"
5272
let g_edges = html(svg,/*html*/`<g ig="edges">`)
5373
for(let [eid,e] of Object.entries(g.edges)){
5474
let [x1,y1,x2,y2] = [e.outV.viewBox.x,e.outV.viewBox.y,e.inV.viewBox.x,e.inV.viewBox.y]
@@ -59,7 +79,7 @@ class Render{
5979
for(let [vid,v] of Object.entries(g.vertices)){
6080
let [x,y,w,h] = [-v.viewBox.width/2,-v.viewBox.height/2,v.viewBox.width,v.viewBox.height]
6181
v.svg = html(g_vertices,/*html*/`<g id="vert_${v.name}"/>`)
62-
html(v.svg,/*html*/`<rect class="vertex" id="${v.id}" x="${x}" y="${y}" rx="3" width="${w}" height="${h}" fill="rgb(100,205,100)" />`)
82+
html(v.svg,/*html*/`<rect class="vertex" id="${v.id}" x="${x}" y="${y}" rx="3" width="${w}" height="${h}" fill="rgb(100,205,100)" filter="url(#${defaultFilter})" />`)
6383
html(v.svg,/*html*/`<text x="0" y="0" dominant-baseline="middle" text-anchor="middle" style="pointer-events:none">${v.label}</text>`)
6484
v.svg.setAttribute("transform", `translate(${v.viewBox.x},${v.viewBox.y}) rotate(${0})`);
6585
}

0 commit comments

Comments
 (0)