Realization of Fireworks Special Effects by JavaScript of Object Oriented

  • 2021-11-13 00:42:14
  • OfStack

In this paper, we share the specific code of JavaScript to realize fireworks special effects for your reference. The specific contents are as follows

This special effect uses object-oriented programming

Analysis

OOA

Click to trigger the event Fireworks movement is divided into two stages

Fly upward
Explosion

OOD


class FireWork{
    constructor(){
        
    }
    bindEvent(){
        let _this = this;
        ele.onclick = function(){
            //fly End and then call boom Function 
            _this.fly(_this.boom);
        }
    }
    fly(boom){
        
    }
    boom(){
        
    }
}

Design and Implementation of CSS

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;
    }

Programming Implementation of JS

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 in box In ; 
        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);
    }
    //  In dom Object is created inside the 1 Event names  :  Array corresponding to event handler ; 
    //  Determines whether the current event handler 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, it will be the same as the original dom Function name conflict ; 
    //  Because the special event name will cause the event to fail to trigger, we will split the event name here ; 
    dom.addEventListener(event_name.split(".")[0], handler_selector)
}
function off(dom, event_name) {
    //  Get to dom Object corresponding to the event name in the 1 Group event handler ; 
    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);
    })
    //  Empty dom Groups of event handlers in the ; 
    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 ways to animate  "buffer" , "liner"
    var _style = getComputedStyle( dom );
    
    // - 1.  Data deformation  ; 
    for(var attr in attrs ){
        attrs[attr] = {
              target : attrs[attr],
              now    : _style[attr]
        }
        // - 2.  Speed  :  Uniform motion speed positive and negative  ; 
        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 ){
              //  Fetch the current value and attribute value  ; 
              // attrs[attr].target :  Target value ; 
              // attrs[attr].now    :  Current value ; 
    
              let { target , now } = attrs[attr];
    
              //  Buffer the speed when moving ; 
              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 a data loop, it will execute at least 1 Times ; 
                          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 an object ; 
                    attrs[attr].now = now;
              }
        }
    } , 30)
}

Related articles: