Skip to content

Commit

Permalink
feat: add draw events to PaintEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrosman committed May 12, 2024
1 parent 3ffff52 commit fc0526d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { ref } from 'vue';
import PaintEditor from './components/PaintEditor.vue'
import type { SaveParameters, Shape } from './types'
import type { DrawEvent, SaveParameters, Shape } from './types'
import { toCanvas } from './utils/toCanvas';
import { exportSvg } from './utils/exportSvg';
import { useStorage } from '@vueuse/core';
Expand All @@ -19,6 +19,10 @@ function save({ svg, tools, history }: SaveParameters) {
toCanvas({ svg, canvas: canvasRef, tools, history })
}
function logEvent(event: DrawEvent) {
console.log(event)
}
const history = useStorage<Shape[]>("history", [{
type: "crop",
x: 50,
Expand Down Expand Up @@ -51,4 +55,9 @@ const history = useStorage<Shape[]>("history", [{
current state (like in localStorage or on a server). Try to draw something and reload the page to see localStorage
in action. </p>
<paint-editor v-model:history="history" class="vue-draw" @save="save" :tools></paint-editor>

<h1>Using events</h1>
<p>You can hook into events that are emitted from the component. Watch the console while drawing to see it in action.
</p>
<paint-editor class="vue-draw" @save="save" :tools @draw-start="logEvent" @draw-end="logEvent"></paint-editor>
</template>
6 changes: 6 additions & 0 deletions src/components/PaintEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import PaintRenderer from './PaintRenderer.vue';
const emit = defineEmits<{
(e: 'save', { svg, tools, history }: SaveParameters): void
(e: 'drawStart', event: DrawEvent): void
(e: 'draw', event: DrawEvent): void
(e: 'drawEnd', event: DrawEvent): void
(e: 'clear'): void
}>()
Expand Down Expand Up @@ -51,12 +54,15 @@ const { posStart, posEnd, isSwiping } = usePointerSwipe(svgRef, {
threshold: 0,
onSwipeStart(e) {
activeShape.value = getActiveTool()?.onDrawStart?.(drawEvent.value) ?? activeShape.value
emit('drawStart', drawEvent.value)
},
onSwipe(e) {
activeShape.value = getActiveTool()?.onDraw?.(drawEvent.value) ?? activeShape.value
emit('draw', drawEvent.value)
},
onSwipeEnd() {
activeShape.value = getActiveTool()?.onDrawEnd?.(drawEvent.value) ?? activeShape.value
emit('drawEnd', drawEvent.value)
if (activeShape.value) {
history.value.push(activeShape.value)
activeShape.value = undefined
Expand Down

0 comments on commit fc0526d

Please sign in to comment.