diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-09 10:31:11 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-09 10:31:11 -0700 |
commit | 4dbcd4a1b703d8b0ee822914ecb6e55d9e9bb37c (patch) | |
tree | 5b787c81be5402f80a835ac1e1a58f6311c4ce05 /web/web_svg.c | |
parent | 5bc3e0d95cd4ae4e12178988a639202ceaf50451 (diff) | |
download | dartboat-4dbcd4a1b703d8b0ee822914ecb6e55d9e9bb37c.tar.gz dartboat-4dbcd4a1b703d8b0ee822914ecb6e55d9e9bb37c.tar.xz |
web: dynamically generate SVG and groups
Diffstat (limited to 'web/web_svg.c')
-rw-r--r-- | web/web_svg.c | 55 |
1 files changed, 47 insertions, 8 deletions
diff --git a/web/web_svg.c b/web/web_svg.c index b52705f..470b7ec 100644 --- a/web/web_svg.c +++ b/web/web_svg.c @@ -26,7 +26,8 @@ static inline struct dom_elem *svg_elem_init(char *name, int size_attrs) } // draws a ring if angles are equal -struct dom_elem *gen_arc(double a1, double a2, double r1, double r2, char *col) +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 }); @@ -61,17 +62,17 @@ struct dom_elem *gen_arc(double a1, double a2, double r1, double r2, char *col) return e; } -struct dom_elem *gen_segment(double a, double r1, double r2, char *col) +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); } -struct dom_elem *gen_ring(double r, double w, char *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); } -struct dom_elem *gen_circle(double r, char *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); @@ -80,7 +81,8 @@ struct dom_elem *gen_circle(double r, char *col) return e; } -struct dom_elem *gen_line(double a, double r1, double r2, double w, char *col) +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 }); @@ -96,7 +98,7 @@ struct dom_elem *gen_line(double a, double r1, double r2, double w, char *col) return e; } -int draw_spider(int elemc, struct dom_elem **elemv) +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); @@ -123,7 +125,7 @@ int draw_spider(int elemc, struct dom_elem **elemv) return elemc; } -int draw_numbers(int elemc, struct dom_elem **elemv) +static int draw_numbers(int elemc, struct dom_elem **elemv) { elemv[elemc++] = gen_ring(DIAMETER/2 - WIRE_WIDTH*4, WIRE_WIDTH, "#ddd"); @@ -152,7 +154,7 @@ int draw_numbers(int elemc, struct dom_elem **elemv) return elemc; } -void svg_draw_board() +static void draw_board() { struct dom_elem **elemv = malloc(200 * sizeof(*elemv)); int elemc = 0; @@ -183,6 +185,37 @@ void svg_draw_board() 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"); @@ -199,3 +232,9 @@ void svg_clear_points() { dom_set_content("#dartboard .overlay", NULL); } + +void svg_init() +{ + create_board(); + draw_board(); +} |