From 8c3f08187f9fcf5a81a44863b67a1232eb9ab842 Mon Sep 17 00:00:00 2001 From: Kim716 Date: Wed, 15 May 2024 17:49:47 +0800 Subject: [PATCH] fix: movable point coordX --- packages/package.json | 2 +- packages/perseus/src/util/interactive.ts | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/package.json b/packages/package.json index b4503fb17f..aa166b305f 100644 --- a/packages/package.json +++ b/packages/package.json @@ -1,6 +1,6 @@ { "name": "@junyiacademy/perseus-core", - "version": "1.0.34", + "version": "1.0.35", "main": "./dist/index.js", "types": "./dist/index.d.ts", "license": "MIT", diff --git a/packages/perseus/src/util/interactive.ts b/packages/perseus/src/util/interactive.ts index 90f6a2616d..91c635dbf9 100644 --- a/packages/perseus/src/util/interactive.ts +++ b/packages/perseus/src/util/interactive.ts @@ -622,9 +622,13 @@ _.extend(GraphUtils.Graphie.prototype, { }, vertexLabel: "", mouseTarget: null, + start: 0, + end: 10, }, options, ); + const initialX = movablePoint.coord[0]; + let isInitial = true; const normalColor = movablePoint.constraints.fixed ? KhanColors.DYNAMIC @@ -666,9 +670,23 @@ _.extend(GraphUtils.Graphie.prototype, { // snap coordinates to grid if (movablePoint.snapX !== 0) { + // 讓座標是根據移動起始點去計算,而非從 0 coordX = - Math.round(coordX / movablePoint.snapX) * - movablePoint.snapX; + Math.round( + (coordX - movablePoint.start) / movablePoint.snapX, + ) * + movablePoint.snapX + + movablePoint.start; + + coordX = coordX > movablePoint.end ? movablePoint.end : coordX; + coordX = + coordX < movablePoint.start ? movablePoint.start : coordX; + + // 即便點的初始位置不在可以拖動的路徑上,仍先可以放置於上面,避免使用者困惑,拖動後就只能放置在安排好的位置 + if (isInitial) { + coordX = initialX; + isInitial = false; + } } if (movablePoint.snapY !== 0) { coordY = @@ -723,7 +741,6 @@ _.extend(GraphUtils.Graphie.prototype, { graph.range[1][1] - mouseY / graph.scale[1], ); } - const result = movablePoint.applyConstraint([coordX, coordY]); return result; };