line-and-surface/src/components/VideoScroll.vue

169 lines
4.4 KiB
Vue
Raw Normal View History

2021-01-09 20:36:20 +01:00
<template>
2021-04-22 11:27:11 +02:00
<div class="video-scroll" ref="root" v-if="definition.directions.length > 0">
2021-04-22 11:33:33 +02:00
<img
class="visible displayed loaded"
:src="definition.files[0]"
:style="{
top: `${Math.round(definition.top)}px`,
left: `${Math.round(definition.left)}px`,
width: isHorizontal ? `${Math.round(definition.width)}px` : 'auto',
height: isVertical ? `${Math.round(definition.height)}px` : 'auto',
transform: `rotate(${definition.angle}deg)`,
}"
2021-01-11 19:29:35 +01:00
/>
2021-01-11 19:29:35 +01:00
<!--suppress RequiredAttributes -->
2021-04-22 11:33:33 +02:00
<img
v-for="(file, idx) in dynamicFiles"
:key="`${idx}_${file.src}`"
:data-src="file.src"
:style="{
top: `${Math.round(file.top)}px`,
left: `${Math.round(file.left)}px`,
width: `${Math.round(definition.width)}px`,
height: `${Math.round(definition.height)}px`,
transform: `rotate(${definition.angle}deg)`,
}"
2021-01-11 19:29:35 +01:00
/>
2021-01-09 20:36:20 +01:00
</div>
</template>
<script lang="ts">
2021-04-22 11:33:33 +02:00
import { defineComponent, PropType } from "vue";
import { rotate } from "@/utils";
2021-01-09 20:36:20 +01:00
export default defineComponent({
name: "VideoScroll",
props: {
definition: {
2021-01-12 18:05:28 +01:00
type: Object as PropType<VideoScrollDef>,
2021-04-22 11:33:33 +02:00
required: true,
},
2021-01-09 20:36:20 +01:00
},
computed: {
2021-04-22 11:33:33 +02:00
dynamicFiles(): { top: number; left: number; src: string }[] {
return this.definition.files.slice(1).map((src: string, idx: number) => {
2021-04-22 11:33:33 +02:00
const cy =
this.definition.top +
(this.isVertical
? this.definition.height * (idx + 1) * this.verticalDirection
: 0);
const cx =
this.definition.left +
(this.isHorizontal
? this.definition.width * (idx + 1) * this.horizontalDirection
: 0);
const [left, top] = rotate(
cx,
cy,
this.definition.left,
this.definition.top,
this.definition.angle
);
return { top, left, src };
});
},
isHorizontal(): boolean {
return this.definition.directions.some(
2021-04-22 11:33:33 +02:00
(dir: VideoScrollDirection) =>
dir === VideoScrollDirection.LEFT ||
dir === VideoScrollDirection.RIGHT
);
},
isVertical(): boolean {
return this.definition.directions.some(
2021-04-22 11:33:33 +02:00
(dir: VideoScrollDirection) =>
dir === VideoScrollDirection.UP || dir === VideoScrollDirection.DOWN
);
},
horizontalDirection(): number {
2021-04-22 11:33:33 +02:00
return this.definition.directions.includes(VideoScrollDirection.RIGHT)
? 1
: -1;
},
verticalDirection(): number {
2021-04-22 11:33:33 +02:00
return this.definition.directions.includes(VideoScrollDirection.DOWN)
? 1
: -1;
},
2021-01-11 19:29:35 +01:00
},
mounted() {
2021-01-12 18:05:28 +01:00
const observer = new IntersectionObserver((entries, _) => {
entries.forEach((entry) => {
const element = entry.target as HTMLImageElement;
if (entry.isIntersecting) {
element.classList.add("visible");
if (!element.src) {
2021-04-22 11:33:33 +02:00
console.debug(
`[VIDEOSCROLL] Intersected, loading ${element.dataset.src}`
);
element.src = element.dataset.src!;
setTimeout(() => {
element.classList.add("displayed");
}, 3000);
element.onload = () => {
element.classList.add("displayed");
element.classList.add("loaded");
if (this.isHorizontal) {
element.style.height = "auto";
} else {
element.style.width = "auto";
}
};
}
} else {
element.classList.remove("visible");
}
2021-01-09 20:53:56 +01:00
});
});
2021-04-22 11:33:33 +02:00
if (this.$refs.root) {
2021-04-22 11:27:11 +02:00
Array.from((this.$refs.root as Element).children).forEach((el) => {
observer.observe(el);
});
}
2021-04-22 11:33:33 +02:00
},
2021-01-09 20:36:20 +01:00
});
export enum VideoScrollDirection {
RIGHT = "right",
LEFT = "left",
UP = "up",
2021-04-22 11:33:33 +02:00
DOWN = "down",
2021-01-09 20:36:20 +01:00
}
export interface VideoScrollDef {
2021-04-22 11:33:33 +02:00
id: string;
top: number;
left: number;
angle: number;
width: number;
height: number;
directions: VideoScrollDirection[];
files: string[];
2021-01-09 20:36:20 +01:00
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
.video-scroll img {
position: absolute;
image-rendering: optimizeSpeed;
background: grey;
visibility: hidden;
opacity: 0;
2021-04-22 11:33:33 +02:00
transition: opacity 0.5s;
}
.video-scroll img.visible {
visibility: visible !important;
}
.video-scroll img.displayed {
opacity: 1 !important;
}
.video-scroll img.loaded {
background: transparent !important;
}
2021-01-09 20:36:20 +01:00
</style>