-
Notifications
You must be signed in to change notification settings - Fork 93
feat: Allow custom SVG path generation with tests #188
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
apps/docs/src/content/docs/examples/custom-path-generation.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| title: Custom Path Generation | ||
| description: Demonstrates how to provide a custom function to generate SVG path strings from points using the getSvgPathFromPoints prop. | ||
| --- | ||
| import { CustomPathGenerationExample } from "../../../examples/CustomPathGeneration"; | ||
|
|
||
| The `getSvgPathFromPoints` prop allows you to define your own logic for converting a series of points into an SVG path string. This gives you full control over the appearance of the strokes, enabling you to create straight lines, dashed lines, or any other custom path shape. | ||
|
|
||
| Below is an example that uses `getSvgPathFromPoints` to render dashed lines. | ||
|
|
||
| <CustomPathGenerationExample client:load /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| title: ReactSketchCanvas Props | ||
| description: Props for the ReactSketchCanvas component. | ||
| --- | ||
|
|
||
| The `ReactSketchCanvas` component accepts the following props: | ||
|
|
||
| - **`getSvgPathFromPoints`**: | ||
| - **Type**: `(points: Point[]) => string` | ||
| - **Description**: Optional. Callback to generate a custom SVG path string from points. This allows you to override the default bezier curve smoothing and implement any path generation logic you need. The function receives an array of `Point` objects and should return a valid SVG path string. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import React from "react"; | ||
| import { ReactSketchCanvas, Point } from "react-sketch-canvas"; | ||
|
|
||
| const styles = { | ||
| border: "0.0625rem solid #9c9c9c", | ||
| borderRadius: "0.25rem", | ||
| }; | ||
|
|
||
| const generateDashedLinePath = (points: Point[]): string => { | ||
| if (points.length < 2) { | ||
| return points.length === 1 ? `M ${points[0].x} ${points[0].y}` : ""; | ||
| } | ||
|
|
||
| let path = `M ${points[0].x} ${points[0].y}`; | ||
| const dashLength = 10; | ||
| const gapLength = 5; | ||
| let currentSegmentLength = 0; | ||
| let drawingDash = true; | ||
|
|
||
| for (let i = 1; i < points.length; i++) { | ||
| const p1 = points[i-1]; | ||
| const p2 = points[i]; | ||
| const segmentDx = p2.x - p1.x; | ||
| const segmentDy = p2.y - p1.y; | ||
| const segmentLength = Math.sqrt(segmentDx * segmentDx + segmentDy * segmentDy); | ||
|
|
||
| if (segmentLength === 0) continue; | ||
|
|
||
| const dxNormalized = segmentDx / segmentLength; | ||
| const dyNormalized = segmentDy / segmentLength; | ||
|
|
||
| let drawnLengthOnSegment = 0; | ||
| while(drawnLengthOnSegment < segmentLength) { | ||
| const lengthToDraw = drawingDash ? dashLength : gapLength; | ||
| const remainingLengthInCurrentPart = lengthToDraw - currentSegmentLength; | ||
|
|
||
| if (drawnLengthOnSegment + remainingLengthInCurrentPart >= segmentLength) { | ||
| // Finish this segment | ||
| if (drawingDash) { | ||
| path += ` L ${p2.x} ${p2.y}`; | ||
| } else { | ||
| path += ` M ${p2.x} ${p2.y}`; | ||
| } | ||
| currentSegmentLength += (segmentLength - drawnLengthOnSegment); | ||
| drawnLengthOnSegment = segmentLength; | ||
| } else { | ||
| // Draw part of dash/gap and switch | ||
| drawnLengthOnSegment += remainingLengthInCurrentPart; | ||
| currentSegmentLength = 0; | ||
|
|
||
| const currentX = p1.x + dxNormalized * drawnLengthOnSegment; | ||
| const currentY = p1.y + dyNormalized * drawnLengthOnSegment; | ||
|
|
||
| if (drawingDash) { | ||
| path += ` L ${currentX} ${currentY}`; | ||
| } else { | ||
| path += ` M ${currentX} ${currentY}`; | ||
| } | ||
| drawingDash = !drawingDash; | ||
| } | ||
| } | ||
| } | ||
| return path; | ||
| }; | ||
|
|
||
| const CustomPathGenerationExample = () => ( | ||
| <ReactSketchCanvas | ||
| style={styles} | ||
| width="100%" | ||
| height="400px" | ||
| strokeWidth={4} | ||
| strokeColor="blue" | ||
| getSvgPathFromPoints={generateDashedLinePath} | ||
| /> | ||
| ); | ||
|
|
||
| export default CustomPathGenerationExample; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { test, expect } from '@playwright/test'; | ||
|
|
||
| test.describe('ReactSketchCanvas - E2E Custom Path Generation', () => { | ||
| test('should render with straight line custom path generator', async ({ page }) => { | ||
| await page.goto('/?name=straightLine'); // Navigate to the new test case | ||
|
|
||
| const canvas = page.locator('#straight-line-canvas'); | ||
| await expect(canvas).toBeVisible(); | ||
|
|
||
| // Simulate drawing | ||
| const canvasBoundingBox = await canvas.boundingBox(); | ||
| if (!canvasBoundingBox) throw new Error('Canvas not found or not visible'); | ||
|
|
||
| await page.mouse.move(canvasBoundingBox.x + 10, canvasBoundingBox.y + 10); | ||
| await page.mouse.down(); | ||
| await page.mouse.move(canvasBoundingBox.x + 50, canvasBoundingBox.y + 50); | ||
| await page.mouse.move(canvasBoundingBox.x + 100, canvasBoundingBox.y + 20); | ||
| await page.mouse.up(); | ||
|
|
||
| // The path ID is constructed by the component like: {canvasId}__{strokeGroupIndex}__{pathIndex} | ||
| // For a single stroke, this should be {canvasId}__0__0 if using older versions, | ||
| // or more likely {canvasId}__stroke-group-0__paths__0 | ||
| // We'll use a selector that finds the path within the specific canvas. | ||
| const pathElement = page.locator('#straight-line-canvas path'); | ||
| await expect(pathElement).toHaveCount(1); // Ensure only one path is drawn | ||
| const dAttribute = await pathElement.getAttribute('d'); | ||
| expect(dAttribute).toBe('M 10 10 L 50 50 L 100 20'); | ||
| }); | ||
|
|
||
| test('should render with zig-zag line custom path generator', async ({ page }) => { | ||
| await page.goto('/?name=zigZagLine'); // Navigate to the other new test case | ||
|
|
||
| const canvas = page.locator('#zigzag-line-canvas'); | ||
| await expect(canvas).toBeVisible(); | ||
|
|
||
| const canvasBoundingBox = await canvas.boundingBox(); | ||
| if (!canvasBoundingBox) throw new Error('Canvas not found or not visible'); | ||
|
|
||
| await page.mouse.move(canvasBoundingBox.x + 10, canvasBoundingBox.y + 10); | ||
| await page.mouse.down(); | ||
| await page.mouse.move(canvasBoundingBox.x + 50, canvasBoundingBox.y + 50); | ||
| await page.mouse.move(canvasBoundingBox.x + 100, canvasBoundingBox.y + 20); | ||
| await page.mouse.up(); | ||
|
|
||
| const pathElement = page.locator('#zigzag-line-canvas path'); | ||
| await expect(pathElement).toHaveCount(1); | ||
| const dAttribute = await pathElement.getAttribute('d'); | ||
| // Expected path: M 10 10 L 50 50+5 L 100 20 | ||
| expect(dAttribute).toBe('M 10 10 L 50 55 L 100 20'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment references the prompt context and is not meaningful in the codebase. Remove it to keep the file clean.