-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglitcher
executable file
·200 lines (176 loc) · 5.3 KB
/
glitcher
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
#!/bin/bash
OUT="${1}"
TXT="${*:2}"
GLITCHER_DEBUG="${GLITCHER_DEBUG:-0}"
[ ! -t 0 ] && { read -r TXT; }
function print_help {
cat <<EOF
$ glitcher FILE TEXT...
eg.
glitcher out.svg hello world
EOF
}
! [[ "$BASH_VERSION" =~ 5.* ]] && {
echo "[ERROR] Bash v5 or higher is required, found $BASH_VERSION"
exit 1
}
[ "$OUT" = "" ] || [ "$TXT" = "" ] && {
echo " [ERROR] bad argument"
print_help
exit 1
}
! [ "$OSTYPE" = "linux-gnu" ] && {
echo "[WARNING] only tested on linux-gnu"
sleep 3
}
function rnd {
awk \
-v s="$RANDOM" \
-v min="${1:-0}" \
-v max="${2:-1}" \
-v neg="${3:-0}" '
BEGIN {
srand(s);
printf"%.2f", min+rand()*(max-min)*(neg==0?1:rand()>.5?1:-1)
}'
}
function rnd_seq {
local upper="${1:?BAD}"
local keys=()
local vals=''
local steps="$(((RANDOM % 8) + 8))"
for ((i = 0; i < steps - 2; i++)); do keys+=("$(rnd 0.08 1)"); done
mapfile -t keys < <(
printf '0;\n'
printf '%s;\n' "${keys[@]}" | sort -g | uniq
)
for _ in "${keys[@]}"; do vals+="$(rnd 0.001 "$upper" 1); "; done
local ks="${keys[*]}"
printf '%s\n%s' "${ks::(-1)}" "${vals::(-2)}"
}
mapfile -t B_SHIFT < <(rnd_seq 0.016)
mapfile -t R_SHIFT < <(rnd_seq 0.016)
read -r B_DUR < <(rnd 50 60)
read -r R_DUR < <(rnd 55 60)
read -r WIDTH < <(bc <<<"${#TXT} * 17")
((GLITCHER_DEBUG)) && RECT='<rect fill="#ee33aa" width="100%" height="100%" x="0" y="0" />'
cat <<EOF >"$OUT"
<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 ${WIDTH} 32" >
<!-- made with glitcher-cli v0.6
█▀▀ █░░ █ ▀█▀ █▀▀ █░█ █▀▀ █▀█
█▄█ █▄▄ █ ░█░ █▄▄ █▀█ ██▄ █▀▄
https://github.com/metaory/glitcher-cli -->
${RECT}
<text filter="url(#glitch)"
fill="#EEAAFF"
font-family="monospace, serif"
font-weight="bolder"
font-size="x-large"
text-anchor="middle"
dominant-baseline="mathematical"
x="50%" y="42%"
>${TXT}</text>
<defs>
<filter id="glitch"
primitiveUnits="objectBoundingBox"
x="-20%"
y="0%"
width="120%"
height="100%">
<feColorMatrix in="SourceGraphic"
result="red"
type="matrix"
values="1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0" />
<feColorMatrix in="SourceGraphic"
result="green"
type="matrix"
values="0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0" />
<feColorMatrix in="SourceGraphic"
result="blue"
type="matrix"
values="0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0" />
<feOffset in="red"
result="red-shifted"
dx="-0.01"
dy="0">
<animate attributeName="dx"
keyTimes="${R_SHIFT[0]}"
values="${R_SHIFT[1]}"
begin="0"
dur="${R_DUR}s"
calcMode="discrete"
repeatCount="indefinite"
fill="freeze" />
</feOffset>
<feOffset in="blue"
result="blue-shifted"
dx="0.01"
dy="0">
<animate attributeName="dx"
keyTimes="${B_SHIFT[0]}"
values="${B_SHIFT[1]}"
begin="0"
dur="${B_DUR}s"
calcMode="discrete"
repeatCount="indefinite"
fill="freeze" />
</feOffset>
<feBlend mode="screen"
in="red-shifted"
in2="green"
result="red-green" />
<feBlend mode="screen"
in="red-green"
in2="blue-shifted"
result="blended" />
EOF
I=0
NW=0
SLICES=()
function put_slice {
((I++))
read -r S1 < <(rnd 6 24)
read -r S2 < <(rnd 6 16)
read -r DR < <(rnd 30 60)
read -r NX < <(bc <<<"$NW + $S1")
mapfile -t SEQ < <(rnd_seq 0.012)
SA="slice_${I}a"
SB="slice_${I}b"
SLICES+=("$SA" "$SB")
cat <<EOF >>"$OUT"
<feOffset in="blended"
dx="0"
dy="0"
y="${NW}%"
height="${S1}%"
result="${SA}" />
<feOffset in="blended"
dx="0"
dy="0"
y="${NX}%"
height="${S2}%"
result="${SB}">
<animate attributeName="dx"
keyTimes="${SEQ[0]}"
values="${SEQ[1]}"
begin="0s"
dur="${DR}s"
calcMode="discrete"
repeatCount="indefinite"
fill="freeze" />
</feOffset>
EOF
read -r NW < <(bc <<<"($NX + $S2)*1/1")
}
while ((NW <= 100)); do put_slice; done
cat <<<' <feMerge>' >>"$OUT"
for S in "${SLICES[@]}"; do cat <<<" <feMergeNode in=""\"${S}\""" />" >>"$OUT"; done
cat <<EOF >>"$OUT"
</feMerge>
</filter>
</defs>
</svg>
EOF
echo "created $OUT successfully."
# vim: ft=bash