I'm trying to make a website of mine responsive. It uses JavaScript
to let you select parts of an image on PC
but it doesn't work on mobile. That's because of the events used - mouseup
, mousedown
, mousemove
. How can I refactor my code so that it works on mobile? All I need to know is the coordinates of the selection.
Current code:
editCanvas.addEventListener("mousedown", (event) => {
painting = true;
canvasPosition = editCanvas.getBoundingClientRect();
startCoords = {
x: event.clientX - canvasPosition.left,
y: event.clientY - canvasPosition.top,
};
});
editCanvas.addEventListener("mousemove", (event) => {
if (painting) {
canvasPosition = editCanvas.getBoundingClientRect();
let endCoords = {
x: event.clientX - canvasPosition.left,
y: event.clientY - canvasPosition.top,
};
renderSelections();
dimensions = {
x: startCoords.x,
y: startCoords.y,
width: endCoords.x - startCoords.x,
height: endCoords.y - startCoords.y,
};
drawSelectionRect("#", dimensions, strokeStyle, "#BBBB");
}
});
editCanvas.addEventListener("mouseup", () => (painting = false));
function drawSelectionRect(
text,
{ x, y, width, height },
{ strokeWidth, lineDash, strokeColor },
fillColor
) {
ctx.lineWidth = strokeWidth;
ctx.setLineDash(lineDash);
ctx.strokeStyle = strokeColor;
ctx.strokeRect(x, y, width, height);
ctx.fillStyle = fillColor;
ctx.fillRect(x, y, width, height);
ctx.fillStyle = "#000";
ctx.font = "16px sans-serif";
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillText(text, x + width / 2, y + height / 2, width);
}
Update:
editCanvas.addEventListener("mousedown", (event) => {
painting = true;
canvasPosition = editCanvas.getBoundingClientRect();
startCoords = {
x: event.clientX - canvasPosition.left,
y: event.clientY - canvasPosition.top,
};
});
editCanvas.addEventListener("mousemove", (event) => {
if (painting) {
canvasPosition = editCanvas.getBoundingClientRect();
let endCoords = {
x: event.clientX - canvasPosition.left,
y: event.clientY - canvasPosition.top,
};
renderSelections();
dimensions = {
x: startCoords.x,
y: startCoords.y,
width: endCoords.x - startCoords.x,
height: endCoords.y - startCoords.y,
};
dimensions = correctDimensions(dimensions);
drawSelectionRect("#", dimensions, strokeStyle, "#BBBB");
}
});
editCanvas.addEventListener("mouseup", () => (painting = false));
editCanvas.addEventListener("touchstart", (event) => {
event.preventDefault();
painting = true;
canvasPosition = editCanvas.getBoundingClientRect();
startCoords = {
x: event.touches[0].clientX - canvasPosition.left,
y: event.touches[0].clientY - canvasPosition.top,
};
});
editCanvas.addEventListener("touchmove", (event) => {
event.preventDefault();
if (painting) {
canvasPosition = editCanvas.getBoundingClientRect();
let endCoords = {
x: event.touches[0].clientX - canvasPosition.left,
y: event.touches[0].clientY - canvasPosition.top,
};
renderSelections();
dimensions = {
x: startCoords.x,
y: startCoords.y,
width: endCoords.x - startCoords.x,
height: endCoords.y - startCoords.y,
};
dimensions = correctDimensions(dimensions);
drawSelectionRect("#", dimensions, strokeStyle, "#BBBB");
}
});
editCanvas.addEventListener("touchend", () => (painting = false));
function drawSelectionRect(
text,
{ x, y, width, height },
{ strokeWidth, lineDash, strokeColor },
fillColor
) {
ctx.lineWidth = strokeWidth;
ctx.setLineDash(lineDash);
ctx.strokeStyle = strokeColor;
ctx.strokeRect(x, y, width, height);
ctx.fillStyle = fillColor;
ctx.fillRect(x, y, width, height);
ctx.fillStyle = "#000";
ctx.font = "16px sans-serif";
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillText(text, x + width / 2, y + height / 2, width);
}