I'll help you modify the code to add mouse interaction for viewing and moving shifters, and expand the direction types to include Left, Right, Top, and Bottom. Here's an updated version of the code:
const canvas = document.getElementById('pixelCanvas');
const ctx = canvas.getContext('2d');
const pixelData = ctx.createImageData(canvas.width, canvas.height);
const shifterControls = document.getElementById('shifterControls');
const shifterSpeedInput = document.getElementById('shifterSpeed');
const shifterColorInput = document.getElementById('shifterColor');
const applyShifterButton = document.getElementById('applyShifter');
let shifters = [];
let currentShifter = null;
let isDragging = false;
let dragOffsetX = 0;
let dragOffsetY = 0;
function drawPixelData() {
ctx.putImageData(pixelData, 0, 0);
// Draw shifter boundaries
shifters.forEach((shifter, index) => {
ctx.strokeStyle = index === shifters.indexOf(currentShifter) ? 'yellow' : 'white';
ctx.lineWidth = 2;
if (shifter.direction === 'left') {
ctx.strokeRect(0, shifter.position, shifter.width, canvas.height);
} else if (shifter.direction === 'right') {
ctx.strokeRect(canvas.width - shifter.width, shifter.position, shifter.width, canvas.height);
} else if (shifter.direction === 'top') {
ctx.strokeRect(shifter.position, 0, canvas.width, shifter.width);
} else if (shifter.direction === 'bottom') {
ctx.strokeRect(shifter.position, canvas.height - shifter.width, canvas.width, shifter.width);
}
});
}
function applyShifter() {
if (currentShifter) {
currentShifter.speed = parseInt(shifterSpeedInput.value);
currentShifter.color = shifterColorInput.value;
}
}
function updateShifters() {
shifters.forEach(shifter => {
if (shifter.direction === 'left') {
for (let y = 0; y < canvas.height; y++) {
for (let x = 0; x < canvas.width - shifter.width; x++) {
const index = (y * canvas.width + x) * 4;
const newIndex = (y * canvas.width + x + shifter.width) * 4;
pixelData.data[index] = pixelData.data[newIndex];
pixelData.data[index + 1] = pixelData.data[newIndex + 1];
pixelData.data[index + 2] = pixelData.data[newIndex + 2];
pixelData.data[index + 3] = pixelData.data[newIndex + 3];
}
for (let x = canvas.width - shifter.width; x < canvas.width; x++) {
const index = (y * canvas.width + x) * 4;
pixelData.data[index] = parseInt(shifter.color.slice(1, 3), 16);
pixelData.data[index + 1] = parseInt(shifter.color.slice(3, 5), 16);
pixelData.data[index + 2] = parseInt(shifter.color.slice(5, 7), 16);
pixelData.data[index + 3] = 255;
}
}
} else if (shifter.direction === 'right') {
for (let y = 0; y < canvas.height; y++) {
for (let x = canvas.width - 1; x >= shifter.width; x--) {
const index = (y * canvas.width + x) * 4;
const newIndex = (y * canvas.width + x - shifter.width) * 4;
pixelData.data[index] = pixelData.data[newIndex];
pixelData.data[index + 1] = pixelData.data[newIndex + 1];
pixelData.data[index + 2] = pixelData.data[newIndex + 2];
pixelData.data[index + 3] = pixelData.data[newIndex + 3];
}
for (let x = 0; x < shifter.width; x++) {
const index = (y * canvas.width + x) * 4;
pixelData.data[index] = parseInt(shifter.color.slice(1, 3), 16);
pixelData.data[index + 1] = parseInt(shifter.color.slice(3, 5), 16);
pixelData.data[index + 2] = parseInt(shifter.color.slice(5, 7), 16);
pixelData.data[index + 3] = 255;
}
}
} else if (shifter.direction === 'top') {
for (let x = 0; x < canvas.width; x++) {
for (let y = 0; y < canvas.height - shifter.width; y++) {
const index = (y * canvas.width + x) * 4;
const newIndex = ((y + shifter.width) * canvas.width + x) * 4;
pixelData.data[index] = pixelData.data[newIndex];
pixelData.data[index + 1] = pixelData.data[newIndex + 1];
pixelData.data[index + 2] = pixelData.data[newIndex + 2];
pixelData.data[index + 3] = pixelData.data[newIndex + 3];
}
for (let y = canvas.height - shifter.width; y < canvas.height; y++) {
const index = (y * canvas.width + x) * 4;
pixelData.data[index] = parseInt(shifter.color.slice(1, 3), 16);
pixelData.data[index + 1] = parseInt(shifter.color.slice(3, 5), 16);
pixelData.data[index + 2] = parseInt(shifter.color.slice(5, 7), 16);
pixelData.data[index + 3] = 255;
}
}
} else if (shifter.direction === 'bottom') {
for (let x = 0; x < canvas.width; x++) {
for (let y = canvas.height - 1; y >= shifter.width; y--) {
const index = (y * canvas.width + x) * 4;
const newIndex = ((y - shifter.width) * canvas.width + x) * 4;
pixelData.data[index] = pixelData.data[newIndex];
pixelData.data[index + 1] = pixelData.data[newIndex + 1];
pixelData.data[index + 2] = pixelData.data[newIndex + 2];
pixelData.data[index + 3] = pixelData.data[newIndex + 3];
}
for (let y = 0; y < shifter.width; y++) {
const index = (y * canvas.width + x) * 4;
pixelData.data[index] = parseInt(shifter.color.slice(1, 3), 16);
pixelData.data[index + 1] = parseInt(shifter.color.slice(3, 5), 16);
pixelData.data[index + 2] = parseInt(shifter.color.slice(5, 7), 16);
pixelData.data[index + 3] = 255;
}
}
}
});
drawPixelData();
}
function animate() {
updateShifters();
requestAnimationFrame(animate);
}
function addShifter(direction, width, position, speed, color) {
shifters.push({ direction, width, position, speed, color });
}
function selectShifter(shifter) {
currentShifter = shifter;
shifterSpeedInput.value = shifter.speed;
shifterColorInput.value = shifter.color;
}
function isPointInShifter(x, y, shifter) {
if (shifter.direction === 'left') {
return x >= 0 && x < shifter.width;
} else if (shifter.direction === 'right') {
return x >= canvas.width - shifter.width && x < canvas.width;
} else if (shifter.direction === 'top') {
return y >= 0 && y < shifter.width;
} else if (shifter.direction === 'bottom') {
return y >= canvas.height - shifter.width && y < canvas.height;
}
return false;
}
canvas.addEventListener('mousedown', (e) => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const clickedShifter = shifters.find(shifter => isPointInShifter(x, y, shifter));
if (clickedShifter) {
isDragging = true;
selectShifter(clickedShifter);
dragOffsetX = x;
dragOffsetY = y;
}
});
canvas.addEventListener('mousemove', (e) => {
if (isDragging && currentShifter) {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
// Implement dragging logic based on shifter direction
if (currentShifter.direction === 'left' || currentShifter.direction === 'right') {
currentShifter.position = Math.max(0, Math.min(canvas.height, y));
} else if (currentShifter.direction === 'top' || currentShifter.direction === 'bottom') {
currentShifter.position = Math.max(0, Math.min(canvas.width, x));
}
}
});
canvas.addEventListener('mouseup', () => {
isDragging = false;
});
canvas.addEventListener('mouseleave', () => {
isDragging = false;
});
// Add example shifters
addShifter('left', 50, 0, 1, '#ff0000');
addShifter('right', 50, 0, 1, '#00ff00');
addShifter('top', 20, 0, 1, '#0000ff');
addShifter('bottom', 20, 0, 1, '#f0ff00');
applyShifterButton.addEventListener('click', applyShifter);
animate();