Skip to content

Commit 654074e

Browse files
committed
refactoring: svg stuff in own class
1 parent 8aeb25b commit 654074e

File tree

6 files changed

+103
-124
lines changed

6 files changed

+103
-124
lines changed

03/solve.js

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -170,39 +170,19 @@ adventofcode.day3_get_bounds = function(node_1, node_2) {
170170
* @param {object[]} crossings found crossings [x,y,node1,node2]
171171
*/
172172
adventofcode.day3_print_svg = function(paths, crossings) {
173-
let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
174-
svg.setAttribute('version', '1.1');
175-
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
176-
svg.setAttribute('viewBox', (this.day3_min_x - 50) + ' ' + (this.day3_min_y - 50) + ' ' + (this.day3_max_x + Math.abs(this.day3_min_x) + 100) + ' ' + (this.day3_max_y + Math.abs(this.day3_min_y) + 100));
177-
178-
let path_1 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
179-
path_1.setAttribute('d', 'M' + paths[0].map(node => node.x+','+node.y).join(" "));
180-
path_1.setAttribute('stroke', 'red');
181-
path_1.setAttribute('stroke-width', '30');
182-
path_1.setAttribute('fill', 'none');
183-
svg.appendChild(path_1);
184-
185-
let path_2 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
186-
path_2.setAttribute('d', 'M' + paths[1].map(node => node.x+','+node.y).join(" "));
187-
path_2.setAttribute('stroke', 'green');
188-
path_2.setAttribute('stroke-width', '30');
189-
path_2.setAttribute('fill', 'none');
190-
svg.appendChild(path_2);
191-
192-
let origin = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
193-
origin.setAttribute('cx', '0');
194-
origin.setAttribute('cy', '0');
195-
origin.setAttribute('r', '100');
196-
origin.setAttribute('fill', 'black');
197-
svg.appendChild(origin);
173+
let svg = aoc_svg.svg(
174+
this.day3_max_x + Math.abs(this.day3_min_x) + 100,
175+
this.day3_max_y + Math.abs(this.day3_min_y) + 100,
176+
this.day3_min_x - 50,
177+
this.day3_min_y - 50
178+
);
179+
180+
svg.appendChild(aoc_svg.path(paths[0].map(node => node.x+','+node.y), 30, 'red'));
181+
svg.appendChild(aoc_svg.path(paths[1].map(node => node.x+','+node.y), 30, 'green'));
182+
svg.appendChild(aoc_svg.circle(0, 0, 100));
198183

199184
crossings.forEach(crossing => {
200-
let dot = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
201-
dot.setAttribute('cx', crossing.x);
202-
dot.setAttribute('cy', crossing.y);
203-
dot.setAttribute('r', '70');
204-
dot.setAttribute('fill', 'blue');
205-
svg.appendChild(dot);
185+
svg.appendChild(aoc_svg.circle(crossing.x, crossing.y, 70, 'blue'));
206186
});
207187

208188
document.querySelector('#output_area').appendChild(svg);

08/solve.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,19 @@ adventofcode.day8_build_layers = function(input) {
6060
};
6161

6262
adventofcode.day8_print_svg = function(layers) {
63-
let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
64-
svg.setAttribute('version', '1.1');
65-
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
66-
svg.setAttribute('viewBox', '0 0 ' + this.day8_width + ' ' + this.day8_height);
63+
let svg = aoc_svg.svg(this.day8_width, this.day8_height);
6764

6865
layers = layers.reverse();
6966

7067
layers.forEach(layer => {
71-
let layergroup = document.createElementNS('http://www.w3.org/2000/svg', 'g');
68+
let layergroup = aoc_svg.group();
7269

7370
layer.split("").forEach((color,idx) => {
7471
if (color < 2) {
7572
const y = Math.floor(idx / this.day8_width);
7673
const x = idx % this.day8_width;
7774

78-
let pixel = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
79-
pixel.setAttribute('x', x);
80-
pixel.setAttribute('y', y);
81-
pixel.setAttribute('width', 1);
82-
pixel.setAttribute('height', 1);
83-
pixel.setAttribute('fill', color === '0' ? 'white' : 'black');
84-
85-
layergroup.appendChild(pixel);
75+
layergroup.appendChild(aoc_svg.rect(x, y, 1, 1, color === '0' ? 'white' : 'black'));
8676
}
8777
});
8878

11/solve.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,10 @@ adventofcode.day11_paint_hull = function(tiles) {
7474
min_y = 0,
7575
max_y = 0;
7676

77-
svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
78-
svg.setAttribute('version', '1.1');
79-
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
80-
81-
let background = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
82-
background.setAttribute('fill', 'black');
83-
background.setAttribute('width', '100%');
84-
background.setAttribute('height', '100%');
77+
let svg = aoc_svg.svg();
78+
79+
let background = aoc_svg.rect();
80+
8581
svg.appendChild(background);
8682

8783
tiles.forEach(tile => {
@@ -90,18 +86,11 @@ adventofcode.day11_paint_hull = function(tiles) {
9086
min_y = Math.min(min_y, tile.y);
9187
max_y = Math.max(max_y, tile.y);
9288

93-
if (tile.color === 1) {
94-
let pixel = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
95-
pixel.setAttribute('x', tile.x);
96-
pixel.setAttribute('y', tile.y);
97-
pixel.setAttribute('width', '1');
98-
pixel.setAttribute('height', '1');
99-
pixel.setAttribute('fill', 'white');
100-
svg.appendChild(pixel);
101-
}
89+
if (tile.color === 1)
90+
svg.appendChild(aoc_svg.rect(tile.x, tile.y, 1, 1, 'white'));
10291
});
10392

104-
svg.setAttribute('viewBox', min_x + ' ' + min_y +' ' + (max_x - min_x + 1) + ' ' + (max_y - min_y + 1));
93+
aoc_svg.setViewBox(svg, max_x - min_x + 1, max_y - min_y + 1, min_x, min_y);
10594

10695
document.querySelector('#output_area').appendChild(svg);
10796
};

13/solve.js

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,40 +79,16 @@ adventofcode.day13_print_screen = function(tiles, max_x, max_y) {
7979
if (document.querySelector('#game_area'))
8080
svg = document.querySelector('#game_area');
8181
else
82-
{
83-
svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
84-
svg.setAttribute('id', 'game_area');
85-
svg.setAttribute('version', '1.1');
86-
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
87-
svg.setAttribute('viewBox', '0 0 ' + (max_x * 20 + 20) + ' ' + (max_y * 20 + 20));
88-
}
82+
svg = aoc_svg.svg(max_x + 1, max_y + 1);
8983

90-
tiles[0].forEach(tile => svg.appendChild(this.day13_get_svg_pixel(tile, 'black')));
91-
tiles[1].forEach(tile => svg.appendChild(this.day13_get_svg_pixel(tile, 'gray')));
92-
tiles[2].forEach(tile => svg.appendChild(this.day13_get_svg_pixel(tile, 'yellow')));
93-
tiles[3].forEach(tile => svg.appendChild(this.day13_get_svg_pixel(tile, 'blue')));
84+
tiles[0].forEach(tile => svg.appendChild(aoc_svg.rect(tile.x, tile.y, 1, 1, 'black')));
85+
tiles[1].forEach(tile => svg.appendChild(aoc_svg.rect(tile.x, tile.y, 1, 1, 'gray')));
86+
tiles[2].forEach(tile => svg.appendChild(aoc_svg.rect(tile.x, tile.y, 1, 1, 'yellow')));
87+
tiles[3].forEach(tile => svg.appendChild(aoc_svg.rect(tile.x, tile.y, 1, 1, 'blue')));
9488

9589
tiles[4].forEach(tile => {
96-
let ball = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
97-
ball.setAttribute('cx', tile.x * 20 + 10);
98-
ball.setAttribute('cy', tile.y * 20 + 10);
99-
ball.setAttribute('r', '8');
100-
ball.setAttribute('fill', 'red');
101-
svg.appendChild(ball);
90+
svg.appendChild(aoc_svg.circle(parseInt(tile.x) + .5, parseInt(tile.y) + .5, .5, 'red'));
10291
});
10392

10493
document.querySelector('#output_area').appendChild(svg);
105-
};
106-
107-
adventofcode.day13_get_svg_pixel = function(tile, color) {
108-
let pixel = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
109-
110-
pixel.setAttribute('x', tile.x * 20);
111-
pixel.setAttribute('y', tile.y * 20);
112-
pixel.setAttribute('width', '19');
113-
pixel.setAttribute('height', '19');
114-
115-
pixel.setAttribute('fill', color);
116-
117-
return pixel;
11894
};

15/solve.js

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,15 @@ adventofcode.day15_get_move_queue = function(last_move) {
9999
};
100100

101101
adventofcode.day15_draw_map = function(map) {
102-
let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
103-
svg.setAttribute('version', '1.1');
104-
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
102+
let svg = aoc_svg.svg();
105103

106104
let min_x = 0,
107105
min_y = 0,
108106
max_x = 0,
109107
max_y = 0;
110108

111-
let background = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
112-
background.setAttribute('width', '100%');
113-
background.setAttribute('height', '100%');
114-
background.setAttribute('fill', 'grey');
109+
let background = aoc_svg.rect(0, 0, '100%', '100%', 'grey');
110+
115111
svg.appendChild(background);
116112

117113
map.forEach(cell => {
@@ -120,32 +116,21 @@ adventofcode.day15_draw_map = function(map) {
120116
min_y = Math.min(min_y, cell.y);
121117
max_y = Math.max(max_y, cell.y);
122118

123-
let pixel = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
124-
pixel.setAttribute('x', cell.x);
125-
pixel.setAttribute('y', cell.y);
126-
pixel.setAttribute('width', '1');
127-
pixel.setAttribute('height', '1');
119+
let color = 'black';
128120

129121
switch (cell.state) {
130-
case 0: pixel.setAttribute('fill', 'grey'); break;
131-
case 1: pixel.setAttribute('fill', 'black'); break;
132-
case 2: pixel.setAttribute('fill', 'red'); break;
122+
case 0: color = 'grey'; break;
123+
case 1: color = 'black'; break;
124+
case 2: color = 'red'; break;
133125
}
134126

135-
svg.appendChild(pixel);
127+
svg.appendChild(aoc_svg.rect(cell.x, cell.y, 1, 1, color));
136128
});
137129

138-
background.setAttribute('x', min_x);
139-
background.setAttribute('y', min_y);
140-
141-
let origin = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
142-
origin.setAttribute('cx', .5);
143-
origin.setAttribute('cy', .5);
144-
origin.setAttribute('r', .5);
145-
origin.setAttribute('fill','green');
146-
svg.appendChild(origin);
130+
svg.appendChild(aoc_svg.circle(.5, .5, .5, 'green'));
147131

148-
svg.setAttribute('viewBox', min_x + ' ' + min_y + ' ' + (max_x - min_x + 1) + ' ' + (max_y - min_y + 1));
132+
aoc_svg.setViewBox(svg, max_x - min_x + 1, max_y - min_y + 1, min_x, min_y);
133+
aoc_svg.setPosition(background, min_x, min_y);
149134

150135
// draw path
151136
let x = .5,
@@ -163,12 +148,7 @@ adventofcode.day15_draw_map = function(map) {
163148
nodes.push(x+','+y);
164149
});
165150

166-
let path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
167-
path.setAttribute('d', 'M' + nodes.join(" "));
168-
path.setAttribute('stroke', 'yellow');
169-
path.setAttribute('stroke-width', '.1');
170-
path.setAttribute('fill', 'none');
171-
svg.appendChild(path);
151+
svg.appendChild(aoc_svg.path(nodes, .1, 'yellow'));
172152

173153
document.querySelectorAll("#output_area svg").forEach(elem => elem.remove());
174154
document.querySelector('#output_area').appendChild(svg);

lib.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,68 @@ let aoc_intcode = {
147147
else
148148
return program[pointer];
149149
}
150+
};
151+
152+
let aoc_svg = {
153+
svg: function(width = 100, height = 100, x = 0, y = 0) {
154+
let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
155+
156+
svg.setAttribute('version', '1.1');
157+
svg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
158+
159+
svg.setAttribute('viewBox', x + ' ' + y + ' ' + width + ' ' + height);
160+
161+
return svg;
162+
},
163+
setViewBox: function(svg, width, height, x, y) {
164+
svg.setAttribute('viewBox', x + ' ' + y + ' ' + width + ' ' + height);
165+
},
166+
group: function() {
167+
return document.createElementNS('http://www.w3.org/2000/svg', 'g');
168+
},
169+
rect: function(x = 0, y = 0, width = '100%', height = '100%', fill = 'black') {
170+
let rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
171+
172+
rect.setAttribute('x', x);
173+
rect.setAttribute('y', y);
174+
175+
rect.setAttribute('width', width);
176+
rect.setAttribute('height', height);
177+
178+
rect.setAttribute('fill', fill);
179+
180+
return rect;
181+
},
182+
circle: function(x = 0, y = 0, radius = 1, fill = 'black') {
183+
let circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
184+
185+
circle.setAttribute('cx', x);
186+
circle.setAttribute('cy', y);
187+
circle.setAttribute('r', radius);
188+
189+
circle.setAttribute('fill', fill);
190+
191+
return circle;
192+
},
193+
path: function(coordinates = [], width = 1, color = 'black') {
194+
let path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
195+
196+
path.setAttribute('d', 'M' + coordinates.join(" "));
197+
198+
path.setAttribute('stroke', color);
199+
path.setAttribute('stroke-width', width);
200+
201+
path.setAttribute('fill', 'none');
202+
203+
return path;
204+
},
205+
setPosition: function(element, x = 0, y = 0) {
206+
if (element.nodeName === 'circle') {
207+
element.setAttribute('cx', x);
208+
element.setAttribute('cy', y);
209+
} else {
210+
element.setAttribute('x', x);
211+
element.setAttribute('y', y);
212+
}
213+
}
150214
};

0 commit comments

Comments
 (0)