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

Bootstrap 3.0 study notes page layout

This time we will mainly learn about layout, whic...

A simple way to build a Docker environment

First, let’s understand what Docker is? Docker is...

Solve the problem of inconsistent MySQL storage time

After obtaining the system time using Java and st...

Hide HTML elements through display or visibility

Sometimes we need to control whether HTML elements...

SVN installation and basic operation (graphic tutorial)

Table of contents 1. What is SVN 2. Svn server an...

Deployment and Chinese translation of the docker visualization tool Portainer

#docker search #docker pull portainer 1. Download...

VMware vsphere 6.5 installation tutorial (picture and text)

vmware vsphere 6.5 is the classic version of vsph...

Vue implements Dialog encapsulation

Table of contents Vue2 Writing Vue3 plugin versio...

W3C Tutorial (8): W3C XML Schema Activities

XML Schema is an XML-based alternative to DTD. XM...

Detailed explanation of the use of the <meta> tag in HTML

In the web pages we make, if we want more people ...

How to show or hide common icons on the desktop in Windows Server 2012

Windows Server 2012 and Windows Server 2008 diffe...

How to replace all tags in html text

(?i) means do not match case. Replace all uppercas...

Vuex modularization and namespaced example demonstration

1. Purpose: Make the code easier to maintain and ...

Summary of methods for querying MySQL user permissions

Introduce two methods to view MySQL user permissi...

js version to realize calculator function

This article example shares the specific code of ...