-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·144 lines (123 loc) · 5.12 KB
/
app.js
File metadata and controls
executable file
·144 lines (123 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var pixels;
let thresh = 20; // Raised default threshold (was 10) — reduces noise lines
const MAX_LINES = 20000; // Raised from 5000
const MAX_DIM = 300; // Auto-downscale images larger than this
function processImg() {
var uploader = document.querySelector('input[type=file]').files[0];
if (!uploader) return;
var canvas = document.getElementById("cnvs");
var ctx = canvas.getContext("2d");
var reader = new FileReader();
var img = new Image();
// Allow threshold to be controlled by a slider if present
var sliderEl = document.getElementById("threshSlider");
if (sliderEl) thresh = parseInt(sliderEl.value) || thresh;
reader.addEventListener("load", () => { img.src = reader.result; });
reader.readAsDataURL(uploader);
img.onload = () => {
document.getElementById('filename').innerText = uploader.name;
// --- Auto-downscale large images ---
let w = img.width;
let h = img.height;
if (w > MAX_DIM || h > MAX_DIM) {
let scale = Math.min(MAX_DIM / w, MAX_DIM / h);
w = Math.round(w * scale);
h = Math.round(h * scale);
console.log(`Downscaled to ${w}x${h}`);
}
canvas.width = w;
canvas.height = h;
ctx.drawImage(img, 0, 0, w, h);
console.log("Image drawing complete.");
pixels = ctx.getImageData(0, 0, w, h);
console.log(`Processing ${pixels.width}x${pixels.height} image, threshold=${thresh}`);
let lines = [];
let tooComplex = false;
// --- Horizontal lines ---
outer_h:
for (let y = 0; y < pixels.height - 1; y++) {
let hasLine = false;
let lineStart = -1;
for (let x = 0; x < pixels.width; x++) {
let pixLine = false;
for (let k = 0; k < 3; k++) { // skip alpha channel (k<3)
if (Math.abs(getPixel(x, y, k) - getPixel(x, y + 1, k)) >= thresh) {
pixLine = true;
break;
}
}
if (pixLine) {
if (!hasLine) {
hasLine = true;
lineStart = getX(x);
}
} else {
if (hasLine) {
lines.push({ dir: 1, offset: getY(y) - 0.5, start: lineStart, end: getX(x) });
hasLine = false;
if (lines.length >= MAX_LINES) { tooComplex = true; break outer_h; }
}
}
}
if (hasLine) {
lines.push({ dir: 1, offset: getY(y) - 0.5, start: lineStart, end: getX(pixels.width) });
}
}
// --- Vertical lines ---
if (!tooComplex) {
outer_v:
for (let x = 0; x < pixels.width - 1; x++) {
let hasLine = false;
let lineStart = -1;
for (let y = pixels.height - 1; y >= 0; y--) {
let pixLine = false;
for (let k = 0; k < 3; k++) {
if (Math.abs(getPixel(x, y, k) - getPixel(x + 1, y, k)) >= thresh) {
pixLine = true;
break;
}
}
if (pixLine) {
if (!hasLine) {
hasLine = true;
lineStart = getY(y);
}
} else {
if (hasLine) {
lines.push({ dir: 0, offset: getX(x) + 0.5, start: lineStart - 1, end: getY(y) - 1 });
hasLine = false;
if (lines.length >= MAX_LINES) { tooComplex = true; break outer_v; }
}
}
}
if (hasLine) {
lines.push({ dir: 0, offset: getX(x) + 0.5, start: lineStart - 1, end: getY(pixels.height) - 1 });
}
}
}
let statusEl = document.getElementById('filename');
if (tooComplex) {
statusEl.innerText = `Still too complex (${lines.length} lines)! Try increasing threshold or using a simpler image.`;
} else {
statusEl.innerText = `${uploader.name} — ${lines.length} lines generated`;
}
console.log(`Done! Created ${lines.length} lines.`);
let output = document.querySelector("#output");
output.innerHTML = "";
lines.forEach(line => { output.innerHTML += getLineEquation(line); });
output.select();
document.execCommand("copy");
};
}
function getLineEquation(line) {
if (line.dir === 0) {
return `x = ${line.offset}\\left\\{${line.start}\\le y \\le${line.end}\\right\\}\n`;
} else {
return `y = ${line.offset}\\left\\{${line.start}\\le x \\le${line.end}\\right\\}\n`;
}
}
function getPixel(i, j, c) {
return pixels.data[j * 4 * pixels.width + 4 * i + c];
}
function getX(x) { return x - pixels.width / 2; }
function getY(y) { return -y + pixels.height / 2; }