function EtchASketch(options) {
const defaultOptions = {
width: 600,
height: 400,
x: 600 / 2,
y: 400 / 2,
dx: 2,
dy: 2
};
const { width, height, x, y, dx, dy } = { ...defaultOptions, ...options };
const context = DOM.context2d(width, height, 1);
context.canvas.style = "border: solid 65px #ec100d; border-radius: 20px";
context.fillStyle = "#d9dae6";
context.fillRect(0, 0, width, height);
const cursor = { x, y };
context.beginPath();
context.strokeStyle = "black";
context.stroke();
document.addEventListener("keydown", (event) => {
switch (event.key) {
case "ArrowRight":
cursor.x = Math.min(cursor.x + dx, width);
context.lineTo(cursor.x, cursor.y);
context.stroke();
event.preventDefault();
return;
case "ArrowLeft":
cursor.x = Math.max(cursor.x - dx, 0);
context.lineTo(cursor.x, cursor.y);
context.stroke();
event.preventDefault();
return;
case "ArrowUp":
cursor.y = Math.max(cursor.y - dy, 0);
context.lineTo(cursor.x, cursor.y);
context.stroke();
event.preventDefault();
return;
case "ArrowDown":
cursor.y = Math.min(cursor.y + dy, height);
context.lineTo(cursor.x, cursor.y);
context.stroke();
event.preventDefault();
return;
}
});
return context.canvas;
}