-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Simplify the logic for calculating sizes and extract it int…
…o useSizes (#513) * refactor: Simplify the logic for calculating sizes and extract it into useSizes * fix: Complete the types * refactor: Clarified the meaning of the variables.
- Loading branch information
1 parent
0be3969
commit 91e047a
Showing
2 changed files
with
36 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { computed, readonly } from 'vue' | ||
import type { MaybeRefOrGetter, MaybeRef, ComputedRef, Ref } from 'vue' | ||
import { refDebounced, toValue, useElementSize, useWindowSize } from '@vueuse/core' | ||
|
||
export interface SizesType { | ||
height: Readonly<Ref<number>> | ||
width: Readonly<Ref<number>> | ||
aspectRatio: ComputedRef<number> | ||
} | ||
|
||
export default function useSizes( | ||
windowSize: MaybeRefOrGetter<boolean>, | ||
canvas: MaybeRef<HTMLCanvasElement>, | ||
debounceMs: number = 10, | ||
) { | ||
const reactiveSize = toValue(windowSize) | ||
? useWindowSize() | ||
: useElementSize(computed(() => toValue(canvas).parentElement)) | ||
|
||
const debouncedReactiveWidth = readonly(refDebounced(reactiveSize.width, debounceMs)) | ||
const debouncedReactiveHeight = readonly(refDebounced(reactiveSize.height, debounceMs)) | ||
|
||
const aspectRatio = computed(() => debouncedReactiveWidth.value / debouncedReactiveHeight.value) | ||
|
||
return { | ||
height: debouncedReactiveHeight, | ||
width: debouncedReactiveWidth, | ||
aspectRatio, | ||
} | ||
} |
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