@cocorof/graphier 1.4.3 → 1.4.4

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
@@ -2247,6 +2247,7 @@ function createScene(container, backgroundColor, style, rendererConfig) {
2247
2247
  controls.zoomSpeed = 1.2;
2248
2248
  controls.minDistance = 10;
2249
2249
  controls.maxDistance = 3e4;
2250
+ controls.screenSpacePanning = true;
2250
2251
  scene.add(new THREE__namespace.AmbientLight(16777215, 0.8));
2251
2252
  const stars = createStarField(4e3, 8e3);
2252
2253
  stars.visible = style.starField;
@@ -2260,26 +2261,34 @@ function createScene(container, backgroundColor, style, rendererConfig) {
2260
2261
  controls.enableZoom = false;
2261
2262
  const flags = {
2262
2263
  is2D: false,
2263
- keyboardMode3D: cameraMode
2264
+ keyboardMode3D: cameraMode,
2265
+ rotateButton: null
2264
2266
  };
2265
- const _wheelDir = new THREE__namespace.Vector3();
2267
+ const _wheelOffset = new THREE__namespace.Vector3();
2266
2268
  const onWheel = (e) => {
2267
2269
  if (flags.is2D) return;
2268
2270
  e.preventDefault();
2269
2271
  let delta = -e.deltaY;
2270
2272
  if (e.deltaMode === 1) delta *= 40;
2271
2273
  if (e.deltaMode === 2) delta *= 800;
2272
- camera.getWorldDirection(_wheelDir);
2273
- const dist = camera.position.distanceTo(controls.target);
2274
+ const target = controls.target;
2275
+ _wheelOffset.copy(camera.position).sub(target);
2276
+ const dist = _wheelOffset.length();
2274
2277
  const moveAmount = delta * 2e-3 * Math.max(dist * 0.1, 1);
2275
- camera.position.addScaledVector(_wheelDir, moveAmount);
2276
- controls.target.addScaledVector(_wheelDir, moveAmount);
2278
+ const newDist = Math.min(
2279
+ Math.max(dist - moveAmount, controls.minDistance),
2280
+ controls.maxDistance
2281
+ );
2282
+ _wheelOffset.setLength(newDist);
2283
+ camera.position.copy(target).add(_wheelOffset);
2277
2284
  };
2278
2285
  canvas.addEventListener("wheel", onWheel, { passive: false });
2286
+ const trackballCleanup = setupTrackballRotation(canvas, camera, controls, flags);
2279
2287
  const scrollCleanup = () => {
2280
2288
  canvas.removeEventListener("wheel", onWheel);
2281
2289
  canvas.removeEventListener("dragstart", onDragStart);
2282
2290
  canvas.removeEventListener("selectstart", onSelectStart);
2291
+ trackballCleanup();
2283
2292
  };
2284
2293
  return {
2285
2294
  scene,
@@ -2296,24 +2305,80 @@ function createScene(container, backgroundColor, style, rendererConfig) {
2296
2305
  flags
2297
2306
  };
2298
2307
  }
2299
- const MOUSE_ACTION = {
2300
- rotate: THREE__namespace.MOUSE.ROTATE,
2301
- pan: THREE__namespace.MOUSE.PAN
2302
- };
2308
+ const TRACKBALL_SPEED = 4e-3;
2309
+ function setupTrackballRotation(canvas, camera, controls, flags) {
2310
+ let rotating = false;
2311
+ let lastX = 0;
2312
+ let lastY = 0;
2313
+ const _offset2 = new THREE__namespace.Vector3();
2314
+ const _right2 = new THREE__namespace.Vector3();
2315
+ const _upAxis = new THREE__namespace.Vector3();
2316
+ const _q2 = new THREE__namespace.Quaternion();
2317
+ const _qPitch = new THREE__namespace.Quaternion();
2318
+ function onDown(e) {
2319
+ if (flags.rotateButton == null || e.button !== flags.rotateButton) return;
2320
+ rotating = true;
2321
+ lastX = e.clientX;
2322
+ lastY = e.clientY;
2323
+ try {
2324
+ canvas.setPointerCapture(e.pointerId);
2325
+ } catch {
2326
+ }
2327
+ }
2328
+ function onMove(e) {
2329
+ if (!rotating) return;
2330
+ const dx = e.clientX - lastX;
2331
+ const dy = e.clientY - lastY;
2332
+ lastX = e.clientX;
2333
+ lastY = e.clientY;
2334
+ if (!dx && !dy) return;
2335
+ const target = controls.target;
2336
+ _offset2.copy(camera.position).sub(target);
2337
+ _right2.setFromMatrixColumn(camera.matrix, 0);
2338
+ _upAxis.setFromMatrixColumn(camera.matrix, 1);
2339
+ _q2.setFromAxisAngle(_upAxis, -dx * TRACKBALL_SPEED);
2340
+ _qPitch.setFromAxisAngle(_right2, -dy * TRACKBALL_SPEED);
2341
+ _q2.multiply(_qPitch);
2342
+ _offset2.applyQuaternion(_q2);
2343
+ camera.up.applyQuaternion(_q2).normalize();
2344
+ camera.position.copy(target).add(_offset2);
2345
+ camera.lookAt(target);
2346
+ }
2347
+ function onUp(e) {
2348
+ if (!rotating) return;
2349
+ rotating = false;
2350
+ try {
2351
+ canvas.releasePointerCapture(e.pointerId);
2352
+ } catch {
2353
+ }
2354
+ }
2355
+ canvas.addEventListener("pointerdown", onDown);
2356
+ canvas.addEventListener("pointermove", onMove);
2357
+ canvas.addEventListener("pointerup", onUp);
2358
+ canvas.addEventListener("pointercancel", onUp);
2359
+ return () => {
2360
+ canvas.removeEventListener("pointerdown", onDown);
2361
+ canvas.removeEventListener("pointermove", onMove);
2362
+ canvas.removeEventListener("pointerup", onUp);
2363
+ canvas.removeEventListener("pointercancel", onUp);
2364
+ };
2365
+ }
2303
2366
  function applyNavigationMode(state, is2D, nav) {
2304
2367
  const { controls, camera, keyboard } = state;
2305
2368
  state.flags.is2D = is2D;
2306
2369
  const left = (nav == null ? void 0 : nav.leftButton) ?? (is2D ? "pan" : "rotate");
2307
2370
  const right = (nav == null ? void 0 : nav.rightButton) ?? (is2D ? "rotate" : "pan");
2371
+ const NONE = -1;
2308
2372
  controls.mouseButtons = {
2309
- LEFT: MOUSE_ACTION[left],
2373
+ LEFT: left === "pan" ? THREE__namespace.MOUSE.PAN : NONE,
2310
2374
  MIDDLE: THREE__namespace.MOUSE.DOLLY,
2311
- RIGHT: MOUSE_ACTION[right]
2375
+ RIGHT: right === "pan" ? THREE__namespace.MOUSE.PAN : NONE
2312
2376
  };
2313
- controls.enableRotate = left === "rotate" || right === "rotate";
2377
+ controls.enableRotate = false;
2378
+ state.flags.rotateButton = left === "rotate" ? 0 : right === "rotate" ? 2 : null;
2314
2379
  controls.touches = left === "pan" ? { ONE: THREE__namespace.TOUCH.PAN, TWO: THREE__namespace.TOUCH.DOLLY_PAN } : { ONE: THREE__namespace.TOUCH.ROTATE, TWO: THREE__namespace.TOUCH.DOLLY_PAN };
2315
2380
  controls.enableZoom = is2D;
2316
- controls.screenSpacePanning = is2D;
2381
+ controls.screenSpacePanning = true;
2317
2382
  const kb = (nav == null ? void 0 : nav.keyboard) ?? (is2D ? "pan" : state.flags.keyboardMode3D);
2318
2383
  if (kb === "off") {
2319
2384
  keyboard.setEnabled(false);
@@ -2649,6 +2714,7 @@ function animateCamera(camera, controls, targetPos, targetLookAt, duration) {
2649
2714
  function zoomToFitPositions(camera, controls, positions, duration, padding) {
2650
2715
  const count = positions.length / 3;
2651
2716
  if (count === 0) return;
2717
+ camera.up.set(0, 1, 0);
2652
2718
  let cx = 0, cy = 0, cz = 0;
2653
2719
  for (let i = 0; i < count; i++) {
2654
2720
  cx += positions[i * 3];
@@ -2893,8 +2959,8 @@ function setupInteraction(canvas, camera, controls, getNodesMesh, getNodes, call
2893
2959
  }
2894
2960
  }
2895
2961
  function onContextMenu2(e) {
2896
- if (!callbacks.onContextMenu) return;
2897
2962
  e.preventDefault();
2963
+ if (!callbacks.onContextMenu) return;
2898
2964
  const nodesMesh = getNodesMesh();
2899
2965
  if (!nodesMesh) return;
2900
2966
  const rect = canvas.getBoundingClientRect();