@codehz/draw-call 0.4.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ import { ReadableStream } from "node:stream/web";
2
+
1
3
  //#region src/types/base.d.ts
2
4
  type Size = number | `${number}%` | "auto" | "fill";
3
5
  interface ColorStop {
@@ -335,7 +337,7 @@ interface TransformSimpleObject {
335
337
  /** 垂直偏移角度(弧度) */
336
338
  skewY?: number;
337
339
  }
338
- interface DOMMatrix2DInit {
340
+ interface DOMMatrix2DInit$1 {
339
341
  a?: number;
340
342
  b?: number;
341
343
  c?: number;
@@ -343,7 +345,7 @@ interface DOMMatrix2DInit {
343
345
  e?: number;
344
346
  f?: number;
345
347
  }
346
- type TransformValue = TransformSimpleObject | DOMMatrix2DInit | [number, number, number, number, number, number];
348
+ type TransformValue = TransformSimpleObject | DOMMatrix2DInit$1 | [number, number, number, number, number, number];
347
349
  interface TransformProps {
348
350
  children: Element;
349
351
  /** 变换值(rotate/skew 使用弧度) */
@@ -363,28 +365,567 @@ interface CustomDrawElement extends ElementBase, CustomDrawProps {
363
365
  }
364
366
  type Element = BoxElement | TextElement | RichTextElement | ImageElement | SvgElement | StackElement | TransformElement | CustomDrawElement;
365
367
  //#endregion
366
- //#region src/canvas.d.ts
367
- interface CanvasOptions {
368
+ //#region node_modules/@napi-rs/canvas/index.d.ts
369
+ interface CanvasRenderingContext2D$1 extends CanvasCompositing, CanvasDrawPath, CanvasFillStrokeStyles, CanvasFilters, CanvasImageData, CanvasImageSmoothing, CanvasPath, CanvasPathDrawingStyles, CanvasRect, CanvasSettings, CanvasShadowStyles, CanvasState, CanvasText, CanvasTextDrawingStyles, CanvasTransform, CanvasPDFAnnotations {}
370
+ interface CanvasState {
371
+ isContextLost(): boolean;
372
+ reset(): void;
373
+ restore(): void;
374
+ save(): void;
375
+ }
376
+ interface CanvasShadowStyles {
377
+ shadowBlur: number;
378
+ shadowColor: string;
379
+ shadowOffsetX: number;
380
+ shadowOffsetY: number;
381
+ }
382
+ interface CanvasRenderingContext2DSettings {
383
+ alpha?: boolean;
384
+ colorSpace?: PredefinedColorSpace;
385
+ desynchronized?: boolean;
386
+ willReadFrequently?: boolean;
387
+ }
388
+ interface CanvasSettings {
389
+ getContextAttributes(): CanvasRenderingContext2DSettings;
390
+ }
391
+ interface CanvasRect {
392
+ clearRect(x: number, y: number, w: number, h: number): void;
393
+ fillRect(x: number, y: number, w: number, h: number): void;
394
+ strokeRect(x: number, y: number, w: number, h: number): void;
395
+ }
396
+ interface TextMetrics {
397
+ readonly actualBoundingBoxAscent: number;
398
+ readonly actualBoundingBoxDescent: number;
399
+ readonly actualBoundingBoxLeft: number;
400
+ readonly actualBoundingBoxRight: number;
401
+ readonly alphabeticBaseline: number;
402
+ readonly emHeightAscent: number;
403
+ readonly emHeightDescent: number;
404
+ readonly fontBoundingBoxAscent: number;
405
+ readonly fontBoundingBoxDescent: number;
406
+ readonly hangingBaseline: number;
407
+ readonly ideographicBaseline: number;
408
+ readonly width: number;
409
+ }
410
+ interface CanvasText {
411
+ fillText(text: string, x: number, y: number, maxWidth?: number): void;
412
+ measureText(text: string): TextMetrics;
413
+ strokeText(text: string, x: number, y: number, maxWidth?: number): void;
414
+ }
415
+ type CanvasLineCap = 'butt' | 'round' | 'square';
416
+ type CanvasLineJoin = 'bevel' | 'miter' | 'round';
417
+ interface CanvasPathDrawingStyles {
418
+ lineCap: CanvasLineCap;
419
+ lineDashOffset: number;
420
+ lineJoin: CanvasLineJoin;
421
+ lineWidth: number;
422
+ miterLimit: number;
423
+ getLineDash(): number[];
424
+ setLineDash(segments: number[]): void;
425
+ }
426
+ interface CanvasPath {
427
+ arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void;
428
+ arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void;
429
+ bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void;
430
+ closePath(): void;
431
+ ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void;
432
+ lineTo(x: number, y: number): void;
433
+ moveTo(x: number, y: number): void;
434
+ quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void;
435
+ rect(x: number, y: number, w: number, h: number): void;
436
+ roundRect(x: number, y: number, w: number, h: number, radii?: number | DOMPointInit | (number | DOMPointInit)[]): void;
437
+ }
438
+ type ImageSmoothingQuality = 'high' | 'low' | 'medium';
439
+ interface CanvasImageSmoothing {
440
+ imageSmoothingEnabled: boolean;
441
+ imageSmoothingQuality: ImageSmoothingQuality;
442
+ }
443
+ interface CanvasTransform {
444
+ resetTransform(): void;
445
+ rotate(angle: number): void;
446
+ scale(x: number, y: number): void;
447
+ setTransform(a: number, b: number, c: number, d: number, e: number, f: number): void;
448
+ setTransform(transform?: DOMMatrix2DInit): void;
449
+ transform(a: number, b: number, c: number, d: number, e: number, f: number): void;
450
+ translate(x: number, y: number): void;
451
+ }
452
+ interface CanvasPDFAnnotations {
453
+ /**
454
+ * Create a clickable URL link annotation in a PDF document.
455
+ * This is only effective when used with PDF documents.
456
+ * @param left - Left coordinate of the link rectangle
457
+ * @param top - Top coordinate of the link rectangle
458
+ * @param right - Right coordinate of the link rectangle
459
+ * @param bottom - Bottom coordinate of the link rectangle
460
+ * @param url - The URL to link to
461
+ */
462
+ annotateLinkUrl(left: number, top: number, right: number, bottom: number, url: string): void;
463
+ /**
464
+ * Create a named destination at a specific point in a PDF document.
465
+ * This destination can be used as a target for internal links.
466
+ * @param x - X coordinate of the destination point
467
+ * @param y - Y coordinate of the destination point
468
+ * @param name - Name identifier for the destination
469
+ */
470
+ annotateNamedDestination(x: number, y: number, name: string): void;
471
+ /**
472
+ * Create a link to a named destination within the PDF document.
473
+ * This is only effective when used with PDF documents.
474
+ * @param left - Left coordinate of the link rectangle
475
+ * @param top - Top coordinate of the link rectangle
476
+ * @param right - Right coordinate of the link rectangle
477
+ * @param bottom - Bottom coordinate of the link rectangle
478
+ * @param name - Name of the destination to link to
479
+ */
480
+ annotateLinkToDestination(left: number, top: number, right: number, bottom: number, name: string): void;
481
+ }
482
+ type PredefinedColorSpace = 'display-p3' | 'srgb';
483
+ interface ImageDataSettings {
484
+ colorSpace?: PredefinedColorSpace;
485
+ }
486
+ interface CanvasImageData {
487
+ createImageData(sw: number, sh: number, settings?: ImageDataSettings): ImageData;
488
+ createImageData(imagedata: ImageData): ImageData;
489
+ getImageData(sx: number, sy: number, sw: number, sh: number, settings?: ImageDataSettings): ImageData;
490
+ putImageData(imagedata: ImageData, dx: number, dy: number): void;
491
+ putImageData(imagedata: ImageData, dx: number, dy: number, dirtyX: number, dirtyY: number, dirtyWidth: number, dirtyHeight: number): void;
492
+ }
493
+ type CanvasDirection = 'inherit' | 'ltr' | 'rtl';
494
+ type CanvasFontKerning = 'auto' | 'none' | 'normal';
495
+ type CanvasFontStretch = 'condensed' | 'expanded' | 'extra-condensed' | 'extra-expanded' | 'normal' | 'semi-condensed' | 'semi-expanded' | 'ultra-condensed' | 'ultra-expanded';
496
+ type CanvasFontVariantCaps = 'all-petite-caps' | 'all-small-caps' | 'normal' | 'petite-caps' | 'small-caps' | 'titling-caps' | 'unicase';
497
+ type CanvasTextAlign = 'center' | 'end' | 'left' | 'right' | 'start';
498
+ type CanvasTextBaseline = 'alphabetic' | 'bottom' | 'hanging' | 'ideographic' | 'middle' | 'top';
499
+ type CanvasTextRendering = 'auto' | 'geometricPrecision' | 'optimizeLegibility' | 'optimizeSpeed';
500
+ interface CanvasTextDrawingStyles {
501
+ direction: CanvasDirection;
502
+ font: string;
503
+ fontKerning: CanvasFontKerning;
504
+ fontStretch: CanvasFontStretch;
505
+ fontVariantCaps: CanvasFontVariantCaps;
506
+ letterSpacing: string;
507
+ textAlign: CanvasTextAlign;
508
+ textBaseline: CanvasTextBaseline;
509
+ textRendering: CanvasTextRendering;
510
+ wordSpacing: string;
511
+ fontVariationSettings: string;
512
+ lang: string;
513
+ }
514
+ interface CanvasFilters {
515
+ filter: string;
516
+ }
517
+ interface CanvasFillStrokeStyles {
518
+ fillStyle: string | CanvasGradient$1 | CanvasPattern$1;
519
+ strokeStyle: string | CanvasGradient$1 | CanvasPattern$1;
520
+ createConicGradient(startAngle: number, x: number, y: number): CanvasGradient$1;
521
+ createLinearGradient(x0: number, y0: number, x1: number, y1: number): CanvasGradient$1;
522
+ createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number): CanvasGradient$1;
523
+ }
524
+ type CanvasFillRule = 'evenodd' | 'nonzero';
525
+ interface CanvasDrawPath {
526
+ beginPath(): void;
527
+ clip(fillRule?: CanvasFillRule): void;
528
+ clip(path: Path2D, fillRule?: CanvasFillRule): void;
529
+ fill(fillRule?: CanvasFillRule): void;
530
+ fill(path: Path2D, fillRule?: CanvasFillRule): void;
531
+ isPointInPath(x: number, y: number, fillRule?: CanvasFillRule): boolean;
532
+ isPointInPath(path: Path2D, x: number, y: number, fillRule?: CanvasFillRule): boolean;
533
+ isPointInStroke(x: number, y: number): boolean;
534
+ isPointInStroke(path: Path2D, x: number, y: number): boolean;
535
+ stroke(): void;
536
+ stroke(path: Path2D): void;
537
+ }
538
+ type GlobalCompositeOperation = 'color' | 'color-burn' | 'color-dodge' | 'copy' | 'darken' | 'destination-atop' | 'destination-in' | 'destination-out' | 'destination-over' | 'difference' | 'exclusion' | 'hard-light' | 'hue' | 'lighten' | 'lighter' | 'luminosity' | 'multiply' | 'overlay' | 'saturation' | 'screen' | 'soft-light' | 'source-atop' | 'source-in' | 'source-out' | 'source-over' | 'xor';
539
+ interface CanvasCompositing {
540
+ globalAlpha: number;
541
+ globalCompositeOperation: GlobalCompositeOperation;
542
+ }
543
+ interface DOMPointInit {
544
+ w?: number;
545
+ x?: number;
546
+ y?: number;
547
+ z?: number;
548
+ }
549
+ interface CanvasPattern$1 {
550
+ setTransform(transform?: DOMMatrix2DInit): void;
551
+ }
552
+ interface CanvasGradient$1 {
553
+ addColorStop(offset: number, color: string): void;
554
+ }
555
+ interface DOMMatrixInit extends DOMMatrix2DInit {
556
+ is2D?: boolean;
557
+ m13?: number;
558
+ m14?: number;
559
+ m23?: number;
560
+ m24?: number;
561
+ m31?: number;
562
+ m32?: number;
563
+ m33?: number;
564
+ m34?: number;
565
+ m43?: number;
566
+ m44?: number;
567
+ } // ----------- added types
568
+ interface DOMMatrix2DInit {
569
+ a: number;
570
+ b: number;
571
+ c: number;
572
+ d: number;
573
+ e: number;
574
+ f: number;
575
+ }
576
+ interface DOMMatrixReadOnly {
577
+ readonly a: number;
578
+ readonly b: number;
579
+ readonly c: number;
580
+ readonly d: number;
581
+ readonly e: number;
582
+ readonly f: number;
583
+ readonly is2D: boolean;
584
+ readonly isIdentity: boolean;
585
+ readonly m11: number;
586
+ readonly m12: number;
587
+ readonly m13: number;
588
+ readonly m14: number;
589
+ readonly m21: number;
590
+ readonly m22: number;
591
+ readonly m23: number;
592
+ readonly m24: number;
593
+ readonly m31: number;
594
+ readonly m32: number;
595
+ readonly m33: number;
596
+ readonly m34: number;
597
+ readonly m41: number;
598
+ readonly m42: number;
599
+ readonly m43: number;
600
+ readonly m44: number;
601
+ flipX(): DOMMatrix;
602
+ flipY(): DOMMatrix;
603
+ inverse(): DOMMatrix;
604
+ multiply(other?: DOMMatrixInit): DOMMatrix;
605
+ rotate(rotX?: number, rotY?: number, rotZ?: number): DOMMatrix;
606
+ rotateAxisAngle(x?: number, y?: number, z?: number, angle?: number): DOMMatrix;
607
+ rotateFromVector(x?: number, y?: number): DOMMatrix;
608
+ scale(scaleX?: number, scaleY?: number, scaleZ?: number, originX?: number, originY?: number, originZ?: number): DOMMatrix;
609
+ scale3d(scale?: number, originX?: number, originY?: number, originZ?: number): DOMMatrix;
610
+ skewX(sx?: number): DOMMatrix;
611
+ skewY(sy?: number): DOMMatrix;
612
+ toFloat32Array(): Float32Array;
613
+ toFloat64Array(): Float64Array;
614
+ transformPoint(point?: DOMPointInit): DOMPoint;
615
+ translate(tx?: number, ty?: number, tz?: number): DOMMatrix;
616
+ toString(): string;
617
+ }
618
+ interface DOMMatrix extends DOMMatrixReadOnly {
619
+ a: number;
620
+ b: number;
621
+ c: number;
622
+ d: number;
623
+ e: number;
624
+ f: number;
625
+ m11: number;
626
+ m12: number;
627
+ m13: number;
628
+ m14: number;
629
+ m21: number;
630
+ m22: number;
631
+ m23: number;
632
+ m24: number;
633
+ m31: number;
634
+ m32: number;
635
+ m33: number;
636
+ m34: number;
637
+ m41: number;
638
+ m42: number;
639
+ m43: number;
640
+ m44: number;
641
+ invertSelf(): DOMMatrix;
642
+ multiplySelf(other?: DOMMatrixInit): DOMMatrix;
643
+ preMultiplySelf(other?: DOMMatrixInit): DOMMatrix;
644
+ rotateAxisAngleSelf(x?: number, y?: number, z?: number, angle?: number): DOMMatrix;
645
+ rotateFromVectorSelf(x?: number, y?: number): DOMMatrix;
646
+ rotateSelf(rotX?: number, rotY?: number, rotZ?: number): DOMMatrix;
647
+ scale3dSelf(scale?: number, originX?: number, originY?: number, originZ?: number): DOMMatrix;
648
+ scaleSelf(scaleX?: number, scaleY?: number, scaleZ?: number, originX?: number, originY?: number, originZ?: number): DOMMatrix;
649
+ setMatrixValue(transformList: string): DOMMatrix;
650
+ skewXSelf(sx?: number): DOMMatrix;
651
+ skewYSelf(sy?: number): DOMMatrix;
652
+ translateSelf(tx?: number, ty?: number, tz?: number): DOMMatrix;
653
+ toJSON(): { [K in OmitNeverOfMatrix]: DOMMatrix[K] };
654
+ }
655
+ type OmitMatrixMethod = { [K in keyof DOMMatrix]: DOMMatrix[K] extends ((...args: any[]) => any) ? never : K };
656
+ type OmitNeverOfMatrix = OmitMatrixMethod[keyof OmitMatrixMethod];
657
+ declare const DOMMatrix: {
658
+ prototype: DOMMatrix;
659
+ new (init?: string | number[]): DOMMatrix;
660
+ fromFloat32Array(array32: Float32Array): DOMMatrix;
661
+ fromFloat64Array(array64: Float64Array): DOMMatrix;
662
+ fromMatrix(other?: DOMMatrixInit): DOMMatrix;
663
+ };
664
+ interface DOMPointReadOnly {
665
+ readonly w: number;
666
+ readonly x: number;
667
+ readonly y: number;
668
+ readonly z: number;
669
+ matrixTransform(matrix?: DOMMatrixInit): DOMPoint;
670
+ }
671
+ interface DOMPoint extends DOMPointReadOnly {
672
+ w: number;
673
+ x: number;
674
+ y: number;
675
+ z: number;
676
+ toJSON(): Omit<DOMPoint, 'matrixTransform' | 'toJSON'>;
677
+ }
678
+ declare const DOMPoint: {
679
+ prototype: DOMPoint;
680
+ new (x?: number, y?: number, z?: number, w?: number): DOMPoint;
681
+ fromPoint(other?: DOMPointInit): DOMPoint;
682
+ };
683
+ declare class ImageData {
684
+ /**
685
+ * Returns the one-dimensional array containing the data in RGBA order, as integers in the range 0 to 255.
686
+ */
687
+ readonly data: Uint8ClampedArray;
688
+ /**
689
+ * Returns the actual dimensions of the data in the ImageData object, in pixels.
690
+ */
691
+ readonly height: number;
692
+ /**
693
+ * Returns the actual dimensions of the data in the ImageData object, in pixels.
694
+ */
695
+ readonly width: number;
696
+ constructor(sw: number, sh: number, attr?: {
697
+ colorSpace?: ColorSpace;
698
+ });
699
+ constructor(imageData: ImageData, attr?: {
700
+ colorSpace?: ColorSpace;
701
+ });
702
+ constructor(data: Uint8ClampedArray | Uint16Array | Float16Array | Float32Array, sw: number, sh?: number);
703
+ }
704
+ declare class Image$1 {
705
+ constructor(); // attrs only affects SVG
706
+ constructor(width: number, height: number, attrs?: {
707
+ colorSpace?: ColorSpace;
708
+ });
368
709
  width: number;
369
710
  height: number;
370
- pixelRatio?: number;
371
- imageSmoothingEnabled?: boolean;
372
- imageSmoothingQuality?: "low" | "medium" | "high";
373
- canvas?: {
374
- getContext(type: "2d"): CanvasRenderingContext2D | null;
375
- width: number;
376
- height: number;
711
+ readonly naturalWidth: number;
712
+ readonly naturalHeight: number;
713
+ readonly complete: boolean;
714
+ readonly currentSrc: string | null;
715
+ alt: string; // the src can be a Uint8Array or a string
716
+ // if it's a string, it can be a file path, a data URL, a remote URL, or a SVG string
717
+ src: Uint8Array | string;
718
+ onload?(): void;
719
+ onerror?(err: Error): void;
720
+ decode(): Promise<void>;
721
+ }
722
+ declare class Path2D {
723
+ constructor(path?: Path2D | string);
724
+ addPath(path: Path2D, transform?: DOMMatrix2DInit): void;
725
+ arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise?: boolean): void;
726
+ arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): void;
727
+ bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number): void;
728
+ closePath(): void;
729
+ ellipse(x: number, y: number, radiusX: number, radiusY: number, rotation: number, startAngle: number, endAngle: number, anticlockwise?: boolean): void;
730
+ lineTo(x: number, y: number): void;
731
+ moveTo(x: number, y: number): void;
732
+ quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void;
733
+ rect(x: number, y: number, w: number, h: number): void;
734
+ roundRect(x: number, y: number, w: number, h: number, radii?: number | number[]): void; // PathKit methods
735
+ op(path: Path2D, operation: PathOp): Path2D;
736
+ toSVGString(): string;
737
+ getFillType(): FillType;
738
+ getFillTypeString(): string;
739
+ setFillType(type: FillType): void;
740
+ simplify(): Path2D;
741
+ asWinding(): Path2D;
742
+ stroke(stroke?: StrokeOptions): Path2D;
743
+ transform(transform: DOMMatrix2DInit): Path2D;
744
+ getBounds(): [left: number, top: number, right: number, bottom: number];
745
+ computeTightBounds(): [left: number, top: number, right: number, bottom: number];
746
+ trim(start: number, end: number, isComplement?: boolean): Path2D;
747
+ dash(on: number, off: number, phase: number): Path2D;
748
+ round(radius: number): Path2D;
749
+ equals(path: Path2D): boolean;
750
+ }
751
+ interface StrokeOptions {
752
+ width?: number;
753
+ miterLimit?: number;
754
+ cap?: StrokeCap;
755
+ join?: StrokeJoin;
756
+ }
757
+ interface SKRSContext2D extends CanvasRenderingContext2D$1 {
758
+ canvas: Canvas;
759
+ /**
760
+ * @param startAngle The angle at which to begin the gradient, in radians. Angle measurements start vertically above the centre and move around clockwise.
761
+ * @param x The x-axis coordinate of the centre of the gradient.
762
+ * @param y The y-axis coordinate of the centre of the gradient.
763
+ */
764
+ createConicGradient(startAngle: number, x: number, y: number): CanvasGradient$1;
765
+ drawImage(image: Image$1 | Canvas, dx: number, dy: number): void;
766
+ drawImage(image: Image$1 | Canvas, dx: number, dy: number, dw: number, dh: number): void;
767
+ drawImage(image: Image$1 | Canvas, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): void;
768
+ createPattern(image: Image$1 | ImageData | Canvas | SvgCanvas, repeat: 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat' | null): CanvasPattern$1;
769
+ getContextAttributes(): {
770
+ alpha: boolean;
771
+ desynchronized: boolean;
377
772
  };
773
+ getTransform(): DOMMatrix;
774
+ letterSpacing: string;
775
+ wordSpacing: string;
378
776
  }
379
- interface LayoutSize {
777
+ type ColorSpace = 'srgb' | 'display-p3';
778
+ interface ContextAttributes {
779
+ alpha?: boolean;
780
+ colorSpace?: ColorSpace;
781
+ }
782
+ interface SvgCanvas {
783
+ width: number;
784
+ height: number;
785
+ getContext(contextType: '2d', contextAttributes?: ContextAttributes): SKRSContext2D;
786
+ getContent(): Buffer;
787
+ }
788
+ interface AvifConfig {
789
+ /** 0-100 scale, 100 is lossless */
790
+ quality?: number;
791
+ /** 0-100 scale */
792
+ alphaQuality?: number;
793
+ /** rav1e preset 1 (slow) 10 (fast but crappy), default is 4 */
794
+ speed?: number;
795
+ /** How many threads should be used (0 = match core count) */
796
+ threads?: number;
797
+ /** set to '4:2:0' to use chroma subsampling, default '4:4:4' */
798
+ chromaSubsampling?: ChromaSubsampling;
799
+ }
800
+ /**
801
+ * https://en.wikipedia.org/wiki/Chroma_subsampling#Types_of_sampling_and_subsampling
802
+ * https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Video_concepts
803
+ */
804
+ declare enum ChromaSubsampling {
805
+ /**
806
+ * Each of the three Y'CbCr components has the same sample rate, thus there is no chroma subsampling. This scheme is sometimes used in high-end film scanners and cinematic post-production.
807
+ * Note that "4:4:4" may instead be wrongly referring to R'G'B' color space, which implicitly also does not have any chroma subsampling (except in JPEG R'G'B' can be subsampled).
808
+ * Formats such as HDCAM SR can record 4:4:4 R'G'B' over dual-link HD-SDI.
809
+ */
810
+ Yuv444 = 0,
811
+ /**
812
+ * The two chroma components are sampled at half the horizontal sample rate of luma: the horizontal chroma resolution is halved. This reduces the bandwidth of an uncompressed video signal by one-third.
813
+ * Many high-end digital video formats and interfaces use this scheme:
814
+ * - [AVC-Intra 100](https://en.wikipedia.org/wiki/AVC-Intra)
815
+ * - [Digital Betacam](https://en.wikipedia.org/wiki/Betacam#Digital_Betacam)
816
+ * - [Betacam SX](https://en.wikipedia.org/wiki/Betacam#Betacam_SX)
817
+ * - [DVCPRO50](https://en.wikipedia.org/wiki/DV#DVCPRO) and [DVCPRO HD](https://en.wikipedia.org/wiki/DV#DVCPRO_HD)
818
+ * - [Digital-S](https://en.wikipedia.org/wiki/Digital-S)
819
+ * - [CCIR 601](https://en.wikipedia.org/wiki/Rec._601) / [Serial Digital Interface](https://en.wikipedia.org/wiki/Serial_digital_interface) / [D1](https://en.wikipedia.org/wiki/D-1_(Sony))
820
+ * - [ProRes (HQ, 422, LT, and Proxy)](https://en.wikipedia.org/wiki/Apple_ProRes)
821
+ * - [XDCAM HD422](https://en.wikipedia.org/wiki/XDCAM)
822
+ * - [Canon MXF HD422](https://en.wikipedia.org/wiki/Canon_XF-300)
823
+ */
824
+ Yuv422 = 1,
825
+ /**
826
+ * n 4:2:0, the horizontal sampling is doubled compared to 4:1:1,
827
+ * but as the **Cb** and **Cr** channels are only sampled on each alternate line in this scheme, the vertical resolution is halved.
828
+ * The data rate is thus the same.
829
+ * This fits reasonably well with the PAL color encoding system, since this has only half the vertical chrominance resolution of [NTSC](https://en.wikipedia.org/wiki/NTSC).
830
+ * It would also fit extremely well with the [SECAM](https://en.wikipedia.org/wiki/SECAM) color encoding system,
831
+ * since like that format, 4:2:0 only stores and transmits one color channel per line (the other channel being recovered from the previous line).
832
+ * However, little equipment has actually been produced that outputs a SECAM analogue video signal.
833
+ * In general, SECAM territories either have to use a PAL-capable display or a [transcoder](https://en.wikipedia.org/wiki/Transcoding) to convert the PAL signal to SECAM for display.
834
+ */
835
+ Yuv420 = 2,
836
+ /**
837
+ * What if the chroma subsampling model is 4:0:0?
838
+ * That says to use every pixel of luma data, but that each row has 0 chroma samples applied to it. The resulting image, then, is comprised solely of the luminance data—a greyscale image.
839
+ */
840
+ Yuv400 = 3
841
+ }
842
+ interface ConvertToBlobOptions {
843
+ mime?: string;
844
+ quality?: number;
845
+ }
846
+ declare class Canvas {
847
+ constructor(width: number, height: number, flag?: SvgExportFlag);
380
848
  width: number;
381
849
  height: number;
850
+ getContext(contextType: '2d', contextAttributes?: ContextAttributes): SKRSContext2D;
851
+ encodeSync(format: 'webp' | 'jpeg', quality?: number): Buffer;
852
+ encodeSync(format: 'png'): Buffer;
853
+ encodeSync(format: 'avif', cfg?: AvifConfig): Buffer;
854
+ encodeSync(format: 'gif', quality?: number): Buffer;
855
+ encode(format: 'webp' | 'jpeg', quality?: number): Promise<Buffer>;
856
+ encode(format: 'png'): Promise<Buffer>;
857
+ encode(format: 'avif', cfg?: AvifConfig): Promise<Buffer>;
858
+ encode(format: 'gif', quality?: number): Promise<Buffer>;
859
+ encodeStream(format: 'webp' | 'jpeg', quality?: number): ReadableStream<Buffer>;
860
+ encodeStream(format: 'png'): ReadableStream<Buffer>;
861
+ toBuffer(mime: 'image/png'): Buffer;
862
+ toBuffer(mime: 'image/jpeg' | 'image/webp', quality?: number): Buffer;
863
+ toBuffer(mime: 'image/avif', cfg?: AvifConfig): Buffer;
864
+ toBuffer(mime: 'image/gif', quality?: number): Buffer; // raw pixels
865
+ data(): Buffer;
866
+ toDataURL(mime?: 'image/png'): string;
867
+ toDataURL(mime: 'image/jpeg' | 'image/webp', quality?: number): string;
868
+ toDataURL(mime: 'image/gif', quality?: number): string;
869
+ toDataURL(mime?: 'image/jpeg' | 'image/webp' | 'image/png' | 'image/gif', quality?: number): string;
870
+ toDataURL(mime?: 'image/avif', cfg?: AvifConfig): string;
871
+ toDataURLAsync(mime?: 'image/png'): Promise<string>;
872
+ toDataURLAsync(mime: 'image/jpeg' | 'image/webp', quality?: number): Promise<string>;
873
+ toDataURLAsync(mime: 'image/gif', quality?: number): Promise<string>;
874
+ toDataURLAsync(mime?: 'image/jpeg' | 'image/webp' | 'image/png' | 'image/gif', quality?: number): Promise<string>;
875
+ toDataURLAsync(mime?: 'image/avif', cfg?: AvifConfig): Promise<string>;
876
+ toBlob(callback: (blob: Blob | null) => void, mime?: string, quality?: number): void;
877
+ convertToBlob(options?: ConvertToBlobOptions): Promise<Blob>;
878
+ }
879
+ declare enum PathOp {
880
+ Difference = 0,
881
+ // subtract the op path from the first path
882
+ Intersect = 1,
883
+ // intersect the two paths
884
+ Union = 2,
885
+ // union (inclusive-or) the two paths
886
+ Xor = 3,
887
+ // exclusive-or the two paths
888
+ ReverseDifference = 4 // subtract the first path from the op path
889
+ }
890
+ declare enum FillType {
891
+ Winding = 0,
892
+ EvenOdd = 1,
893
+ InverseWinding = 2,
894
+ InverseEvenOdd = 3
895
+ }
896
+ declare enum StrokeJoin {
897
+ Miter = 0,
898
+ Round = 1,
899
+ Bevel = 2
900
+ }
901
+ declare enum StrokeCap {
902
+ Butt = 0,
903
+ Round = 1,
904
+ Square = 2
905
+ }
906
+ declare enum SvgExportFlag {
907
+ ConvertTextToPaths = 0x01,
908
+ NoPrettyXML = 0x02,
909
+ RelativePathEncoding = 0x04
910
+ }
911
+ //#endregion
912
+ //#region src/canvas.d.ts
913
+ interface CanvasOptions<T extends HTMLCanvasElement | OffscreenCanvas | Canvas = HTMLCanvasElement | OffscreenCanvas | Canvas> {
914
+ width: number;
915
+ height: number;
916
+ pixelRatio?: number;
917
+ /** 根据内容调整画布大小 */
918
+ fitContent?: boolean;
919
+ updateStyles?: boolean;
920
+ imageSmoothingEnabled?: boolean;
921
+ imageSmoothingQuality?: "low" | "medium" | "high";
922
+ canvas?: T;
382
923
  }
383
- interface DrawCallCanvas {
924
+ interface DrawCallCanvas<T extends HTMLCanvasElement | OffscreenCanvas | Canvas = HTMLCanvasElement> {
384
925
  readonly width: number;
385
926
  readonly height: number;
386
927
  readonly pixelRatio: number;
387
- readonly canvas: HTMLCanvasElement;
928
+ readonly canvas: T;
388
929
  render(element: Element): LayoutNode;
389
930
  clear(): void;
390
931
  getContext(): CanvasRenderingContext2D;
@@ -396,7 +937,7 @@ interface DrawCallCanvas {
396
937
  *
397
938
  * 在浏览器环境中使用,支持传入已有的 canvas 实例
398
939
  */
399
- declare function createCanvas(options: CanvasOptions): DrawCallCanvas;
940
+ declare function createCanvas<T extends HTMLCanvasElement | OffscreenCanvas | Canvas = HTMLCanvasElement>(options: CanvasOptions<T>): DrawCallCanvas<T>;
400
941
  //#endregion
401
942
  //#region src/components/Box.d.ts
402
943
  declare function Box(props: BoxProps): BoxElement;
@@ -449,4 +990,4 @@ declare function printLayout(node: LayoutNode): void;
449
990
  */
450
991
  declare function layoutToString(node: LayoutNode, _indent?: string): string;
451
992
  //#endregion
452
- export { type AlignItems, type AlignSelf, type Border, type Bounds, Box, type BoxElement, type BoxProps, type CanvasOptions, type Color, type ColorStop, type ContainerLayoutProps, CustomDraw, type CustomDrawElement, type CustomDrawProps, type DrawCallCanvas, type Element, type FlexDirection, type FontProps, type GradientDescriptor, Image, type JustifyContent, type LayoutNode, type LayoutProps, type LayoutSize, type LinearGradientDescriptor, type MeasureContext, type ProxiedCanvasContextOptions, type RadialGradientDescriptor, RichText, type RichTextElement, type RichTextProps, type RichTextSpan, type Shadow, type Size, type Spacing, Stack, type StackAlign, type StackElement, type StackProps, type StrokeProps, Svg, type SvgAlign, type SvgChild, type SvgCircleChild, type SvgElement, type SvgEllipseChild, type SvgGroupChild, type SvgLineChild, type SvgPathChild, type SvgPolygonChild, type SvgPolylineChild, type SvgProps, type SvgRectChild, type SvgStyleProps, type SvgTextChild, type SvgTransformProps, Text, type TextElement, type TextProps, Transform, type TransformElement, type TransformProps, computeLayout, createCanvas, createCanvasMeasureContext, layoutToString, linearGradient, printLayout, radialGradient, svg };
993
+ export { type AlignItems, type AlignSelf, type Border, type Bounds, Box, type BoxElement, type BoxProps, type CanvasOptions, type Color, type ColorStop, type ContainerLayoutProps, CustomDraw, type CustomDrawElement, type CustomDrawProps, type DrawCallCanvas, type Element, type FlexDirection, type FontProps, type GradientDescriptor, Image, type JustifyContent, type LayoutNode, type LayoutProps, type LinearGradientDescriptor, type MeasureContext, type ProxiedCanvasContextOptions, type RadialGradientDescriptor, RichText, type RichTextElement, type RichTextProps, type RichTextSpan, type Shadow, type Size, type Spacing, Stack, type StackAlign, type StackElement, type StackProps, type StrokeProps, Svg, type SvgAlign, type SvgChild, type SvgCircleChild, type SvgElement, type SvgEllipseChild, type SvgGroupChild, type SvgLineChild, type SvgPathChild, type SvgPolygonChild, type SvgPolylineChild, type SvgProps, type SvgRectChild, type SvgStyleProps, type SvgTextChild, type SvgTransformProps, Text, type TextElement, type TextProps, Transform, type TransformElement, type TransformProps, computeLayout, createCanvas, createCanvasMeasureContext, layoutToString, linearGradient, printLayout, radialGradient, svg };
package/browser/index.js CHANGED
@@ -1832,8 +1832,17 @@ function renderNode(ctx, node) {
1832
1832
  function createCanvas(options) {
1833
1833
  const { width, height, pixelRatio = 1 } = options;
1834
1834
  const canvas = options.canvas ?? createRawCanvas(width * pixelRatio, height * pixelRatio);
1835
+ if (options.canvas) {
1836
+ canvas.width = width * pixelRatio;
1837
+ canvas.height = height * pixelRatio;
1838
+ if ("style" in canvas && options.updateStyles !== false) {
1839
+ canvas.style.width = `${width}px`;
1840
+ canvas.style.height = `${height}px`;
1841
+ }
1842
+ }
1835
1843
  const ctx = canvas.getContext("2d");
1836
1844
  if (!ctx) throw new Error("Failed to get 2d context");
1845
+ ctx.resetTransform();
1837
1846
  if (options.imageSmoothingEnabled !== void 0) ctx.imageSmoothingEnabled = options.imageSmoothingEnabled;
1838
1847
  if (options.imageSmoothingQuality !== void 0) ctx.imageSmoothingQuality = options.imageSmoothingQuality;
1839
1848
  if (pixelRatio !== 1) ctx.scale(pixelRatio, pixelRatio);
@@ -1850,6 +1859,19 @@ function createCanvas(options) {
1850
1859
  minHeight: 0,
1851
1860
  maxHeight: height
1852
1861
  });
1862
+ if (options.fitContent) {
1863
+ const contentWidth = layoutTree.layout.width;
1864
+ const contentHeight = layoutTree.layout.height;
1865
+ if (canvas.width !== contentWidth * pixelRatio || canvas.height !== contentHeight * pixelRatio) {
1866
+ canvas.width = contentWidth * pixelRatio;
1867
+ canvas.height = contentHeight * pixelRatio;
1868
+ if ("style" in canvas && options.updateStyles !== false) {
1869
+ canvas.style.width = `${contentWidth}px`;
1870
+ canvas.style.height = `${contentHeight}px`;
1871
+ }
1872
+ if (pixelRatio !== 1) ctx.scale(pixelRatio, pixelRatio);
1873
+ }
1874
+ }
1853
1875
  renderNode(ctx, layoutTree);
1854
1876
  return layoutTree;
1855
1877
  },
package/examples/demo.ts CHANGED
@@ -40,17 +40,13 @@ const dpr = window.devicePixelRatio || 1;
40
40
  const width = 720;
41
41
  const height = 600;
42
42
 
43
- canvasEl.width = width * dpr;
44
- canvasEl.height = height * dpr;
45
- canvasEl.style.width = `${width}px`;
46
- canvasEl.style.height = `${height}px`;
47
-
48
43
  // 创建 Canvas 实例
49
44
  const canvas = createCanvas({
50
45
  width,
51
46
  height,
52
47
  pixelRatio: dpr,
53
48
  canvas: canvasEl,
49
+ fitContent: true,
54
50
  });
55
51
 
56
52
  // 渲染演示内容