@tsdraw/core 0.8.5 → 0.9.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.
package/dist/index.cjs CHANGED
@@ -372,9 +372,12 @@ function resolveThemeColor(colorStyle, theme) {
372
372
  return DARK_COLORS[colorStyle] ?? lightThemeColor;
373
373
  }
374
374
  var CanvasRenderer = class {
375
- theme = "light";
375
+ _theme = "light";
376
+ get theme() {
377
+ return this._theme;
378
+ }
376
379
  setTheme(theme) {
377
- this.theme = theme;
380
+ this._theme = theme;
378
381
  }
379
382
  render(ctx, viewport, shapes) {
380
383
  ctx.save();
@@ -392,7 +395,7 @@ var CanvasRenderer = class {
392
395
  const width = (STROKE_WIDTHS[shape.props.size] ?? 3.5) * shape.props.scale;
393
396
  const samples = flattenSegments(shape);
394
397
  if (samples.length === 0) return;
395
- const color = resolveThemeColor(shape.props.color, this.theme);
398
+ const color = resolveThemeColor(shape.props.color, this._theme);
396
399
  const fillStyle = shape.props.fill ?? "none";
397
400
  if (shape.props.isClosed && fillStyle !== "none") {
398
401
  this.paintClosedShapeFill(ctx, samples, color, fillStyle);
@@ -458,7 +461,7 @@ var CanvasRenderer = class {
458
461
  ctx.fillStyle = color;
459
462
  ctx.globalAlpha = 0.55;
460
463
  } else if (fillStyle === "none") {
461
- ctx.fillStyle = this.theme === "dark" ? "#0f0f0f" : "#fafafa";
464
+ ctx.fillStyle = this._theme === "dark" ? "#0f0f0f" : "#fafafa";
462
465
  ctx.globalAlpha = 1;
463
466
  } else {
464
467
  ctx.fillStyle = color;
@@ -554,6 +557,102 @@ function getLineDash(dash, width) {
554
557
  }
555
558
  }
556
559
 
560
+ // src/canvas/backgroundRenderer.ts
561
+ var DEFAULT_SPACING = 20;
562
+ var DEFAULT_LINE_WIDTH = 0.5;
563
+ var DEFAULT_DOT_RADIUS = 1;
564
+ var DEFAULT_OPACITY = 0.25;
565
+ function resolvePresetPatternColor(colorLight, colorDark, theme) {
566
+ if (theme === "dark") return colorDark ?? colorLight ?? "#888888";
567
+ return colorLight ?? "#c0c0c0";
568
+ }
569
+ function visiblePageRect(viewport, canvasWidth, canvasHeight) {
570
+ return {
571
+ minX: (0 - viewport.x) / viewport.zoom,
572
+ minY: (0 - viewport.y) / viewport.zoom,
573
+ maxX: (canvasWidth - viewport.x) / viewport.zoom,
574
+ maxY: (canvasHeight - viewport.y) / viewport.zoom
575
+ };
576
+ }
577
+ function drawHorizontalLines(ctx, visible, spacing, lineWidth, color, opacity) {
578
+ const startY = Math.floor(visible.minY / spacing) * spacing;
579
+ ctx.save();
580
+ ctx.strokeStyle = color;
581
+ ctx.lineWidth = lineWidth / ctx.getTransform().a;
582
+ ctx.globalAlpha = opacity;
583
+ ctx.beginPath();
584
+ for (let y = startY; y <= visible.maxY; y += spacing) {
585
+ ctx.moveTo(visible.minX, y);
586
+ ctx.lineTo(visible.maxX, y);
587
+ }
588
+ ctx.stroke();
589
+ ctx.restore();
590
+ }
591
+ function drawGridLines(ctx, visible, spacing, lineWidth, color, opacity) {
592
+ const startX = Math.floor(visible.minX / spacing) * spacing;
593
+ const startY = Math.floor(visible.minY / spacing) * spacing;
594
+ const compensatedWidth = lineWidth / ctx.getTransform().a;
595
+ ctx.save();
596
+ ctx.strokeStyle = color;
597
+ ctx.lineWidth = compensatedWidth;
598
+ ctx.globalAlpha = opacity;
599
+ ctx.beginPath();
600
+ for (let x = startX; x <= visible.maxX; x += spacing) {
601
+ ctx.moveTo(x, visible.minY);
602
+ ctx.lineTo(x, visible.maxY);
603
+ }
604
+ for (let y = startY; y <= visible.maxY; y += spacing) {
605
+ ctx.moveTo(visible.minX, y);
606
+ ctx.lineTo(visible.maxX, y);
607
+ }
608
+ ctx.stroke();
609
+ ctx.restore();
610
+ }
611
+ function drawDotPattern(ctx, visible, spacing, dotRadius, color, opacity) {
612
+ const startX = Math.floor(visible.minX / spacing) * spacing;
613
+ const startY = Math.floor(visible.minY / spacing) * spacing;
614
+ const compensatedRadius = dotRadius / ctx.getTransform().a;
615
+ ctx.save();
616
+ ctx.fillStyle = color;
617
+ ctx.globalAlpha = opacity;
618
+ ctx.beginPath();
619
+ for (let x = startX; x <= visible.maxX; x += spacing) {
620
+ for (let y = startY; y <= visible.maxY; y += spacing) {
621
+ ctx.moveTo(x + compensatedRadius, y);
622
+ ctx.arc(x, y, compensatedRadius, 0, Math.PI * 2);
623
+ }
624
+ }
625
+ ctx.fill();
626
+ ctx.restore();
627
+ }
628
+ function renderCanvasBackground(ctx, viewport, canvasWidth, canvasHeight, options, theme) {
629
+ if (!options || options.type === "blank") return;
630
+ if (options.type === "custom") {
631
+ options.render(ctx, viewport, canvasWidth, canvasHeight);
632
+ return;
633
+ }
634
+ const spacing = options.spacing ?? DEFAULT_SPACING;
635
+ if (spacing <= 0) return;
636
+ const color = resolvePresetPatternColor(options.color, options.colorDark, theme);
637
+ const opacity = options.opacity ?? DEFAULT_OPACITY;
638
+ const visible = visiblePageRect(viewport, canvasWidth, canvasHeight);
639
+ ctx.save();
640
+ ctx.translate(viewport.x, viewport.y);
641
+ ctx.scale(viewport.zoom, viewport.zoom);
642
+ switch (options.type) {
643
+ case "lines":
644
+ drawHorizontalLines(ctx, visible, spacing, options.size ?? DEFAULT_LINE_WIDTH, color, opacity);
645
+ break;
646
+ case "grid":
647
+ drawGridLines(ctx, visible, spacing, options.size ?? DEFAULT_LINE_WIDTH, color, opacity);
648
+ break;
649
+ case "dots":
650
+ drawDotPattern(ctx, visible, spacing, options.size ?? DEFAULT_DOT_RADIUS, color, opacity);
651
+ break;
652
+ }
653
+ ctx.restore();
654
+ }
655
+
557
656
  // src/input/inputManager.ts
558
657
  var InputManager = class {
559
658
  _current = { x: 0, y: 0 };
@@ -2318,6 +2417,7 @@ exports.pageToScreen = pageToScreen;
2318
2417
  exports.panViewport = panViewport;
2319
2418
  exports.pointHitsShape = pointHitsShape;
2320
2419
  exports.recordsToDocumentSnapshot = recordsToDocumentSnapshot;
2420
+ exports.renderCanvasBackground = renderCanvasBackground;
2321
2421
  exports.resolveThemeColor = resolveThemeColor;
2322
2422
  exports.rotatePoint = rotatePoint;
2323
2423
  exports.screenToPage = screenToPage;