designtopack/src/components/project/virtual-sample/Interactive360.vue

191 lines
4.6 KiB
Vue
Raw Normal View History

2024-12-16 15:46:19 +01:00
<template>
<figure>
2024-12-16 18:30:14 +01:00
<div
@mousedown="enableDragToRotate"
class="drag-zone"
:class="{ grabbing: isDragToRotateEnabled }"
></div>
2024-12-20 15:52:42 +01:00
<img
:src="
virtualSampleStore.isCompareModeEnabled
? currentFile.url
: openedFile.url
"
alt=""
width="500"
height="500"
/>
2024-12-16 15:46:19 +01:00
</figure>
2024-12-16 18:30:14 +01:00
<div
id="helper"
class="flex flex-col | bg-black-50"
:hidden="isHelperHidden ? true : undefined"
>
<p class="rounded-lg | text-white bg-grey-800 | p-16">
Vous pouvez visualiser léchantillon sous différents angles en cliquant et
en déplaçant la souris de gauche à droite ou de bas en haut.
</p>
<button class="btn" @click="isHelperHidden = true">Jai compris</button>
2024-12-16 18:19:39 +01:00
</div>
2024-12-16 15:46:19 +01:00
</template>
<script setup>
import throttle from "lodash/throttle";
import { ref, computed, watch } from "vue";
import { useVirtualSampleStore } from "../../../stores/virtualSample";
2024-12-16 16:27:21 +01:00
import { storeToRefs } from "pinia";
import { useDialogStore } from "../../../stores/dialog";
2024-12-16 15:46:19 +01:00
const { activeTrack } = defineProps({
activeTrack: Object,
});
2024-12-17 11:27:02 +01:00
// Helper
const { openedFile } = storeToRefs(useDialogStore());
2024-12-16 15:46:19 +01:00
const virtualSampleStore = useVirtualSampleStore();
2024-12-16 18:30:14 +01:00
const isHelperHidden = ref(localStorage.getItem("isHelperHidden"));
localStorage.setItem("isHelperHidden", true);
2024-12-17 11:27:02 +01:00
// Grab interaction
2024-12-16 15:46:19 +01:00
const yMax = computed(() => {
return parseInt(
activeTrack.files[activeTrack.files.length - 1].name.charAt(0)
);
});
const xMax = computed(() => {
return parseInt(
activeTrack.files[activeTrack.files.length - 1].name
.split("_")[1]
.split(".")[0]
);
});
2024-12-17 16:43:53 +01:00
const frontXView = (xMax.value + 1) / 2;
const currentX = ref(frontXView);
2024-12-16 15:46:19 +01:00
const currentY = ref(0);
const currentFileIndex = computed(() => currentY.value + "_" + currentX.value);
const currentFile = computed(() =>
activeTrack.files.find((file) => file.name.includes(currentFileIndex.value))
);
watch(
currentFile,
() => {
virtualSampleStore.currentFile = currentFile.value;
openedFile.value = currentFile.value;
},
{
immediate: true,
}
);
2024-12-16 15:46:19 +01:00
function rotateX(direction) {
if (direction == "left") {
currentX.value = currentX.value === 0 ? xMax.value : currentX.value - 1;
}
if (direction == "right") {
currentX.value = currentX.value === xMax.value ? 0 : currentX.value + 1;
}
}
const isDragToRotateEnabled = ref(false);
window.addEventListener("mouseup", disableDragToRotate);
function enableDragToRotate() {
isDragToRotateEnabled.value = true;
}
function disableDragToRotate() {
isDragToRotateEnabled.value = false;
}
let previousMouseXPos;
let previousMouseYPos;
const DRAG_STEP = 20;
function dragToRotate(event) {
handleHorizontalRotation(event);
handleVerticalRotation(event);
}
function handleHorizontalRotation(event) {
if (previousMouseXPos === undefined) {
previousMouseXPos = event.clientX;
return;
}
if (event.clientX > previousMouseXPos + DRAG_STEP) {
rotateX("left");
previousMouseXPos = event.clientX;
} else if (event.clientX < previousMouseXPos - DRAG_STEP) {
rotateX("right");
previousMouseXPos = event.clientX;
}
}
function handleVerticalRotation(event) {
if (previousMouseYPos === undefined) {
previousMouseYPos = event.clientY;
return;
}
if (event.clientY > previousMouseYPos + DRAG_STEP) {
if (currentY.value < yMax.value) {
currentY.value++;
previousMouseYPos = event.clientY;
}
} else if (event.clientY < previousMouseYPos - DRAG_STEP) {
if (currentY.value > 0) {
currentY.value--;
previousMouseYPos = event.clientY;
}
}
}
2024-12-17 12:06:06 +01:00
function resetView() {
2024-12-17 16:43:53 +01:00
currentX.value = frontXView;
2024-12-17 12:06:06 +01:00
currentY.value = 0;
}
2024-12-16 15:46:19 +01:00
const throttledDragToRotate = throttle(dragToRotate, 50);
watch(isDragToRotateEnabled, (newValue) => {
if (newValue) {
window.addEventListener("mousemove", throttledDragToRotate);
} else {
window.removeEventListener("mousemove", throttledDragToRotate);
previousMouseXPos = undefined;
previousMouseYPos = undefined;
}
});
2024-12-16 16:27:21 +01:00
const { isLoopAnimationEnabled } = storeToRefs(useVirtualSampleStore());
function loopAnimation() {
rotateX("right");
}
let animationIntervalId;
watch(isLoopAnimationEnabled, (newValue) => {
if (newValue) {
animationIntervalId = setInterval(loopAnimation, 300);
} else {
clearInterval(animationIntervalId);
}
});
2024-12-17 12:06:06 +01:00
// Images preload
const imageUrls = computed(() => activeTrack.files.map((file) => file.url));
watch(
imageUrls,
() => {
preloadImages();
resetView();
},
{ immediate: true }
);
function preloadImages() {
imageUrls.value.forEach((imageUrl) => {
const image = new Image();
image.src = imageUrl;
});
}
2024-12-16 15:46:19 +01:00
</script>