zoukankan      html  css  js  c++  java
  • JS实现类似CAD的获取点

    JS通过async/await、Promise相结合的方式等待获取点。

    async drawLine() {
        let sp = await this.getPoint({
    	msg: "指定第一个点:"
        });
        let ep = await this.getPoint({
    	msg: "指定下一点:"
        });
        var line = window.svg.shape.Line.prototype.createLine(sp.userPt, ep.userPt);
        if (line) {
    	var layer = window.svg.diagramManager.getLayer("0");
    	layer.appendChild(line);
        }
    }
    

    上面代码实现了画线的过程,用了两次await来获取起点和终点,其中createLine和getLayer函数,用到了本人的网页CAD框架,可以用其它方式替代。

    那么getPoint怎么实现呢?

    async getPoint(resolve) {
        resolve = resolve || {};
        return new Promise((resolve, reject) => {
            this.promiseResolve = resolve
        });
    }

    这里用到了Promise,等待鼠标点击时处理resolve。

    鼠标点击的事件

    async onMouseDown(e) {
        return this.promiseResolve(e);
    }

    这样就触发了resolve回调,实现了获取点。

    注:这里没有对错误、异常等情况进行处理。

  • 相关阅读:
    closure
    运算符优先级
    css妙用
    BFC (块级格式化上下文)
    display:table-cell 详解
    line-height深入理解
    margin collapse
    探究 CSS 解析原理
    python入门
    spring与线程安全
  • 原文地址:https://www.cnblogs.com/ztcad/p/15602094.html
Copyright © 2011-2022 走看看