у меня есть вот такой код
но сецчас округление происходит только вправую сторону
и когда мой примитив прижат к правой грнице, поесле округления он смещается вправо еще больше и вылетает за границу
что это
ипсравь это
import { useEffect, useRef } from 'react';
import Konva from 'konva';
import { IPoint } from '../models/infrastucture/point';
import { useGridStore } from '../store';
import { useDocumentContext } from './useDocumentContext';
import { Subject } from 'rxjs';
import { ISize } from '../models/infrastucture/size';
import { PrimitiveBase } from '../models/base/primitives.base';
import Node = Konva.Node;
import Stage = Konva.Stage;
import Line = Konva.Line;
import Group = Konva.Group;
The issue you're encountering seems to be related to how the position of nodes is being rounded off and adjusted when they're close to the right boundary of your defined space. When an element gets rounded, it might be moving too far to one side, especially if it's near the edge of the boundary, which can cause it to exceed the limits.
Here are a few things you can try to resolve the issue:
Adjust Rounding Logic
Modify Rounding Logic: Ensure that you're rounding in a way that keeps your elements within bounds. Consider using Math.floor or Math.ceil conditionally based on the position relative to the grid and boundaries.
Conditional Adjustments: Instead of directly rounding up, figure out the nearest grid point that doesn't exceed the boundaries and adjust the position accordingly.
Review Boundary Checks
Check Constraint Logic: Make sure your boundary condition checks are precise and not allowing elements to exceed limits. Any calculation that involves comparing positions should correctly take care of the maximum limit:
Snap Calculation: Before setting the calculated position with node.position(newPosition), ensure it's clamped correctly within boundaries.
Handle Closest Boundary: You might want to adjust the manner in which rounding is applied, perhaps by choosing the closest valid position in comparison to both the grid and the boundary.
const updatePosition = (x: number, y: number) => {
const node = ref.current;
if (!node) return;
let newPosition = {
x: Math.round(x / gridSize) * gridSize,
y: Math.round(y / gridSize) * gridSize,
};
// Clamp new position within bounds
if (newPosition.x < 0) newPosition.x = 0;
if (newPosition.y < 0) newPosition.y = 0;
if (newPosition.x > document.activePage.width - node.width()) {
newPosition.x = document.activePage.width - node.width();
}
if (newPosition.y > document.activePage.height - node.height()) {
newPosition.y = document.activePage.height - node.height();
}
node.position(newPosition);
positionSource.next({ id: id, x: newPosition.x, y: newPosition.y });
};
Debugging
Add console logs or breakpoints to track how the position changes throughout drag events and see where it exceeds bounds.
Test with different grid sizes and boundary scenarios to see how it affects rounding and adjust accordingly.
Hopefully, these steps will help fix the issue where the elements are moving out of bounds upon rounding.