鍍金池/ 問答/HTML5  HTML/ 用ES6寫法 寫一個(gè)canvas類,頁(yè)面未生成canvas圖像

用ES6寫法 寫一個(gè)canvas類,頁(yè)面未生成canvas圖像

    let canvasNode = document.getElementById('canvas'),
        ctxs = canvasNode.getContext("2d");

    console.log(canvasNode)


    class CircleProgress {
        constructor(ctxs, width, height, arc) {
            this.ctx = ctxs
            this.ctx.canvas.width = width
            this.ctx.canvas.height = width
            this.arc = arc
        }

        clearFill() {
            this.ctx.clearRect(0, 0, this.width, this.width);
        }

        fillBg() {
            this.ctx.beginPath();
            this.ctx.lineWidth = this.arc;
            this.ctx.strokeStyle = '#ccc';
            this.ctx.arc(this.width / 2, this.width / 2, 45, 0, 2 * Math.PI);
            this.ctx.stroke();
        }

        fillArc(x) {
            this.ctx.beginPath();
            this.ctx.lineWidth = this.arc;
            this.ctx.strokeStyle = '#ccc';
            this.ctx.arc(this.width / 2, this.width / 2, 45, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180);
            this.ctx.stroke();
        }

        fillText(x) {
            this.ctx.font = '14px' + ' Arial';
            this.ctx.fillStyle = 'red';
            this.ctx.textBaseline = "middle";
            this.ctx.textAlign = 'center';
            this.ctx.fillText(x.toFixed(1) + '%', this.width / 2, this.width / 2);
        }

        fill(x) {
            this.fillBg();
            this.fillArc(x);
            this.fillText(x);
        }

        testFn() {
            return this.ctx
        }

    }

    let cicle = new CircleProgress(ctxs, 100, 100, 10)

    cicle.fill(100)
回答
編輯回答
糖豆豆

沒有編譯吧。瀏覽器不認(rèn)識(shí)class

2017年7月24日 04:13
編輯回答
解夏
this.ctx.canvas.width = width
this.ctx.canvas.height = height
this.width = width
this.height = height
2018年5月23日 00:51