whispervis/animate.go

92 lines
2.3 KiB
Go
Raw Normal View History

2018-09-05 16:53:09 +03:00
package main
2018-09-17 22:11:04 +03:00
import (
"fmt"
2018-09-20 22:16:21 +03:00
"time"
2018-09-20 15:41:24 +03:00
2018-09-17 22:11:04 +03:00
"github.com/gopherjs/gopherjs/js"
"github.com/status-im/simulation/propagation"
2018-09-17 22:11:04 +03:00
)
2018-09-05 16:53:09 +03:00
const (
// TODO(divan): move this as variables to the frontend
BlinkDecay = 100 * time.Millisecond // time for highlighted node/link to be active
AnimationSlowdown = 1 // slowdown factor for propagation animation
FPS = 20 // default FPS
)
2018-09-20 22:16:21 +03:00
// animate fires up as an requestAnimationFrame handler.
2018-09-17 22:11:04 +03:00
func (w *WebGLScene) animate() {
if w.renderer == nil {
2018-09-05 16:53:09 +03:00
return
}
2018-09-17 22:11:04 +03:00
w.controls.Update()
2018-09-05 16:53:09 +03:00
time.AfterFunc((1000/FPS)*time.Millisecond, func() {
js.Global.Call("requestAnimationFrame", w.animate)
})
2018-09-05 16:53:09 +03:00
2018-09-17 22:11:04 +03:00
if w.autoRotate {
pos := w.graphGroup.Object.Get("rotation")
2018-09-17 22:11:04 +03:00
pos.Set("y", pos.Get("y").Float()+float64(0.001))
2018-09-05 16:53:09 +03:00
}
2018-10-12 22:34:30 +02:00
if w.wobble {
w.wobbling.Animate()
w.updatePositions()
}
2018-09-17 22:11:04 +03:00
w.renderer.Render(w.scene, w.camera)
2018-09-05 16:53:09 +03:00
}
2018-09-17 22:11:04 +03:00
// ToggleAutoRotation switches auto rotation option.
func (w *WebGLScene) ToggleAutoRotation() {
w.autoRotate = !w.autoRotate
2018-09-05 16:53:09 +03:00
}
2018-09-20 15:41:24 +03:00
2018-10-12 22:34:30 +02:00
// ToggleWobbling switches wobbling option.
func (w *WebGLScene) ToggleWobbling() {
w.wobble = !w.wobble
}
2018-09-20 22:16:21 +03:00
// BlinkNode animates a single node blinking. Node specified by its idx.
func (w *WebGLScene) BlinkNode(id int) {
node := w.nodes[id]
node.Set("material", BlinkedNodeMaterial)
restore := func() { node.Object.Set("material", DefaultNodeMaterial) }
time.AfterFunc(BlinkDecay, restore)
}
// BlinkEdge animates a single edge blinking. Edge specified by its idx.
func (w *WebGLScene) BlinkEdge(id int) {
edge := w.lines[id]
edge.Set("material", BlinkedEdgeMaterial)
restore := func() { edge.Object.Set("material", DefaultEdgeMaterial) }
time.AfterFunc(BlinkDecay, restore)
2018-09-20 15:41:24 +03:00
}
// AnimatePropagation visualizes propagation of message based on plog.
func (w *WebGLScene) AnimatePropagation(plog *propagation.Log) {
fmt.Println("Animating plog")
for i, ts := range plog.Timestamps {
duration := time.Duration(time.Duration(ts) * time.Millisecond)
duration = duration * AnimationSlowdown
nodes := plog.Nodes[i]
edges := plog.Indices[i]
fn := func() {
// blink nodes for this timestamp
for _, idx := range nodes {
w.BlinkNode(idx)
}
// blink links for this timestamp
for _, idx := range edges {
w.BlinkEdge(idx)
}
}
time.AfterFunc(duration, fn)
}
}