-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstretch.vue
63 lines (54 loc) · 1.51 KB
/
stretch.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<template>
<div class="mask" ref="maskRef">
<div class="content" ref="contentRef">
<slot></slot>
</div>
</div>
</template>
<script>
import { onMounted, onBeforeUnmount, nextTick, ref } from 'vue'
export default {
props: {
maxScale: {
type: Number,
default: Number.MAX_VALUE,
}
},
setup(props) {
const contentRef = ref(null)
const maskRef = ref(null)
const scale = ref(1.0)
const height = ref('unset')
let observer
onMounted(() => {
observer = new ResizeObserver(async () => {
const { value: content } = contentRef
const { value: mask } = maskRef
scale.value = 1.0
await nextTick()
let maskBounds = mask.getBoundingClientRect()
let contentBounds = content.getBoundingClientRect()
// TODO Calculate scale based on orientation and aspect fit / aspect fill
scale.value = Math.min(props.maxScale, maskBounds.right / contentBounds.right)
// TODO Revise height workaround to avoid observer recursion
// await nextTick()
// contentBounds = content.getBoundingClientRect()
// height.value = `${contentBounds.height}px`
})
observer.observe(maskRef.value)
})
onBeforeUnmount(() => observer.unobserve(maskRef.value))
return { contentRef, height, maskRef, scale }
}
}
</script>
<style lang="scss" scoped>
.content {
display: inline-block;
transform: scale(v-bind(scale));
transform-origin: top left;
}
.mask {
height: v-bind(height);
}
</style>