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

223 lines
5.8 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"
2025-02-26 16:23:32 +01:00
@touchstart="enableDragToRotate"
2024-12-16 18:30:14 +01:00
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';
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();
2025-01-07 16:54:08 +01:00
const { isDownloadTriggered } = storeToRefs(useVirtualSampleStore());
const isHelperHidden = ref(localStorage.getItem('isHelperHidden'));
localStorage.setItem('isHelperHidden', true);
2024-12-16 18:30:14 +01:00
2024-12-17 11:27:02 +01:00
// Grab interaction
2025-02-26 16:23:32 +01:00
const yMax = computed(() =>
parseInt(activeTrack.files[activeTrack.files.length - 1].name.split('_')[0])
);
const xMax = computed(() =>
parseInt(
2024-12-16 15:46:19 +01:00
activeTrack.files[activeTrack.files.length - 1].name
.split('_')[1]
.split('.')[0]
2025-02-26 16:23:32 +01:00
)
);
2024-12-16 15:46:19 +01:00
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);
2024-12-16 15:46:19 +01:00
const currentFile = computed(() =>
activeTrack.files.find((file) => file.name.includes(currentFileIndex.value))
);
2025-02-26 16:23:32 +01:00
watch(
currentFile,
() => {
virtualSampleStore.currentFile = currentFile.value;
openedFile.value = currentFile.value;
},
2025-02-26 16:23:32 +01:00
{ immediate: true }
);
2024-12-16 15:46:19 +01:00
2025-02-26 16:23:32 +01:00
// Rotation
2024-12-16 15:46:19 +01:00
function rotateX(direction) {
2025-02-26 16:23:32 +01:00
if (direction === 'left') {
2024-12-16 15:46:19 +01:00
currentX.value = currentX.value === 0 ? xMax.value : currentX.value - 1;
2025-02-26 16:23:32 +01:00
} else if (direction === 'right') {
2024-12-16 15:46:19 +01:00
currentX.value = currentX.value === xMax.value ? 0 : currentX.value + 1;
}
}
const isDragToRotateEnabled = ref(false);
2025-02-26 16:23:32 +01:00
let previousMouseXPos;
let previousMouseYPos;
const DRAG_STEP = 20;
function enableDragToRotate(event) {
2025-02-27 17:08:35 +01:00
console.log(event);
2025-02-26 16:23:32 +01:00
if (event.type.startsWith('touch')) event.preventDefault();
const isTouch = event.type.startsWith('touch');
2024-12-16 15:46:19 +01:00
isDragToRotateEnabled.value = true;
2025-02-26 16:23:32 +01:00
const clientX = isTouch ? event.touches[0].clientX : event.clientX;
const clientY = isTouch ? event.touches[0].clientY : event.clientY;
previousMouseXPos = clientX;
previousMouseYPos = clientY;
2024-12-16 15:46:19 +01:00
}
2025-02-26 16:23:32 +01:00
2024-12-16 15:46:19 +01:00
function disableDragToRotate() {
isDragToRotateEnabled.value = false;
}
function dragToRotate(event) {
2025-02-26 16:23:32 +01:00
if (event.type.startsWith('touch')) event.preventDefault();
const isTouch = event.type.startsWith('touch');
const clientX = isTouch ? event.touches[0].clientX : event.clientX;
const clientY = isTouch ? event.touches[0].clientY : event.clientY;
handleHorizontalRotation(clientX);
handleVerticalRotation(clientY);
2024-12-16 15:46:19 +01:00
}
2025-02-26 16:23:32 +01:00
function handleHorizontalRotation(clientX) {
2024-12-16 15:46:19 +01:00
if (previousMouseXPos === undefined) {
2025-02-26 16:23:32 +01:00
previousMouseXPos = clientX;
2024-12-16 15:46:19 +01:00
return;
}
2025-02-26 16:23:32 +01:00
if (clientX > previousMouseXPos + DRAG_STEP) {
rotateX('left');
2025-02-26 16:23:32 +01:00
previousMouseXPos = clientX;
} else if (clientX < previousMouseXPos - DRAG_STEP) {
rotateX('right');
2025-02-26 16:23:32 +01:00
previousMouseXPos = clientX;
2024-12-16 15:46:19 +01:00
}
}
2025-02-26 16:23:32 +01:00
function handleVerticalRotation(clientY) {
2024-12-16 15:46:19 +01:00
if (previousMouseYPos === undefined) {
2025-02-26 16:23:32 +01:00
previousMouseYPos = clientY;
2024-12-16 15:46:19 +01:00
return;
}
2025-02-26 16:23:32 +01:00
if (clientY > previousMouseYPos + DRAG_STEP) {
2024-12-16 15:46:19 +01:00
if (currentY.value < yMax.value) {
currentY.value++;
2025-02-26 16:23:32 +01:00
previousMouseYPos = clientY;
2024-12-16 15:46:19 +01:00
}
2025-02-26 16:23:32 +01:00
} else if (clientY < previousMouseYPos - DRAG_STEP) {
2024-12-16 15:46:19 +01:00
if (currentY.value > 0) {
currentY.value--;
2025-02-26 16:23:32 +01:00
previousMouseYPos = clientY;
2024-12-16 15:46:19 +01:00
}
}
}
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);
2025-02-26 16:23:32 +01:00
window.addEventListener('touchmove', throttledDragToRotate, {
passive: false,
});
2024-12-16 15:46:19 +01:00
} else {
window.removeEventListener('mousemove', throttledDragToRotate);
2025-02-26 16:23:32 +01:00
window.removeEventListener('touchmove', throttledDragToRotate);
2024-12-16 15:46:19 +01:00
previousMouseXPos = undefined;
previousMouseYPos = undefined;
}
});
2024-12-16 16:27:21 +01:00
2025-02-26 16:23:32 +01:00
window.addEventListener('mouseup', disableDragToRotate);
window.addEventListener('touchend', disableDragToRotate);
2024-12-16 16:27:21 +01:00
const { isLoopAnimationEnabled } = storeToRefs(useVirtualSampleStore());
function loopAnimation() {
rotateX('right');
2024-12-16 16:27:21 +01:00
}
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;
});
}
2025-01-07 16:54:08 +01:00
// Download image
watch(isDownloadTriggered, (newValue) => {
if (!newValue) return;
const downloadNode = document.createElement('a');
downloadNode.setAttribute('href', currentFile.value.source);
downloadNode.setAttribute('download', '');
2025-01-07 16:54:08 +01:00
document.body.appendChild(downloadNode);
downloadNode.click();
document.body.removeChild(downloadNode);
});
2024-12-16 15:46:19 +01:00
</script>