whispervis/animate.go

49 lines
1.1 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 (
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"
)
2018-09-05 16:53:09 +03:00
2018-09-20 22:16:21 +03:00
const BlinkDecay = 100 * time.Millisecond
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
2018-09-17 22:11:04 +03:00
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-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-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
}