ThePlaceHolders/static/canvas.js

208 lines
5.7 KiB
JavaScript
Raw Normal View History

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-24 23:51:19 -05:00
let zoom = 1.0;
let baseGridCount = 500; // Base grid count at zoom = 1
2024-10-30 16:30:24 -05:00
let gridCount = 500;
let pixelSize = canvasSize / gridCount;
2024-10-29 21:32:06 -05:00
canvas.width = canvasSize;
canvas.height = canvasSize;
2024-10-29 21:32:06 -05:00
function updateGridCount() {
2024-10-30 16:30:24 -05:00
return Math.floor(gridCount / 2); // Adjust grid count based on zoom
2024-10-29 21:32:06 -05:00
}
2024-10-24 23:51:19 -05:00
function updatePixelSize() {
return canvasSize / gridCount; // Update pixel size based on new grid count
2024-10-24 23:51:19 -05:00
}
2024-10-23 18:01:27 -05:00
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;
2024-10-23 18:01:27 -05:00
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;
2024-10-23 18:01:27 -05:00
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-23 18:01:27 -05:00
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 * gridCount),
Math.round(this.y / canvasSize * gridCount)
2024-10-16 22:30:14 -05:00
);
}
// Convert data position to canvas position
dataToCanvas() {
return new Point(
this.x / gridCount * canvasSize,
this.y / gridCount * canvasSize
2024-10-16 22:30:14 -05:00
);
}
// 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-29 21:32:06 -05:00
function draw(point, color) {
point = point.canvasToData().dataToCanvas();
2024-10-29 21:32:06 -05:00
ctx.fillStyle = color;
ctx.fillRect(point.x, point.y, pixelSize, pixelSize);
2024-10-16 18:41:26 -05:00
}
2024-10-30 16:30:24 -05:00
/*
2024-10-16 18:41:26 -05:00
canvas.addEventListener("mousedown", (e) => {
let x = e.clientX - canvas.getBoundingClientRect().left - (pixelSize / 2);
let y = e.clientY - canvas.getBoundingClientRect().top - (pixelSize / 2);
2024-10-29 21:32:06 -05:00
let color = document.getElementById("stroke").value;
let point = new Point(x, y);
2024-10-29 21:32:06 -05:00
draw(point, color);
});
2024-10-30 16:30:24 -05:00
*/
2024-10-29 21:32:06 -05:00
2024-10-30 16:30:24 -05:00
canvas.addEventListener("mousedown", (e) => {
let x = e.clientX - canvas.getBoundingClientRect().left - (pixelSize / 2);
let y = e.clientY - canvas.getBoundingClientRect().top - (pixelSize / 2);
if (x > 249){
if (y < 250){
console.log("Quadrant 1");
console.log("Previous Grid Size:", gridCount);
console.log("Previous PixelSize:", pixelSize);
gridCount = updateGridCount();
pixelSize = updatePixelSize();
console.log("Updated Grid Size:", gridCount);
console.log("Updated PixelSize:", pixelSize);
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawStoredPixels(1);
}
else{
console.log("Quadrant 4");
}
}
else{
if (y < 250){
console.log("Quadrant 2");
}
else{
console.log("Quadrant 3");
}
}
console.log("This is where the user clicked to zoom:", x);
console.log("This is where the user clicked to zoom:", y);
});
const pixelData = {};
function storePixel(x, y, color){
pixelData[`${x},${y}`] = color;
}
for (let i = 0; i < 500; i++){
storePixel(i, 250, '#0000FF');
storePixel(i, i, '#FF0000');
storePixel(i, 499 - i, '#FFFF00');
storePixel(250, i, '#FF6600')
}
function drawStoredPixels(quadrant) {
if (quadrant == 1){
for (const [key, color] of Object.entries(pixelData)) {
const [x, y] = key.split(',').map(Number);
if (x >= 250 && x <= 499 && y >= 0 && y <= 249) {
const pixelCanvasPos = new Point(x, y);
ctx.fillStyle = color;
console.log(pixelCanvasPos);
draw(pixelCanvasPos, color);
}
}
}
else{
for (const [key, color] of Object.entries(pixelData)) {
const [x, y] = key.split(',').map(Number);
const pixelCanvasPos = new Point(x, y);
ctx.fillStyle = color;
draw(pixelCanvasPos, color);
}
}
}
drawStoredPixels();
/*
2024-10-29 21:32:06 -05:00
document.getElementById("zoomIn").addEventListener("click", () => {
if (zoom < 100.0) {
if (zoom == 1.0){
zoom = 4.0;
}
else{
zoom += 4.0;
}
gridCount = updateGridCount();
pixelSize = updatePixelSize();
console.log(zoom);
console.log(gridCount);
2024-10-29 21:32:06 -05:00
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
2024-10-29 21:32:06 -05:00
drawGrid(); // Redraw the grid with updated zoom
});
2024-10-30 16:30:24 -05:00
/*
2024-10-29 21:32:06 -05:00
document.getElementById("zoomOut").addEventListener("click", () => {
if (zoom > 1.0) {
if (zoom <= 4.0){
zoom = 1.0;
}
else{
zoom -= 4.0;
}
gridCount = updateGridCount();
pixelSize = updatePixelSize();
console.log(zoom);
console.log(gridCount);
2024-10-29 21:32:06 -05:00
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
2024-10-29 21:32:06 -05:00
drawGrid(); // Redraw the grid with updated zoom
2024-10-23 18:09:48 -05:00
});
2024-10-30 16:30:24 -05:00
*/