feat(projects): synchronize previews with thumbnails scrolling
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import gsap from "gsap";
|
|
||||||
import SELECTOR_IMAGE from "/assets/images/projects/bottom-screen/selector.webp";
|
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 PROJECT_SQUARE_IMAGE from "/assets/images/projects/bottom-screen/project-square.webp";
|
||||||
|
|
||||||
@@ -11,51 +10,17 @@ const [selectorImage, projectSquareImage, ...projectThumbnails] = useImages(
|
|||||||
...store.projects.map((x) => x.thumbnail),
|
...store.projects.map((x) => x.thumbnail),
|
||||||
);
|
);
|
||||||
|
|
||||||
const offsetX = ref(0);
|
|
||||||
|
|
||||||
const getBounds = () => ({
|
const getBounds = () => ({
|
||||||
startIndex: Math.max(store.currentProject - 3, 0),
|
startIndex: Math.max(store.currentProject - 3, 0),
|
||||||
endIndex: Math.min(store.currentProject + 3 + 1, store.projects.length),
|
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) => {
|
useRender((ctx) => {
|
||||||
const { startIndex, endIndex } = getBounds();
|
const { startIndex, endIndex } = getBounds();
|
||||||
|
|
||||||
for (let i = startIndex; i < endIndex; i += 1) {
|
for (let i = startIndex; i < endIndex; i += 1) {
|
||||||
const offsetFromCenter = i - store.currentProject;
|
const offsetFromCenter = i - store.currentProject;
|
||||||
const x = 101 + 69 * offsetFromCenter + offsetX.value;
|
const x = 101 + 69 * offsetFromCenter + store.offsetX;
|
||||||
|
|
||||||
ctx.drawImage(projectSquareImage!, x, 81);
|
ctx.drawImage(projectSquareImage!, x, 81);
|
||||||
ctx.drawImage(projectThumbnails[i]!, x + 7, 88);
|
ctx.drawImage(projectThumbnails[i]!, x + 7, 88);
|
||||||
@@ -76,10 +41,10 @@ useRender((ctx) => {
|
|||||||
useKeyDown((key) => {
|
useKeyDown((key) => {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case "ArrowLeft":
|
case "ArrowLeft":
|
||||||
scrollProjects("left");
|
store.scrollProjects("left");
|
||||||
break;
|
break;
|
||||||
case "ArrowRight":
|
case "ArrowRight":
|
||||||
scrollProjects("right");
|
store.scrollProjects("right");
|
||||||
break;
|
break;
|
||||||
case "Enter":
|
case "Enter":
|
||||||
case " ":
|
case " ":
|
||||||
@@ -90,9 +55,9 @@ useKeyDown((key) => {
|
|||||||
|
|
||||||
useScreenMouseWheel((dy) => {
|
useScreenMouseWheel((dy) => {
|
||||||
if (dy > 0) {
|
if (dy > 0) {
|
||||||
scrollProjects("right");
|
store.scrollProjects("right");
|
||||||
} else if (dy < 0) {
|
} else if (dy < 0) {
|
||||||
scrollProjects("left");
|
store.scrollProjects("left");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -101,14 +66,13 @@ useScreenClick((x, y) => {
|
|||||||
|
|
||||||
for (let i = startIndex; i < endIndex; i += 1) {
|
for (let i = startIndex; i < endIndex; i += 1) {
|
||||||
const offsetFromCenter = i - store.currentProject;
|
const offsetFromCenter = i - store.currentProject;
|
||||||
const rectX = 101 + 69 * offsetFromCenter + offsetX.value;
|
const rectX = 101 + 69 * offsetFromCenter + store.offsetX;
|
||||||
|
|
||||||
if (rectContains([rectX, 81, 54, 54], [x, y])) {
|
if (rectContains([rectX, 81, 54, 54], [x, y])) {
|
||||||
if (i === store.currentProject) {
|
if (i === store.currentProject) {
|
||||||
store.visitProject();
|
store.visitProject();
|
||||||
} else {
|
} else {
|
||||||
animateScrolling((i - store.currentProject) * 69);
|
store.scrollToProject(i);
|
||||||
store.currentProject = i;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,11 @@ onMounted(() => {
|
|||||||
projectPreviews.value = useImages(...store.projects.map((x) => x.preview));
|
projectPreviews.value = useImages(...store.projects.map((x) => x.preview));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const getBounds = () => ({
|
||||||
|
startIndex: Math.max(store.currentProject - 1, 0),
|
||||||
|
endIndex: Math.min(store.currentProject + 1 + 1, store.projects.length),
|
||||||
|
});
|
||||||
|
|
||||||
useRender((ctx) => {
|
useRender((ctx) => {
|
||||||
const project = store.projects[store.currentProject];
|
const project = store.projects[store.currentProject];
|
||||||
if (!project) return;
|
if (!project) return;
|
||||||
@@ -15,11 +20,18 @@ useRender((ctx) => {
|
|||||||
ctx.font = "10px NDS10";
|
ctx.font = "10px NDS10";
|
||||||
ctx.fillStyle = "#000000";
|
ctx.fillStyle = "#000000";
|
||||||
|
|
||||||
const preview = projectPreviews.value[store.currentProject];
|
const { startIndex, endIndex } = getBounds();
|
||||||
if (!preview) return;
|
|
||||||
|
|
||||||
const x = (256 - preview.width) / 2;
|
for (let i = startIndex; i < endIndex; i += 1) {
|
||||||
|
const preview = projectPreviews.value[i];
|
||||||
|
if (!preview) continue;
|
||||||
|
|
||||||
|
const offsetFromCenter = i - store.currentProject;
|
||||||
|
const baseX = (256 - preview.width) / 2;
|
||||||
|
const x = baseX + 256 * offsetFromCenter + store.offsetX * (256 / 69);
|
||||||
const y = (192 - preview.height) / 2 + 10;
|
const y = (192 - preview.height) / 2 + 10;
|
||||||
|
|
||||||
|
ctx.save();
|
||||||
ctx.translate(x, y);
|
ctx.translate(x, y);
|
||||||
|
|
||||||
ctx.fillStyle = "#a6a6a6";
|
ctx.fillStyle = "#a6a6a6";
|
||||||
@@ -33,6 +45,8 @@ useRender((ctx) => {
|
|||||||
ctx.shadowOffsetY = 2;
|
ctx.shadowOffsetY = 2;
|
||||||
|
|
||||||
ctx.drawImage(preview, 0, 0);
|
ctx.drawImage(preview, 0, 0);
|
||||||
|
ctx.restore();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
defineOptions({
|
defineOptions({
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { MarkdownRoot } from "@nuxt/content";
|
import type { MarkdownRoot } from "@nuxt/content";
|
||||||
|
import gsap from "gsap";
|
||||||
|
|
||||||
type MarkdownBody = (
|
type MarkdownBody = (
|
||||||
| {
|
| {
|
||||||
@@ -68,6 +69,7 @@ export const useProjectsStore = defineStore("projects", {
|
|||||||
}[],
|
}[],
|
||||||
currentProject: 0,
|
currentProject: 0,
|
||||||
loading: true,
|
loading: true,
|
||||||
|
offsetX: 0,
|
||||||
}),
|
}),
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
@@ -99,5 +101,49 @@ export const useProjectsStore = defineStore("projects", {
|
|||||||
const url = this.projects[this.currentProject]!.url;
|
const url = this.projects[this.currentProject]!.url;
|
||||||
if (url) navigateTo(url, { external: true, open: { target: "_blank" } });
|
if (url) navigateTo(url, { external: true, open: { target: "_blank" } });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
scrollProjects(direction: "left" | "right") {
|
||||||
|
let offset = 0;
|
||||||
|
|
||||||
|
if (
|
||||||
|
direction === "right" &&
|
||||||
|
this.currentProject < this.projects.length - 1
|
||||||
|
) {
|
||||||
|
this.currentProject += 1;
|
||||||
|
offset = 69;
|
||||||
|
} else if (direction === "left" && this.currentProject > 0) {
|
||||||
|
this.currentProject -= 1;
|
||||||
|
offset = -69;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offset !== 0) {
|
||||||
|
gsap.fromTo(
|
||||||
|
this,
|
||||||
|
{ offsetX: offset },
|
||||||
|
{
|
||||||
|
offsetX: 0,
|
||||||
|
duration: 0.1,
|
||||||
|
ease: "none",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
scrollToProject(index: number) {
|
||||||
|
if (index === this.currentProject) return;
|
||||||
|
|
||||||
|
const offset = (index - this.currentProject) * 69;
|
||||||
|
this.currentProject = index;
|
||||||
|
|
||||||
|
gsap.fromTo(
|
||||||
|
this,
|
||||||
|
{ offsetX: offset },
|
||||||
|
{
|
||||||
|
offsetX: 0,
|
||||||
|
duration: 0.1,
|
||||||
|
ease: "none",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user