2024-10-09 23:18:42 -05:00
|
|
|
const canvas = document.getElementById("canvas");
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
|
2024-10-16 22:30:14 -05:00
|
|
|
const canvasSize = 500;
|
2024-10-16 18:41:26 -05:00
|
|
|
const dataSize = 1000;
|
2024-10-23 18:01:27 -05:00
|
|
|
const gridCount = 10 ;
|
2024-10-16 22:30:14 -05:00
|
|
|
canvas.width = canvasSize;
|
|
|
|
canvas.height = canvasSize;
|
|
|
|
|
2024-10-23 18:01:27 -05:00
|
|
|
//let pixelSize = canvasSize / dataSize;
|
|
|
|
let pixelSize = canvasSize / gridCount;
|
|
|
|
const centerPixel = pixelSize / 2;
|
|
|
|
|
|
|
|
function drawGrid() {
|
|
|
|
ctx.strokeStyle = '#000'; // Set grid line color
|
|
|
|
ctx.lineWidth = 1; // Set grid line width
|
|
|
|
|
|
|
|
// Draw horizontal lines
|
|
|
|
for (let i = 0; i <= gridCount; i++) {
|
|
|
|
let y = i * pixelSize;
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(0, y); // Start the line at the left edge (x = 0)
|
|
|
|
ctx.lineTo(canvasSize, y); // Draw to the right edge (x = canvasSize)
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw vertical lines
|
|
|
|
for (let i = 0; i <= gridCount; i++) {
|
|
|
|
let x = i * pixelSize;
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(x, 0); // Start the line at the top edge (y = 0)
|
|
|
|
ctx.lineTo(x, canvasSize); // Draw to the bottom edge (y = canvasSize)
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drawGrid();
|
|
|
|
|
2024-10-16 22:30:14 -05:00
|
|
|
class Point {
|
|
|
|
// new Point(x, y)
|
|
|
|
constructor(x, y) {
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
}
|
|
|
|
// Convert canvas position to data position
|
|
|
|
canvasToData() {
|
|
|
|
return new Point(
|
|
|
|
Math.round(this.x / canvasSize * dataSize),
|
|
|
|
Math.round(this.y / canvasSize * dataSize)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Convert data position to canvas position
|
|
|
|
dataToCanvas() {
|
|
|
|
return new Point(
|
|
|
|
this.x / dataSize * canvasSize,
|
|
|
|
this.y / dataSize * canvasSize
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// Convert to array index
|
|
|
|
index() {
|
|
|
|
return this.y * dataSize + this.x;
|
|
|
|
}
|
|
|
|
// Basic math functions
|
|
|
|
plus(otherPoint) {
|
|
|
|
return new Point(
|
|
|
|
this.x + otherPoint.x,
|
|
|
|
this.y + otherPoint.y
|
|
|
|
);
|
|
|
|
}
|
|
|
|
minus(otherPoint) {
|
|
|
|
return new Point(
|
|
|
|
this.x - otherPoint.x,
|
|
|
|
this.y - otherPoint.y
|
|
|
|
);
|
|
|
|
}
|
|
|
|
scaleBy(number) {
|
|
|
|
return new Point(
|
|
|
|
this.x * number,
|
|
|
|
this.y * number
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-10-16 18:41:26 -05:00
|
|
|
|
2024-10-23 18:01:27 -05:00
|
|
|
function draw(point) {
|
|
|
|
ctx.fillRect(point.x, point.y, pixelSize, pixelSize);
|
2024-10-16 18:41:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas.addEventListener("mousedown", (e) => {
|
2024-10-16 22:30:14 -05:00
|
|
|
let x = e.clientX - canvas.getBoundingClientRect().left - centerPixel;
|
|
|
|
let y = e.clientY - canvas.getBoundingClientRect().top - centerPixel;
|
2024-10-23 18:01:27 -05:00
|
|
|
let point = new Point(x, y)
|
|
|
|
draw(point);
|
|
|
|
});
|