Skip to content

Commit 541674b

Browse files
committed
refactor presets + fix error toast z index
1 parent 467537e commit 541674b

18 files changed

+456
-713
lines changed

presets/Alien.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,42 @@ export function renderSVG(qr, params) {
3434
const rand = getSeededRand(params["Seed"]);
3535
const rangeStr = (min, max) => (rand() * (max - min) + min).toFixed(2);
3636

37-
const matrixWidth = qr.version * 4 + 17;
37+
const rowLen = qr.version * 4 + 17;
3838
const margin = params["Margin"];
3939
const bg = params["Background"];
4040
const dots = params["Dots"];
4141
const lines = params["Lines"];
4242

43-
const size = matrixWidth + 2 * margin;
43+
const size = rowLen + 2 * margin;
4444
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${-margin} ${-margin} ${size} ${size}">`;
4545
svg += `<rect x="${-margin}" y="${-margin}" width="${size}" height="${size}" fill="${bg}"/>`;
4646

4747
let linesLayer = `<g stroke="${lines}">`;
4848
let dotsLayer = `<g fill="${dots}">`;
4949

5050
function matrix(x, y) {
51-
return qr.matrix[y * matrixWidth + x];
51+
return qr.matrix[y * rowLen + x];
5252
}
5353

54-
const rightVisited = Array(matrixWidth * matrixWidth).fill(false);
55-
const leftVisited = Array(matrixWidth * matrixWidth).fill(false);
54+
const rightVisited = Array(rowLen * rowLen).fill(false);
55+
const leftVisited = Array(rowLen * rowLen).fill(false);
5656
function visited1(x, y) {
57-
return rightVisited[y * matrixWidth + x];
57+
return rightVisited[y * rowLen + x];
5858
}
5959
function visited2(x, y) {
60-
return leftVisited[y * matrixWidth + x];
60+
return leftVisited[y * rowLen + x];
6161
}
6262
function setVisited1(x, y) {
63-
rightVisited[y * matrixWidth + x] = true;
63+
rightVisited[y * rowLen + x] = true;
6464
}
6565
function setVisited2(x, y) {
66-
leftVisited[y * matrixWidth + x] = true;
66+
leftVisited[y * rowLen + x] = true;
6767
}
6868

6969
for (const [x, y] of [
7070
[0, 0],
71-
[matrixWidth - 7, 0],
72-
[0, matrixWidth - 7],
71+
[rowLen - 7, 0],
72+
[0, rowLen - 7],
7373
]) {
7474
dotsLayer += `<circle cx="${x + 3.5}" cy="${y + 3.5}" r="1.5"/>`;
7575

@@ -88,8 +88,8 @@ export function renderSVG(qr, params) {
8888
linesLayer += `<line x1="${x + 6.5}" y1="${y + 0.5}" x2="${x + 0.5}" y2="${y + 6.5}" stroke-width="${rangeStr(0.3, 0.6)}"/>`;
8989
}
9090

91-
for (let y = 0; y < matrixWidth; y++) {
92-
for (let x = 0; x < matrixWidth; x++) {
91+
for (let y = 0; y < rowLen; y++) {
92+
for (let x = 0; x < rowLen; x++) {
9393
const module = matrix(x, y);
9494
if (module & Module.FINDER) continue;
9595

@@ -100,8 +100,8 @@ export function renderSVG(qr, params) {
100100
let nx = x + 1;
101101
let ny = y + 1;
102102
while (
103-
nx < matrixWidth &&
104-
ny < matrixWidth &&
103+
nx < rowLen &&
104+
ny < rowLen &&
105105
matrix(nx, ny) & Module.ON &&
106106
!visited1(nx, ny)
107107
) {
@@ -119,7 +119,7 @@ export function renderSVG(qr, params) {
119119
let ny = y + 1;
120120
while (
121121
nx >= 0 &&
122-
ny < matrixWidth &&
122+
ny < rowLen &&
123123
matrix(nx, ny) & Module.ON &&
124124
!visited2(nx, ny)
125125
) {

presets/Basic.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const paramsSchema = {
5555
};
5656

5757
export async function renderSVG(qr, params) {
58-
const matrixWidth = qr.version * 4 + 17;
58+
const rowLen = qr.version * 4 + 17;
5959
const margin = params["Margin"];
6060
const fg = params["Foreground"];
6161
const bg = params["Background"];
@@ -65,7 +65,7 @@ export async function renderSVG(qr, params) {
6565
const logoRatio = params["Logo size"];
6666
const showLogoData = params["Show data behind logo"];
6767

68-
const size = matrixWidth + 2 * margin;
68+
const size = rowLen + 2 * margin;
6969
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${-margin} ${-margin} ${size} ${size}">`;
7070
svg += `<rect x="${-margin}" y="${-margin}" width="${size}" height="${size}" fill="${bg}"/>`;
7171

@@ -91,8 +91,8 @@ export async function renderSVG(qr, params) {
9191

9292
for (const [x, y] of [
9393
[0, 0],
94-
[matrixWidth - 7, 0],
95-
[0, matrixWidth - 7],
94+
[rowLen - 7, 0],
95+
[0, rowLen - 7],
9696
]) {
9797
if (defaultShape) {
9898
svg += roundedRect(x, y, 7, lgRadius, true);
@@ -113,10 +113,10 @@ export async function renderSVG(qr, params) {
113113
if (!defaultShape || !roundness) svg += `<path d="`;
114114

115115
const logoInner = Math.floor(((1 - logoRatio) * size) / 2 - margin);
116-
const logoUpper = matrixWidth - logoInner;
116+
const logoUpper = rowLen - logoInner;
117117

118-
for (let y = 0; y < matrixWidth; y++) {
119-
for (let x = 0; x < matrixWidth; x++) {
118+
for (let y = 0; y < rowLen; y++) {
119+
for (let x = 0; x < rowLen; x++) {
120120
if (
121121
file &&
122122
!showLogoData &&
@@ -127,7 +127,7 @@ export async function renderSVG(qr, params) {
127127
) {
128128
continue;
129129
}
130-
const module = qr.matrix[y * matrixWidth + x];
130+
const module = qr.matrix[y * rowLen + x];
131131
if (!(module & Module.ON)) continue;
132132
if (module & Module.FINDER) continue;
133133

presets/Blocks.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const paramsSchema = {
5454
};
5555

5656
export function renderSVG(qr, params) {
57-
const matrixWidth = qr.version * 4 + 17;
57+
const rowLen = qr.version * 4 + 17;
5858
const margin = params["Margin"];
5959
const bg = params["Background"];
6060
const fc = params["Finder"];
@@ -71,7 +71,7 @@ export function renderSVG(qr, params) {
7171
const ct = params["Cross thickness"];
7272
const co = ct / Math.sqrt(8); // offset
7373

74-
const size = matrixWidth + 2 * margin;
74+
const size = rowLen + 2 * margin;
7575
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${-margin} ${-margin} ${size} ${size}">`;
7676
svg += `<rect x="${-margin}" y="${-margin}" width="${size}" height="${size}" fill="${bg}"/>`;
7777

@@ -80,22 +80,22 @@ export function renderSVG(qr, params) {
8080
let hLayer = `<g fill="${hc}">`;
8181

8282
function matrix(x, y) {
83-
return qr.matrix[y * matrixWidth + x];
83+
return qr.matrix[y * rowLen + x];
8484
}
8585

86-
const visitedMatrix = Array(matrixWidth * matrixWidth).fill(false);
86+
const visitedMatrix = Array(rowLen * rowLen).fill(false);
8787
function visited(x, y) {
88-
return visitedMatrix[y * matrixWidth + x];
88+
return visitedMatrix[y * rowLen + x];
8989
}
9090
function setVisited(x, y) {
91-
visitedMatrix[y * matrixWidth + x] = true;
91+
visitedMatrix[y * rowLen + x] = true;
9292
}
9393

9494
svg += `<g fill="${fc}">`;
9595
for (const [x, y] of [
9696
[0, 0],
97-
[matrixWidth - 7, 0],
98-
[0, matrixWidth - 7],
97+
[rowLen - 7, 0],
98+
[0, rowLen - 7],
9999
]) {
100100
svg += `<rect x="${x + 2}" y="${y}" width="3" height="1"/>`;
101101
svg += `<rect x="${x + 2}" y="${y + 2}" width="3" height="3"/>`;
@@ -105,17 +105,17 @@ export function renderSVG(qr, params) {
105105
}
106106
svg += `</g>`;
107107

108-
for (let y = 0; y < matrixWidth; y++) {
109-
for (let x = 0; x < matrixWidth; x++) {
108+
for (let y = 0; y < rowLen; y++) {
109+
for (let x = 0; x < rowLen; x++) {
110110
const module = matrix(x, y);
111111
if (module & Module.FINDER) continue;
112112
if (!(module & Module.ON)) continue;
113113
if (visited(x, y)) continue;
114114
setVisited(x, y);
115115

116116
if (
117-
y < matrixWidth - 2 &&
118-
x < matrixWidth - 2 &&
117+
y < rowLen - 2 &&
118+
x < rowLen - 2 &&
119119
matrix(x + 2, y) &
120120
matrix(x, y + 2) &
121121
matrix(x + 1, y + 1) &
@@ -141,8 +141,8 @@ export function renderSVG(qr, params) {
141141
}
142142
}
143143
if (
144-
y < matrixWidth - 1 &&
145-
x < matrixWidth - 1 &&
144+
y < rowLen - 1 &&
145+
x < rowLen - 1 &&
146146
matrix(x + 1, y) & matrix(x, y + 1) & matrix(x + 1, y + 1) & Module.ON
147147
) {
148148
if (
@@ -163,7 +163,7 @@ export function renderSVG(qr, params) {
163163
}
164164

165165
let ny = y + 1;
166-
while (ny < matrixWidth && matrix(x, ny) & Module.ON && !visited(x, ny)) {
166+
while (ny < rowLen && matrix(x, ny) & Module.ON && !visited(x, ny)) {
167167
ny++;
168168
}
169169
if (ny - y > 2) {
@@ -176,7 +176,7 @@ export function renderSVG(qr, params) {
176176
}
177177

178178
let nx = x + 1;
179-
while (nx < matrixWidth && matrix(nx, y) & Module.ON && !visited(nx, y)) {
179+
while (nx < rowLen && matrix(nx, y) & Module.ON && !visited(nx, y)) {
180180
setVisited(nx, y);
181181
nx++;
182182
}

presets/Bubbles.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ export function renderSVG(qr, params) {
5252
? (min, max) => (rand() * (max - min) + min).toFixed(2)
5353
: (min, max) => ((max - min) / 2 + min).toFixed(2);
5454

55-
const matrixWidth = qr.version * 4 + 17;
55+
const rowLen = qr.version * 4 + 17;
5656
const margin = params["Margin"];
5757
const bg = params["Background"];
5858

59-
const size = matrixWidth + 2 * margin;
59+
const size = rowLen + 2 * margin;
6060
let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${-margin} ${-margin} ${size} ${size}">`;
6161
svg += `<rect x="${-margin}" y="${-margin}" width="${size}" height="${size}" fill="${bg}"/>`;
6262

@@ -66,36 +66,36 @@ export function renderSVG(qr, params) {
6666
let layer4 = `<g fill="${params["Tiny circle"]}">`;
6767

6868
function matrix(x, y) {
69-
return qr.matrix[y * matrixWidth + x];
69+
return qr.matrix[y * rowLen + x];
7070
}
7171

72-
const visitedMatrix = Array(matrixWidth * matrixWidth).fill(false);
72+
const visitedMatrix = Array(rowLen * rowLen).fill(false);
7373
function visited(x, y) {
74-
return visitedMatrix[y * matrixWidth + x];
74+
return visitedMatrix[y * rowLen + x];
7575
}
7676
function setVisited(x, y) {
77-
visitedMatrix[y * matrixWidth + x] = true;
77+
visitedMatrix[y * rowLen + x] = true;
7878
}
7979

8080
const fc = params["Finder"];
8181
for (const [x, y] of [
8282
[0, 0],
83-
[matrixWidth - 7, 0],
84-
[0, matrixWidth - 7],
83+
[rowLen - 7, 0],
84+
[0, rowLen - 7],
8585
]) {
8686
svg += `<circle cx="${x + 3.5}" cy="${y + 3.5}" r="3" fill="none" stroke="${fc}" stroke-width="1"/>`;
8787
svg += `<circle cx="${x + 3.5}" cy="${y + 3.5}" r="1.5" fill="${fc}"/>`;
8888
}
8989

90-
for (let y = 0; y < matrixWidth; y++) {
91-
for (let x = 0; x < matrixWidth; x++) {
90+
for (let y = 0; y < rowLen; y++) {
91+
for (let x = 0; x < rowLen; x++) {
9292
const module = matrix(x, y);
9393
if (module & Module.FINDER) continue;
9494
if (visited(x, y)) continue;
9595

9696
if (
97-
y < matrixWidth - 2 &&
98-
x < matrixWidth - 2 &&
97+
y < rowLen - 2 &&
98+
x < rowLen - 2 &&
9999
matrix(x + 1, y) &
100100
matrix(x, y + 1) &
101101
matrix(x + 2, y + 1) &
@@ -118,8 +118,8 @@ export function renderSVG(qr, params) {
118118
setVisited(x, y);
119119

120120
if (
121-
y < matrixWidth - 1 &&
122-
x < matrixWidth - 1 &&
121+
y < rowLen - 1 &&
122+
x < rowLen - 1 &&
123123
matrix(x + 1, y) &
124124
matrix(x, y + 1) &
125125
matrix(x + 1, y + 1) &
@@ -134,7 +134,7 @@ export function renderSVG(qr, params) {
134134
continue;
135135
}
136136
if (
137-
x < matrixWidth - 1 &&
137+
x < rowLen - 1 &&
138138
matrix(x + 1, y) & Module.ON &&
139139
!visited(x + 1, y)
140140
) {
@@ -143,7 +143,7 @@ export function renderSVG(qr, params) {
143143
continue;
144144
}
145145
if (
146-
y < matrixWidth - 1 &&
146+
y < rowLen - 1 &&
147147
matrix(x, y + 1) & Module.ON &&
148148
!visited(x, y + 1)
149149
) {

0 commit comments

Comments
 (0)