@paperpic/vue 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/docs/examples/attach-and-scan.md +196 -0
- package/dist/docs/examples/custom-toolbar.md +183 -0
- package/dist/docs/guide/accessibility.md +1 -1
- package/dist/docs/guide/editing.md +6 -15
- package/dist/docs/guide/getting-started.md +83 -10
- package/dist/docs/guide/loading.md +128 -0
- package/dist/docs/guide/performance.md +17 -3
- package/dist/docs/guide/saving.md +6 -0
- package/dist/docs/guide/styling.md +2 -0
- package/dist/docs/guide/tools/adjust.md +2 -0
- package/dist/docs/guide/tools/crop.md +2 -0
- package/dist/docs/guide/tools/rotate-and-flip.md +3 -1
- package/dist/docs/guide/troubleshooting.md +45 -0
- package/dist/docs/guide/viewer.md +1 -1
- package/dist/docs/reference/events.md +11 -7
- package/dist/docs/reference/methods.md +8 -8
- package/dist/docs/reference/options.md +7 -7
- package/dist/docs/reference/react.md +11 -11
- package/dist/docs/reference/state.md +4 -4
- package/dist/docs/reference/types.md +301 -0
- package/dist/docs/reference/vue.md +10 -10
- package/dist/index.d.ts +10 -12
- package/package.json +3 -3
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Attach and scan
|
|
3
|
+
description: A complete example where the user takes a photo of a document or picks a file, straightens and cleans it up in PaperPic, and sends the result to your server, in plain JavaScript, Vue and React.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Attach and scan
|
|
7
|
+
|
|
8
|
+
Most apps that take documents ask the user for a photo of one, such as a receipt for an expense or a signed form. This example lets the user take the photo or pick a file, edit it in PaperPic, and send the result to your server.
|
|
9
|
+
|
|
10
|
+
`upload` is the function from [Upload to a server](./upload), which sends the image and fails when the server does not accept it.
|
|
11
|
+
|
|
12
|
+
:::tabs key:framework
|
|
13
|
+
== JavaScript
|
|
14
|
+
|
|
15
|
+
```html
|
|
16
|
+
<label>
|
|
17
|
+
Photo of the document
|
|
18
|
+
<input id="photo" type="file" accept="image/*" capture="environment" />
|
|
19
|
+
</label>
|
|
20
|
+
<div id="viewer" style="height: 480px"></div>
|
|
21
|
+
<p id="status" role="status"></p>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { PaperPic } from '@paperpic/core'
|
|
26
|
+
import '@paperpic/core/style.css'
|
|
27
|
+
|
|
28
|
+
const input = document.querySelector<HTMLInputElement>('#photo')!
|
|
29
|
+
const status = document.querySelector('#status')!
|
|
30
|
+
|
|
31
|
+
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
|
|
32
|
+
alt: 'The document you attached',
|
|
33
|
+
maxOutputSize: 3000,
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
input.addEventListener('change', () => {
|
|
37
|
+
const file = input.files?.[0]
|
|
38
|
+
if (file) {
|
|
39
|
+
status.textContent = ''
|
|
40
|
+
pic.setOptions({ src: file })
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// The edit starts as soon as the photo is on display
|
|
45
|
+
pic.on('load', () => pic.edit())
|
|
46
|
+
|
|
47
|
+
pic.on('error', ({ phase }) => {
|
|
48
|
+
if (phase === 'load') {
|
|
49
|
+
status.textContent = 'This file is not an image the browser can open'
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
pic.on('apply', async ({ blob, type }) => {
|
|
54
|
+
status.textContent = 'Sending…'
|
|
55
|
+
try {
|
|
56
|
+
await upload(blob, type)
|
|
57
|
+
status.textContent = 'Sent'
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
status.textContent = 'The document could not be sent'
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
== Vue
|
|
66
|
+
|
|
67
|
+
```vue
|
|
68
|
+
<script setup lang="ts">
|
|
69
|
+
import type { ExportedImage, PaperPicEvents } from '@paperpic/vue'
|
|
70
|
+
import { PaperPic } from '@paperpic/vue'
|
|
71
|
+
import { shallowRef, useTemplateRef } from 'vue'
|
|
72
|
+
import '@paperpic/vue/style.css'
|
|
73
|
+
|
|
74
|
+
const pic = useTemplateRef('pic')
|
|
75
|
+
const photo = shallowRef<File>()
|
|
76
|
+
const status = shallowRef('')
|
|
77
|
+
|
|
78
|
+
function choose(event: Event) {
|
|
79
|
+
const file = (event.target as HTMLInputElement).files?.[0]
|
|
80
|
+
if (file) {
|
|
81
|
+
status.value = ''
|
|
82
|
+
photo.value = file
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function failed({ phase }: PaperPicEvents['error']) {
|
|
87
|
+
if (phase === 'load') {
|
|
88
|
+
status.value = 'This file is not an image the browser can open'
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function send({ blob, type }: ExportedImage) {
|
|
93
|
+
status.value = 'Sending…'
|
|
94
|
+
try {
|
|
95
|
+
await upload(blob, type)
|
|
96
|
+
status.value = 'Sent'
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
status.value = 'The document could not be sent'
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
</script>
|
|
103
|
+
|
|
104
|
+
<template>
|
|
105
|
+
<label>
|
|
106
|
+
Photo of the document
|
|
107
|
+
<input type="file" accept="image/*" capture="environment" @change="choose">
|
|
108
|
+
</label>
|
|
109
|
+
<div v-if="photo" style="height: 480px">
|
|
110
|
+
<PaperPic
|
|
111
|
+
ref="pic"
|
|
112
|
+
:src="photo"
|
|
113
|
+
alt="The document you attached"
|
|
114
|
+
:max-output-size="3000"
|
|
115
|
+
@load="pic?.instance?.edit()"
|
|
116
|
+
@error="failed"
|
|
117
|
+
@apply="send"
|
|
118
|
+
/>
|
|
119
|
+
</div>
|
|
120
|
+
<p role="status">
|
|
121
|
+
{{ status }}
|
|
122
|
+
</p>
|
|
123
|
+
</template>
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
== React
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
import type { ExportedImage, PaperPicEvents, PaperPicInstance } from '@paperpic/react'
|
|
130
|
+
import { PaperPic } from '@paperpic/react'
|
|
131
|
+
import { useRef, useState } from 'react'
|
|
132
|
+
import '@paperpic/react/style.css'
|
|
133
|
+
|
|
134
|
+
export function AttachDocument() {
|
|
135
|
+
const picRef = useRef<PaperPicInstance>(null)
|
|
136
|
+
const [photo, setPhoto] = useState<File>()
|
|
137
|
+
const [status, setStatus] = useState('')
|
|
138
|
+
|
|
139
|
+
function choose(file: File | undefined) {
|
|
140
|
+
if (file) {
|
|
141
|
+
setStatus('')
|
|
142
|
+
setPhoto(file)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function failed({ phase }: PaperPicEvents['error']) {
|
|
147
|
+
if (phase === 'load') {
|
|
148
|
+
setStatus('This file is not an image the browser can open')
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function send({ blob, type }: ExportedImage) {
|
|
153
|
+
setStatus('Sending…')
|
|
154
|
+
try {
|
|
155
|
+
await upload(blob, type)
|
|
156
|
+
setStatus('Sent')
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
setStatus('The document could not be sent')
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<>
|
|
165
|
+
<label>
|
|
166
|
+
Photo of the document
|
|
167
|
+
<input type="file" accept="image/*" capture="environment" onChange={event => choose(event.target.files?.[0])} />
|
|
168
|
+
</label>
|
|
169
|
+
{photo && (
|
|
170
|
+
<div style={{ height: 480 }}>
|
|
171
|
+
<PaperPic
|
|
172
|
+
ref={picRef}
|
|
173
|
+
src={photo}
|
|
174
|
+
alt="The document you attached"
|
|
175
|
+
maxOutputSize={3000}
|
|
176
|
+
onLoad={() => picRef.current?.edit()}
|
|
177
|
+
onError={failed}
|
|
178
|
+
onApply={send}
|
|
179
|
+
/>
|
|
180
|
+
</div>
|
|
181
|
+
)}
|
|
182
|
+
<p role="status">{status}</p>
|
|
183
|
+
</>
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
:::
|
|
189
|
+
|
|
190
|
+
## Notes
|
|
191
|
+
|
|
192
|
+
- `capture="environment"` opens the back camera on phones, and computers ignore it. Without it, phones offer the camera and the photo library, which suits documents the user already has.
|
|
193
|
+
- The viewer opens in the edit mode once the photo loads, so the user goes straight to Straighten or Clean up. Leave out the `load` listener to show the photo first.
|
|
194
|
+
- `phase: 'load'` tells a file the browser cannot open, such as a HEIC photo outside Safari, from a failed edit. [Loading images](../guide/loading#formats) lists the formats.
|
|
195
|
+
- The photo stays in the browser until Apply calls `upload`. PaperPic sends nothing on its own.
|
|
196
|
+
- [`maxOutputSize`](../reference/options#maxoutputsize) keeps the upload small. Phone photos have 12 megapixels or more, and documents stay readable at 3000 pixels on the longest side.
|
|
@@ -203,6 +203,189 @@ export function Receipt() {
|
|
|
203
203
|
|
|
204
204
|
:::
|
|
205
205
|
|
|
206
|
+
## A slider of your own {#slider}
|
|
207
|
+
|
|
208
|
+
A slider of your own for Clean up opens the tool first. Each move then calls `setThreshold()`, which changes what Clean up shows, and `confirmTool()` keeps the last value as one edit. Here Done also applies the result, and Cancel leaves the edit mode, so these controls are the whole interface.
|
|
209
|
+
|
|
210
|
+
:::tabs key:framework
|
|
211
|
+
== JavaScript
|
|
212
|
+
|
|
213
|
+
```html
|
|
214
|
+
<button id="clean-up" type="button">Clean up</button>
|
|
215
|
+
<div id="ruler" hidden>
|
|
216
|
+
<label>
|
|
217
|
+
Sensitivity
|
|
218
|
+
<input id="sensitivity" type="range" min="0" max="1" step="0.05" />
|
|
219
|
+
</label>
|
|
220
|
+
<button id="done" type="button">Done</button>
|
|
221
|
+
<button id="cancel" type="button">Cancel</button>
|
|
222
|
+
</div>
|
|
223
|
+
<div id="viewer" style="height: 480px"></div>
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
```ts
|
|
227
|
+
import { PaperPic } from '@paperpic/core'
|
|
228
|
+
import '@paperpic/core/style.css'
|
|
229
|
+
|
|
230
|
+
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
|
|
231
|
+
src: '/receipt.jpg',
|
|
232
|
+
alt: 'A receipt',
|
|
233
|
+
toolbar: false,
|
|
234
|
+
})
|
|
235
|
+
const slider = document.querySelector<HTMLInputElement>('#sensitivity')!
|
|
236
|
+
|
|
237
|
+
document.querySelector('#clean-up')!.addEventListener('click', () => {
|
|
238
|
+
pic.edit()
|
|
239
|
+
pic.openTool('threshold')
|
|
240
|
+
})
|
|
241
|
+
slider.addEventListener('input', () => pic.setThreshold({ sensitivity: slider.valueAsNumber }))
|
|
242
|
+
document.querySelector('#done')!.addEventListener('click', () => {
|
|
243
|
+
pic.confirmTool()
|
|
244
|
+
pic.apply()
|
|
245
|
+
})
|
|
246
|
+
document.querySelector('#cancel')!.addEventListener('click', () => {
|
|
247
|
+
pic.cancelTool()
|
|
248
|
+
pic.cancel()
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
pic.on('change', ({ activeTool, threshold }) => {
|
|
252
|
+
const open = activeTool === 'threshold'
|
|
253
|
+
document.querySelector<HTMLElement>('#ruler')!.hidden = !open
|
|
254
|
+
document.querySelector<HTMLElement>('#clean-up')!.hidden = open
|
|
255
|
+
slider.value = String(threshold.sensitivity)
|
|
256
|
+
})
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
== Vue
|
|
260
|
+
|
|
261
|
+
```vue
|
|
262
|
+
<script setup lang="ts">
|
|
263
|
+
import type { PaperPicState } from '@paperpic/vue'
|
|
264
|
+
import { PaperPic } from '@paperpic/vue'
|
|
265
|
+
import { computed, shallowRef, useTemplateRef } from 'vue'
|
|
266
|
+
import '@paperpic/vue/style.css'
|
|
267
|
+
|
|
268
|
+
const pic = useTemplateRef<InstanceType<typeof PaperPic>>('pic')
|
|
269
|
+
const viewer = computed(() => pic.value?.instance)
|
|
270
|
+
const state = shallowRef<PaperPicState>()
|
|
271
|
+
|
|
272
|
+
function open() {
|
|
273
|
+
viewer.value?.edit()
|
|
274
|
+
viewer.value?.openTool('threshold')
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function slide(event: Event) {
|
|
278
|
+
viewer.value?.setThreshold({ sensitivity: (event.target as HTMLInputElement).valueAsNumber })
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function done() {
|
|
282
|
+
viewer.value?.confirmTool()
|
|
283
|
+
viewer.value?.apply()
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function cancel() {
|
|
287
|
+
viewer.value?.cancelTool()
|
|
288
|
+
viewer.value?.cancel()
|
|
289
|
+
}
|
|
290
|
+
</script>
|
|
291
|
+
|
|
292
|
+
<template>
|
|
293
|
+
<div v-if="state?.activeTool === 'threshold'">
|
|
294
|
+
<label>
|
|
295
|
+
Sensitivity
|
|
296
|
+
<input type="range" min="0" max="1" step="0.05" :value="state.threshold.sensitivity" @input="slide">
|
|
297
|
+
</label>
|
|
298
|
+
<button type="button" @click="done">
|
|
299
|
+
Done
|
|
300
|
+
</button>
|
|
301
|
+
<button type="button" @click="cancel">
|
|
302
|
+
Cancel
|
|
303
|
+
</button>
|
|
304
|
+
</div>
|
|
305
|
+
<button v-else type="button" :disabled="state?.status !== 'ready'" @click="open">
|
|
306
|
+
Clean up
|
|
307
|
+
</button>
|
|
308
|
+
<div style="height: 480px">
|
|
309
|
+
<PaperPic
|
|
310
|
+
ref="pic"
|
|
311
|
+
src="/receipt.jpg"
|
|
312
|
+
alt="A receipt"
|
|
313
|
+
:toolbar="false"
|
|
314
|
+
@change="next => (state = next)"
|
|
315
|
+
@load="state = viewer?.getState()"
|
|
316
|
+
/>
|
|
317
|
+
</div>
|
|
318
|
+
</template>
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
== React
|
|
322
|
+
|
|
323
|
+
```tsx
|
|
324
|
+
import type { PaperPicInstance, PaperPicState } from '@paperpic/react'
|
|
325
|
+
import { PaperPic } from '@paperpic/react'
|
|
326
|
+
import { useRef, useState } from 'react'
|
|
327
|
+
import '@paperpic/react/style.css'
|
|
328
|
+
|
|
329
|
+
export function CleanUpRuler() {
|
|
330
|
+
const picRef = useRef<PaperPicInstance>(null)
|
|
331
|
+
const [state, setState] = useState<PaperPicState>()
|
|
332
|
+
const viewer = () => picRef.current!
|
|
333
|
+
|
|
334
|
+
function open() {
|
|
335
|
+
viewer().edit()
|
|
336
|
+
viewer().openTool('threshold')
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function done() {
|
|
340
|
+
viewer().confirmTool()
|
|
341
|
+
viewer().apply()
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function cancel() {
|
|
345
|
+
viewer().cancelTool()
|
|
346
|
+
viewer().cancel()
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return (
|
|
350
|
+
<>
|
|
351
|
+
{state?.activeTool === 'threshold'
|
|
352
|
+
? (
|
|
353
|
+
<div>
|
|
354
|
+
<label>
|
|
355
|
+
Sensitivity
|
|
356
|
+
<input
|
|
357
|
+
type="range"
|
|
358
|
+
min="0"
|
|
359
|
+
max="1"
|
|
360
|
+
step="0.05"
|
|
361
|
+
value={state.threshold.sensitivity}
|
|
362
|
+
onChange={event => viewer().setThreshold({ sensitivity: event.target.valueAsNumber })}
|
|
363
|
+
/>
|
|
364
|
+
</label>
|
|
365
|
+
<button type="button" onClick={done}>Done</button>
|
|
366
|
+
<button type="button" onClick={cancel}>Cancel</button>
|
|
367
|
+
</div>
|
|
368
|
+
)
|
|
369
|
+
: (
|
|
370
|
+
<button type="button" disabled={state?.status !== 'ready'} onClick={open}>Clean up</button>
|
|
371
|
+
)}
|
|
372
|
+
<div style={{ height: 480 }}>
|
|
373
|
+
<PaperPic
|
|
374
|
+
ref={picRef}
|
|
375
|
+
src="/receipt.jpg"
|
|
376
|
+
alt="A receipt"
|
|
377
|
+
toolbar={false}
|
|
378
|
+
onChange={setState}
|
|
379
|
+
onLoad={() => setState(viewer().getState())}
|
|
380
|
+
/>
|
|
381
|
+
</div>
|
|
382
|
+
</>
|
|
383
|
+
)
|
|
384
|
+
}
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
:::
|
|
388
|
+
|
|
206
389
|
## Notes
|
|
207
390
|
|
|
208
391
|
- With `toolbar: false`, the built-in edit bar is hidden too, so Crop and Straighten show their handles but no Done button. Your interface confirms them with [`confirmTool()`](../reference/methods#confirmtool), as `Enter` does.
|
|
@@ -14,7 +14,7 @@ People read and fix documents with a mouse, a finger, the keyboard or a screen r
|
|
|
14
14
|
- **Your own controls, if any.** With `toolbar: false`, the buttons you build replace the built-in ones, and they need their own names and keyboard support.
|
|
15
15
|
- **A visible focus.** Keep your CSS from hiding the focus outline of the viewer.
|
|
16
16
|
|
|
17
|
-
## Keyboard
|
|
17
|
+
## Keyboard {#keyboard}
|
|
18
18
|
|
|
19
19
|
With the focus on the viewer:
|
|
20
20
|
|
|
@@ -82,11 +82,11 @@ Apply cannot be interrupted. It blocks the controls and shows a message while it
|
|
|
82
82
|
|
|
83
83
|
## What your code receives
|
|
84
84
|
|
|
85
|
-
| Event | When
|
|
86
|
-
| -------- |
|
|
87
|
-
| `apply` | When the result is ready, with `{ blob, width, height, type }`
|
|
88
|
-
| `change` | After each action of the user
|
|
89
|
-
| `error` | When
|
|
85
|
+
| Event | When |
|
|
86
|
+
| -------- | -------------------------------------------------------------------------- |
|
|
87
|
+
| `apply` | When the result is ready, with `{ blob, width, height, type }` |
|
|
88
|
+
| `change` | After each action of the user that changes the [state](../reference/state) |
|
|
89
|
+
| `error` | When the image cannot be loaded, or an edit cannot be shown or applied |
|
|
90
90
|
|
|
91
91
|
:::tabs key:framework
|
|
92
92
|
== JavaScript
|
|
@@ -128,13 +128,4 @@ After Apply, passing the same `src` again does nothing, so the result stays on d
|
|
|
128
128
|
|
|
129
129
|
## Keyboard
|
|
130
130
|
|
|
131
|
-
With the focus on the viewer
|
|
132
|
-
|
|
133
|
-
| Key | Action |
|
|
134
|
-
| -------------------------- | ------------------------------------------------ |
|
|
135
|
-
| `Ctrl+Z` | Undo |
|
|
136
|
-
| `Ctrl+Shift+Z` or `Ctrl+Y` | Redo |
|
|
137
|
-
| `Enter` | Confirm the open tool, or else apply the changes |
|
|
138
|
-
| `Esc` | Close the open tool, or else cancel the changes |
|
|
139
|
-
|
|
140
|
-
On macOS, `Cmd` works like `Ctrl`.
|
|
131
|
+
With the focus on the viewer, keys undo, redo, confirm and cancel. [Accessibility](./accessibility#keyboard) lists every key.
|
|
@@ -9,31 +9,95 @@ This page takes you from installing PaperPic to showing an image and getting the
|
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
::::tabs key:framework
|
|
13
13
|
== JavaScript
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
::: code-group
|
|
16
|
+
|
|
17
|
+
```sh [npm]
|
|
18
|
+
npm install @paperpic/core
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```sh [pnpm]
|
|
16
22
|
pnpm add @paperpic/core
|
|
17
23
|
```
|
|
18
24
|
|
|
25
|
+
```sh [yarn]
|
|
26
|
+
yarn add @paperpic/core
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```sh [bun]
|
|
30
|
+
bun add @paperpic/core
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
:::
|
|
34
|
+
|
|
19
35
|
== Vue
|
|
20
36
|
|
|
21
|
-
|
|
37
|
+
::: code-group
|
|
38
|
+
|
|
39
|
+
```sh [npm]
|
|
40
|
+
npm install @paperpic/vue
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```sh [pnpm]
|
|
22
44
|
pnpm add @paperpic/vue
|
|
23
45
|
```
|
|
24
46
|
|
|
47
|
+
```sh [yarn]
|
|
48
|
+
yarn add @paperpic/vue
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```sh [bun]
|
|
52
|
+
bun add @paperpic/vue
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
:::
|
|
56
|
+
|
|
25
57
|
`@paperpic/vue` requires Vue 3.5 or newer.
|
|
26
58
|
|
|
27
59
|
== React
|
|
28
60
|
|
|
29
|
-
|
|
61
|
+
::: code-group
|
|
62
|
+
|
|
63
|
+
```sh [npm]
|
|
64
|
+
npm install @paperpic/react
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```sh [pnpm]
|
|
30
68
|
pnpm add @paperpic/react
|
|
31
69
|
```
|
|
32
70
|
|
|
33
|
-
|
|
71
|
+
```sh [yarn]
|
|
72
|
+
yarn add @paperpic/react
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
```sh [bun]
|
|
76
|
+
bun add @paperpic/react
|
|
77
|
+
```
|
|
34
78
|
|
|
35
79
|
:::
|
|
36
80
|
|
|
81
|
+
`@paperpic/react` requires React 19 or newer.
|
|
82
|
+
|
|
83
|
+
::::
|
|
84
|
+
|
|
85
|
+
### Without a bundler {#cdn}
|
|
86
|
+
|
|
87
|
+
The core also loads from a CDN that serves the files of the package as they are, such as jsDelivr. Import `dist/index.js` from a module script, and link `dist/style.css`:
|
|
88
|
+
|
|
89
|
+
```html
|
|
90
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@paperpic/core@0.3/dist/style.css" />
|
|
91
|
+
<div id="viewer" style="height: 480px"></div>
|
|
92
|
+
<script type="module">
|
|
93
|
+
import { PaperPic } from 'https://cdn.jsdelivr.net/npm/@paperpic/core@0.3/dist/index.js'
|
|
94
|
+
|
|
95
|
+
const pic = new PaperPic(document.querySelector('#viewer'), { src: '/receipt.jpg', alt: 'A receipt' })
|
|
96
|
+
</script>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Browsers only start a worker from the origin of the page, so the editing runs on the main thread there: it works, but large photos freeze the page for a moment on Apply. Serve the files from your own origin, or use a bundler, to keep the worker. The Vue and React components need a bundler.
|
|
100
|
+
|
|
37
101
|
## Show an image
|
|
38
102
|
|
|
39
103
|
The viewer fills its container, so give the container a height. Import the stylesheet once in your app.
|
|
@@ -87,7 +151,7 @@ export function Receipt() {
|
|
|
87
151
|
|
|
88
152
|
:::
|
|
89
153
|
|
|
90
|
-
`src` takes a URL from the same origin as your page, or a `File` that the user picks, such as from an `<input type="file">`. `alt` describes the image for screen readers.
|
|
154
|
+
`src` takes a URL from the same origin as your page, or a `File` that the user picks, such as from an `<input type="file">`. `alt` describes the image for screen readers. [Loading images](./loading) covers images from other origins, formats and load errors.
|
|
91
155
|
|
|
92
156
|
## Get the edited image
|
|
93
157
|
|
|
@@ -126,19 +190,28 @@ async function upload(blob: Blob) {
|
|
|
126
190
|
|
|
127
191
|
The `blob` keeps the format of the source, or is PNG when the user cleaned up the document. The [`output`](../reference/options#output) option changes it.
|
|
128
192
|
|
|
193
|
+
## Remove the viewer {#destroy}
|
|
194
|
+
|
|
195
|
+
The Vue and React components create the viewer when they mount and destroy it when they unmount. With the core, call [`destroy()`](../reference/methods#destroy) when you take the viewer out of the page, such as when the route changes:
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
pic.destroy()
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
It removes the viewer from its container, stops its listeners and its worker, and frees the memory of the image. To show it again, create a new one.
|
|
202
|
+
|
|
129
203
|
## Only show the image
|
|
130
204
|
|
|
131
205
|
To show a document that people should not change, turn editing off with `editable: false`. The [Viewer](./viewer) page shows it at work.
|
|
132
206
|
|
|
133
207
|
## Requirements
|
|
134
208
|
|
|
135
|
-
|
|
136
|
-
- The editing runs in a Web Worker, which bundlers such as Vite and webpack pick up on their own. If the worker cannot start, the editing runs on the main thread: it still works, but the page can freeze for a moment with large images.
|
|
137
|
-
- With a Content Security Policy, allow the worker with `worker-src 'self'`, and allow `img-src blob:`, because the viewer shows attached files and results through `blob:` URLs.
|
|
138
|
-
- With server-side rendering, such as in Nuxt or Next.js, the Vue and React components render an empty element on the server and create the viewer in the browser. With the core, create the viewer in the browser yourself.
|
|
209
|
+
PaperPic works in the browsers of [Baseline Widely Available](https://web.dev/baseline). [Performance and support](./performance) covers the worker, Content Security Policy, server-side rendering and large photos.
|
|
139
210
|
|
|
140
211
|
## Next steps
|
|
141
212
|
|
|
213
|
+
- [Loading images](./loading): where the image comes from, and which formats open.
|
|
142
214
|
- [Viewer](./viewer): zoom, fit and read-only use.
|
|
143
215
|
- [Editing](./editing): the edit mode, the tools, Undo and Apply.
|
|
144
216
|
- [Options](../reference/options): everything you can configure.
|
|
217
|
+
- [Troubleshooting](./troubleshooting): when the viewer does not look or work as it should.
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Loading images
|
|
3
|
+
description: How to give PaperPic an image, from a URL, a file the user picks or a Blob you fetch, which formats it opens, and how your code learns that the image loaded or failed.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Loading images
|
|
7
|
+
|
|
8
|
+
PaperPic shows the image that you pass in `src`. This page covers where that image can come from, which formats open, and how your code learns that the image is ready or that it failed.
|
|
9
|
+
|
|
10
|
+
## Where the image comes from
|
|
11
|
+
|
|
12
|
+
`src` takes one of three things:
|
|
13
|
+
|
|
14
|
+
- **A URL from the same origin as your page**, such as `/documents/42.jpg`.
|
|
15
|
+
- **A `File`** that the user picks with `<input type="file">`. [Attach and scan](../examples/attach-and-scan) shows the whole flow, from the camera to the upload.
|
|
16
|
+
- **A `Blob`** that your code holds, such as the body of a response.
|
|
17
|
+
|
|
18
|
+
For a `File` or a `Blob`, PaperPic creates the `blob:` URL it needs, and frees it when the image changes or the viewer is destroyed.
|
|
19
|
+
|
|
20
|
+
## Images from another origin, or behind a login {#fetch}
|
|
21
|
+
|
|
22
|
+
PaperPic reads the pixels of the image to edit it. Browsers only allow that for an image from another origin when it arrives with CORS, which PaperPic does not request. Such an image shows, but the first edit fails with an [`error`](../reference/events#error) that says so.
|
|
23
|
+
|
|
24
|
+
Fetch the image yourself, and pass the `Blob` as `src`. This also covers images behind a login, because your request carries the cookies of the user:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
async function fetchImage(url: string): Promise<Blob> {
|
|
28
|
+
const response = await fetch(url, { credentials: 'include' })
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw new Error(`The image could not be fetched: status ${response.status}`)
|
|
31
|
+
}
|
|
32
|
+
return response.blob()
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The other server must allow your origin with CORS, as for any `fetch` from the page.
|
|
37
|
+
|
|
38
|
+
## Formats {#formats}
|
|
39
|
+
|
|
40
|
+
PaperPic opens every format that the browser shows in an `<img>`:
|
|
41
|
+
|
|
42
|
+
- **JPEG, PNG, WebP and AVIF** open in every [supported browser](./performance#browser-support).
|
|
43
|
+
- **SVG** opens when the file has `width` and `height` attributes. Without them, it has no size, and the load fails.
|
|
44
|
+
- **HEIC**, the format of iPhone photos, only opens in Safari. When an iPhone user picks a photo with `<input type="file" accept="image/*">`, iOS usually hands the page a JPEG copy. A HEIC file that reaches another browser in another way fails to load.
|
|
45
|
+
|
|
46
|
+
Phones often save a photo sideways, with an EXIF tag that says how to turn it. PaperPic shows the photo the way the tag says, as an `<img>` does, and the result of Apply comes out upright.
|
|
47
|
+
|
|
48
|
+
## Know when it loads or fails
|
|
49
|
+
|
|
50
|
+
The [`load`](../reference/events#load) event reports that the image is on display, with its size. If the image cannot load, because it is missing, it is not an image or the browser cannot decode it, the viewer shows a message, and the [`error`](../reference/events#error) event carries `phase: 'load'`:
|
|
51
|
+
|
|
52
|
+
:::tabs key:framework
|
|
53
|
+
== JavaScript
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { PaperPic } from '@paperpic/core'
|
|
57
|
+
|
|
58
|
+
const pic = new PaperPic(container, { src: '/documents/42.jpg', alt: 'Invoice 42' })
|
|
59
|
+
|
|
60
|
+
pic.on('load', ({ width, height }) => {
|
|
61
|
+
console.log(`The image is ${width}×${height} pixels`)
|
|
62
|
+
})
|
|
63
|
+
pic.on('error', ({ error, phase }) => {
|
|
64
|
+
if (phase === 'load') {
|
|
65
|
+
console.error(error.message)
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
== Vue
|
|
71
|
+
|
|
72
|
+
```vue
|
|
73
|
+
<script setup lang="ts">
|
|
74
|
+
import type { PaperPicEvents } from '@paperpic/vue'
|
|
75
|
+
import { PaperPic } from '@paperpic/vue'
|
|
76
|
+
|
|
77
|
+
function loaded({ width, height }: PaperPicEvents['load']) {
|
|
78
|
+
console.log(`The image is ${width}×${height} pixels`)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function failed({ error, phase }: PaperPicEvents['error']) {
|
|
82
|
+
if (phase === 'load') {
|
|
83
|
+
console.error(error.message)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
</script>
|
|
87
|
+
|
|
88
|
+
<template>
|
|
89
|
+
<div style="height: 480px">
|
|
90
|
+
<PaperPic src="/documents/42.jpg" alt="Invoice 42" @load="loaded" @error="failed" />
|
|
91
|
+
</div>
|
|
92
|
+
</template>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
== React
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
import type { PaperPicEvents } from '@paperpic/react'
|
|
99
|
+
import { PaperPic } from '@paperpic/react'
|
|
100
|
+
|
|
101
|
+
function loaded({ width, height }: PaperPicEvents['load']) {
|
|
102
|
+
console.log(`The image is ${width}×${height} pixels`)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function failed({ error, phase }: PaperPicEvents['error']) {
|
|
106
|
+
if (phase === 'load') {
|
|
107
|
+
console.error(error.message)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function Invoice() {
|
|
112
|
+
return (
|
|
113
|
+
<div style={{ height: 480 }}>
|
|
114
|
+
<PaperPic src="/documents/42.jpg" alt="Invoice 42" onLoad={loaded} onError={failed} />
|
|
115
|
+
</div>
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
:::
|
|
121
|
+
|
|
122
|
+
The `status` of [`getState()`](../reference/state) gives the same answer at any time: `'empty'`, `'loading'`, `'ready'` or `'error'`.
|
|
123
|
+
|
|
124
|
+
## Change or reload the image
|
|
125
|
+
|
|
126
|
+
- **A new `src`** loads the new image. While the user is editing, it discards the edits.
|
|
127
|
+
- **The same `src`** does nothing, so the result of Apply stays on display.
|
|
128
|
+
- **[`reload()`](../reference/methods#reload)** loads the current `src` again, which brings back the original after an Apply.
|