MarcusCalidus / marcuscalidus-svg-panel

Grafana panel for displaying metric sensitive SVG images using the snap svg library
MIT License
63 stars 28 forks source link

Animate a svg in a pannel #56

Open SamuelJoly opened 4 years ago

SamuelJoly commented 4 years ago

I'm trying to animate a rectangle (rotation) on a pannel. My code doesn't work and I'm wondering why. I see the rectangle in the pannel, but it doesn't rotate. This is my code

image image

I used this example : http://svg.dabbles.info/snaptut-transform-groups

Thanks

MarcusCalidus commented 4 years ago

Animation in Snap is best performed by defining the animation in onInit(). There ist the Animation example button in the plugin. Unfortunately this is only visible on dark dashboards ;)

The idea is to define the animation in onInit() like this:

this.draw = function() {
  this.angle--;
  var s = Snap(svgnode);
  var w = s.select("#wheel"); 
  w.attr({ transform: "rotate("+this.angle+" 23.592 19.5)"});
}

this.animate = function() {
    if (this.isVentilating) {  
      this.draw();
      window.requestAnimationFrame(this.animate.bind(this));
    }
}

this.stopAnimation = function() {
    this.isVentilating = false;
}

this.startAnimation = function() {
    if (!this.isVentilating) {
      this.isVentilating = true;
      this.animate();
    }
}

to start or stop the animation call the functions this.startAnimation() and this.stopAnimation() in onHandleMetric(). To have the animation start immediately, regardless of the data handled you can of course start the animation directly in onInit().