-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
On pointer move is recording coordinates too frequent, adding a throttling to optimise #163
Comments
approach updated by using storing reference and early return |
if adding dependency is fine, just add below handler to the
|
Fixes #163 Add throttling to pointer move events in the Canvas component to optimize coordinate recording. - Modify `packages/react-sketch-canvas/src/Canvas/index.tsx` to add throttling to the `handlePointerMove` function using a custom `useThrottledCallback` function. - Add `throttleTime` to `CanvasProps` in `packages/react-sketch-canvas/src/Canvas/types.ts` with a default value of 20. - Add `throttleTime` to `ReactSketchCanvasProps` in `packages/react-sketch-canvas/src/ReactSketchCanvas/types.ts` with a default value of 20. - Add `useThrottledCallback` function in `packages/react-sketch-canvas/src/Canvas/utils.tsx` to throttle a callback function. - Update `README.md` to include documentation on the new `throttleTime` configuration option. - Add tests to verify throttling behavior in `packages/tests/src/actions/throttling.spec.ts`. - Write vitest tests for `useThrottledCallback` function in `packages/react-sketch-canvas/src/Canvas/__test__/utils.test.tsx`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/vinothpandian/react-sketch-canvas/issues/163?shareId=XXXX-XXXX-XXXX-XXXX).
* Add throttling to pointer move event Fixes #163 Add throttling to pointer move events in the Canvas component to optimize coordinate recording. - Modify `packages/react-sketch-canvas/src/Canvas/index.tsx` to add throttling to the `handlePointerMove` function using a custom `useThrottledCallback` function. - Add `throttleTime` to `CanvasProps` in `packages/react-sketch-canvas/src/Canvas/types.ts` with a default value of 20. - Add `throttleTime` to `ReactSketchCanvasProps` in `packages/react-sketch-canvas/src/ReactSketchCanvas/types.ts` with a default value of 20. - Add `useThrottledCallback` function in `packages/react-sketch-canvas/src/Canvas/utils.tsx` to throttle a callback function. - Update `README.md` to include documentation on the new `throttleTime` configuration option. - Add tests to verify throttling behavior in `packages/tests/src/actions/throttling.spec.ts`. - Write vitest tests for `useThrottledCallback` function in `packages/react-sketch-canvas/src/Canvas/__test__/utils.test.tsx`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/vinothpandian/react-sketch-canvas/issues/163?shareId=XXXX-XXXX-XXXX-XXXX). * fix: tests * fix: set default throttleTime to 0 * chore: remove lint in ci * fix: workflow
I had the same problem, but adding throttle function would cause some other problem if moving quickly. My current solution is to filter out the extra points when they get too close |
would you mind elaborate what is that problem caused by moving too fast? filtering the point if they get too close is also an viable approach, but you may also want to check if the Math.sqrt / sin cos tan do not have performance issue, i am not too sure about this part, but imo looks quite intense for an every ms operation, especially for mid to low end devices |
Don't worry fast problem, the main problem I have encountered is still the problem you described. I just simply judged the distance between two points, and ignored the point when it was less than a certain value |
Is your feature request related to a problem? Please describe.
(please correct me if the time is not correct)
which cause a problem of :
Describe the solution you'd like
- i have tried to add a interval inside thehandlePointerMove
function of<Canvas/>
Updated on 7 Aug
below is a result comparsion (please forgive my poor hand writing)
Draw Back
however, this solution have some draw back:the throtting ms is kind of limited if you are aiming for the best result.when drawing too fast, the path will 'wiggle', especially when the throttling is above 50 ms, i am still studying the rootcause, would love to have some advise if anyone have any insight, but i think this draw back is not critial1. the path is not going to change too much when wiggling (if throttling is under 20ms)2. personally think it look like a fun visual effect of drawing on the boardDescribe alternatives you've considered
smoothing
factor inside<Path/>
, but result is not as good as expected due to the coordinates are too closeAdditional context
but due to the 'wiggle' issue, maybe a fixed number just like thesmoothing
inside<Path/>
is also considerableThe text was updated successfully, but these errors were encountered: