es6摇一摇类库

github - /public/js/tools/play/shake

    import shake from "./shake.js";
    let shake = new shake();
    //会默认启动监听,如果需要二次启动必须使停止监听以后
    shake.stop(); //移除监听
    shake.start();//开始监听
    //let shake = new shake({step:300,stopTime:3000}) 
复制代码
参数说明例子
step晃动力度{step:300}默认300
stopTime结束时常{stopTime:3000}默认3000
event事件监听start,actio,stop
start开始on("start",fn)
actio摇动中on("action",fn)
stop结束on("stop",(number,th)=>{})
number,thnumber:摇动次数th: 当前指针
/**
 * 摇一摇
 * @class shake
 * let shake = new shake({step:300,stopTime:3000}) 
 * step:晃动力度
 * stopTime:停止时常
 * event: start action stop  
 * shake.on("start",(number,this))
 * number: 摇动次数
 * this: shake类
 */
class shake{
    constructor(arg){
        this.mot={
            step:300, //摇动力度
            stopTime:3000,//秒停止
            last_update: 0,
             x:0,
             y:0, 
             z:0, 
             last_x:0, 
             last_y:0, 
             last_z:0,
             pow:1
       }
       this.time=0;
       Object.assign(this.mot,arg);
       this.bindEvent();
       this.handEvent=[];//记录
       this.check=1;//是否启动中
    }
  
    setTime=()=>{
        clearTimeout(this.time);
        this.time = setTimeout(arg=>{
           // this.unbindEvent();
            this.triger("stop");
            this.mot.pow=1;
        },3000)
    }
    on(ev,callback){
        callback = callback||(function(){});
        let fn = this.handEvent.find(arg=>arg.event);
        if(!fn){
             this.handEvent.push({event:ev,callback})
        }
        return this;
    }
    start(){
        if(this.check==0){
            this.bindEvent();
        }
       
    }
    stop(){
        this.check=0;
       this.unbindEvent();
       return this;
       //this.handEvent=[];//记录
    }
    triger(ev){
       let fn = this.handEvent.find(arg=>arg.ev);
       switch(ev){
           case "start":
           case "action":
           case "stop":console.log(ev);break;
       }
       if(fn){
           fn.callback(this.mot.pow,this);
       }
    }
    bindSelfEvent=()=>{

    }
    bindEvent(){
        //监控摇一摇
        window.addEventListener("devicemotion", this.EVENT_Deviceorientation, false);
        
    }
    unbindEvent(){
        Object.assign(this.mot,{ pow:1});
        window.removeEventListener("devicemotion",this.EVENT_Deviceorientation);
    }
    EVENT_Deviceorientation=(eventData)=>{
        var acceleration = eventData.accelerationIncludingGravity;
        var curTime = new Date().getTime();
        if ((curTime - this.mot.last_update) > 300) {
            var diffTime = curTime - this.mot.last_update;
            this.mot.last_update = curTime;
            this.mot.x = acceleration.x;
            this.mot.y = acceleration.y;
            this.mot.z = acceleration.z;
            var speed = Math.abs(this.mot.x + this.mot.y + this.mot.z - this.mot.last_x - this.mot.last_y - this.mot.last_z) / diffTime * 10000;
            if (speed > this.mot.step) {
                if(this.mot.pow==1){
               
                    this.triger("start");
                }
                
                this.triger("action"); 
              this.mot.pow+=1;
              this.setTime();
            }
            this.mot.last_x = this.mot.x;
            this.mot.last_y = this.mot.y;
            this.mot.last_z = this.mot.z;
        }
    }
}
export default shake;
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值