Skip to content

Commit

Permalink
chore: add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vinothpandian committed Feb 20, 2024
1 parent b798782 commit bcbb141
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 8 deletions.
4 changes: 2 additions & 2 deletions apps/docs/src/components/LiveCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface LiveCodeProps {
export function LiveCode({
files,
editorClassName = "h-64",
previewClassName = "h-64",
previewClassName = "h-80",
}: LiveCodeProps) {
return (
<div className="reset-wrapper mt-4">
Expand All @@ -42,7 +42,7 @@ export function LiveCode({
>
<SandpackCodeEditor showTabs />
</SandpackLayout>
<div className="rounded-b-lg bg-zinc-900 p-4">
<div className="rounded-b-lg bg-zinc-900 p-2">
<div className="overflow-hidden rounded bg-white p-1">
<SandpackPreview
className={previewClassName}
Expand Down
15 changes: 15 additions & 0 deletions apps/docs/src/content/docs/examples/background.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Background
description: Setting up background of the canvas.
sidebar:
order: 5
---
import { LiveCode } from "../../../components/LiveCode";
import Background from "../../../examples/Background.tsx?raw";
import BackgroundDataURI from "../../../examples/BackgroundDataURI.tsx?raw";

<LiveCode client:load previewClassName="h-96" files={{ "App.tsx": Background }} />

## Data URI as Background

<LiveCode client:load previewClassName="h-96" files={{ "App.tsx": BackgroundDataURI }} />
11 changes: 11 additions & 0 deletions apps/docs/src/content/docs/examples/sketching-time.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Sketching time
description: Measuring sketching time
sidebar:
order: 6
---

import { LiveCode } from "../../../components/LiveCode";
import SketchingTime from "../../../examples/SketchingTime.tsx?raw";

<LiveCode client:load files={{ "App.tsx": SketchingTime }} />
10 changes: 10 additions & 0 deletions apps/docs/src/content/docs/examples/undo-redo.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Undo, Redo, Clear, and Reset
description: Undo, Redo, Clear, and Reset the canvas.
sidebar:
order: 4
---
import { LiveCode } from "../../../components/LiveCode";
import UndoRedo from "../../../examples/UndoRedo.tsx?raw";

<LiveCode client:load previewClassName="h-96" files={{ "App.tsx": UndoRedo }} />
3 changes: 2 additions & 1 deletion apps/docs/src/content/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ hero:

import { Card, CardGrid } from "@astrojs/starlight/components";
import { HomePageDemo } from "../../components/HomePageDemo";
import { Link } from "@astrojs/check";

<HomePageDemo client:load />

## Features

<CardGrid>
<Card title="Multi input support" icon="pencil">
Accepts input from Mouse, touch, and graphic tablets.
Accepts input from Mouse, touch, and graphic tablets. Example <a href="/react-sketch-canvas/examples/input-devices/">here</a>.
</Card>
<Card title="Vector-first drawing" icon="add-document">
Freehand drawing with vector output with PNG & JPEG export.
Expand Down
79 changes: 79 additions & 0 deletions apps/docs/src/examples/Background.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ReactSketchCanvas } from "react-sketch-canvas";
import { type ChangeEvent, useState } from "react";

const somePreserveAspectRatio = [
"none",
"xMinYMin",
"xMidYMin",
"xMaxYMin",
"xMinYMid",
"xMidYMid",
"xMaxYMid",
"xMinYMax",
"xMidYMax",
"xMaxYMax",
] as const;

type SomePreserveAspectRatio = (typeof somePreserveAspectRatio)[number];

export default function App() {
const [backgroundImage, setBackgroundImage] = useState(
"https://images.pexels.com/photos/1193743/pexels-photo-1193743.jpeg?cs=srgb&fm=jpg",
);
const [preserveAspectRatio, setPreserveAspectRatio] =
useState<SomePreserveAspectRatio>("none");

const handlePreserveAspectRatioChange = (
event: ChangeEvent<HTMLSelectElement>,
) => {
setPreserveAspectRatio(event.target.value as SomePreserveAspectRatio);
};

const handleBackgroundImageChange = (
event: ChangeEvent<HTMLInputElement>,
) => {
setBackgroundImage(event.target.value);
};

return (
<div className="d-flex flex-column gap-2 p-2">
<h1>Tools</h1>
<div className="d-flex gap-2 flex-column">
<div className="mb-3">
<label htmlFor="backgroundImage" className="form-label">
Background Image
</label>
<input
type="text"
className="form-control"
id="backgroundImage"
placeholder="URL of the image to use as a background"
value={backgroundImage}
onChange={handleBackgroundImageChange}
/>
</div>
<label htmlFor="preserveAspectRatio" className="form-label">
Preserve Aspect Ratio
</label>
<select
id="preserveAspectRatio"
className="form-select form-select-sm"
aria-label="Preserve Aspect Ratio options"
value={preserveAspectRatio}
onChange={handlePreserveAspectRatioChange}
>
{somePreserveAspectRatio.map((value) => (
<option key={value} value={value}>
{value}
</option>
))}
</select>
</div>
<h1>Canvas</h1>
<ReactSketchCanvas
backgroundImage={backgroundImage}
preserveBackgroundImageAspectRatio={preserveAspectRatio}
/>
</div>
);
}
79 changes: 79 additions & 0 deletions apps/docs/src/examples/BackgroundDataURI.tsx

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/docs/src/examples/Colors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function App() {
};

return (
<div className="d-flex flex-column gap-2">
<div className="d-flex flex-column gap-2 p-2">
<h1>Tools</h1>
<div className="d-flex gap-2 align-items-center ">
<label htmlFor="color">Stroke color</label>
Expand Down
8 changes: 5 additions & 3 deletions apps/docs/src/examples/Drawing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ export default function App() {
};

return (
<div className="d-flex flex-column gap-2">
<div className="d-flex flex-column gap-2 p-2">
<h1>Tools</h1>
<div className="d-flex gap-2 align-items-center ">
<button
className="btn btn-sm btn-primary"
type="button"
className="btn btn-sm btn-outline-primary"
disabled={!eraseMode}
onClick={handlePenClick}
>
Pen
</button>
<button
className="btn btn-sm btn-primary"
type="button"
className="btn btn-sm btn-outline-primary"
disabled={eraseMode}
onClick={handleEraserClick}
>
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/examples/ReadOnly.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function App() {
};

return (
<div className="d-flex flex-column gap-2">
<div className="d-flex flex-column gap-2 p-2">
<h1>Tools</h1>
<div className="d-flex gap-2 align-items-center ">
<div className="form-check form-switch">
Expand Down
35 changes: 35 additions & 0 deletions apps/docs/src/examples/SketchingTime.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
ReactSketchCanvas,
type ReactSketchCanvasRef,
} from "react-sketch-canvas";
import { useRef, useState } from "react";

export default function App() {
const canvasRef = useRef<ReactSketchCanvasRef>(null);
const [sketchingTime, setSketchingTime] = useState(0);

const handleSketchingTime = async () => {
const time = (await canvasRef.current?.getSketchingTime()) || 0;
setSketchingTime(time);
};

const sketchingTimeInSeconds = (sketchingTime / 1_000).toLocaleString();

return (
<div className="d-flex flex-column gap-2 p-2">
<h1>Tools</h1>
<div className="d-flex gap-2 align-items-center ">
<button
type="button"
className="btn btn-sm btn-outline-primary"
onClick={handleSketchingTime}
>
Get Sketching Time
</button>
<span>{sketchingTimeInSeconds} seconds</span>
</div>
<h1>Canvas</h1>
<ReactSketchCanvas ref={canvasRef} withTimestamp />
</div>
);
}
91 changes: 91 additions & 0 deletions apps/docs/src/examples/UndoRedo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
ReactSketchCanvas,
type ReactSketchCanvasRef,
} from "react-sketch-canvas";
import { useRef, useState } from "react";

export default function App() {
const canvasRef = useRef<ReactSketchCanvasRef>(null);
const [eraseMode, setEraseMode] = useState(false);

const handleEraserClick = () => {
setEraseMode(true);
canvasRef.current?.eraseMode(true);
};

const handlePenClick = () => {
setEraseMode(false);
canvasRef.current?.eraseMode(false);
};

const handleUndoClick = () => {
canvasRef.current?.undo();
};

const handleRedoClick = () => {
canvasRef.current?.redo();
};

const handleClearClick = () => {
canvasRef.current?.clearCanvas();
};

const handleResetClick = () => {
canvasRef.current?.resetCanvas();
};

return (
<div className="d-flex flex-column gap-2 p-2">
<h1>Tools</h1>
<div className="d-flex gap-2 align-items-center ">
<button
type="button"
className="btn btn-sm btn-outline-primary"
disabled={!eraseMode}
onClick={handlePenClick}
>
Pen
</button>
<button
type="button"
className="btn btn-sm btn-outline-primary"
disabled={eraseMode}
onClick={handleEraserClick}
>
Eraser
</button>
<div className="vr" />
<button
type="button"
className="btn btn-sm btn-outline-primary"
onClick={handleUndoClick}
>
Undo
</button>
<button
type="button"
className="btn btn-sm btn-outline-primary"
onClick={handleRedoClick}
>
Redo
</button>
<button
type="button"
className="btn btn-sm btn-outline-primary"
onClick={handleClearClick}
>
Clear
</button>
<button
type="button"
className="btn btn-sm btn-outline-primary"
onClick={handleResetClick}
>
Reset
</button>
</div>
<h1>Canvas</h1>
<ReactSketchCanvas ref={canvasRef} />
</div>
);
}
3 changes: 3 additions & 0 deletions packages/react-sketch-canvas/src/ReactSketchCanvas/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export interface ReactSketchCanvasRef extends CanvasRef {
loadPaths: (paths: CanvasPath[]) => void;
/**
* Get the current drawing time in milliseconds. This will only work if withTimestamp prop is set to true.
*
* @remarks
* This does not include the idle time when the user is not drawing. It only includes the time when the user is drawing on the canvas.
*/
getSketchingTime: () => Promise<number>;
/**
Expand Down

0 comments on commit bcbb141

Please sign in to comment.