Skip to content
This repository was archived by the owner on May 27, 2024. It is now read-only.

Commit 17f58b6

Browse files
authored
Merge pull request #45 from PsycleResearch/fix/add-boundaries-to-coordinates
fix: Add boundaries to coordinates
2 parents 166064a + 07087ae commit 17f58b6

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@psycle/repsycle",
3-
"version": "0.0.32",
3+
"version": "0.0.33",
44
"description": "Psycle Research front-end toolkit",
55
"author": "Psycle Research",
66
"keywords": [

src/draw-zone/components.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
Svg,
3333
Use,
3434
} from '@svgdotjs/svg.js'
35-
import { bgrToHex, uuid4 } from '../helpers'
35+
import { bgrToHex, clamp, uuid4 } from '../helpers'
3636
import { isTouchDevice } from '../utils'
3737

3838
import { Interactable } from '@interactjs/types'
@@ -203,8 +203,8 @@ function DrawZoneInner({
203203
onChange(
204204
elements.map((element) => {
205205
const points = element.points.map(({ x, y }) => ({
206-
x: Math.round(x),
207-
y: Math.round(y),
206+
x: clamp(Math.round(x), 0, pictureSize.width),
207+
y: clamp(Math.round(y), 0, pictureSize.height),
208208
}))
209209
const minX = Math.min(...points.map(({ x }) => x))
210210
const minY = Math.min(...points.map(({ y }) => y))
@@ -226,7 +226,7 @@ function DrawZoneInner({
226226
}),
227227
)
228228
},
229-
[contentHidden, onChange, shape],
229+
[contentHidden, onChange, shape, pictureSize],
230230
)
231231

232232
return (

src/helpers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BGR } from "./types"
1+
import { BGR } from './types'
22

33
export function uuid4() {
44
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
@@ -15,6 +15,10 @@ export function hasValue(e: unknown): boolean {
1515
return e !== undefined && e !== null
1616
}
1717

18+
export function clamp(num: number, min: number, max: number) {
19+
return Math.max(min, Math.min(max, num))
20+
}
21+
1822
interface TruncateOptions {
1923
length?: number | null
2024
ending?: string | null

0 commit comments

Comments
 (0)