allow specifying multiple directions (e.g. "up right")

master
Tomáš Mládek 2021-01-12 00:15:42 +01:00
parent 47e2e19a3c
commit f6c6e3a412
2 changed files with 42 additions and 40 deletions

View File

@ -305,9 +305,13 @@ async function processScrolls(svg: XMLDocument): Promise<VideoScrollDef[]> {
console.debug(`[SVG/VIDEOSCROLLS] Found video scroll #${el.id}: ${descNode?.textContent}`); console.debug(`[SVG/VIDEOSCROLLS] Found video scroll #${el.id}: ${descNode?.textContent}`);
const [directionString, filesURL] = descNode!.textContent!.split("\n"); const [directionString, filesURL] = descNode!.textContent!.split("\n");
if (!Object.values(VideoScrollDirection).includes(directionString as VideoScrollDirection)) { const directions: VideoScrollDirection[] = directionString.split(" ").map((direction) => {
throw new Error("Unknown direction string."); if (!Object.values(VideoScrollDirection).includes(direction as VideoScrollDirection)) {
} throw new Error(`Unknown direction string: "${direction}"`);
}
return direction as VideoScrollDirection;
});
console.debug(`[SVG/VIDEOSCROLLS] Fetching ${filesURL}...`); console.debug(`[SVG/VIDEOSCROLLS] Fetching ${filesURL}...`);
const fileFetch = await fetch(`content/${filesURL}`); const fileFetch = await fetch(`content/${filesURL}`);
@ -335,7 +339,7 @@ async function processScrolls(svg: XMLDocument): Promise<VideoScrollDef[]> {
angle, angle,
width: w * ratio, width: w * ratio,
height: h * ratio, height: h * ratio,
direction: directionString as VideoScrollDirection, directions,
files files
}; };
}) })

View File

@ -6,7 +6,7 @@
top: `${Math.round(definition.top)}px`, top: `${Math.round(definition.top)}px`,
left: `${Math.round(definition.left)}px`, left: `${Math.round(definition.left)}px`,
width: isHorizontal ? `${Math.round(definition.width)}px` : 'auto', width: isHorizontal ? `${Math.round(definition.width)}px` : 'auto',
height: isHorizontal ? 'auto' : `${Math.round(definition.height)}px`, height: isVertical ? `${Math.round(definition.height)}px` : 'auto',
rotate: `${definition.angle}deg` rotate: `${definition.angle}deg`
}" }"
/> />
@ -14,7 +14,6 @@
<!--suppress RequiredAttributes --> <!--suppress RequiredAttributes -->
<img v-for="file in dynamicFiles" <img v-for="file in dynamicFiles"
:data-src="file.src" :data-src="file.src"
:data-direction="definition.direction"
:style="{ :style="{
top: `${Math.round(file.top)}px`, top: `${Math.round(file.top)}px`,
left: `${Math.round(file.left)}px`, left: `${Math.round(file.left)}px`,
@ -27,7 +26,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import {defineComponent, onMounted, ref} from "vue"; import {defineComponent} from "vue";
import {rotate} from "@/utils"; import {rotate} from "@/utils";
export default defineComponent({ export default defineComponent({
@ -42,54 +41,53 @@ export default defineComponent({
dynamicFiles(): { [key: string]: string } { dynamicFiles(): { [key: string]: string } {
return this.definition.files.slice(1).map((src: string, idx: number) => { return this.definition.files.slice(1).map((src: string, idx: number) => {
const cy = this.definition.top + const cy = this.definition.top +
(this.isHorizontal ? 0 : (this.definition.height * (idx + 1) * this.direction)); (this.isVertical ? (this.definition.height * (idx + 1) * this.verticalDirection) : 0);
const cx = this.definition.left + const cx = this.definition.left +
(this.isHorizontal ? (this.definition.width * (idx + 1) * this.direction) : 0); (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); const [left, top] = rotate(cx, cy, this.definition.left, this.definition.top, this.definition.angle);
return {top, left, src}; return {top, left, src};
}); });
}, },
isHorizontal(): boolean { isHorizontal(): boolean {
return this.definition.direction === "left" || this.definition.direction === "right"; return this.definition.directions.some(
(dir: VideoScrollDirection) => dir === VideoScrollDirection.LEFT || dir === VideoScrollDirection.RIGHT
);
}, },
direction(): number { isVertical(): boolean {
return (this.definition.direction === "right" || this.definition.direction === "down") ? 1 : -1; return this.definition.directions.some(
(dir: VideoScrollDirection) => dir === VideoScrollDirection.UP || dir === VideoScrollDirection.DOWN
);
},
horizontalDirection(): number {
return this.definition.directions.includes(VideoScrollDirection.RIGHT) ? 1 : -1;
},
verticalDirection(): number {
return this.definition.directions.includes(VideoScrollDirection.DOWN) ? 1 : -1;
} }
}, },
setup(props) { mounted() {
const root = ref<Element | null>(null); const observer = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
console.debug(`[VIDEOSCROLL] Initializing ${props.definition.files[0]}...`); const element = entry.target as HTMLImageElement;
console.debug(props.definition); if (entry.isIntersecting) {
if (!element.src) {
onMounted(() => { console.debug(`[VIDEOSCROLL] Intersected, loading ${element.dataset.src}`);
const observer = new IntersectionObserver((entries, observer) => { element.src = element.dataset.src!;
entries.forEach((entry) => { element.onload = () => {
const element = entry.target as HTMLImageElement; element.classList.add("loaded");
if (entry.isIntersecting) { if (this.isHorizontal) {
if (!element.src) {
console.debug(`[VIDEOSCROLL] Intersected, loading ${element.dataset.src}`);
element.src = element.dataset.src!;
if (element.dataset.direction == "left" || element.dataset.direction == "right") {
element.style.height = "auto"; element.style.height = "auto";
} else { } else {
element.style.width = "auto"; element.style.width = "auto";
} }
element.onload = () => { };
element.classList.add("loaded");
};
}
} }
}); }
});
Array.from((root.value as Element).children).forEach((el) => {
observer.observe(el);
}); });
}); });
Array.from((this.$refs.root as Element).children).forEach((el) => {
return { observer.observe(el);
root });
};
} }
}); });
@ -106,7 +104,7 @@ export interface VideoScrollDef {
angle: number, angle: number,
width: number, width: number,
height: number, height: number,
direction: VideoScrollDirection, directions: VideoScrollDirection[],
files: string[] files: string[]
} }