Skip to content

Commit

Permalink
Merge pull request #10 from ssr765/main
Browse files Browse the repository at this point in the history
Draw on correct position when resizing editor
  • Loading branch information
robertrosman authored Aug 21, 2024
2 parents bc7724d + 1ad9087 commit 40076d5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/components/VpEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ const props = withDefaults(
{ width: 1280, height: 720 }
)
const widthRef = ref(props.width);
const heightRef = ref(props.height);
const history = defineModel<Shape[]>('history', { default: [] })
const vpImage = ref()
Expand All @@ -36,8 +39,8 @@ const { activeShape, setTool, undo, redo, save, reset } = useEditor({
tools: props.tools,
history: toRef(history),
settings: toRef(settings),
width: props.width,
height: props.height,
width: widthRef,
height: heightRef,
emit
})
Expand All @@ -52,7 +55,7 @@ onMounted(() => {

<template>
<div class="vue-paint vp-editor" :class="`active-tool-${settings.tool}`">
<vp-image ref="vpImage" :tools :activeShape :history :width :height />
<vp-image ref="vpImage" :tools :activeShape :history :width="widthRef" :height="heightRef" />

<slot name="toolbar" :set-tool :undo :save :reset :settings>
<vp-toolbar v-model:settings="settings" @set-tool="setTool" @undo="undo" @redo="redo" @save="save" @reset="reset"
Expand Down
14 changes: 6 additions & 8 deletions src/composables/useDraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export interface UseDrawOptions {
onDrawStart?: () => void
onDraw?: () => void
onDrawEnd?: () => void
width: number
height: number,
width: Ref<number>
height: Ref<number>
snapAngles?: Ref<number[] | undefined>
}

Expand Down Expand Up @@ -43,20 +43,18 @@ export function useDraw({
left: 0,
top: 0
})

const snapPosition = computed(() => snapToAngle({
snapAngles,
posStart,
x: Math.round(((absoluteX.value - left.value) * width) / scaledWidth.value),
y: Math.round(((absoluteY.value - top.value) * height) / scaledHeight.value)
x: Math.round(((absoluteX.value - left.value) * width.value) / scaledWidth.value),
y: Math.round(((absoluteY.value - top.value) * height.value) / scaledHeight.value)
}))
const x = computed(() => snapPosition.value.x)
const y = computed(() => snapPosition.value.y)

const minX = computed(() => Math.max(0, Math.min(posStart.x, x.value)))
const minY = computed(() => Math.max(0, Math.min(posStart.y, y.value)))
const maxX = computed(() => Math.min(width, Math.max(posStart.x, x.value)))
const maxY = computed(() => Math.min(height, Math.max(posStart.y, y.value)))
const maxX = computed(() => Math.min(width.value, Math.max(posStart.x, x.value)))
const maxY = computed(() => Math.min(height.value, Math.max(posStart.y, y.value)))
const isInside = computed(
() =>
absoluteX.value >= left.value &&
Expand Down
4 changes: 2 additions & 2 deletions src/composables/useEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface UseEditorOptions {
tools: Tool<any>[]
history: Ref<ImageHistory<Tool<any>[]>>
settings: Ref<Settings>
width: number
height: number
width: Ref<number>
height: Ref<number>
emit?: Function
}

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ export interface DrawEvent {
id: string

/** The width of the image in pixels. Note that it might be scaled down by the browser, but this is the base all other values will relate to. */
width: number
width: Ref<number>

/** The height of the image in pixels. Note that it might be scaled down by the browser, but this is the base all other values will relate to. */
height: number
height: Ref<number>

/** The X position of the pointer/cursor relative to the image left edge. */
x: number
Expand Down

0 comments on commit 40076d5

Please sign in to comment.