mh-two-thousand-and-two
2024-03-25 b8c93990f3fa5e50a8aca16bdc9c2758168aa0fd
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/**
 * Path 代理,可以在`buildPath`中用于替代`ctx`, 会保存每个path操作的命令到pathCommands属性中
 * 可以用于 isInsidePath 判断以及获取boundingRect
 */
 
// TODO getTotalLength, getPointAtLength, arcTo
 
/* global Float32Array */
 
import * as vec2 from './vector';
import BoundingRect from './BoundingRect';
import {devicePixelRatio as dpr} from '../config';
import { fromLine, fromCubic, fromQuadratic, fromArc } from './bbox';
import { cubicLength, cubicSubdivide, quadraticLength, quadraticSubdivide } from './curve';
 
const CMD = {
    M: 1,
    L: 2,
    C: 3,
    Q: 4,
    A: 5,
    Z: 6,
    // Rect
    R: 7
};
 
// const CMD_MEM_SIZE = {
//     M: 3,
//     L: 3,
//     C: 7,
//     Q: 5,
//     A: 9,
//     R: 5,
//     Z: 1
// };
 
interface ExtendedCanvasRenderingContext2D extends CanvasRenderingContext2D {
    dpr?: number
}
 
const tmpOutX: number[] = [];
const tmpOutY: number[] = [];
 
const min: number[] = [];
const max: number[] = [];
const min2: number[] = [];
const max2: number[] = [];
const mathMin = Math.min;
const mathMax = Math.max;
const mathCos = Math.cos;
const mathSin = Math.sin;
const mathAbs = Math.abs;
 
const PI = Math.PI;
const PI2 = PI * 2;
 
const hasTypedArray = typeof Float32Array !== 'undefined';
 
const tmpAngles: number[] = [];
 
function modPI2(radian: number) {
    // It's much more stable to mod N instedof PI
    const n = Math.round(radian / PI * 1e8) / 1e8;
    return (n % 2) * PI;
}
/**
 * Normalize start and end angles.
 * startAngle will be normalized to 0 ~ PI*2
 * sweepAngle(endAngle - startAngle) will be normalized to 0 ~ PI*2 if clockwise.
 * -PI*2 ~ 0 if anticlockwise.
 */
export function normalizeArcAngles(angles: number[], anticlockwise: boolean): void {
    let newStartAngle = modPI2(angles[0]);
    if (newStartAngle < 0) {
        // Normlize to 0 - PI2
        newStartAngle += PI2;
    }
 
    let delta = newStartAngle - angles[0];
    let newEndAngle = angles[1];
    newEndAngle += delta;
 
    // https://github.com/chromium/chromium/blob/c20d681c9c067c4e15bb1408f17114b9e8cba294/third_party/blink/renderer/modules/canvas/canvas2d/canvas_path.cc#L184
    // Is circle
    if (!anticlockwise && newEndAngle - newStartAngle >= PI2) {
        newEndAngle = newStartAngle + PI2;
    }
    else if (anticlockwise && newStartAngle - newEndAngle >= PI2) {
        newEndAngle = newStartAngle - PI2;
    }
    // Make startAngle < endAngle when clockwise, otherwise endAngle < startAngle.
    // The sweep angle can never been larger than P2.
    else if (!anticlockwise && newStartAngle > newEndAngle) {
        newEndAngle = newStartAngle + (PI2 - modPI2(newStartAngle - newEndAngle));
    }
    else if (anticlockwise && newStartAngle < newEndAngle) {
        newEndAngle = newStartAngle - (PI2 - modPI2(newEndAngle - newStartAngle));
    }
 
    angles[0] = newStartAngle;
    angles[1] = newEndAngle;
}
 
 
export default class PathProxy {
 
    dpr = 1
 
    data: number[] | Float32Array
 
    /**
     * Version is for tracking if the path has been changed.
     */
    private _version: number
 
    /**
     * If save path data.
     */
    private _saveData: boolean
 
    /**
     * If the line segment is too small to draw. It will be added to the pending pt.
     * It will be added if the subpath needs to be finished before stroke, fill, or starting a new subpath.
     */
    private _pendingPtX: number;
    private _pendingPtY: number;
    // Distance of pending pt to previous point.
    // 0 if there is no pending point.
    // Only update the pending pt when distance is larger.
    private _pendingPtDist: number;
 
