Vue
@paperpic/vue wraps the viewer in a component for Vue 3.5 or newer. The options are props, the events are component events, and the methods are on the instance that the component exposes.
<script setup lang="ts">
import type { ExportedImage } from '@paperpic/vue'
import { PaperPic } from '@paperpic/vue'
import '@paperpic/vue/style.css'
function upload({ blob }: ExportedImage) {
// Send the edited image to your server
}
</script>
<template>
<div style="height: 480px">
<PaperPic src="/receipt.jpg" alt="A receipt" @apply="upload" />
</div>
</template>The component fills its parent element, so give the parent a height.
Props
Every option is a prop with the same name, such as :editable="false" or :aspect-ratio="210 / 297". A change of a prop reaches the viewer through setOptions(). class and style go to the element that holds the viewer, which also has the class paperpic-host.
An object or array with the same contents as before, such as :tools="['crop', 'rotate']", changes nothing, so the template can build them. A function is new on every render of the parent, so build labels that hold functions, such as zoomLevel, with computed.
Events
| Event | Payload |
|---|---|
apply | ExportedImage |
change | PaperPicState |
download | PaperPicDownload |
error | { error, phase } |
load | Size |
Events describes each one. A change of a prop never emits change. State shows how to keep the state in a shallowRef with @change, @load and @error.
Methods
The component exposes the viewer as instance, with the methods, such as zoomIn() or apply(). It is null until the component mounts:
<script setup lang="ts">
import { PaperPic } from '@paperpic/vue'
import { useTemplateRef } from 'vue'
const pic = useTemplateRef('pic')
</script>
<template>
<button type="button" @click="pic?.instance?.zoomIn()">
Zoom in
</button>
<div style="height: 480px">
<PaperPic ref="pic" src="/receipt.jpg" :toolbar="false" />
</div>
</template>The component creates the viewer when it mounts and destroys it when it unmounts, so do not call destroy(). Change the props instead of calling setOptions(), which the next change of a prop would undo, and use the component events instead of on().
Passing the same src again does not reload the image, so it keeps the result of an edit. To bring back the original, call instance.reload().
Types
@paperpic/vue exports every type of @paperpic/core, so you do not need to install it: Types shows each one. It also exports the types of the component: PaperPicProps, with its props, and PaperPicInstance, the viewer in instance.
Server-side rendering
With server-side rendering, such as in Nuxt, the component renders an empty element on the server and creates the viewer in the browser. In a Nuxt app, @paperpic/nuxt sets up the component and its stylesheet for you.