-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e8b5a6
commit 6d354b6
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,70 @@ | ||
<script lang="ts"> | ||
import * as signaturePad from "@zag-js/signature-pad" | ||
import { normalizeProps, useMachine } from "@zag-js/svelte" | ||
import { signaturePadControls } from "@zag-js/shared" | ||
import { useControls } from "$lib/use-controls.svelte" | ||
import Toolbar from "$lib/components/toolbar.svelte" | ||
import { RotateCcw } from "lucide-svelte" | ||
const controls = useControls(signaturePadControls) | ||
let url = $state("") | ||
const setUrl = (value: string) => (url = value) | ||
const [_state, send] = useMachine( | ||
signaturePad.machine({ | ||
id: "1", | ||
onDrawEnd(details) { | ||
details.getDataUrl("image/png").then(setUrl) | ||
}, | ||
drawing: { | ||
fill: "red", | ||
size: 4, | ||
simulatePressure: true, | ||
}, | ||
}), | ||
{ | ||
context: controls.context, | ||
}, | ||
) | ||
const api = $derived(signaturePad.connect(_state, send, normalizeProps)) | ||
</script> | ||
|
||
<main class="signature-pad"> | ||
<div {...api.rootProps}> | ||
<!-- svelte-ignore a11y-label-has-associated-control --> | ||
<label {...api.labelProps}>Signature Pad</label> | ||
|
||
<div {...api.controlProps}> | ||
<svg {...api.layerProps}> | ||
{#each api.paths as path} | ||
<path {...api.getLayerPathProps({ path })} /> | ||
{/each} | ||
{#if api.currentPath} | ||
<path {...api.getLayerPathProps({ path: api.currentPath })} /> | ||
{/if} | ||
</svg> | ||
|
||
<div {...api.lineProps} /> | ||
</div> | ||
|
||
<button {...api.clearTriggerProps}> | ||
<RotateCcw /> | ||
</button> | ||
</div> | ||
|
||
<button | ||
onClick={() => { | ||
api.getDataUrl("image/png").then(setUrl) | ||
}} | ||
> | ||
Show Image | ||
</button> | ||
|
||
{#if url} | ||
<img data-part="preview" alt="signature" src={url} /> | ||
{/if} | ||
</main> | ||
|
||
<Toolbar {controls} state={_state} /> |