JS Achieves Particle Shuttle

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

    1. Expected effect

    In the large screen scene, add a cool particle shuttle background to the decision report, and the effect is shown in the following figure:

    1.gif


    2. Implementation ideas

    Set initialization JS event to generate dynamic background.


    II. Example

    1. Design report

    Click the body component to add after initialization events, as shown in the figure below:

    2.png

    The JavaScript code is as follows:

    $("body").prepend('<canvas id="mycanvas" style="position:absolute;z-index:-2; background-color:black;"></canvas>');
    var canvas = mycanvas; 
    var w = window.innerWidth;
    var h = window.innerHeight;
    canvas.width = w;
    canvas.height = h;
    canvas.backgroundColor = "#000";
    var ctx = canvas.getContext('2d');
     
    function Build({
        this.ctx = ctx;
        this.counts = 3000;  
        this.maxSize = 4;  
        this.halfWidth = w / 2,
            this.halfHeight = h / 2;
        this.arr = [];  
    }
    Build.prototype.add = function(coor{
        var grd = this.ctx.createRadialGradient(coor.x, coor.y, coor.size / 2, coor.x, coor.y, coor.size);
        grd.addColorStop(0"white");
        grd.addColorStop(1, coor.color);
        this.ctx.fillStyle = grd;
        this.ctx.beginPath();
        this.ctx.arc(coor.x, coor.y, coor.size, 0Math.PI * 2true);
        this.ctx.transform(10010, coor.z);
        this.ctx.closePath();
        this.ctx.fill();
    }
    Build.prototype.init = function({
        this.run();
        this.render();
        this.animate();
    }
    Build.prototype.run = function({
        var nums = 0;
        while (nums < this.counts) {
            var coor = {
                x: Math.ceil(Math.random() * w),
                y: Math.ceil(Math.random() * h),
                posx: Math.random() * w - this.halfWidth,
                posy: Math.random() * h - this.halfHeight,
                fl: 100,
                speed: Math.random() * 2,
                posz: Math.random() * 250,
                r: Math.ceil(Math.random() * this.maxSize),
                color: "rgba(" + Math.ceil(Math.random() * 255) + "," + Math.ceil(Math.random() * 255) + "," + Math.ceil(Math.random() * 255) + "," + Math.random() + ")"
            };
            this.arr.push(coor);
            nums++;
        }
    }
    Build.prototype.clear = function({
        ctx.clearRect(00, w, h);
    }
    Build.prototype.render = function({
        this.clear();
        for (var item of this.arr) {
            this.draw(item);
        }
    }
    Build.prototype.animate = function({
            var _this = this;
            this.render(); 
            window.requestAnimationFrame(function({
                _this.animate();
            });
        },
        Build.prototype.draw = function(item{
            if (item.posz > -item.fl) { 
                var scale = item.fl / (item.fl + item.posz); 
                item.x = this.halfWidth + item.posx * scale;
                item.y = this.halfHeight + item.posy * scale;
                item.size = item.r * scale;
                item.posz -= item.speed;
            } else { 
                item.posz = Math.random() * 250;
            }
            this.add(item);
        }
    var app = new Build();
    app.init();
    window.addEventListener('resize'function({
        canvas.width = w = window.innerWidth;
        canvas.height = h = window.innerHeight;
    }, false); 
    Show Code


    2. Effect preview

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


    III. Template download

    For completed templates, please refer to:

    %fr_HOME%\webapps\webroot\WEB-INF\reportlets\doc-EN\JS\JS_particle_shuttle.frm

    Click to download the template:JS_particle_shuttle.frm



    Attachment List


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

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

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

    不再提示

    9s后關閉

    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