File: pathConvert.html

package info (click to toggle)
node-zrender 5.4.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,544 kB
  • sloc: javascript: 1,259; makefile: 2
file content (175 lines) | stat: -rw-r--r-- 6,028 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Path to Bezier</title>
    <script src="https://cdn.jsdelivr.net/npm/dat.gui@0.7.6/build/dat.gui.js"></script>
    <script src="lib/config.js"></script>
</head>
<body>
    <style>
        html, bod {
            width: 100%;
            height: 100%;
            margin: 0;
        }
    </style>
    <canvas id="main" width="1600" height="1600"></canvas>
    <script type="importmap">
    {
        "imports": {
          "zrender/": "../",
          "tslib": "../node_modules/tslib/tslib.es6.js"
        }
      }
    </script>
    <script type="module">
        import Sector from "zrender/lib/graphic/shape/Sector.js";
        import PathProxy from "zrender/lib/core/PathProxy.js";
        import {pathToBezierCurves, pathToPolygons} from "zrender/lib/tool/convertPath.js";
        import {alignBezierCurves} from "zrender/lib/tool/morphPath.js";

        const canvas = document.querySelector('#main');
        const ctx = canvas.getContext('2d');

        const R = 20;
        const G = 50;

        const config = {
            startAngle: 0
        }

        function drawPolygon(data) {
            ctx.beginPath();
            ctx.moveTo(data[0], data[1])
            for (let i = 2; i < data.length;) {
                ctx.lineTo(data[i++], data[i++]);
            }
        }
        function drawBezier(data) {
            ctx.beginPath();
            for (let i = 0; i < data.length;) {
                i === 0 && ctx.moveTo(data[i++], data[i++]);
                ctx.bezierCurveTo(data[i++], data[i++], data[i++], data[i++], data[i++], data[i++]);
            }
        }
        // We choose the most complex sector path to test.
        function buildSector() {
            for (let k = 0; k < 4; k++) {
                for (let i = 0; i < 20; i++) {
                    const sign = 1 - (k % 2) * 2;
                    const endAngle = config.startAngle + (Math.PI * 2 * (1 - i / 10));
                    const cx = i * G + G;
                    const cy = G + k * G * 3;
                    const r = R;
                    const anticlockwise = k < 2;
                    // const circleArgs = [x * G + G, G + k * G * 2, R, startAngle, endAngle, k < 2];
                    // const circlePath = new PathProxy();
                    // circlePath.beginPath();
                    // circlePath.arc.apply(circlePath, circleArgs);

                    const sector = new Sector({
                        shape: {
                            cx, cy, r, startAngle: config.startAngle, endAngle,
                            clockwise: !anticlockwise,
                            r0: r / 2
                        }
                    });
                    sector.createPathProxy();
                    sector.buildPath(sector.path, sector.shape);

                    ctx.beginPath();
                    sector.path.rebuildPath(ctx);
                    ctx.strokeStyle = 'green';
                    ctx.lineWidth = 2;
                    ctx.stroke();

                    ctx.fillStyle = 'rgba(0,0,0,0.2)'
                    const arr = pathToBezierCurves(sector.path);
                    ctx.save();
                    ctx.translate(0, G);
                    ctx.strokeStyle = 'red';
                    drawBezier(arr[0]);
                    ctx.stroke();
                    ctx.fill();
                    ctx.restore();


                    const polygon = pathToPolygons(sector.path);
                    ctx.save();
                    ctx.translate(0, G * 2);
                    ctx.strokeStyle = 'blue';
                    drawPolygon(polygon[0]);
                    ctx.stroke();
                    ctx.fill();
                    ctx.restore();
                }
            }
        }

        function buildPolygon() {
            for (let k = 0; k < 20; k++) {
                const polygonPoints = [];
                const N = k + 3;
                const dStep = 2 * Math.PI / N;
                for (let i = 0; i < N; i++) {
                    polygonPoints.push(
                        [k * G + G + Math.cos(i * dStep) * R, G * 14 + Math.sin(i * dStep) * R]
                    );
                }

                const polygonPath = new PathProxy();
                for (let i = 0; i < polygonPoints.length; i++) {
                    const x = polygonPoints[i][0];
                    const y = polygonPoints[i][1];
                    i === 0 ? polygonPath.moveTo(x, y) : polygonPath.lineTo(x, y);
                }
                polygonPath.closePath();
                const arr = pathToBezierCurves(polygonPath);

                ctx.beginPath();
                ctx.strokeStyle = 'green';
                ctx.lineWidth = 2;
                for (let i = 0; i < polygonPoints.length; i++) {
                    const x = polygonPoints[i][0];
                    const y = polygonPoints[i][1];
                    i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
                }
                ctx.closePath();
                ctx.stroke();

                ctx.fillStyle = 'rgba(0,0,0,0.2)'
                ctx.save();
                ctx.translate(0, G);
                ctx.strokeStyle = 'red';
                ctx.lineWidth = 1;
                drawBezier(arr[0]);
                ctx.stroke();
                ctx.fill();
                ctx.restore();

                const polygon = pathToPolygons(polygonPath);
                ctx.save();
                ctx.translate(0, G * 2);
                ctx.strokeStyle = 'blue';
                drawPolygon(polygon[0]);
                ctx.stroke();
                ctx.fill();
                ctx.restore();
            }
        }

        function update() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            buildSector();
            buildPolygon();
        }


        const gui = new dat.GUI();
        gui.add(config, 'startAngle', -10, 10).onChange(update)

        update();
    </script>
</body>
</html>