// images.js // Assumes uploadFile, showContextMenu… · The Chat Archive
Explore // images.js // Assumes uploadFile, showContextMenu, addResizeListeners are available… Save Share Download ▾ Report// images.js
// Assumes uploadFile, showContextMenu, addResizeListeners are available globally
socket.on('new_image', (data) => {
const image = createImageElement(data);
document.getElementById('board').appendChild(image);
});
socket.on('updated_image', (data) => {
const image = document.querySelector(.image[data-id="${data.id}"]);
if (image) {
image.style.left = ${data.x}px;
image.style.top = ${data.y}px;
image.style.width = ${data.width}px;
image.style.height = ${data.height}px;
image.querySelector('img').src = data.url;
}
});
socket.on('element_deleted', (data) => {
if (data.type === 'image') {
const element = document.querySelector(.image[data-id="${data.id}"]);
if (element) {
element.remove();
}
}
});
function createImageElement(data) {
const image = document.createElement('div');
image.className = 'image';
image.dataset.id = data.id;
image.style.left = ${data.x}px;
image.style.top = ${data.y}px;
image.style.width = ${data.width}px;
image.style.height = ${data.height}px;
code Copy code
const img = document.createElement('img');
img.src = data.url;
img.style.width = '100%';
img.style.height = '100%';
img.style.objectFit = 'contain'; // Maintains aspect ratio
image.appendChild(img);
addImageEventListeners(image, data.id);
return image;
}
function addImageEventListeners(image, id) {
image.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // Left mouse button
e.preventDefault();
e.stopPropagation();
const startX = e.clientX;
const startY = e.clientY;
const startLeft = parseFloat(image.style.left) || 0;
const startTop = parseFloat(image.style.top) || 0;
Show full message · 451 lines Assistant2024-10-30 09:14 UTC Copy response
Copy first prompt
Other models, same or similar prompt Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Oct 30, 2024
Added to archive Nov 16, 2026
ID 01m7h422hw3nemam5y62h7gp3aSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 828bfc740dcff08f68ad99cb274f5b38
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
function moveImage(e) {
const deltaX = (e.clientX - startX) / scale;
const deltaY = (e.clientY - startY) / scale;
image.style.left = `${startLeft + deltaX}px`;
image.style.top = `${startTop + deltaY}px`;
const x = parseFloat(image.style.left) || 0;
const y = parseFloat(image.style.top) || 0;
const width = image.offsetWidth;
const height = image.offsetHeight;
socket.emit('update_image', { id, url: image.querySelector('img').src, x, y, width, height });
}
document.addEventListener('mousemove', moveImage);
document.addEventListener('mouseup', () => {
document.removeEventListener('mousemove', moveImage);
}, { once: true });
});
image.addEventListener('touchstart', (e) => {
e.preventDefault();
e.stopPropagation();
const startX = e.touches[0].clientX;
const startY = e.touches[0].clientY;
const startLeft = parseFloat(image.style.left) || 0;
const startTop = parseFloat(image.style.top) || 0;
function moveImage(e) {
const deltaX = (e.touches[0].clientX - startX) / scale;
const deltaY = (e.touches[0].clientY - startY) / scale;
image.style.left = `${startLeft + deltaX}px`;
image.style.top = `${startTop + deltaY}px`;
const x = parseFloat(image.style.left) || 0;
const y = parseFloat(image.style.top) || 0;
const width = image.offsetWidth;
const height = image.offsetHeight;
socket.emit('update_image', { id, url: image.querySelector('img').src, x, y, width, height });
}
document.addEventListener('touchmove', moveImage);
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', moveImage);
}, { once: true });
});
image.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.clientX, e.clientY);
});
image.addEventListener('touchstart', (e) => {
if (e.touches.length === 2) {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.touches[0].clientX, e.touches[0].clientY);
}
});
addResizeListeners(image, id, 'image');
function handleExistingImages() {
document.querySelectorAll('.image').forEach(image => {
const id = image.dataset.id;
const url = image.querySelector('img').src;
const x = image.offsetLeft;
const y = image.offsetTop;
const width = image.offsetWidth;
const height = image.offsetHeight;
code Copy code
image.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.clientX, e.clientY);
});
addImageEventListeners(image, id);
});
let scale = 0.5; // Initial scale
let translateX = 0;
let translateY = 0;
const board = document.getElementById('board');
const boardWrapper = document.getElementById('board-wrapper');
// Constants for zoom
const MIN_SCALE = 0.1; // Minimum scale
const MAX_SCALE = 5; // Maximum scale
const ZOOM_FACTOR = 0.1; // Zoom speed
// Apply initial scale
board.style.transform = translate(${translateX}px, ${translateY}px) scale(${scale});
boardWrapper.style.backgroundPosition = ${translateX}px ${translateY}px;
// Show images after setting scale
document.querySelectorAll('.image').forEach(img => {
img.classList.remove('hidden');
});
boardWrapper.addEventListener('wheel', (e) => {
e.preventDefault();
code Copy code
const previousScale = scale;
scale += e.deltaY < 0 ? ZOOM_FACTOR : -ZOOM_FACTOR;
scale = Math.min(Math.max(MIN_SCALE, scale), MAX_SCALE);
// Calculate cursor position relative to the board
const rect = board.getBoundingClientRect();
const offsetX = (e.clientX - rect.left) / previousScale;
const offsetY = (e.clientY - rect.top) / previousScale;
// Adjust translate to keep cursor point at the same board position
translateX -= offsetX * (scale - previousScale);
translateY -= offsetY * (scale - previousScale);
board.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scale})`;
boardWrapper.style.backgroundPosition = `${translateX}px ${translateY}px`;
boardWrapper.addEventListener('touchstart', (e) => {
if (e.touches.length === 2) {
e.preventDefault();
const touch1 = e.touches[0];
const touch2 = e.touches[1];
const startDistance = Math.hypot(touch2.clientX - touch1.clientX, touch2.clientY - touch1.clientY);
const startScale = scale;
code Copy code
function zoom(e) {
if (e.touches.length !== 2) return;
const touch1 = e.touches[0];
const touch2 = e.touches[1];
const currentDistance = Math.hypot(touch2.clientX - touch1.clientX, touch2.clientY - touch1.clientY);
const newScale = startScale * (currentDistance / startDistance);
scale = Math.min(Math.max(MIN_SCALE, newScale), MAX_SCALE);
// Calculate midpoint of touches
const midX = (touch1.clientX + touch2.clientX) / 2;
const midY = (touch1.clientY + touch2.clientY) / 2;
// Calculate offset relative to previous scale
const rect = board.getBoundingClientRect();
const offsetX = (midX - rect.left) / startScale;
const offsetY = (midY - rect.top) / startScale;
// Adjust translate to keep midpoint at same board position
translateX -= offsetX * (scale - startScale);
translateY -= offsetY * (scale - startScale);
board.style.transform = `translate(${translateX}px, ${translateY}px) scale(${scale})`;
boardWrapper.style.backgroundPosition = `${translateX}px ${translateY}px`;
}
document.addEventListener('touchmove', zoom);
document.addEventListener('touchend', () => {
document.removeEventListener('touchmove', zoom);
}, { once: true });
}
let isPanning = false;
let startPanX, startPanY;
boardWrapper.addEventListener('mousedown', (e) => {
if (e.button === 2) { // Right mouse button
isPanning = true;
startPanX = e.clientX - translateX;
startPanY = e.clientY - translateY;
boardWrapper.style.cursor = 'grabbing';
}
});
boardWrapper.addEventListener('mouseup', (e) => {
if (e.button === 2) { // Right mouse button
isPanning = false;
boardWrapper.style.cursor = 'grab';
}
});
boardWrapper.addEventListener('mousemove', (e) => {
if (!isPanning) return;
e.preventDefault();
translateX = e.clientX - startPanX;
translateY = e.clientY - startPanY;
board.style.transform = translate(${translateX}px, ${translateY}px) scale(${scale});
boardWrapper.style.backgroundPosition = ${translateX}px ${translateY}px;
});
// Add touch events for panning
boardWrapper.addEventListener('touchstart', (e) => {
if (e.touches.length === 1) {
isPanning = true;
startPanX = e.touches[0].clientX - translateX;
startPanY = e.touches[0].clientY - translateY;
boardWrapper.style.cursor = 'grabbing';
}
});
boardWrapper.addEventListener('touchend', (e) => {
isPanning = false;
boardWrapper.style.cursor = 'grab';
});
boardWrapper.addEventListener('touchmove', (e) => {
if (!isPanning) return;
e.preventDefault();
translateX = e.touches[0].clientX - startPanX;
translateY = e.touches[0].clientY - startPanY;
board.style.transform = translate(${translateX}px, ${translateY}px) scale(${scale});
boardWrapper.style.backgroundPosition = ${translateX}px ${translateY}px;
});
// Prevent context menu on board-wrapper
boardWrapper.addEventListener('contextmenu', (e) => {
e.preventDefault();
});
// Проверяем, что 'socket' уже инициализирован
if (!window.socket) {
console.error('Socket is not initialized!');
}
// Event listener для кнопки 'add-note'
document.getElementById('add-note').addEventListener('click', () => {
const x = Math.floor(Math.random() * window.innerWidth);
const y = Math.floor(Math.random() * window.innerHeight);
const width = 200; // Ширина по умолчанию
const height = 40; // Высота по умолчанию
socket.emit('create_note', { text: 'New Comment', x, y, width, height });
});
// Event listener для кнопки 'add-image'
document.getElementById('add-image').addEventListener('click', () => {
const formHtml = <div id="image-form" style="position:fixed; top:50%; left:50%; transform:translate(-50%, -50%); background:white; padding:20px; border-radius:8px; box-shadow:0 0 10px rgba(0,0,0,0.1); z-index: 1000;"> <label for="image-url" style="display:block; margin-bottom:8px;">Image URL:</label> <input type="text" id="image-url" placeholder="Enter image URL" style="width: calc(100% - 22px); padding:10px; margin-bottom:12px; border:1px solid #ccc; border-radius:4px;"><br> <label for="image-file" style="display:block; margin-bottom:8px;">Or Upload File:</label> <input type="file" id="image-file" accept="image/*" style="margin-bottom:12px;"><br> <button id="add-image-submit" style="padding:10px 20px; background-color:#007bff; color:white; border:none; border-radius:4px; cursor:pointer; margin-right:8px;">Add Image</button> <button id="cancel-image" style="padding:10px 20px; background-color:#ccc; color:white; border:none; border-radius:4px; cursor:pointer;">Cancel</button> </div> ;
document.body.insertAdjacentHTML('beforeend', formHtml);
code Copy code
document.getElementById('add-image-submit').addEventListener('click', async () => {
const url = document.getElementById('image-url').value;
const fileInput = document.getElementById('image-file');
let imageUrl;
if (url) {
imageUrl = url;
} else if (fileInput.files.length > 0) {
const file = fileInput.files[0];
imageUrl = await uploadFile(file);
}
if (imageUrl) {
const x = Math.floor(Math.random() * window.innerWidth);
const y = Math.floor(Math.random() * window.innerHeight);
const img = new Image();
img.src = imageUrl;
img.onload = () => {
const maxWidth = 720;
const maxHeight = 720;
let width = img.naturalWidth;
let height = img.naturalHeight;
if (width > maxWidth || height > maxHeight) {
const ratio = Math.min(maxWidth / width, maxHeight / height);
width *= ratio;
height *= ratio;
}
socket.emit('create_image', { url: imageUrl, x, y, width, height });
closeModal();
};
}
});
document.getElementById('cancel-image').addEventListener('click', closeModal);
function closeModal() {
const form = document.getElementById('image-form');
if (form) {
form.remove();
}
}
// Обработка существующих заметок и изображений после загрузки страницы
document.addEventListener('DOMContentLoaded', () => {
handleExistingNotes();
handleExistingImages();
});
code Copy code
<!-- Подключение Socket.io -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
<!-- Инициализация Socket -->
<script src="{{ url_for('static', filename='js/socket.js') }}"></script>
<!-- Другие JavaScript файлы -->
<script src="{{ url_for('static', filename='js/upload.js') }}"></script>
<script src="{{ url_for('static', filename='js/contextMenu.js') }}"></script>
<script src="{{ url_for('static', filename='js/resize.js') }}"></script>
<script src="{{ url_for('static', filename='js/notes.js') }}"></script>
<script src="{{ url_for('static', filename='js/images.js') }}"></script>
<script src="{{ url_for('static', filename='js/board.js') }}"></script>
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
function showContextMenu(element, id, x, y) {
const contextMenu = document.createElement('div');
contextMenu.className = 'context-menu';
contextMenu.style.position = 'absolute';
contextMenu.style.left = ${x}px;
contextMenu.style.top = ${y}px;
contextMenu.style.backgroundColor = 'white';
contextMenu.style.borderRadius = '4px';
contextMenu.style.border = '1px solid #ccc';
contextMenu.style.boxShadow = '0 0 10px rgba(0,0,0,0.1)';
contextMenu.style.zIndex = '1000';
code Copy code
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.style.padding = '5px 10px';
deleteButton.style.border = 'none';
deleteButton.style.backgroundColor = '#ff4d4d';
deleteButton.style.color = 'white';
deleteButton.style.borderRadius = '4px';
deleteButton.style.cursor = 'pointer';
deleteButton.style.width = '100%';
deleteButton.style.textAlign = 'left';
deleteButton.addEventListener('click', () => {
element.remove();
socket.emit('delete_element', { id: id, type: element.classList.contains('note') ? 'note' : 'image' });
contextMenu.remove();
});
contextMenu.appendChild(deleteButton);
document.body.appendChild(contextMenu);
// Close context menu when clicking outside
document.addEventListener('click', (e) => {
if (!contextMenu.contains(e.target)) {
contextMenu.remove();
}
}, { once: true });
Помоги мне. Мне надо чтобы на устройствах с тачпадом в контекстном меню изображения была еще кнопка "Move" и при нажатии на которую включался режим перемещения элемента. Пока на нее не нажать элемент не должен перемещаться. Это нужно только для тачпада, для мыши все оставляем как сейчас есть. Убедись что логика управления мышью не нарушена
To implement the requested functionality—adding a "Move" button to the context menu on touchpad devices and enabling movement mode only after clicking this button—you'll need to make modifications to both contextMenu.js and images.js. Below are the step-by-step instructions along with the necessary code changes to achieve this without disrupting the existing mouse-based interactions.
1. Detecting Touchpad Devices
First, we need a reliable way to detect touchpad (touch-enabled) devices. While it's challenging to perfectly distinguish between touchpads and traditional touch devices, a common approach is to check for touch event support. This isn't foolproof but works well in most scenarios.
2. Modifying contextMenu.js
We'll update the showContextMenu function to include an additional "Move" button for touch-enabled devices. When this button is clicked, it will enable the movement mode for the selected image.
Updated contextMenu.js:
javascript Copy code
// contextMenu.js
function isTouchDevice() {
return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
}
function showContextMenu(element, id, x, y) {
// Remove any existing context menu
const existingMenu = document.querySelector('.context-menu');
if (existingMenu) existingMenu.remove();
const contextMenu = document.createElement('div');
contextMenu.className = 'context-menu';
contextMenu.style.position = 'absolute';
contextMenu.style.left = `${x}px`;
contextMenu.style.top = `${y}px`;
contextMenu.style.backgroundColor = 'white';
contextMenu.style.borderRadius = '4px';
contextMenu.style.border = '1px solid #ccc';
contextMenu.style.boxShadow = '0 0 10px rgba(0,0,0,0.1)';
contextMenu.style.zIndex = '1000';
contextMenu.style.padding = '5px 0';
// Delete Button
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.style.padding = '5px 20px';
deleteButton.style.border = 'none';
deleteButton.style.backgroundColor = '#ff4d4d';
deleteButton.style.color = 'white';
deleteButton.style.borderRadius = '4px';
deleteButton.style.cursor = 'pointer';
deleteButton.style.width = '100%';
deleteButton.style.textAlign = 'left';
deleteButton.addEventListener('click', () => {
element.remove();
socket.emit('delete_element', { id: id, type: element.classList.contains('note') ? 'note' : 'image' });
contextMenu.remove();
});
contextMenu.appendChild(deleteButton);
// If it's a touch device, add the "Move" button
if (isTouchDevice()) {
const moveButton = document.createElement('button');
moveButton.textContent = 'Move';
moveButton.style.padding = '5px 20px';
moveButton.style.border = 'none';
moveButton.style.backgroundColor = '#4CAF50';
moveButton.style.color = 'white';
moveButton.style.borderRadius = '4px';
moveButton.style.cursor = 'pointer';
moveButton.style.width = '100%';
moveButton.style.textAlign = 'left';
moveButton.style.marginTop = '5px';
moveButton.addEventListener('click', () => {
// Enable move mode for this element
element.classList.add('move-mode');
contextMenu.remove();
});
contextMenu.appendChild(moveButton);
}
document.body.appendChild(contextMenu);
// Close context menu when clicking outside
document.addEventListener('click', (e) => {
if (!contextMenu.contains(e.target)) {
contextMenu.remove();
}
}, { once: true });
}
Explanation of Changes:
Device Detection: Added the isTouchDevice function to check if the device supports touch events.
Context Menu Structure: Updated the showContextMenu function to first remove any existing context menu to prevent duplicates.
"Move" Button: If the device is touch-enabled, a "Move" button is appended to the context menu. Clicking this button adds a move-mode class to the element, signaling that it can now be moved.
Styling Enhancements: Added some padding and margin to improve the visual appearance of the context menu.
3. Modifying images.js
Next, we'll update the image movement logic to respect the move-mode state. Images should only be draggable on touch devices if they have the move-mode class. On mouse devices, the behavior remains unchanged.
Updated images.js:
Explanation of Changes:
Movement Control:
Mouse Events: For mouse devices, movement remains as before. Users can drag images without needing to activate a move mode.
Touch Events: On touch devices, movement is only enabled if the image has the move-mode class (added via the "Move" button in the context menu).
startDrag Function:
Created a separate startDrag function to handle both mouse and touch dragging logic. This function takes an additional parameter isTouch to differentiate between touch and mouse events.
After dragging on touch devices, if the image was in move-mode, the mode is deactivated by removing the move-mode class. This ensures that movement mode is session-based and not persistent.
Event Listener Adjustments:
Touch Start: Modified the touchstart event listener to check for the move-mode class before initiating a drag.
Prevent Default for Multiple Touches: Kept the existing logic for handling two-finger touches to show the context menu.
Class Management:
The move-mode class is used as a flag to determine if an image should be movable on touch devices. This class is added via the "Move" button in the context menu and removed after the drag operation concludes.
4. Updating CSS (Optional but Recommended)
It's essential to provide visual feedback that an image is in "move-mode". You can add some CSS to highlight movable images.
Add the following CSS to your style.css:
css Copy code
/* style.css */
/* Existing styles... */
.image.move-mode {
border: 2px dashed #4CAF50;
cursor: grab;
}
Explanation:
The .move-mode class adds a dashed green border to indicate that the image is now in move mode.
The cursor changes to a grab icon to provide a visual cue to the user.
5. Ensuring Non-Intrusive Changes
These modifications ensure that:
Mouse Users: Experience no changes. They can continue to drag images as before without any additional steps.
Touchpad/Touch Device Users: Must click the "Move" button in the context menu to initiate dragging. This prevents accidental movements and provides controlled interaction.
6. Final Integration
Ensure that all your JavaScript files are correctly linked in your HTML and that contextMenu.js and images.js are updated with the above changes. Here's how your HTML should roughly look:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Miro Clone</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<div id="board-wrapper">
<div id="board">
{% for note in notes %}
<div class="note" data-id="{{ note.id }}" style="left: {{ note.x }}px; top: {{ note.y }}px; width: {{ note.width }}px; height: {{ note.height }}px;">
<textarea>{{ note.text }}</textarea>
</div>
{% endfor %}
{% for image in images %}
<div class="image hidden" data-id="{{ image.id }}" style="left: {{ image.x }}px; top: {{ image.y }}px; width: {{ image.width }}px; height: {{ image.height }}px;">
<img src="{{ image.url }}" style="width: 100%; height: 100%; object-fit: contain;">
</div>
{% endfor %}
</div>
</div>
<button id="add-note">Add Comment</button>
<button id="add-image">Add Image</button>
<!-- Подключение Socket.io -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
<!-- Инициализация Socket -->
<script src="{{ url_for('static', filename='js/socket.js') }}"></script>
<!-- Другие JavaScript файлы -->
<script src="{{ url_for('static', filename='js/upload.js') }}"></script>
<script src="{{ url_for('static', filename='js/contextMenu.js') }}"></script>
<script src="{{ url_for('static', filename='js/resize.js') }}"></script>
<script src="{{ url_for('static', filename='js/notes.js') }}"></script>
<script src="{{ url_for('static', filename='js/images.js') }}"></script>
<script src="{{ url_for('static', filename='js/board.js') }}"></script>
<script src="{{ url_for('static', filename='js/main.js') }}"></script>
</body>
</html>
7. Testing the Implementation
After making these changes:
On Mouse Devices:
Right-click an image to open the context menu.
The context menu should only have the "Delete" button.
You should be able to drag images as before without any restrictions.
On Touchpad/Touch Devices:
Long-press or use the appropriate gesture to open the context menu.
The context menu should have both "Delete" and "Move" buttons.
Without clicking "Move", attempting to drag the image should have no effect.
After clicking "Move", the image should be draggable.
Once dragging is complete, the image exits "move-mode" and requires "Move" to be clicked again for subsequent drags.
8. Additional Considerations
Accessibility: Ensure that keyboard users can also interact with the context menu and "Move" functionality if applicable.
State Management: If your application involves more complex state management (e.g., multiple elements in different modes), consider using a more robust state management solution or framework.
Cleanup: Ensure that multiple context menus don't stack by removing any existing context menu before creating a new one.
By following these steps and implementing the above code changes, you can achieve the desired functionality where touchpad users have enhanced control over moving images without affecting the existing mouse interactions.
Show full message · 358 lines Copy first response
Not yet included in a published release.
// images.js
// Assumes uploadFile, showContextMenu, addResizeListeners are available globally
socket.on('new_image', (data) => {
const image = createImageElement(data);
document.getElementById('board').appendChild(image);
});
socket.on('updated_image', (data) => {
const image = document.querySelector(`.image[data-id="${data.id}"]`);
if (image) {
image.style.left = `${data.x}px`;
image.style.top = `${data.y}px`;
image.style.width = `${data.width}px`;
image.style.height = `${data.height}px`;
image.querySelector('img').src = data.url;
}
});
socket.on('element_deleted', (data) => {
if (data.type === 'image') {
const element = document.querySelector(`.image[data-id="${data.id}"]`);
if (element) {
element.remove();
}
}
});
function createImageElement(data) {
const image = document.createElement('div');
image.className = 'image';
image.dataset.id = data.id;
image.style.left = `${data.x}px`;
image.style.top = `${data.y}px`;
image.style.width = `${data.width}px`;
image.style.height = `${data.height}px`;
const img = document.createElement('img');
img.src = data.url;
img.style.width = '100%';
img.style.height = '100%';
img.style.objectFit = 'contain'; // Maintains aspect ratio
image.appendChild(img);
addImageEventListeners(image, data.id);
return image;
}
function addImageEventListeners(image, id) {
// Handle mouse events
image.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // Left mouse button
// On mouse devices, allow movement without needing "Move" button
startDrag(e, image, id, false);
});
// Handle touch events
image.addEventListener('touchstart', (e) => {
if (e.touches.length !== 1) return;
// On touch devices, allow movement only if in "move-mode"
if (!image.classList.contains('move-mode')) return;
startDrag(e.touches[0], image, id, true);
});
image.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.clientX, e.clientY);
});
image.addEventListener('touchstart', (e) => {
if (e.touches.length === 2) {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.touches[0].clientX, e.touches[0].clientY);
}
});
addResizeListeners(image, id, 'image');
}
function startDrag(event, image, id, isTouch) {
event.preventDefault();
event.stopPropagation();
const startX = event.clientX;
const startY = event.clientY;
const startLeft = parseFloat(image.style.left) || 0;
const startTop = parseFloat(image.style.top) || 0;
function moveImage(e) {
const currentX = isTouch ? e.clientX : e.clientX;
const currentY = isTouch ? e.clientY : e.clientY;
const deltaX = (currentX - startX) / scale;
const deltaY = (currentY - startY) / scale;
image.style.left = `${startLeft + deltaX}px`;
image.style.top = `${startTop + deltaY}px`;
const x = parseFloat(image.style.left) || 0;
const y = parseFloat(image.style.top) || 0;
const width = image.offsetWidth;
const height = image.offsetHeight;
socket.emit('update_image', { id, url: image.querySelector('img').src, x, y, width, height });
}
document.addEventListener(isTouch ? 'touchmove' : 'mousemove', moveImage);
document.addEventListener(isTouch ? 'touchend' : 'mouseup', () => {
document.removeEventListener(isTouch ? 'touchmove' : 'mousemove', moveImage);
// If it was touch and in move-mode, remove the mode after dragging
if (isTouch && image.classList.contains('move-mode')) {
image.classList.remove('move-mode');
}
}, { once: true });
}
function handleExistingImages() {
document.querySelectorAll('.image').forEach(image => {
const id = image.dataset.id;
const url = image.querySelector('img').src;
const x = image.offsetLeft;
const y = image.offsetTop;
const width = image.offsetWidth;
const height = image.offsetHeight;
image.addEventListener('dblclick', (e) => {
e.preventDefault();
e.stopPropagation();
showContextMenu(image, id, e.clientX, e.clientY);
});
addImageEventListeners(image, id);
});
}