125 lines
2.9 KiB
Vue
125 lines
2.9 KiB
Vue
<script setup lang="ts">
|
|
// TODO: handle mouse wheel
|
|
|
|
import gsap from "gsap";
|
|
import SELECTOR_IMAGE from "/assets/images/projects/bottom-screen/selector.webp";
|
|
import PROJECT_SQUARE_IMAGE from "/assets/images/projects/bottom-screen/project-square.webp";
|
|
import { useKeyDown } from "~/composables/useKeyDown";
|
|
|
|
const store = useProjectsStore();
|
|
|
|
const [selectorImage, projectSquareImage, ...projectThumbnails] = useImages(
|
|
SELECTOR_IMAGE,
|
|
PROJECT_SQUARE_IMAGE,
|
|
...store.projects.map((x) => x.thumbnail),
|
|
);
|
|
|
|
const offsetX = ref(0);
|
|
|
|
const getBounds = () => ({
|
|
startIndex: Math.max(store.currentProject - 3, 0),
|
|
endIndex: Math.min(store.currentProject + 3 + 1, store.projects.length),
|
|
});
|
|
|
|
const animateScrolling = (offset: number) => {
|
|
gsap.fromTo(
|
|
offsetX,
|
|
{
|
|
value: offset,
|
|
},
|
|
{
|
|
value: 0,
|
|
duration: 0.075,
|
|
ease: "none",
|
|
},
|
|
);
|
|
};
|
|
|
|
const scrollProjects = (direction: "left" | "right") => {
|
|
let offset = 0;
|
|
if (
|
|
direction === "right" &&
|
|
store.currentProject < store.projects.length - 1
|
|
) {
|
|
store.currentProject += 1;
|
|
offset = 69;
|
|
} else if (direction === "left" && store.currentProject > 0) {
|
|
store.currentProject -= 1;
|
|
offset = -69;
|
|
}
|
|
|
|
if (offset != 0) {
|
|
animateScrolling(offset);
|
|
}
|
|
};
|
|
|
|
useRender((ctx) => {
|
|
const { startIndex, endIndex } = getBounds();
|
|
|
|
for (let i = startIndex; i < endIndex; i += 1) {
|
|
const offsetFromCenter = i - store.currentProject;
|
|
const x = 101 + 69 * offsetFromCenter + offsetX.value;
|
|
|
|
ctx.drawImage(projectSquareImage!, x, 81);
|
|
ctx.drawImage(projectThumbnails[i]!, x + 7, 88);
|
|
}
|
|
|
|
ctx.drawImage(selectorImage!, 96, 76);
|
|
|
|
ctx.font = "10px NDS10";
|
|
|
|
const lines = store.projects[store.currentProject]!.description.split("\n");
|
|
const textY = lines.length === 1 ? 35 : 28;
|
|
for (let i = 0; i < lines.length; i += 1) {
|
|
const { actualBoundingBoxRight: width } = ctx.measureText(lines[i]!);
|
|
ctx.fillText(lines[i]!, Math.floor(128 - width / 2), textY + 15 * i);
|
|
}
|
|
});
|
|
|
|
useKeyDown((key) => {
|
|
switch (key) {
|
|
case "ArrowLeft":
|
|
scrollProjects("left");
|
|
break;
|
|
case "ArrowRight":
|
|
scrollProjects("right");
|
|
break;
|
|
case "Enter":
|
|
case " ":
|
|
store.visitProject();
|
|
break;
|
|
}
|
|
});
|
|
|
|
useMouseWheel((dy) => {
|
|
if (dy > 0) {
|
|
scrollProjects("right");
|
|
} else if (dy < 0) {
|
|
scrollProjects("left");
|
|
}
|
|
});
|
|
|
|
useScreenClick((x, y) => {
|
|
const { startIndex, endIndex } = getBounds();
|
|
|
|
for (let i = startIndex; i < endIndex; i += 1) {
|
|
const offsetFromCenter = i - store.currentProject;
|
|
const rectX = 101 + 69 * offsetFromCenter + offsetX.value;
|
|
|
|
if (rectContains([rectX, 81, 54, 54], [x, y])) {
|
|
if (i === store.currentProject) {
|
|
store.visitProject();
|
|
} else {
|
|
animateScrolling((i - store.currentProject) * 69);
|
|
store.currentProject = i;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
defineOptions({
|
|
render: () => null,
|
|
});
|
|
</script>
|