1const initCanvas = (canvas, size, ratio) => {
3 // let ctx = canvas.getContext('2d')
5 const dpi200 = window.devicePixelRatio * (200 / 72);
7 canvas.width = x * dpi200;
8 canvas.height = y * dpi200;
9 canvas.style.width = x + 'px';
10 canvas.style.height = y + 'px';
12 canvas.getContext('2d').scale(dpi200, dpi200);
18const mapToRange = (v, l1, h1, l2, h2) =>
19 l2 + ((h2 - l2) * (v - l1)) / (h1 - l1);
21const seq = num => Array.from(Array(num), (_, i) => i);
23const compose = (...fns) =>
24 fns.reduce((f, g) => (...xs) => {
26 return Array.isArray(r) ? f(...r) : f(r);
31 at: (m, r, c) => m[r][c],
32 nn: (m, r, c) => m[r - 1][c],
33 ne: (m, r, c) => m[r - 1][c + 1],
34 ee: (m, r, c) => m[r][c + 1],
35 se: (m, r, c) => m[r + 1][c + 1],
36 ss: (m, r, c) => m[r + 1][c],
37 sw: (m, r, c) => m[r + 1][c - 1],
38 ww: (m, r, c) => m[r][c - 1],
39 nw: (m, r, c) => m[r - 1][c - 1],
42 at: (m, r, c, v) => (m[r][c] = v),
43 nn: (m, r, c, v) => (m[r - 1][c] = v),
44 ne: (m, r, c, v) => (m[r - 1][c + 1] = v),
45 ee: (m, r, c, v) => (m[r][c + 1] = v),
46 se: (m, r, c, v) => (m[r + 1][c + 1] = v),
47 ss: (m, r, c, v) => (m[r + 1][c] = v),
48 sw: (m, r, c, v) => (m[r + 1][c - 1] = v),
49 ww: (m, r, c, v) => (m[r][c - 1] = v),
50 nw: (m, r, c, v) => (m[r - 1][c - 1] = v),
53 at: (r, c) => ({ r: r, c: c }),
54 nn: (r, c) => ({ r: r - 1, c: c }),
55 ne: (r, c) => ({ r: r - 1, c: c + 1 }),
56 ee: (r, c) => ({ r: r, c: c + 1 }),
57 se: (r, c) => ({ r: r + 1, c: c + 1 }),
58 ss: (r, c) => ({ r: r + 1, c: c }),
59 sw: (r, c) => ({ r: r + 1, c: c - 1 }),
60 ww: (r, c) => ({ r: r, c: c - 1 }),
61 nw: (r, c) => ({ r: r - 1, c: c - 1 }),
73 new: (rs, cs) => seq(rs).map(_ => seq(cs).map(_ => 0)),
74 rows: m => m.length - 1,
75 cols: m => m[0].length - 1,
78const chunkStr = (str, size) => {
79 const r = new RegExp(`.{1,${size}}`, 'g');
94const walk = (int, matrix) => {
95 // convert the int to a base8 string, split to array
96 var chunks = int.toString(8).split('');
98 // convert the int to a base10 string
99 var b10 = int.toString(10);
101 // get number of rows and cols
102 var rows = Matrix.rows(matrix);
103 var cols = Matrix.cols(matrix);
105 // split the length-4 number into 2 parts, which is a number from 0-99
106 // if the substring is empty, substitute 10
107 var sx = parseInt(b10.substring(0, 2) || 0, 10);
108 var sy = parseInt(b10.substring(2, 4) || 0, 10);
110 // generate a starting coordinate
112 c: ~~mapToRange(sx, 0, 99, 0, cols),
113 r: ~~mapToRange(sy, 0, 99, 0, rows),
119 for (var i = 0; i < chunks.length; i++) {
120 // Each chunk is a number from 0-7 which refers to a direction assigned in Base3Mapping.
121 // Get the current chunk, and then get the direction it has been paired with.
122 vect = Base3Mapping[chunks[i]];
124 // generate the next coordinate to go to, based on the current coordinate and the direction stored in vect.
125 next = Matrix.coord[vect](coord.r, coord.c);
127 // If at an edge of the matrix, substitute the opposite direction.
128 if (next.r < 0) vect = Matrix.dirs.SS;
129 if (next.c < 0) vect = Matrix.dirs.EE;
130 if (next.r > rows) vect = Matrix.dirs.NN;
131 if (next.c > cols) vect = Matrix.dirs.WW;
133 // If at in the corner of the matrix, substitute an alternative direction.
134 if (next.r === 0 && coord.c === 0) vect = Matrix.dirs.SE;
135 if (next.r > rows && coord.c === 0) vect = Matrix.dirs.NE;
136 if (next.r === 0 && coord.c > cols) vect = Matrix.dirs.SW;
137 if (next.r > rows && coord.c > cols) vect = Matrix.dirs.NW;
139 // get the next coordinate again in case there was an edge/cornercase
140 next = Matrix.coord[vect](coord.r, coord.c);
142 // increment the value at the next coordinate.
147 Matrix.get.at(matrix, next.r, next.c) + 1
150 // set the new current coordinate.
154 // return a 2-ary array in order to pass both values to the next function in the compose pipeline.
155 return [int, matrix];
158const rand = (int, matrix) => {
159 // break base10 integer into chunks with length of 4
160 var chunks = chunkStr(int.toString(10), 4);
161 var cols = Matrix.cols(matrix);
162 var rows = Matrix.rows(matrix);
169 for (var i = 0; i < chunks.length; i++) {
170 // for each chunk in chunks
173 // split the length-4 number into 2 parts, which is a number from 0-99
174 // if the substring is empty, substitute 10
175 x = parseInt(ch.substring(0, 2) || 0, 10);
176 y = parseInt(ch.substring(2, 4) || 0, 10);
178 // map num from 0-99 to 0 - num of cols, then use ~~ for fast math.floor
179 // c (col) and r (row) are coordinates
180 c = ~~mapToRange(x, 0, 99, 0, cols);
181 r = ~~mapToRange(y, 0, 99, 0, rows);
183 // increment number at the coordinate
184 Matrix.set.at(matrix, r, c, Matrix.get.at(matrix, r, c) + 1);
187 // return a 2-ary array in order to pass both values to the next function in the compose pipeline.
188 return [int, matrix];
191export { chunkStr, initCanvas, seq, Matrix, compose, mapToRange, walk, rand };
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list