summaryrefslogtreecommitdiff
path: root/web/web_svg.c
blob: 485583d79c7eb7f4454c1863e97e155611835d3b (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "web_svg.h"
#include "web_dom.h"
#include "web_opts.h"

#include "board.h"
#include "comp.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <emscripten/emscripten.h>

#define NUM_SECTORS 20
#define SECTOR_WIDTH 18.0

#define C_BLACK "#272b2c"
#define C_WHITE "#fbe3ba"
#define C_RED "#f6302f"
#define C_GREEN "#22912d"
#define C_WIRE "#909ca0"
#define C_WIRE_INNER "#d8e6ec"

static inline struct dom_elem *svg_elem_init(char *name, int size_attrs)
{
	return dom_elem_init("http://www.w3.org/2000/svg", name, size_attrs);
}

// draws a ring if angles are equal
static struct dom_elem *gen_arc(double a1, double a2, double r1, double r2,
	char *col)
{
	struct ccoords cc1 = pol_to_cart(
		(struct pcoords){ .a = (a1 == a2 ? 1 : a2), .r = r1 });
	struct ccoords cc2 = pol_to_cart(
		(struct pcoords){ .a = (a1 == a2 ? 0 : a1), .r = r1 });
	struct ccoords cc3 = pol_to_cart(
		(struct pcoords){ .a = (a1 == a2 ? 0 : a1), .r = r2 });
	struct ccoords cc4 = pol_to_cart(
		(struct pcoords){ .a = (a1 == a2 ? 1 : a2), .r = r2 });

	struct dom_elem *e = svg_elem_init("path", 2);
	dom_elem_add_attr(e, "fill", col);

	char buf[512];
	int buflen = 0;
	buflen += sprintf(buf + buflen, "M%f %f", cc1.x, cc1.y);
	buflen += sprintf(buf + buflen, "A%f %f 0 0 0 %f %f",
		r1, r1, cc2.x, cc2.y);
	if (a1 == a2)
		buflen += sprintf(buf + buflen, "A%f %f 0 1 0 %f %f",
			r1, r1, cc1.x, cc1.y);
	buflen += sprintf(buf + buflen, "L%f %f", cc3.x, cc3.y);
	buflen += sprintf(buf + buflen, "A%f %f 0 0 1 %f %f",
		r2, r2, cc4.x, cc4.y);
	if (a1 == a2)
		buflen += sprintf(buf + buflen, "A%f %f 0 1 1 %f %f",
			r2, r2, cc3.x, cc3.y);
	buf[buflen++] = 'Z';
	buf[buflen++] = 0;
	dom_elem_add_attr(e, "d", buf);

	return e;
}

static struct dom_elem *gen_segment(double a, double r1, double r2, char *col)
{
	return gen_arc(a - SECTOR_WIDTH/2, a + SECTOR_WIDTH/2, r1, r2, col);
}

static struct dom_elem *gen_ring(double r, double w, char *col)
{
	return gen_arc(0, 0, r - w/2, r + w/2, col);
}

static struct dom_elem *gen_circle(double r, char *col)
{
	struct dom_elem *e = svg_elem_init("circle", 2);
	dom_elem_add_attr(e, "fill", col);
	dom_elem_add_attrd(e, "r", r);

	return e;
}

static struct dom_elem *gen_line(double a, double r1, double r2, double w,
	char *col)
{
	struct ccoords cc1 = pol_to_cart((struct pcoords){ .a = a, .r = r1 });
	struct ccoords cc2 = pol_to_cart((struct pcoords){ .a = a, .r = r2 });

	struct dom_elem *e = svg_elem_init("line", 6);
	dom_elem_add_attr(e, "stroke", col);
	dom_elem_add_attrd(e, "x1", cc1.x);
	dom_elem_add_attrd(e, "y1", cc1.y);
	dom_elem_add_attrd(e, "x2", cc2.x);
	dom_elem_add_attrd(e, "y2", cc2.y);
	dom_elem_add_attrd(e, "stroke-width", w);

	return e;
}

static int draw_spider(int elemc, struct dom_elem **elemv)
{
	for (int i = 5; i > 1; --i) {
		elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE);
		elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2,
			C_WIRE_INNER);
	}

	for (int i = 0; i < NUM_SECTORS; ++i) {
		double a = 90 - i*SECTOR_WIDTH - SECTOR_WIDTH/2;
		if (a < 0) a += 360;

		elemv[elemc++] = gen_line(a, OUTER_DISTS[1],
			OUTER_DISTS[5] + 10, WIRE_WIDTH, C_WIRE);
		elemv[elemc++] = gen_line(a, OUTER_DISTS[1],
			OUTER_DISTS[5] + 10, WIRE_WIDTH/2, C_WIRE_INNER);
	}

	for (int i = 1; i >= 0; --i) {
		elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH, C_WIRE);
		elemv[elemc++] = gen_ring(OUTER_DISTS[i], WIRE_WIDTH/2,
			C_WIRE_INNER);
	}

	return elemc;
}