    private _ctx: ExtendedCanvasRenderingContext2D
 
    private _xi = 0
    private _yi = 0
 
    private _x0 = 0
    private _y0 = 0
 
    private _len = 0
 
    // Calculating path len and seg len.
    private _pathSegLen: number[]
    private _pathLen: number
    // Unit x, Unit y. Provide for avoiding drawing that too short line segment
    private _ux: number
    private _uy: number
 
    static CMD = CMD
 
    constructor(notSaveData?: boolean) {
        if (notSaveData) {
            this._saveData = false;
        }
 
        if (this._saveData) {
            this.data = [];
        }
    }
 
    increaseVersion() {
        this._version++;
    }
 
    /**
     * Version can be used outside for compare if the path is changed.
     * For example to determine if need to update svg d str in svg renderer.
     */
    getVersion() {
        return this._version;
    }
 
    /**
     * @readOnly
     */
    setScale(sx: number, sy: number, segmentIgnoreThreshold?: number) {
        // Compat. Previously there is no segmentIgnoreThreshold.
        segmentIgnoreThreshold = segmentIgnoreThreshold || 0;
        if (segmentIgnoreThreshold > 0) {
            this._ux = mathAbs(segmentIgnoreThreshold / dpr / sx) || 0;
            this._uy = mathAbs(segmentIgnoreThreshold / dpr / sy) || 0;
        }
    }
 
    setDPR(dpr: number) {
        this.dpr = dpr;
    }
 
    setContext(ctx: ExtendedCanvasRenderingContext2D) {
        this._ctx = ctx;
    }
 
    getContext(): ExtendedCanvasRenderingContext2D {
        return this._ctx;
    }
 
    beginPath() {
        this._ctx && this._ctx.beginPath();
        this.reset();
        return this;
    }
 
    /**
     * Reset path data.
     */
    reset() {
        // Reset
        if (this._saveData) {
            this._len = 0;
        }
 
        if (this._pathSegLen) {
            this._pathSegLen = null;
            this._pathLen = 0;
        }
 
        // Update version
        this._version++;
    }
 
    moveTo(x: number, y: number) {
        // Add pending point for previous path.
        this._drawPendingPt();
 
        this.addData(CMD.M, x, y);
        this._ctx && this._ctx.moveTo(x, y);
 
        // x0, y0, xi, yi 是记录在 _dashedXXXXTo 方法中使用
        // xi, yi 记录当前点, x0, y0 在 closePath 的时候回到起始点。
        // 有可能在 beginPath 之后直接调用 lineTo,这时候 x0, y0 需要
        // 在 lineTo 方法中记录,这里先不考虑这种情况,dashed line 也只在 IE10- 中不支持
        this._x0 = x;
        this._y0 = y;
 
        this._xi = x;
        this._yi = y;
 
        return this;
    }
 
    lineTo(x: number, y: number) {
        const dx = mathAbs(x - this._xi);
        const dy = mathAbs(y - this._yi);
        const exceedUnit = dx > this._ux || dy > this._uy;
 
        this.addData(CMD.L, x, y);
 
        if (this._ctx && exceedUnit) {
            this._ctx.lineTo(x, y);
        }
        if (exceedUnit) {
            this._xi = x;
            this._yi = y;
            this._pendingPtDist = 0;
        }
        else {
            const d2 = dx * dx + dy * dy;
            // Only use the farthest pending point.
            if (d2 > this._pendingPtDist) {
                this._pendingPtX = x;
                this._pendingPtY = y;
                this._pendingPtDist = d2;
            }
        }
 
        return this;
    }
 
