Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

hearts.gno

2.04 Kb · 59 lines
 1package home
 2
 3import (
 4	"encoding/base64"
 5	"strconv"
 6	"strings"
 7)
 8
 9// The hearts wall draws one red heart per address that sent one, row by row,
10// with the slots still free in the last row left as outlines. Past
11// wallCols*wallMaxRows hearts the wall stays full and a line under it counts
12// the rest, so the image never grows past ten rows.
13
14const (
15	wallCols    = 24
16	wallMaxRows = 10
17	wallCell    = 28
18	heartRed    = "#e63946"
19	heartPath   = "M12 21C12 21 3 15.5 3 9.5 3 6.5 5.4 4 8.3 4 10 4 11.2 5 12 6.2 12.8 5 14 4 15.7 4 18.6 4 21 6.5 21 9.5 21 15.5 12 21 12 21Z"
20)
21
22// renderHeartsWall returns the wall as a markdown image; with no heart yet it
23// is one row of empty slots.
24func renderHeartsWall() string {
25	n := Hearts()
26	shown := n
27	if limit := wallCols * wallMaxRows; shown > limit {
28		shown = limit
29	}
30	rows := shown/wallCols + 1
31	if rows > wallMaxRows {
32		rows = wallMaxRows
33	}
34	w := 24 + wallCols*wallCell
35	h := 24 + rows*wallCell
36	more := n - shown
37	if more > 0 {
38		h += 26
39	}
40	var b strings.Builder
41	b.WriteString(`<svg xmlns="http://www.w3.org/2000/svg" width="` + strconv.Itoa(w) + `" height="` + strconv.Itoa(h) +
42		`" viewBox="0 0 ` + strconv.Itoa(w) + ` ` + strconv.Itoa(h) + `"><defs><path id="h" d="` + heartPath + `"/></defs>`)
43	b.WriteString(`<rect width="` + strconv.Itoa(w) + `" height="` + strconv.Itoa(h) + `" rx="12" fill="#10231c"/>`)
44	for i := 0; i < rows*wallCols; i++ {
45		x := 14 + (i%wallCols)*wallCell
46		y := 14 + (i/wallCols)*wallCell
47		style := `fill="` + heartRed + `"`
48		if i >= shown {
49			style = `fill="none" stroke="#6b8f7f" stroke-width="1.5"`
50		}
51		b.WriteString(`<use href="#h" x="` + strconv.Itoa(x) + `" y="` + strconv.Itoa(y) + `" ` + style + `/>`)
52	}
53	if more > 0 {
54		b.WriteString(`<text x="` + strconv.Itoa(w/2) + `" y="` + strconv.Itoa(h-14) + `" text-anchor="middle" ` +
55			`fill="#9fc9b4" style="font:13px sans-serif">and ` + strconv.Itoa(more) + ` more</text>`)
56	}
57	b.WriteString("</svg>")
58	return "![" + strconv.Itoa(n) + " hearts](data:image/svg+xml;base64," + base64.StdEncoding.EncodeToString([]byte(b.String())) + ")\n"
59}