Sure! Below is the complete HTML code for the UV map editor tool you described. This code includes all the functionalities you've requested:
- Canvas for displaying and handling the image
- Button to import an image
- Zoom in/out with the mouse wheel, focusing on the cursor position
- Drag the image using the middle mouse button
- Create selections by holding Ctrl and click-and-drag
- Selections stick to the image when zooming or moving
- Modify selections using handles at the corners and edges
- Assign selections to the six faces of the box
- Export the selections to a
.uv1 file
Please copy the entire code below into a new .html file and open it in your browser to use the tool.
<!DOCTYPE html>
<html>
<head>
<title>UV Map Editor</title>
<style>
body, html {
margin: 0;
padding: 0;
overflow: hidden; /* Disable scrollbars */
width: 100%;
height: 100%;
position: relative;
user-select: none;
}
#canvas {
/* Full page */
width: 100%;
height: 100%;
display: block;
cursor: default;
}
#toolbar {
position: absolute;
top: 10px;
left: 10px;
background: rgba(255,255,255,0.8);
padding: 10px;
z-index: 10;
border-radius: 5px;
}
#toolbar input,
#toolbar select,
#toolbar button {
margin: 5px 0;
}
#faceSelector {
margin-top: 10px;
}
</style>
</head>
<body>
<div id="toolbar">
<input type="file" id="imageLoader" name="imageLoader" accept="image/*"/>
<button id="exportBtn">Export UV1</button>
<div id="faceSelector">
<label for="faceSelect">Assign Face:</label>
<select id="faceSelect">
<option value="">--Select Face--</option>
<option value="front">Front</option>
<option value="back">Back</option>
<option value="right">Right</option>
<option value="left">Left</option>
<option value="top">Top</option>
<option value="bottom">Bottom</option>
</select>
<button id="deleteBtn">Delete Selection</button>
</div>
</div>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.addEventListener('resize', function(){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
draw();
});
var imageLoader = document.getElementById('imageLoader');
imageLoader.addEventListener('change', handleImage, false);
var image = null;
function handleImage(e){
var reader = new FileReader();
reader.onload = function(event){
var img = new Image();
img.onload = function(){
image = img;
initTransforms();
draw();
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
var scale = 1;
var translatePos = { x: 0, y: 0 };
var startDragOffset = {};
var mouseDown = false;
function initTransforms() {
scale = 1;
translatePos = { x: canvas.width / 2 - image.width / 2, y: canvas.height / 2 - image.height / 2 };
}
canvas.addEventListener('mousedown', function(evt){
if(evt.button === 1) { // middle mouse button
mouseDown = true;
startDragOffset.x = evt.clientX - translatePos.x;
startDragOffset.y = evt.clientY - translatePos.y;
evt.preventDefault();
} else if(evt.ctrlKey && evt.button === 0) { // Ctrl + left click
startSelection(evt);
} else if(evt.button === 0) {
checkHandleHit(evt);
}
});
canvas.addEventListener('mouseup', function(evt){
if(evt.button === 1) { // middle mouse button
mouseDown = false;
} else if(evt.button === 0) {
if(selecting) {
finishSelection(evt);
}
resizing = false;
draggingRect = false;
}
});
canvas.addEventListener('mousemove', function(evt){
if(mouseDown) { // panning
translatePos.x = evt.clientX - startDragOffset.x;
translatePos.y = evt.clientY - startDragOffset.y;
draw();
} else if(selecting) {
updateSelection(evt);
} else if(resizing) {
resizeRectangle(evt);
} else if(draggingRect) {
dragRectangle(evt);
}
});
canvas.addEventListener('wheel', function(evt){
// Zoom in or out
evt.preventDefault();
var mousePos = getMousePos(canvas, evt);
var wheel = evt.deltaY < 0 ? 1 : -1;
var zoom = Math.exp(wheel * 0.05);
var beforeZoom = {
x: (mousePos.x - translatePos.x) / scale,
y: (mousePos.y - translatePos.y) / scale
};
scale *= zoom;
var afterZoom = {
x: (mousePos.x - translatePos.x) / scale,
y: (mousePos.y - translatePos.y) / scale
};
translatePos.x += (afterZoom.x - beforeZoom.x) * scale;
translatePos.y += (afterZoom.y - beforeZoom.y) * scale;
draw();
}, { passive: false });
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
function getTransformedPoint(x, y) {
var invScale = 1 / scale;
return {
x: (x - translatePos.x) * invScale,
y: (y - translatePos.y) * invScale
};
}
var selections = []; // array of selection rectangles
var selecting = false;
var selectionStart = {};
var currentSelection = null;
var resizing = false;
var draggingRect = false;
var selectedRect = null;
var selectedHandle = null;
function draw(){
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0); // Reset transformation matrix
ctx.clearRect(0, 0, canvas.width, canvas.height); // clear the canvas
ctx.restore();
if(image){
ctx.save();
ctx.translate(translatePos.x, translatePos.y);
ctx.scale(scale, scale);
ctx.drawImage(image, 0, 0);
// Draw selection rectangles
drawSelections();
ctx.restore();
}
}
function drawSelections(){
ctx.lineWidth = 2 / scale; // adjust line width according to scale
ctx.fillStyle = 'rgba(255,0,0,0.3)';
selections.forEach(function(selection){
if(selection === selectedRect){
ctx.strokeStyle = 'green';
} else {
ctx.strokeStyle = 'red';
}
ctx.beginPath();
ctx.rect(selection.x, selection.y, selection.width, selection.height);
ctx.fill();
ctx.stroke();
drawHandles(selection);
});
if(currentSelection){
ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.rect(currentSelection.x, currentSelection.y, currentSelection.width, currentSelection.height);
ctx.fill();
ctx.stroke();
drawHandles(currentSelection);
}
}
function drawHandles(selection){
var handles = getHandles(selection);
var size = 8 / scale; // size of handle adjusted for scale
ctx.fillStyle = 'blue';
ctx.strokeStyle = 'blue';
handles.forEach(function(handle){
ctx.beginPath();
ctx.rect(handle.x - size / 2, handle.y - size / 2, size, size);
ctx.fill();
ctx.stroke();
});
}
function getHandles(selection){
var x = selection.x;
var y = selection.y;
var w = selection.width;
var h = selection.height;
var handles = [
{ x: x, y: y, position: 'nw' },
{ x: x + w / 2, y: y, position: 'n' },
{ x: x + w, y: y, position: 'ne' },
{ x: x + w, y: y + h / 2, position: 'e' },
{ x: x + w, y: y + h, position: 'se' },
{ x: x + w / 2, y: y + h, position: 's' },
{ x: x, y: y + h, position: 'sw' },
{ x: x, y: y + h / 2, position: 'w' }
];
return handles;
}
function startSelection(evt){
selecting = true;
var mousePos = getTransformedPoint(evt.clientX, evt.clientY);
selectionStart = { x: mousePos.x, y: mousePos.y };
currentSelection = { x: mousePos.x, y: mousePos.y, width: 0, height: 0, face: null };
}
function updateSelection(evt){
var mousePos = getTransformedPoint(evt.clientX, evt.clientY);
currentSelection.width = mousePos.x - selectionStart.x;
currentSelection.height = mousePos.y - selectionStart.y;
draw();
}
function finishSelection(evt){
if(Math.abs(currentSelection.width) > 10 && Math.abs(currentSelection.height) > 10){
selections.push(currentSelection);
}
selecting = false;
currentSelection = null;
draw();
}
function checkHandleHit(evt){
var mousePos = getTransformedPoint(evt.clientX, evt.clientY);
for(var i = selections.length - 1; i >= 0; i--){ // reverse order to select topmost first
var selection = selections[i];
var handle = isOverHandle(selection, mousePos);
if(handle){
resizing = true;
selectedRect = selection;
selectedHandle = handle;
updateFaceSelect();
break;
} else if(isInsideRect(selection, mousePos)){
draggingRect = true;
selectedRect = selection;
startDragOffset.x = mousePos.x - selection.x;
startDragOffset.y = mousePos.y - selection.y;
updateFaceSelect();
break;
}
}
}
function isOverHandle(selection, mousePos){
var handles = getHandles(selection);
var size = 8 / scale; // size of handle adjusted for scale
for(var i = 0; i < handles.length; i++){
var handle = handles[i];
if(mousePos.x >= handle.x - size / 2 && mousePos.x <= handle.x + size / 2 &&
mousePos.y >= handle.y - size / 2 && mousePos.y <= handle.y + size / 2){
return handle.position;
}
}
return null;
}
function isInsideRect(selection, mousePos){
return mousePos.x >= selection.x && mousePos.x <= selection.x + selection.width &&
mousePos.y >= selection.y && mousePos.y <= selection.y + selection.height;
}
function resizeRectangle(evt){
var mousePos = getTransformedPoint(evt.clientX, evt.clientY);
var selection = selectedRect;
var x1 = selection.x;
var y1 = selection.y;
var x2 = selection.x + selection.width;
var y2 = selection.y + selection.height;
switch(selectedHandle){
case 'nw':
x1 = mousePos.x;
y1 = mousePos.y;
break;
case 'n':
y1 = mousePos.y;
break;
case 'ne':
x2 = mousePos.x;
y1 = mousePos.y;
break;
case 'e':
x2 = mousePos.x;
break;
case 'se':
x2 = mousePos.x;
y2 = mousePos.y;
break;
case 's':
y2 = mousePos.y;
break;
case 'sw':
x1 = mousePos.x;
y2 = mousePos.y;
break;
case 'w':
x1 = mousePos.x;
break;
}
// Update selection rectangle
selection.x = Math.min(x1, x2);
selection.y = Math.min(y1, y2);
selection.width = Math.abs(x2 - x1);
selection.height = Math.abs(y2 - y1);
draw();
}
function dragRectangle(evt){
var mousePos = getTransformedPoint(evt.clientX, evt.clientY);
selectedRect.x = mousePos.x - startDragOffset.x;
selectedRect.y = mousePos.y - startDragOffset.y;
draw();
}
function updateFaceSelect(){
var faceSelect = document.getElementById('faceSelect');
if(selectedRect){
faceSelect.value = selectedRect.face || '';
} else {
faceSelect.value = '';
}
}
document.getElementById('faceSelect').addEventListener('change', function(evt){
if(selectedRect){
selectedRect.face = this.value;
}
});
document.getElementById('exportBtn').addEventListener('click', function(){
exportUV1();
});
function exportUV1(){
var lines = [];
selections.forEach(function(selection){
if(selection.face){
var x = Math.round(selection.x);
var y = Math.round(selection.y);
var width = Math.round(selection.width);
var height = Math.round(selection.height);
var line = selection.face + ',' + x + ',' + y + ',' + width + ',' + height;
lines.push(line);
}
});
var uv1Content = lines.join('\n');
download('uvmap.uv1', uv1Content);
}
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
document.getElementById('deleteBtn').addEventListener('click', function(){
if(selectedRect){
var index = selections.indexOf(selectedRect);
if(index > -1){
selections.splice(index,1);
selectedRect = null;
updateFaceSelect();
draw();
}
}
});
document.addEventListener('keydown', function(evt){
if(evt.code === 'Delete' && selectedRect){
var index = selections.indexOf(selectedRect);
if(index > -1){
selections.splice(index,1);
selectedRect = null;
updateFaceSelect();
draw();
}
}
});
</script>
</body>
</html>
Instructions:
-
Load an Image:
- Click on the "Choose File" button to import an image onto the canvas.
-
Zooming:
- Use the mouse wheel to zoom in or out. The zoom will focus on the cursor's position.
-
Panning:
- Click and hold the middle mouse button (scroll wheel button), then move the mouse to drag the image around.
-
Creating Selections:
- Hold down the Ctrl key and use the left mouse button to click and drag over the area you want to select.
-
Modifying Selections:
- Click on a selection to select it (it will be outlined in green).
- Modify the selection by dragging the handles at the corners and edges.
-
Assigning Faces:
- With a selection selected, choose a face from the "Assign Face" dropdown menu.
-
Deleting Selections:
- With a selection selected, click the "Delete Selection" button or press the Delete key on your keyboard.
-
Exporting UV1 File:
- Click on the "Export UV1" button to download the
.uv1 file containing the data of all assigned faces.
Notes:
- The
.uv1 file will contain lines in the format: face_name, rect_x_coordinate, rect_y_coordinate, rect_x_size, rect_y_size.
- Only selections that have a face assigned will be included in the exported file.
- The selections will stay relative to the image when you zoom or pan, ensuring accuracy.
Example of a Generated .uv1 File:
front,100,150,200,250
back,400,450,200,250
left,700,150,200,250
...
Feel free to customize and enhance the code to better suit your needs!