The error you're encountering, Uncaught TypeError: this.activeHandle.includes is not a function, suggests that this.activeHandle is not a string at some point where you're attempting to use the includes method. This typically means activeHandle might be null or some other datatype.
To fix this, let's add checks to ensure that activeHandle is properly initialized as a string and that the handling of activeHandle is correct throughout the code. Here’s how you can address this:
Updated Code with Debugs and Checks
Ensure activeHandle is set correctly and safely checked:
<script>
export default {
data() {
return {
image: null,
isDrawing: false,
isDragging: false,
isResizing: false,
startX: 0,
startY: 0,
currentX: 0,
currentY: 0,
activeRectIndex: null,
rectangles: [],
activeHandle: null,
handleSize: 8 // Size of the resize handles
};
},
methods: {
loadImage(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => this.createImage(e.target.result);
reader.readAsDataURL(file);
},
createImage(src) {
const img = new Image();
img.onload = () => {
this.image = img;
this.setupCanvas();
};
img.src = src;
},
setupCanvas() {
const canvas = this.$refs.canvas;
canvas.width = this.image.width;
canvas.height = this.image.height;
this.drawImage();
},
drawImage() {
const canvas = this.$refs.canvas;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(this.image, 0, 0, canvas.width, canvas.height);
this.drawRectangles();
},
drawRectangles() {
const ctx = this.$refs.canvas.getContext('2d');
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
this.rectangles.forEach((rect, index) => {
const rectX = rect.x * this.$refs.canvas.width;
const rectY = rect.y * this.$refs.canvas.height;
const rectWidth = rect.width * this.$refs.canvas.width;
const rectHeight = rect.height * this.$refs.canvas.height;
ctx.strokeRect(rectX, rectY, rectWidth, rectHeight);
if (index === this.activeRectIndex) {
this.drawHandles(ctx, rectX, rectY, rectWidth, rectHeight);
}
});
},
drawHandles(ctx, x, y, width, height) {
const halfSize = this.handleSize / 2;
const positions = [
{ x: x - halfSize, y: y - halfSize, handle: 'top-left' },
{ x: x + width - halfSize, y: y - halfSize, handle: 'top-right' },
{ x: x - halfSize, y: y + height - halfSize, handle: 'bottom-left' },
{ x: x + width - halfSize, y: y + height - halfSize, handle: 'bottom-right' },
{ x: x + width / 2 - halfSize, y: y - halfSize, handle: 'top-middle' },
{ x: x + width / 2 - halfSize, y: y + height - halfSize, handle: 'bottom-middle' },
{ x: x - halfSize, y: y + height / 2 - halfSize, handle: 'middle-left' },
{ x: x + width - halfSize, y: y + height / 2 - halfSize, handle: 'middle-right' }
];
ctx.fillStyle = 'blue';
positions.forEach(pos => {
ctx.fillRect(pos.x, pos.y, this.handleSize, this.handleSize);
});
},
handleMouseDown(event) {
const rect = this.$refs.canvas.getBoundingClientRect();
const mouseX = (event.clientX - rect.left) * (this.$refs.canvas.width / rect.width);
const mouseY = (event.clientY - rect.top) * (this.$refs.canvas.height / rect.height);
this.activeRectIndex = this.hitTest(mouseX, mouseY);
if (this.activeRectIndex !== null) {
const handleHit = this.hitTestHandles(mouseX, mouseY);
if (handleHit) {
this.isResizing = true;
this.activeHandle = handleHit; // Ensure assignment to a valid handle string
this.startX = mouseX;
this.startY = mouseY;
} else {
this.isDragging = true;
this.startX = mouseX;
this.startY = mouseY;
}
} else {
this.isDrawing = true;
this.startX = mouseX;
this.startY = mouseY;
this.activeRectIndex = null;
this.activeHandle = null; // Reset active handle when drawing new shape
}
},
handleMouseMove(event) {
const canvas = this.$refs.canvas;
const rect = canvas.getBoundingClientRect();
this.currentX = (event.clientX - rect.left) * (canvas.width / rect.width);
this.currentY = (event.clientY - rect.top) * (canvas.height / rect.height);
if (this.isDrawing || this.isDragging || this.isResizing) {
if (this.isResizing && this.activeRectIndex !== null) {
this.resizeRectangle();
} else if (this.isDragging && this.activeRectIndex !== null) {
this.moveRectangle();
}
this.drawImage();
} else {
this.setCursor(event);
}
if (this.isDrawing) {
const ctx = canvas.getContext('2d');
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
const x = Math.min(this.startX, this.currentX);
const y = Math.min(this.startY, this.currentY);
const width = Math.abs(this.currentX - this.startX);
const height = Math.abs(this.currentY - this.startY);
ctx.strokeRect(x, y, width, height);
}
},
setCursor(event) {
const canvas = this.$refs.canvas;
const { clientX, clientY } = event;
const rect = canvas.getBoundingClientRect();
const mouseX = (clientX - rect.left) * (canvas.width / rect.width);
const mouseY = (clientY - rect.top) * (canvas.height / rect.height);
if (this.activeRectIndex !== null && this.hitTestHandles(mouseX, mouseY)) {
switch (this.activeHandle) {
case 'top-left':
case 'bottom-right':
canvas.style.cursor = 'nwse-resize';
break;
case 'top-right':
case 'bottom-left':
canvas.style.cursor = 'nesw-resize';
break;
case 'top-middle':
case 'bottom-middle':
canvas.style.cursor = 'ns-resize';
break;
case 'middle-left':
case 'middle-right':
canvas.style.cursor = 'ew-resize';
break;
default:
canvas.style.cursor = 'default';
}
} else if (this.activeRectIndex !== null) {
// Change cursor to indicate dragging
canvas.style.cursor = 'move';
} else {
canvas.style.cursor = 'default';
}
},
handleMouseUp() {
if (this.isDrawing) {
const canvas = this.$refs.canvas;
const width = canvas.width;
const height = canvas.height;
const x = Math.min(this.startX, this.currentX) / width;
const y = Math.min(this.startY, this.currentY) / height;
const rectWidth = Math.abs(this.currentX - this.startX) / width;
const rectHeight = Math.abs(this.currentY - this.startY) / height;
this.rectangles.push({ x, y, width: rectWidth, height: rectHeight });
this.isDrawing = false;
this.activeRectIndex = this.rectangles.length - 1;
}
if (this.isDragging) {
this.isDragging = false;
}
if (this.isResizing) {
this.isResizing = false;
this.activeHandle = null;
}
this.$refs.canvas.style.cursor = 'default';
},
hitTest(x, y) {
for (let i = 0; i < this.rectangles.length; i++) {
const rect = this.rectangles[i];
const canvas = this.$refs.canvas;
const rectX = rect.x * canvas.width;
const rectY = rect.y * canvas.height;
const rectWidth = rect.width * canvas.width;
const rectHeight = rect.height * canvas.height;
if (x > rectX && x < rectX + rectWidth && y > rectY && y < rectY + rectHeight) {
return i;
}
}
return null;
},
hitTestHandles(x, y) {
if (this.activeRectIndex === null) return null;
const rect = this.rectangles[this.activeRectIndex];
const canvas = this.$refs.canvas;
const rectX = rect.x * canvas.width;
const rectY = rect.y * canvas.height;
const rectWidth = rect.width * canvas.width;
const rectHeight = rect.height * canvas.height;
const halfSize = this.handleSize / 2;
const positions = [
{ x: rectX - halfSize, y: rectY - halfSize, handle: 'top-left' },
{ x: rectX + rectWidth - halfSize, y: rectY - halfSize, handle: 'top-right' },
{ x: rectX - halfSize, y: rectY + rectHeight - halfSize, handle: 'bottom-left' },
{ x: rectX + rectWidth - halfSize, y: rectY + rectHeight - halfSize, handle: 'bottom-right' },
{ x: rectX + rectWidth / 2 - halfSize, y: rectY - halfSize, handle: 'top-middle' },
{ x: rectX + rectWidth / 2 - halfSize, y: rectY + rectHeight - halfSize, handle: 'bottom-middle' },
{ x: rectX - halfSize, y: rectY + rectHeight / 2 - halfSize, handle: 'middle-left' },
{ x: rectX + rectWidth - halfSize, y: rectY + rectHeight / 2 - halfSize, handle: 'middle-right' }
];
for (let i = 0; i < positions.length; i++) {
const pos = positions[i];
if (x > pos.x && x < pos.x + this.handleSize && y > pos.y && y < pos.y + this.handleSize) {
return pos.handle;
}
}
return null;
},
resizeRectangle() {
if (!this.activeHandle || typeof this.activeHandle !== 'string') return;
const rect = this.rectangles[this.activeRectIndex];
const canvas = this.$refs.canvas;
const deltaX = (this.currentX - this.startX) / canvas.width;
const deltaY = (this.currentY - this.startY) / canvas.height;
if (this.activeHandle.includes('left')) {
rect.x += deltaX;
rect.width -= deltaX;
}
if (this.activeHandle.includes('right')) {
rect.width += deltaX;
}
if (this.activeHandle.includes('top')) {
rect.y += deltaY;
rect.height -= deltaY;
}
if (this.activeHandle.includes('bottom')) {
rect.height += deltaY;
}
this.startX = this.currentX;
this.startY = this.currentY;
},
moveRectangle() {
if (this.activeRectIndex === null) return;
const rect = this.rectangles[this.activeRectIndex];
const canvas = this.$refs.canvas;
const offsetX = this.currentX - this.startX;
const offsetY = this.currentY - this.startY;
rect.x += offsetX / canvas.width;
rect.y += offsetY / canvas.height;
this.startX = this.currentX;
this.startY = this.currentY;
},
deleteRectangle(index) {
this.rectangles.splice(index, 1);
this.drawImage();
}
}
};
</script>
Key Adjustments and Checks:
- Check
this.activeHandle: Before using includes, ensure activeHandle is not null and is of type string.
- Initialization and Reset: Ensure
activeHandle is reset to null properly where appropriate, especially after operations.
- Debugging: Use
console.log() to debug the contents of this.activeHandle during various stages of interaction to see why it might not be a string.
By ensuring these checks and refactoring are in place, you should be able to manage handles smoothly and prevent the type error you encountered.