@weasel-js/guides 1.15.1 → 1.16.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.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useState, useRef, useCallback, useEffect } from 'react';
2
- import { sceneBoxFrames, scenePoseFrame, useLatest, viewportWorldRect, tickedSegment, measuredSegmentCommands, AUTO_POSE_DESCRIPTOR, visualBoundsViaDescriptor, rectFromPlane, unionAABB, sceneMagnification, asNodeId, createTransformOp, sceneUnitScopes, screenToWorld, nodeGates, resolveStrokeWidth, resolveUnit, axisUnits, resolveScoped } from '@weasel-js/core';
3
- import { pointInPolygon, invert, mat3, applyToPoint, pxExtent, PATH_M, PATH_L, PATH_Z, pxExtentInFrame } from '@weasel-js/geom';
2
+ import { sceneBoxFrames, scenePoseFrame, useLatest, viewportWorldRect, viewWorldToScreen, tickedSegment, measuredSegmentCommands, AUTO_POSE_DESCRIPTOR, visualBoundsViaDescriptor, rectFromPlane, unionAABB, sceneMagnification, asNodeId, createTransformOp, sceneUnitScopes, screenToWorld, nodeGates, resolveStrokeWidth, resolveUnit, axisUnits, resolveScoped } from '@weasel-js/core';
3
+ import { pointInPolygon, invert, mat3, distance2, rectContainsPointHalfOpen, applyToPoint, pxExtent, PATH_M, PATH_L, PATH_Z, pxExtentInFrame } from '@weasel-js/geom';
4
4
 
5
5
  // src/grid/roundToCell.ts
6
6
  function roundToCell(value, cellSize) {
@@ -592,26 +592,27 @@ function createGuidesLayer(opts) {
592
592
  const guides = opts.getGuides();
593
593
  const gaps = opts.getGaps?.() ?? [];
594
594
  if (guides.length === 0 && gaps.length === 0) return [];
595
- const sx = (x) => (x - view.x) * view.scale.x;
596
- const sy = (y) => (y - view.y) * view.scale.y;
597
595
  const out = [];
598
596
  for (const g of guides) {
599
597
  const span2 = spanned ? g.span : void 0;
600
598
  if (g.axis === "x") {
601
- const x = sx(g.offset);
599
+ const lo3 = viewWorldToScreen(view, { x: g.offset, y: span2?.min ?? 0 });
600
+ const x = lo3.x;
602
601
  if (x < -lineWidth || x > dims.width + lineWidth) continue;
603
- const path = span2 ? tickedSegment(x, sy(span2.min), x, sy(span2.max), ticks) : tickedSegment(x, 0, x, dims.height, 0);
602
+ const path = span2 ? tickedSegment(x, lo3.y, x, viewWorldToScreen(view, { x: g.offset, y: span2.max }).y, ticks) : tickedSegment(x, 0, x, dims.height, 0);
604
603
  out.push({ kind: "path", path, stroke });
605
604
  } else {
606
- const y = sy(g.offset);
605
+ const lo3 = viewWorldToScreen(view, { x: span2?.min ?? 0, y: g.offset });
606
+ const y = lo3.y;
607
607
  if (y < -lineWidth || y > dims.height + lineWidth) continue;
608
- const path = span2 ? tickedSegment(sx(span2.min), y, sx(span2.max), y, ticks) : tickedSegment(0, y, dims.width, y, 0);
608
+ const path = span2 ? tickedSegment(lo3.x, y, viewWorldToScreen(view, { x: span2.max, y: g.offset }).x, y, ticks) : tickedSegment(0, y, dims.width, y, 0);
609
609
  out.push({ kind: "path", path, stroke });
610
610
  }
611
611
  }
612
612
  for (const g of gaps) {
613
- const [x0, y0, x1, y1] = g.axis === "x" ? [sx(g.min), sy(g.at), sx(g.max), sy(g.at)] : [sx(g.at), sy(g.min), sx(g.at), sy(g.max)];
614
- out.push(...measuredSegmentCommands(x0, y0, x1, y1, ticks, gapStroke, format?.(g.max - g.min) ?? null, gapColor));
613
+ const a = viewWorldToScreen(view, g.axis === "x" ? { x: g.min, y: g.at } : { x: g.at, y: g.min });
614
+ const b = viewWorldToScreen(view, g.axis === "x" ? { x: g.max, y: g.at } : { x: g.at, y: g.max });
615
+ out.push(...measuredSegmentCommands(a.x, a.y, b.x, b.y, ticks, gapStroke, format?.(g.max - g.min) ?? null, gapColor));
615
616
  }
616
617
  return out;
617
618
  }
@@ -1135,19 +1136,12 @@ function createSpacingHintsPreset(opts = {}) {
1135
1136
  if (opts.readout !== false) contributions.push(distanceReadout({ ...opts.readout, ...color }));
1136
1137
  return { id, snapSources, contributions };
1137
1138
  }
1138
-
1139
- // src/layout/snaps.ts
1140
- function dist2(a, b) {
1141
- const dx = a.x - b.x;
1142
- const dy = a.y - b.y;
1143
- return dx * dx + dy * dy;
1144
- }
1145
1139
  function nearestOf(targets, pointer) {
1146
1140
  if (targets.length === 0) return null;
1147
1141
  let best = targets[0];
1148
- let bestD = dist2(best.origin, pointer);
1142
+ let bestD = distance2(best.origin, pointer);
1149
1143
  for (let i = 1; i < targets.length; i++) {
1150
- const d = dist2(targets[i].origin, pointer);
1144
+ const d = distance2(targets[i].origin, pointer);
1151
1145
  if (d < bestD) {
1152
1146
  bestD = d;
1153
1147
  best = targets[i];
@@ -1167,18 +1161,15 @@ function nearestWithin(opts) {
1167
1161
  pickTarget(targets, pointer) {
1168
1162
  const got = nearestOf(targets, pointer);
1169
1163
  if (got === null) return null;
1170
- return dist2(got.origin, pointer) <= tol2 ? got : null;
1164
+ return distance2(got.origin, pointer) <= tol2 ? got : null;
1171
1165
  }
1172
1166
  };
1173
1167
  }
1174
- function rectContains(r, p) {
1175
- return p.x >= r.x && p.x < r.x + r.width && p.y >= r.y && p.y < r.y + r.height;
1176
- }
1177
1168
  function containedThenNearest() {
1178
1169
  return {
1179
1170
  pickTarget(targets, pointer) {
1180
1171
  for (const t of targets) {
1181
- if (t.hitBounds && rectContains(t.hitBounds, pointer)) return t;
1172
+ if (t.hitBounds && rectContainsPointHalfOpen(t.hitBounds, pointer.x, pointer.y)) return t;
1182
1173
  }
1183
1174
  return nearestOf(targets, pointer);
1184
1175
  }
@@ -1191,8 +1182,8 @@ function cellAt() {
1191
1182
  return {
1192
1183
  pickTarget(targets, pointer) {
1193
1184
  for (const t of targets) {
1194
- if (t.hitBounds && rectContains(t.hitBounds, pointer)) return t;
1195
- if (isCellMeta(t.meta) && rectContains(t.meta.cellRect, pointer)) return t;
1185
+ if (t.hitBounds && rectContainsPointHalfOpen(t.hitBounds, pointer.x, pointer.y)) return t;
1186
+ if (isCellMeta(t.meta) && rectContainsPointHalfOpen(t.meta.cellRect, pointer.x, pointer.y)) return t;
1196
1187
  }
1197
1188
  return nearestOf(targets, pointer);
1198
1189
  }