whychdw
2021-01-21 98fb1af9509ffbcced0865f402955b938ee29f0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Circle {
    constructor(el) {
        this.el = el;
        this.radius = 100;
        this.width = 100;
        this.height = 100;
        this.canvas = "";
        this.context = "";
        this.x = 50;
        this.y = 50;
        this.init(el);
    }
    init(el) {
        // 生成canvas
        this.canvas = document.createElement("canvas");
        this.setCanvas(el);
        el.appendChild(this.canvas);
        // 设置context
        this.context = this.canvas.getContext('2d');
        // 绘制
        this.draw();
    }
    setCanvas(el) {
        let width = el.offsetWidth;
        let height = el.offsetHeight;
        this.width = width;
        this.height = height;
        this.canvas.width = width;
        this.canvas.height = height;
        this.radius = width>height?height/2:width/2;
        this.x = this.width/2;
        this.y = this.height/2;
    }
    draw() {
 
    }
    getAngle(angle, addAngle) {
        let add = addAngle?addAngle:Math.PI/2;
        let rAngle = angle+add;
        return  rAngle>=Math.PI*2?rAngle:rAngle-Math.PI*2;
    }
    fillCircle(radius, startAngle, endAngle, color) {
        let ctx = this.context;
        let x = this.x;
        let y = this.y;
        radius = radius<0?0:radius;
        ctx.beginPath();
        ctx.fillStyle = color;
        ctx.arc(x, y, radius, startAngle, endAngle);
        ctx.fill();
    }
    strokeCircle(radius, startAngle, endAngle, color, lineWidth, lineType) {
        let ctx = this.context;
        let x = this.x;
        let y = this.y;
        radius = radius<0?0:radius;
        ctx.beginPath();
        ctx.strokeStyle = color;
        ctx.fillStyle = color;
        ctx.lineWidth = lineWidth;
        // 虚线
        if(lineType == 'dash') {
            ctx.setLineDash([4, 4]);
        }else {
            ctx.setLineDash([]);
        }
        ctx.arc(x, y, radius, startAngle, endAngle);
        ctx.stroke();
        ctx.setLineDash([]);
    }
    clear() {
        let ctx = this.context;
        ctx.clearRect(0, 0, this.width, this.height);
    }
    resize() {
        let el = this.el;
        // 清除画布
        this.clear();
        this.setCanvas(el);
        this.draw();
    }
}
export default Circle;