JS Abstract Animation Background

  • Last update:February 22, 2021
  • I. Overview

    1. Expected effect

    In the large screen scenario, add a cool Abstract animation background to the decision report, and the effect is as follows:

    1.gif


    2. Implementation ideas

    Insert JS code, reference external CSS file, generate dynamic abstract background.


    II. Example

    1. Report design

    Create a new dashboard, select the body component, and add After initialization event, as shown in the following figure:

    The JavaScript code is as follows:

    $("body").prepend('<canvas id="canvas" style="position:absolute;z-index:-2;"></canvas>');
    var smoothTrail = function(c, cw, ch){
      this.init = function(){
        this.loop();
      };

      var _this = this;
      this.c = c;
      this.ctx = c.getContext('2d');
      this.cw = cw;
      this.ch = ch;
      this.mx = 0;
      this.my = 0;
      
      //trail
      this.trail = [];
      this.maxTrail = 200;
      this.mouseDown = false;
      
      this.ctx.lineWidth = .1;
      this.ctx.lineJoin = 'round';
      
      this.radius = 1;
      this.speed = 0.4;
      this.angle = 0;
      this.arcx = 0;
      this.arcy = 0;
      this.growRadius = true;
      this.seconds = 0;
      this.milliseconds = 0;
      

      this.rand = function(rMi, rMa){return ~~((Math.random()*(rMa-rMi+1))+rMi);};
      this.hitTest = function(x1, y1, w1, h1, x2, y2, w2, h2){return !(x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1);};
      

      this.createPoint = function(x, y){
        this.trail.push({
          x: x,
          y: y
        });
      };

      this.updateTrail = function(){
        
        if(this.trail.length < this.maxTrail){
          this.createPoint(this.arcx, this.arcy);
        }
        
        if(this.trail.length >= this.maxTrail){
          this.trail.splice(01);
        }
      };
      
      this.updateArc = function(){
        this.arcx = (this.cw/2) + Math.sin(this.angle) * this.radius;
        this.arcy = (this.ch/2) + Math.cos(this.angle) * this.radius;
        var d = new Date();
        this.seconds = d.getSeconds();
        this.milliseconds = d.getMilliseconds();
        this.angle += this.speed*(this.seconds+1+(this.milliseconds/1000));
        
        if(this.radius <= 1){
          this.growRadius = true;
        } 
        if(this.radius >= 200){
          this.growRadius = false;
        }
        
        if(this.growRadius){
          this.radius += 1;
        } else {
          this.radius -= 1;
        }
      };
      

      this.renderTrail = function(){
        var i = this.trail.length;
        
        this.ctx.beginPath();
        while(i--){
          var point = this.trail[i];
          var nextPoint = (i == this.trail.length) ? this.trail[i+1] : this.trail[i];
          
          var c = (point.x + nextPoint.x) / 2;
          var d = (point.y + nextPoint.y) / 2;
          this.ctx.quadraticCurveTo(Math.round(this.arcx), Math.round(this.arcy), c, d);
          
          
          
        };
        this.ctx.strokeStyle = 'hsla('+this.rand(170,300)+', 100%, '+this.rand(5075)+'%, 1)';
        this.ctx.stroke();
        this.ctx.closePath();
        
      };
     

      this.clearCanvas = function(){
        //this.ctx.globalCompositeOperation = 'source-over';
        //this.ctx.clearRect(0,0,this.cw,this.ch);
        
        this.ctx.globalCompositeOperation = 'destination-out';
        this.ctx.fillStyle = 'rgba(0,0,0,.1)';
        this.ctx.fillRect(0,0,this.cw,this.ch);
        this.ctx.globalCompositeOperation = 'lighter';
      };
      

      this.loop = function(){
        var loopIt = function(){
          requestAnimationFrame(loopIt, _this.c);
          _this.clearCanvas();
          _this.updateArc();
          _this.updateTrail();
          _this.renderTrail();
        };
        loopIt();
      };
      
    };

    var isCanvasSupported = function(){
      var elem = document.createElement('canvas');
      return !!(elem.getContext && elem.getContext('2d'));
    };

    var setupRAF = function(){
      var lastTime = 0;
      var vendors = ['ms''moz''webkit''o'];
      for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x){
        window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
        window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
      };
      
      if(!window.requestAnimationFrame){
        window.requestAnimationFrame = function(callback, element){
          var currTime = new Date().getTime();
          var timeToCall = Math.max(016 - (currTime - lastTime));
          var id = window.setTimeout(function({ callback(currTime + timeToCall); }, timeToCall);
          lastTime = currTime + timeToCall;
          return id;
        };
      };
      
      if (!window.cancelAnimationFrame){
        window.cancelAnimationFrame = function(id){
          clearTimeout(id);
        };
      };
    };

      if(isCanvasSupported){
        var c = document.createElement('canvas');
        c.width = 400;
        c.height = 400;
        var cw = c.width;
        var ch = c.height;
        document.body.appendChild(c);
        var cl = new smoothTrail(c, cw, ch);
        
        setupRAF();
        cl.init();
      }
    Show Code


    2. Reference CSS

    1) Copy sty.css to directory %FR_HOME%\webapps\webroot\help\css

    2) Select body, add an initialization event again, and reference the above CSS file, as shown in the figure below:

    截屏2021-02-22 下午5.05.36.png

    The JavaScript code is as follows:

    var link = '<link rel="stylesheet" href="' + FR.server + '/help/css/sty.css">';
    $("head").append(link);


    3. Effect preview

    Save the template, click dashboard preview, the effect is as shown in I.1 expected effect.


    III. Template download

    For completed template, please refer to

    %fr_HOME%\webapps\webroot\WEB-INF\reportlets\doc-EN\JS\JS Abstract animation background.frm

    Click download template: 

    JS Abstract animation background.frm

    Click to download CSS file: 

    sty.css


    Attachment List


    Theme: FineVis Data Visualization
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    7s后關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy