实用JS代码接口

3 篇文章 0 订阅
var Util = {
    // 数组和对象深度拷贝
    clone : function (sObj) {
        if (typeof sObj !== "object" || sObj == null) {
            return sObj;
        }

        var i;
        var cObj = {};

        if (sObj instanceof Array) {
            cObj = [];
        }

        for (i in sObj) {
            cObj[i] = Util.clone(sObj[i]);
        }

        return cObj;
    },

    // 计算两点间的距离
    calcDistance : function (pos1, pos2) {
        var xDiff = pos1.x - pos2.x;
        var yDiff = pos1.y - pos2.y;

        return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
    },
    
    // 判断节点  A 在位置 positionA 是否包含于框 B ,返回对应数据
    nodeIsContainedInRect : function(nodeA, positionA, rectB) {
        var x = 0;
        var y = 0;
        var sizeA = nodeA.getContentSize();
        var positionB = rectB.convertToWorldSpace(cc.p(0, 0));
        var sizeB = rectB.getContentSize();
        
        // 判断左右边界是否超
        if (positionA.x < positionB.x) {
            x = positionA.x - positionB.x;
        } else if (positionA.x + sizeA.width > positionB.x + sizeB.width) {
            y = positionA.x + sizeA.width - (positionB.x + sizeB.width);
        }
        
        // 判断上下边界是否超
        if (positionA.y < positionB.y) {
            y = positionA.y - positionB.y;
        } else if (positionA.y + sizeA.height > positionB.y + sizeB.height) {
            y = positionA.y + sizeA.height - (positionB.y + sizeB.height);
        }
        
        return {x:x, y:y};
    },

    // 判断两个浮点数是否相等,当两个浮点数之间的差距小于一定值时
    // 我们就认为这两个浮点数相等
    realEqual : function (real1, real2, delta) {
        // 如果没有指定两者允许的相差值,则默认为0.1
        if (delta == undefined) {
            delta = 0.01;
        }

        if (Math.abs(real1 - real2) <= delta) {
            // 两者相差小于指定的delta值,认为两数相等
            return true;
        }

        // 两数不相等
        return false;
    },

    // 判断 p1 在 p0 和 p2组成的直线的左边还是右边,返回值大于0表示右边,小于0表示左边,等于0表示在直线上
    pointInLine : function (p0, p1, p2) {
        return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
    },

    // 检查点在任意多边形内,顶点按顺序排列
    checkPointInPolygon : function (p , poly) {
        var px = p.x,
        py = p.y,
        flag = false;

        for(var i = 0, l = poly.length, j = l - 1; i < l; j = i, i++) {
            var sx = poly[i].x,
            sy = poly[i].y,
            tx = poly[j].x,
            ty = poly[j].y;

            // 点与多边形顶点重合
            if((sx === px && sy === py) || (tx === px && ty === py)) {
                return true;
            }

            // 判断线段两端点是否在射线两侧
            if((sy < py && ty >= py) || (sy >= py && ty < py)) {
                // 线段上与射线 Y 坐标相同的点的 X 坐标
                var x = sx + (py - sy) * (tx - sx) / (ty - sy);

                // 点在多边形的边上
                if(x === px) {
                    return true;
                }

                // 射线穿过多边形的边界
                if(x > px) {
                    flag = !flag;
                }
            }
        }

        // 射线穿过多边形边界的次数为奇数时点在多边形内
        return flag ? true : false;
    },

    // 不显示天只显示 时:分:秒   time的单位是秒
    // 相对时间转换
    timeToString3 : function (time) {
        var h, m, s; // 时、分、秒
        var str;
        s = time % 60;
        str = this.numToTwoBitStr(s);

        time = parseInt(time / 60);
        m = time % 60;
        str = this.numToTwoBitStr(m) + ":" + str;

        time = parseInt(time / 60);
        h = time % 24;
        str = this.numToTwoBitStr(h) + ":" + str;
        return str;
    },
    
    // 给一个具体的时间戳,将其显示为时:分:秒
    timeToString4 : function (time) {
        var strTime = "";
        var date = new Date();
        date.setTime(time);
        
        if (date.getHours() < 10) {
            strTime += "0";
        }
        strTime += date.getHours() + ":";
        
        if (date.getMinutes() < 10) {
            strTime += "0";
        }
        strTime += date.getMinutes() + ":";
        
        if (date.getSeconds() < 10) {
            strTime += "0";
        }
        strTime += date.getSeconds();
        
        return strTime;
    },

    // 不显示天只显示 时:分:秒   time的单位是毫秒,一般由Date.now()获得
    // 这个函数和上面函数是不一样的,这是绝对时间转换
    utcTimeToString3 : function (time, showSecond) {
        var h, m, s; // 时、分、秒
        var date = new Date(time);
        if (showSecond == null) {
            showSecond = true;
        }
        if (showSecond) {
            return this.numToTwoBitStr(date.getHours()) + ":" + this.numToTwoBitStr(date.getMinutes()) + ":" + this.numToTwoBitStr(date.getSeconds());
        } else {
            return this.numToTwoBitStr(date.getHours()) + ":" + this.numToTwoBitStr(date.getMinutes());
        }
    },
    
    // 显示月-天  时:分   time的单位是毫秒,一般由Date.now()获得
    utcTimeToString4 : function (time) {
        var h, m, s; // 时、分、秒
        var date = new Date(time);
        return (date.getMonth() + 1) + "-" + date.getDate() + " "+ date.getHours() + ":" + date.getMinutes();
    },

    // 转换成 yy-mm-dd hh:mm:ss
    utcTimeToString5 : function (time) {
        var str;
        var h, m, s; // 时、分、秒
        var date = new Date(time);

        str = date.getFullYear();

        str += "-" + this.numToTwoBitStr(date.getMonth() + 1);
        str += "-" + this.numToTwoBitStr(date.getDate());
        str += " " + this.numToTwoBitStr(date.getHours());
        str += ":" + this.numToTwoBitStr(date.getMinutes());
        str += ":" + this.numToTwoBitStr(date.getSeconds());

        return str;
    },
    
    // 转换成 yy-mm-dd
    utcTimeToString6 : function (time) {
        var str;
        var h, m, s; // 时、分、秒
        var date = new Date(time);

        str = date.getFullYear();

        str += "-" + this.numToTwoBitStr(date.getMonth() + 1);
        str += "-" + this.numToTwoBitStr(date.getDate());
        return str;
    },

    // 将1位数字完成2位字符串,比如1转换成01
    numToTwoBitStr : function (num) {
        var str = "";

        num = parseInt(num);

        if (num < 10) {
            str = "0";
        }

        return str + num;
    },

    // 数组相减,第一个数组减去第二个数组
    arrDive : function (aArr, bArr) {
        if (bArr.length === 0) {
            return aArr;
        }

        var e, diff = [];
        var str = bArr.join("");

        for (e in aArr) {
            if (str.indexOf(aArr[e]) === -1) {
                diff.push(aArr[e]);
            }
        }

        return diff;
    },

    getRid : function () {
        var rid;
        var chars;
        var len, i;

        rid = "";
        chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
        len = 16;

        for (i = 0; i < len; ++i) {
            /* jshint -W016 */
            rid += chars[0 | Math.random() * 61];
        }

        return rid;
    },

    // 格式化时间
    changeTime : function (str) {
        var fullDate = "";
        var tmpMonth = "";
        var tmpDay = "";
        var tmpYear = "";
        var tmpDate = "";
        
        tmpMonth = str.getMonth() + 1;
        tmpMonth = this.numToTwoBitStr(tmpMonth);

        tmpDate = str.getDate();
        tmpDate = this.numToTwoBitStr(tmpDate);

        tmpDay = str.getDay();
        tmpDay = this.numToTwoBitStr(tmpDay);
        
        tmpYear = str.getFullYear();
        
        // 返回年月日
        return {year : tmpYear, month : tmpMonth, date : tmpDate, day : tmpDay};
    },

    // 计算startDay过days天之后是哪一天
    // 参数如:startDay:mm/dd/yy, days:5
    getNextDay : function (strDate, days) {
        var tmpMonth = "";
        var tmpDay = "";
        var tmpYear = "";
        var temp, date;

        tmpMonth = strDate.substring(0, 2);
        tmpDay   = strDate.substring(3, 5);
        tmpYear  = strDate.substring(6, 10);
        date = new Date(tmpYear, tmpMonth, tmpDay);

        /* jshint -W053 */
        temp = new Number(days);
        date.setDate(date.getDate() + temp);

        return this.changeTime(date);
    },

    // 计算离下一个整点的所剩的时间
    getNextHourRemainTime : function () {
        var now;
        var curHourTime;

        now = new Date(Date.now());

        curHourTime = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), 0, 0);
        curHourTime.setTime(curHourTime.getTime() + 1000 * 60 * 60);

        return curHourTime.getTime() - now.getTime();
    },

   // 两位置计算角度
   posCalculateAngle : function(originPos, endPos) {
       var angle;
       angle = Math.atan2(endPos.y - originPos.y, endPos.x - originPos.x) * 180 / Math.PI;
       angle = parseInt(angle < 0 ? angle + 360 : angle);   // 角度不要负的和小数
       return angle;
   },
    
   // 获取角度对应的单位向量
   getAngleVectorUnit : function(angle) {
       return {x : Math.cos(angle * Math.PI /180),
           y : Math.sin(angle * Math.PI /180)};
   },

   // 方向转换成归一化的向量
   directionToVector : function (direction) {
     var moveAngle = direction * (360 / globalConsts.animationDirection);
     return {
         x : Math.cos(moveAngle * Math.PI /180),
         y : Math.sin(moveAngle * Math.PI /180)
     };
   },
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值