JavaScript to achieve fireworks effects (object-oriented)

JavaScript to achieve fireworks effects (object-oriented)

This article shares the specific code for JavaScript to achieve fireworks effects for your reference. The specific content is as follows

This effect uses object-oriented programming

analyze

OOA

  • Click trigger event
  • The fireworks display is divided into two stages

Fly Upward
explode

OOD

class FireWork{
    constructor(){
        
    }
    bindEvent(){
        let _this = this;
        ele.onclick = function(){
            //After fly is finished, call the boom function_this.fly(_this.boom);
        }
    }
    fly(boom){
        
    }
    boom(){
        
    }
}

CSS design implementation

CSS Code

 *{
        margin: 0;
        padding: 0;
    }
    #box{
        width:800px;
        height:600px;
        position: relative;
        margin: 100px auto;
        background:#000000;
        border:2px solid red;
        overflow: hidden;
    }
    .ball{
        width: 40px;
        height: 40px;
        border-radius: 50%;
        position: absolute;
        bottom: 0;
    }

JS programming implementation

JavaScript code

<script src="./utils.js"></script>
<script>
// 
class FireWork{
    constructor(){
        this.box = document.getElementById("box");
        this.box_offset = {
              left : box.offsetLeft,
              top : box.offsetTop
        }
    }
    bindEvent(){
        let _this = this;
        this.box.onclick = function( e ){
              e = e || event;
              _this.x = e.clientX - _this.box_offset.left; 
              _this.y = e.clientY - _this.box_offset.top; 
    
              _this.fly(_this.boom);
        }     
    }     
    fly(boom ){
        let ele = this.createEle();
        let _this = this;
        //Put it in the box; 
        ele.style.left = this.x + "px";
    
        let _left = this.x ; 
        let _top = this.y ; 
    
        animate(ele, {
              top : this.y 
        } , function(){
              ele.remove();
              _this.boom( _left , _top );
        });
    }
    boom( x , y ){
        let r = 100;
        let count = 0 ; 
        let _this = this;
    
        for(let i = 0 ; i < 20 ; i ++){
              let ele = this.createEle();
              ele.style.left = x + "px";
              ele.style.top = y + "px";
              let _left = parseInt(Math.cos( Math.PI / 10 * i ) * r ) + x ;
              let _top = parseInt(Math.sin( Math.PI / 10 * i ) * r) + y
              animate(ele, {
                    left : _left,
                    top : _top,
                    opacity : 0 
              } , function(){
                    ele.remove();
              })
        }
    }
    createEle(){
        let ele = document.createElement("div");
        ele.className = "ball";
        ele.style.backgroundColor = `rgb(${parseInt(Math.random() * 255)},${parseInt(Math.random() * 255)},${parseInt(Math.random() * 255)})`;
        this.box.appendChild(ele);
        return ele ; 
    }
}

new FireWork().bindEvent();
</script>

Referenced utils.js file

function on(dom, event_name, handler_selector, delegation_handler) {
    if( typeof handler_selector === "string" && typeof delegation_handler === "function"){
        return delegation(dom, event_name, handler_selector, delegation_handler);
    }
    // Create an event name in the DOM object: an array corresponding to the event processing function; 
    // Determine whether the current event processing function is in the DOM object;
    var event_type = "_" + event_name;
    if (event_type in dom) {
        dom[event_type].push(handler_selector);
    } else {
        dom[event_type] = [handler_selector];
    }
    // If you use the event name directly as the key value in the object, it will conflict with the original DOM function name; 
    // Because special event names will cause the event to fail to trigger, we need to split the event name here; 
    dom.addEventListener(event_name.split(".")[0], handler_selector)
}
function off(dom, event_name) {
    // Get a set of event processing functions corresponding to the event name in the DOM object; 
    var callback_list = dom["_" + event_name];
    // Remove events according to all functions in the list; 
    callback_list.forEach(function (event_handler) {
        dom.removeEventListener(event_name.split(".")[0], event_handler);
    })
    // Clear the event processing function group in the DOM object; 
    dom["_" + event_name].length = 0;
}
    
function trigger(dom, event_type) {
    dom.dispatchEvent(new Event(event_type));
}
    
