From d52efa467297027b8b7a3951561f576b4bd3ac5c Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 22 Jul 2017 17:37:21 +0200 Subject: [PATCH] visualizer: improve timing --- .../js/app/widgets/plots/visualizer.js | 49 ++++++++----------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/browser/js/app/widgets/plots/visualizer.js b/src/browser/js/app/widgets/plots/visualizer.js index c08a2745..a25602c7 100644 --- a/src/browser/js/app/widgets/plots/visualizer.js +++ b/src/browser/js/app/widgets/plots/visualizer.js @@ -53,41 +53,49 @@ module.exports = class Visualizer extends _plots_base { this.looping = false this.clock = 0 this.lastUpdate = 0 + this.watchDuration = 1000 * this.getProp('duration') + this.ticks = 0 } syncHandle(e) { - if (this.getProp('widgetId')!=e.id || !widgetManager.getWidgetById(e.id).length) return - this.startLoop() + if (this.getProp('widgetId')!=e.id || !widgetManager.getWidgetById(e.id).length || !e.widget) return + this.setValue(e.widget.getValue()) } startLoop() { this.clock = new Date().getTime() - if (!this.looping) this.loop() - + if (!this.looping) { + this.lastUpdate = new Date().getTime() + this.looping = true + this.ticks = 0 + this.loop() + } } loop() { - this.looping = true var t = new Date().getTime() - if (t -this.clock >= 1000 * this.getProp('duration')) { + if (t -this.clock >= this.watchDuration) { this.looping = false - return } - var ticks = Math.floor((t - this.lastUpdate) / (1000/this.fps)) + this.ticks += (t - this.lastUpdate) / (1000/this.fps) + + if (Math.floor(this.ticks) > 0) { + this.shiftData(Math.floor(this.ticks)) + this.ticks -= Math.floor(this.ticks) + this.draw() + } - this.updateData() - if (ticks > 1 && this.lastUpdate != 0) this.shiftData(ticks - 1) - - this.draw() this.lastUpdate = t + if (!this.looping) return + setTimeout(()=>{ this.loop() }, (1000/this.fps)) @@ -121,23 +129,6 @@ module.exports = class Visualizer extends _plots_base { } - - updateData() { - - var id = this.getProp('widgetId'), - widget = widgetManager.getWidgetById(id) - - if (typeof id == 'string' && widget.length) { - var v = widget[widget.length-1].getValue() - this.data.push(v) - this.value = v - this.data.splice(0,1) - } else { - this.shiftData(1) - } - - } - shiftData(n) { for (var i=0; i