[Cocos Creator] 虚线的绘制

// TS
drawLineOfDashes(g: cc.Graphics, from: cc.Vec2, to: cc.Vec2, stroke: boolean = true, length: number = 8, interval: number = 4): void {
    if (g) {
        let off = to.sub(from);
        let dir = off.normalize();
        let dis = off.mag();
        let delta = dir.mul(length + interval);
        let delta1 = dir.mul(length);
        let n = Math.floor(dis / (length + interval));
        for (let i = 0; i < n; ++i) {
            let start = from.add(delta.mul(i));
            g.moveTo(start.x, start.y);
            let end = start.add(delta1);
            g.lineTo(end.x, end.y);
        }
        let start1 = from.add(delta.mul(n));
        g.moveTo(start1.x, start1.y);
        if (length < dis - (length + interval) * n) {
            let end = start1.add(delta1);
            g.lineTo(end.x, end.y);
        } else {
            g.lineTo(to.x, to.y);
        }
        if (stroke) g.stroke();
    }
}

 

你可能感兴趣的:(TS,js,cocoscreator)