static int draw_numbers(int elemc, struct dom_elem **elemv)
{
	elemv[elemc++] = gen_ring(DIAMETER/2 - WIRE_WIDTH*4, WIRE_WIDTH,
		"#ddd");

	int r = DIAMETER/2 - 33/2;
	for (int i = 0; i < 20; ++i) {
		struct dom_elem *e = elemv[elemc++] = svg_elem_init("text", 5);
		dom_elem_add_attr(e, "font-size", "33");
		dom_elem_add_attr(e, "fill", "#fff");
		dom_elem_add_attr(e, "text-anchor", "middle");
		dom_elem_add_attr(e, "dominant-baseline", "central");

		char buf[512];
		double a = 90 - i*SECTOR_WIDTH;
		if (a < 0) a += 360;
		struct ccoords cc = pol_to_cart(
			(struct pcoords){ .a = a, .r = r });
		sprintf(buf, "scale(1 -1) translate(%f %f) rotate(%f)",
			cc.x, -cc.y, a <= 180 ? 90 -a : 270 -a);
		dom_elem_add_attr(e, "transform", buf);

		e->content = malloc(3);
		sprintf(e->content, "%d", SECTORS[i]);
	}

	return elemc;
}

static void draw_board()
{
	struct dom_elem **elemv = malloc(200 * sizeof(*elemv));
	int elemc = 0;

	elemv[elemc++] = gen_circle(DIAMETER/2, C_BLACK);

	for (int i = 5; i > 1; --i) {
		for (int j = 0; j < NUM_SECTORS; ++j) {
			double a = 90 - j*SECTOR_WIDTH;
			if (a < 0) a += 360;
			elemv[elemc++] = gen_segment(a, OUTER_DISTS[i-1],
				OUTER_DISTS[i],
				i % 2 ? (j % 2 ? C_GREEN : C_RED) :
					(j % 2 ? C_WHITE : C_BLACK));
		}
	}

	elemv[elemc++] = gen_circle(OUTER_DISTS[1], C_GREEN);
	elemv[elemc++] = gen_circle(OUTER_DISTS[0], C_RED);

	elemc = draw_spider(elemc, elemv);
	elemc = draw_numbers(elemc, elemv);

	dom_append_elemv("#dartboard .base", elemc, elemv);

	for (int i = 0; i < elemc; ++i)
		dom_elem_free(elemv[i]);
	free(elemv);
}

static void create_board()
{
	struct dom_elem *e = svg_elem_init("svg", 2);
	dom_elem_add_attr(e, "id", "dartboard");
	char *viewbox;
	asprintf(&viewbox, "0 0 %f %f", DIAMETER, DIAMETER);
	dom_elem_add_attr(e, "viewBox", viewbox);
	free(viewbox);
	dom_append_elemv("#dartboard-container", 1, &e);

	struct dom_elem **elemv = malloc(2 * sizeof(*elemv));
	int elemc = 0;

	char *transform;
	asprintf(&transform, "translate(%f %f) scale(1 -1)",
		DIAMETER/2, DIAMETER/2);
	for (int i = 0; i < 2; ++i) {
		struct dom_elem *e = svg_elem_init("g", 2);
		dom_elem_add_attr(e, "class", i ? "overlay" : "base");
		dom_elem_add_attr(e, "transform", transform);
		elemv[elemc++] = e;
	}
	free(transform);

	dom_append_elemv("#dartboard", elemc, elemv);

	for (int i = 0; i < elemc; ++i)
		dom_elem_free(elemv[i]);
	free(elemv);
}

void svg_draw_point(double x, double y)
{
	struct dom_elem *e = gen_circle(6, "#33f");
	dom_elem_add_attr(e, "stroke", "#ff0");
	dom_elem_add_attr(e, "stroke-width", "2");
	dom_elem_add_attrd(e, "cx", x);
	dom_elem_add_attrd(e, "cy", y);

	if (delay_ms >= SVG_THROW_ANIM_MS) {
		char buf[64];
		sprintf(buf, "animation: throw-anim %dms;", SVG_THROW_ANIM_MS);
		dom_elem_add_attr(e, "style", buf);
	}

	dom_append_elemv("#dartboard .overlay", 1, &e);
	dom_elem_free(e);
}

void svg_clear_points()
{
	dom_set_content("#dartboard .overlay", NULL);
}

void svg_init()
{
	create_board();
	draw_board();
}