function delegation(dom, event_name, selector, event_handler) {
    dom.addEventListener(event_name, function (e) {
        e = e || event;
        var target = e.target || e.srcElement;
        while (target !== dom) {
              switch (selector[0]) {
                    case ".":
                          if (selector.slice(1) === target.className) {
                                event_handler.call(target , e )
                                return;
                          }
                    case "#":
                          if (selector.slice(1) === target.id) {
                                event_handler.call(target, e)
                                return;
                          }
                    default:
                          if (selector.toUpperCase() === target.nodeName) {
                                event_handler.call(target, e)
                                return;
                          }
              }
              target = target.parentNode;
        }
    })
}


function animate( dom , attrs , callback , transition = "buffer", speed = 10 ){
    // transition: There are two animation modes: "buffer" and "liner"
    var _style = getComputedStyle( dom );
    
    // - 1. Data deformation; 
    for(var attr in attrs ){
        attrs[attr] = {
              target : attrs[attr],
              now : _style[attr]
        }
        // - 2. Speed: positive or negative speed of uniform motion; 
        if( transition === "liner" ){
              attrs[attr].speed = attrs[attr].target > attrs[attr].now ? speed : - speed;
        }
    
        if( attr === "opacity"){
              attrs[attr].target *= 100 
              attrs[attr].now *= 100
        }else{
              attrs[attr].now = parseInt(attrs[attr].now) 
        }
    }
    // - turn off the start timer;    
    clearInterval( dom.interval );
    dom.interval = setInterval( function() {
        for(var attr in attrs ){
              // Get the current value and attribute value; 
              // attrs[attr].target : target value; 
              // attrs[attr].now : current value; 
    
              let { target , now } = attrs[attr];
    
              // Buffering speed; 
              if( transition === "buffer" ){
                    var speed = (target - now ) / 10 ;
                    speed = speed > 0 ? Math.ceil( speed ) : Math.floor( speed );
              }else if(transition === "liner"){
                    var speed = attrs[attr].speed; 
              }
    
              
              if( Math.abs(target - now) <= Math.abs( speed ) ){
                    
                    if( attr === "opacity"){
                          dom.style[attr] = target / 100;
                    }else{
                          dom.style[attr] = target + "px";
                    }
    
                    delete attrs[attr];
                    for(var attr in attrs ){
                          // If there is data, the loop will be executed at least once; 
                          return false;
                    }
                    clearInterval(dom.interval);
                    typeof callback === "function" ? callback() : "";
              }else{
                    now += speed;
    
                    if( attr === "opacity"){
                          dom.style[attr] = now / 100;
                    }else{
                          dom.style[attr] = now + "px";
                    }
                    // Assign a value to the object; 
                    attrs[attr].now = now;
              }
        }
    } , 30)
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • js simulation to achieve fireworks effects
  • Native JS to achieve fireworks effect
  • Very beautiful js fireworks effect
  • js to achieve click fireworks effect
  • js to achieve cool fireworks effect
  • JavaScript implements fireworks effects with sound effects
  • JavaScript implements five different fireworks effects

<<:  MySQL database optimization: index implementation principle and usage analysis

>>:  A brief discussion on docker compose writing rules

Recommend

WeChat applet implements SMS login in action

Table of contents 1. Interface effect preview 2.u...

How to deploy LNMP & phpMyAdmin in docker

Environmental preparation: Deploy lnmp on a host ...

CentOS7.x uninstall and install MySQL5.7 operation process and encoding format modification method

1. Uninstalling MySQL 5.7 1.1查看yum是否安裝過mysql cd y...

javascript to switch by clicking on the picture

Clicking to switch pictures is very common in lif...

Linux disk sequential writing and random writing methods

1. Introduction ● Random writing will cause the h...

Detailed explanation of 4 common data sources in Spark SQL

Generic load/write methods Manually specify optio...

Native JS to achieve sliding button effect

The specific code of the sliding button made with...

Usage of Linux userdel command

1. Command Introduction The userdel (user delete)...

Native js imitates mobile phone pull-down refresh

This article shares the specific code of js imita...

Detailed explanation of CSS line-height and height

Recently, when I was working on CSS interfaces, I...

Detailed explanation of CSS text decoration text-decoration &amp; text-emphasis

In CSS, text is one of the most common things we ...

Implementing image fragmentation loading function based on HTML code

Today we will implement a fragmented image loadin...