    bezierCurveTo(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) {
        this._drawPendingPt();
 
        this.addData(CMD.C, x1, y1, x2, y2, x3, y3);
        if (this._ctx) {
            this._ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);
        }
        this._xi = x3;
        this._yi = y3;
        return this;
    }
 
    quadraticCurveTo(x1: number, y1: number, x2: number, y2: number) {
        this._drawPendingPt();
 
        this.addData(CMD.Q, x1, y1, x2, y2);
        if (this._ctx) {
            this._ctx.quadraticCurveTo(x1, y1, x2, y2);
        }
        this._xi = x2;
        this._yi = y2;
        return this;
    }
 
    arc(cx: number, cy: number, r: number, startAngle: number, endAngle: number, anticlockwise?: boolean) {
        this._drawPendingPt();
 
        tmpAngles[0] = startAngle;
        tmpAngles[1] = endAngle;
        normalizeArcAngles(tmpAngles, anticlockwise);
 
        startAngle = tmpAngles[0];
        endAngle = tmpAngles[1];
 
        let delta = endAngle - startAngle;
 
        this.addData(
            CMD.A, cx, cy, r, r, startAngle, delta, 0, anticlockwise ? 0 : 1
        );
 
        this._ctx && this._ctx.arc(cx, cy, r, startAngle, endAngle, anticlockwise);
 
        this._xi = mathCos(endAngle) * r + cx;
        this._yi = mathSin(endAngle) * r + cy;
        return this;
    }
 
    // TODO
    arcTo(x1: number, y1: number, x2: number, y2: number, radius: number) {
        this._drawPendingPt();
 
        if (this._ctx) {
            this._ctx.arcTo(x1, y1, x2, y2, radius);
        }
        return this;
    }
 
    // TODO
    rect(x: number, y: number, w: number, h: number) {
        this._drawPendingPt();
 
        this._ctx && this._ctx.rect(x, y, w, h);
        this.addData(CMD.R, x, y, w, h);
        return this;
    }
 
    closePath() {
        // Add pending point for previous path.
        this._drawPendingPt();
 
        this.addData(CMD.Z);
 
        const ctx = this._ctx;
        const x0 = this._x0;
        const y0 = this._y0;
        if (ctx) {
            ctx.closePath();
        }
 
        this._xi = x0;
        this._yi = y0;
        return this;
    }
 
    fill(ctx: CanvasRenderingContext2D) {
        ctx && ctx.fill();
        this.toStatic();
    }
 
    stroke(ctx: CanvasRenderingContext2D) {
        ctx && ctx.stroke();
        this.toStatic();
    }
 
    len() {
        return this._len;
    }
 
    setData(data: Float32Array | number[]) {
 
        const len = data.length;
 
        if (!(this.data && this.data.length === len) && hasTypedArray) {
            this.data = new Float32Array(len);
        }
 
        for (let i = 0; i < len; i++) {
            this.data[i] = data[i];
        }
 
        this._len = len;
    }
 
    appendPath(path: PathProxy | PathProxy[]) {
        if (!(path instanceof Array)) {
            path = [path];
        }
        const len = path.length;
        let appendSize = 0;
        let offset = this._len;
        for (let i = 0; i < len; i++) {
            appendSize += path[i].len();
        }
        if (hasTypedArray && (this.data instanceof Float32Array)) {
            this.data = new Float32Array(offset + appendSize);
        }
        for (let i = 0; i < len; i++) {
            const appendPathData = path[i].data;
            for (let k = 0; k < appendPathData.length; k++) {
                this.data[offset++] = appendPathData[k];
            }
        }
        this._len = offset;
    }
 
    /**
     * 填充 Path 数据。
     * 尽量复用而不申明新的数组。大部分图形重绘的指令数据长度都是不变的。
     */
    addData(
        cmd: number,
        a?: number,
        b?: number,
        c?: number,
        d?: number,
        e?: number,
        f?: number,
        g?: number,
        h?: number
    ) {
        if (!this._saveData) {
            return;
        }
 
        let data = this.data;
        if (this._len + arguments.length > data.length) {
            // 因为之前的数组已经转换成静态的 Float32Array
            // 所以不够用时需要扩展一个新的动态数组
            this._expandData();
            data = this.data;
        }
        for (let i = 0; i < arguments.length; i++) {
            data[this._len++] = arguments[i];
        }
    }
 
    private _drawPendingPt() {
        if (this._pendingPtDist > 0) {
            this._ctx && this._ctx.lineTo(this._pendingPtX, this._pendingPtY);
            this._pendingPtDist = 0;
        }
    }
 
    private _expandData() {
        // Only if data is Float32Array
        if (!(this.data instanceof Array)) {
            const newData = [];
            for (let i = 0; i < this._len; i++) {
                newData[i] = this.data[i];
            }
            this.data = newData;
        }
    }
 
    /**
     * Convert dynamic array to static Float32Array
     *
     * It will still use a normal array if command buffer length is less than 10
     * Because Float32Array itself may take more memory than a normal array.
     *
     * 10 length will make sure at least one M command and one A(arc) command.
     */
    toStatic() {
        if (!this._saveData) {
            return;
        }
 
        this._drawPendingPt();
 
        const data = this.data;
        if (data instanceof Array) {
            data.length = this._len;
            if (hasTypedArray && this._len > 11) {
                this.data = new Float32Array(data);
            }
        }
    }
 
 
    getBoundingRect() {
        min[0] = min[1] = min2[0] = min2[1] = Number.MAX_VALUE;
        max[0] = max[1] = max2[0] = max2[1] = -Number.MAX_VALUE;
 
        const data = this.data;
        let xi = 0;
        let yi = 0;
        let x0 = 0;
        let y0 = 0;
 
        let i;
        for (i = 0; i < this._len;) {
            const cmd = data[i++] as number;
 
            const isFirst = i === 1;
            if (isFirst) {
                // 如果第一个命令是 L, C, Q
                // 则 previous point 同绘制命令的第一个 point
                // 第一个命令为 Arc 的情况下会在后面特殊处理
                xi = data[i];
                yi = data[i + 1];
 
                x0 = xi;
                y0 = yi;
            }
 
            switch (cmd) {
                case CMD.M:
                    // moveTo 命令重新创建一个新的 subpath, 并且更新新的起点
                    // 在 closePath 的时候使用
                    xi = x0 = data[i++];
                    yi = y0 = data[i++];
                    min2[0] = x0;
                    min2[1] = y0;
                    max2[0] = x0;
                    max2[1] = y0;
                    break;
                case CMD.L:
                    fromLine(xi, yi, data[i], data[i + 1], min2, max2);
                    xi = data[i++];
                    yi = data[i++];
                    break;
                case CMD.C:
                    fromCubic(
                        xi, yi, data[i++], data[i++], data[i++], data[i++], data[i], data[i + 1],
                        min2, max2
                    );
                    xi = data[i++];
                    yi = data[i++];
                    break;
                case CMD.Q:
                    fromQuadratic(
                        xi, yi, data[i++], data[i++], data[i], data[i + 1],
                        min2, max2
                    );
                    xi = data[i++];
                    yi = data[i++];
                    break;
                case CMD.A:
                    const cx = data[i++];
                    const cy = data[i++];
                    const rx = data[i++];
                    const ry = data[i++];
                    const startAngle = data[i++];
                    const endAngle = data[i++] + startAngle;
                    // TODO Arc 旋转
                    i += 1;
                    const anticlockwise = !data[i++];
 
                    if (isFirst) {
                        // 直接使用 arc 命令
                        // 第一个命令起点还未定义
                        x0 = mathCos(startAngle) * rx + cx;
                        y0 = mathSin(startAngle) * ry + cy;
                    }
 
                    fromArc(
                        cx, cy, rx, ry, startAngle, endAngle,
                        anticlockwise, min2, max2
                    );
 
                    xi = mathCos(endAngle) * rx + cx;
                    yi = mathSin(endAngle) * ry + cy;
                    break;
                case CMD.R:
                    x0 = xi = data[i++];
                    y0 = yi = data[i++];
                    const width = data[i++];
                    const height = data[i++];
                    // Use fromLine
                    fromLine(x0, y0, x0 + width, y0 + height, min2, max2);
                    break;
                case CMD.Z:
                    xi = x0;
                    yi = y0;
                    break;
            }
 
            // Union
            vec2.min(min, min, min2);
            vec2.max(max, max, max2);
        }
 
        // No data
        if (i === 0) {
            min[0] = min[1] = max[0] = max[1] = 0;
        }
 
        return new BoundingRect(
            min[0], min[1], max[0] - min[0], max[1] - min[1]
        );
    }
 
    private _calculateLength(): number {
        const data = this.data;
        const len = this._len;
        const ux = this._ux;
        const uy = this._uy;
        let xi = 0;
        let yi = 0;
        let x0 = 0;
        let y0 = 0;
 
        if (!this._pathSegLen) {
            this._pathSegLen = [];
        }
        const pathSegLen = this._pathSegLen;
        let pathTotalLen = 0;
        let segCount = 0;
 
        for (let i = 0; i < len;) {
            const cmd = data[i++] as number;
            const isFirst = i === 1;
 
            if (isFirst) {
                // 如果第一个命令是 L, C, Q
                // 则 previous point 同绘制命令的第一个 point
                // 第一个命令为 Arc 的情况下会在后面特殊处理
                xi = data[i];
                yi = data[i + 1];
 
                x0 = xi;
                y0 = yi;
            }
 
            let l = -1;
 
            switch (cmd) {
                case CMD.M:
                    // moveTo 命令重新创建一个新的 subpath, 并且更新新的起点
                    // 在 closePath 的时候使用
                    xi = x0 = data[i++];
                    yi = y0 = data[i++];
                    break;
                case CMD.L: {
                    const x2 = data[i++];
                    const y2 = data[i++];
                    const dx = x2 - xi;
                    const dy = y2 - yi;
                    if (mathAbs(dx) > ux || mathAbs(dy) > uy || i === len - 1) {
                        l = Math.sqrt(dx * dx + dy * dy);
                        xi = x2;
                        yi = y2;
                    }
                    break;
                }
                case CMD.C: {
                    const x1 = data[i++];
                    const y1 = data[i++];
                    const x2 = data[i++];
                    const y2 = data[i++];
                    const x3 = data[i++];
                    const y3 = data[i++];
                    // TODO adaptive iteration
                    l = cubicLength(xi, yi, x1, y1, x2, y2, x3, y3, 10);
                    xi = x3;
                    yi = y3;
                    break;
                }
                case CMD.Q: {
                    const x1 = data[i++];
                    const y1 = data[i++];
                    const x2 = data[i++];
                    const y2 = data[i++];
                    l = quadraticLength(xi, yi, x1, y1, x2, y2, 10);
                    xi = x2;
                    yi = y2;
                    break;
                }
                case CMD.A:
                    // TODO Arc 判断的开销比较大
                    const cx = data[i++];
                    const cy = data[i++];
                    const rx = data[i++];
                    const ry = data[i++];
                    const startAngle = data[i++];
                    let delta = data[i++];
                    const endAngle = delta + startAngle;
                    // TODO Arc 旋转
                    i += 1;
                    if (isFirst) {
                        // 直接使用 arc 命令
                        // 第一个命令起点还未定义
                        x0 = mathCos(startAngle) * rx + cx;
                        y0 = mathSin(startAngle) * ry + cy;
                    }
 
                    // TODO Ellipse
                    l = mathMax(rx, ry) * mathMin(PI2, Math.abs(delta));
 
                    xi = mathCos(endAngle) * rx + cx;
                    yi = mathSin(endAngle) * ry + cy;
                    break;
                case CMD.R: {
                    x0 = xi = data[i++];
                    y0 = yi = data[i++];
                    const width = data[i++];
                    const height = data[i++];
                    l = width * 2 + height * 2;
                    break;
                }
                case CMD.Z: {
                    const dx = x0 - xi;
                    const dy = y0 - yi;
                    l = Math.sqrt(dx * dx + dy * dy);
 
                    xi = x0;
                    yi = y0;
                    break;
                }
            }
 
            if (l >= 0) {
                pathSegLen[segCount++] = l;
                pathTotalLen += l;
            }
        }
 
        // TODO Optimize memory cost.
        this._pathLen = pathTotalLen;
 
        return pathTotalLen;
    }
    /**
     * Rebuild path from current data
     * Rebuild path will not consider javascript implemented line dash.
     * @param {CanvasRenderingContext2D} ctx
     */
    rebuildPath(ctx: PathRebuilder, percent: number) {
        const d = this.data;
        const ux = this._ux;
        const uy = this._uy;
        const len = this._len;
        let x0;
        let y0;
        let xi;
        let yi;
        let x;
        let y;
 
        const drawPart = percent < 1;
        let pathSegLen;
        let pathTotalLen;
        let accumLength = 0;
        let segCount = 0;
        let displayedLength;
 
        let pendingPtDist = 0;
        let pendingPtX: number;
        let pendingPtY: number;
 
 
        if (drawPart) {
            if (!this._pathSegLen) {
                this._calculateLength();
            }
            pathSegLen = this._pathSegLen;
            pathTotalLen = this._pathLen;
            displayedLength = percent * pathTotalLen;
 
            if (!displayedLength) {
                return;
            }
        }
 
        lo: for (let i = 0; i < len;) {
            const cmd = d[i++];
            const isFirst = i === 1;
 
            if (isFirst) {
                // 如果第一个命令是 L, C, Q
                // 则 previous point 同绘制命令的第一个 point
                // 第一个命令为 Arc 的情况下会在后面特殊处理
                xi = d[i];
                yi = d[i + 1];
 
                x0 = xi;
                y0 = yi;
            }
            // Only lineTo support ignoring small segments.
            // Otherwise if the pending point should always been flushed.
            if (cmd !== CMD.L && pendingPtDist > 0) {
                ctx.lineTo(pendingPtX, pendingPtY);
                pendingPtDist = 0;
            }
            switch (cmd) {
                case CMD.M:
                    x0 = xi = d[i++];
                    y0 = yi = d[i++];
                    ctx.moveTo(xi, yi);
                    break;
                case CMD.L: {
                    x = d[i++];
                    y = d[i++];
                    const dx = mathAbs(x - xi);
                    const dy = mathAbs(y - yi);
                    // Not draw too small seg between
                    if (dx > ux || dy > uy) {
                        if (drawPart) {
                            const l = pathSegLen[segCount++];
                            if (accumLength + l > displayedLength) {
                                const t = (displayedLength - accumLength) / l;
                                ctx.lineTo(xi * (1 - t) + x * t, yi * (1 - t) + y * t);
                                break lo;
                            }
                            accumLength += l;
                        }
 
                        ctx.lineTo(x, y);
                        xi = x;
                        yi = y;
                        pendingPtDist = 0;
                    }
                    else {
                        const d2 = dx * dx + dy * dy;
                        // Only use the farthest pending point.
                        if (d2 > pendingPtDist) {
                            pendingPtX = x;
                            pendingPtY = y;
                            pendingPtDist = d2;
                        }
                    }
                    break;
                }
                case CMD.C: {
                    const x1 = d[i++];
                    const y1 = d[i++];
                    const x2 = d[i++];
                    const y2 = d[i++];
                    const x3 = d[i++];
                    const y3 = d[i++];
                    if (drawPart) {
                        const l = pathSegLen[segCount++];
                        if (accumLength + l > displayedLength) {
                            const t = (displayedLength - accumLength) / l;
                            cubicSubdivide(xi, x1, x2, x3, t, tmpOutX);
                            cubicSubdivide(yi, y1, y2, y3, t, tmpOutY);
                            ctx.bezierCurveTo(tmpOutX[1], tmpOutY[1], tmpOutX[2], tmpOutY[2], tmpOutX[3], tmpOutY[3]);
                            break lo;
                        }
                        accumLength += l;
                    }
 
                    ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);
                    xi = x3;
                    yi = y3;
                    break;
                }
                case CMD.Q: {
                    const x1 = d[i++];
                    const y1 = d[i++];
                    const x2 = d[i++];
                    const y2 = d[i++];
 
                    if (drawPart) {
                        const l = pathSegLen[segCount++];
                        if (accumLength + l > displayedLength) {
                            const t = (displayedLength - accumLength) / l;
                            quadraticSubdivide(xi, x1, x2, t, tmpOutX);
                            quadraticSubdivide(yi, y1, y2, t, tmpOutY);
                            ctx.quadraticCurveTo(tmpOutX[1], tmpOutY[1], tmpOutX[2], tmpOutY[2]);
                            break lo;
                        }
                        accumLength += l;
                    }
 
                    ctx.quadraticCurveTo(x1, y1, x2, y2);
                    xi = x2;
                    yi = y2;
                    break;
                }
                case CMD.A:
                    const cx = d[i++];
                    const cy = d[i++];
                    const rx = d[i++];
                    const ry = d[i++];
                    let startAngle = d[i++];
                    let delta = d[i++];
                    const psi = d[i++];
                    const anticlockwise = !d[i++];
                    const r = (rx > ry) ? rx : ry;
                    // const scaleX = (rx > ry) ? 1 : rx / ry;
                    // const scaleY = (rx > ry) ? ry / rx : 1;
                    const isEllipse = mathAbs(rx - ry) > 1e-3;
                    let endAngle = startAngle + delta;
                    let breakBuild = false;
 
                    if (drawPart) {
                        const l = pathSegLen[segCount++];
                        if (accumLength + l > displayedLength) {
                            endAngle = startAngle + delta * (displayedLength - accumLength) / l;
                            breakBuild = true;
                        }
                        accumLength += l;
                    }
                    if (isEllipse && ctx.ellipse) {
                        ctx.ellipse(cx, cy, rx, ry, psi, startAngle, endAngle, anticlockwise);
                    }
                    else {
                        ctx.arc(cx, cy, r, startAngle, endAngle, anticlockwise);
                    }
 
                    if (breakBuild) {
                        break lo;
                    }
 
                    if (isFirst) {
                        // 直接使用 arc 命令
                        // 第一个命令起点还未定义
                        x0 = mathCos(startAngle) * rx + cx;
                        y0 = mathSin(startAngle) * ry + cy;
                    }
                    xi = mathCos(endAngle) * rx + cx;
                    yi = mathSin(endAngle) * ry + cy;
                    break;
                case CMD.R:
                    x0 = xi = d[i];
                    y0 = yi = d[i + 1];
 
                    x = d[i++];
                    y = d[i++];
                    const width = d[i++];
                    const height = d[i++];
 
                    if (drawPart) {
                        const l = pathSegLen[segCount++];
                        if (accumLength + l > displayedLength) {
                            let d = displayedLength - accumLength;
                            ctx.moveTo(x, y);
                            ctx.lineTo(x + mathMin(d, width), y);
                            d -= width;
                            if (d > 0) {
                                ctx.lineTo(x + width, y + mathMin(d, height));
                            }
                            d -= height;
                            if (d > 0) {
                                ctx.lineTo(x + mathMax(width - d, 0), y + height);
                            }
                            d -= width;
                            if (d > 0) {
                                ctx.lineTo(x, y + mathMax(height - d, 0));
                            }
                            break lo;
                        }
                        accumLength += l;
                    }
                    ctx.rect(x, y, width, height);
                    break;
                case CMD.Z:
                    if (drawPart) {
                        const l = pathSegLen[segCount++];
                        if (accumLength + l > displayedLength) {
                            const t = (displayedLength - accumLength) / l;
                            ctx.lineTo(xi * (1 - t) + x0 * t, yi * (1 - t) + y0 * t);
                            break lo;
                        }
                        accumLength += l;
                    }
 
                    ctx.closePath();
                    xi = x0;
                    yi = y0;
            }
        }
    }
 
    clone() {
        const newProxy = new PathProxy();
        const data = this.data;
        newProxy.data = data.slice ? data.slice()
            : Array.prototype.slice.call(data);
        newProxy._len = this._len;
        return newProxy;
    }
 
    private static initDefaultProps = (function () {
        const proto = PathProxy.prototype;
        proto._saveData = true;
        proto._ux = 0;
        proto._uy = 0;
        proto._pendingPtDist = 0;
        proto._version = 0;
    })()
}
 
 
export interface PathRebuilder {
    moveTo(x: number, y: number): void
    lineTo(x: number, y: number): void
    bezierCurveTo(x: number, y: number, x2: number, y2: number, x3: number, y3: number): void
    quadraticCurveTo(x: number, y: number, x2: number, y2: number): void
    arc(cx: number, cy: number, r: number, startAngle: number, endAngle: number, anticlockwise: boolean): void
    // eslint-disable-next-line max-len
    ellipse(cx: number, cy: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise: boolean): void
    rect(x: number, y: number, width: number, height: number): void
    closePath(): void
}