-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflare.js
180 lines (159 loc) · 7.56 KB
/
flare.js
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
svgns = "http://www.w3.org/2000/svg";
svgElem = document.getElementById("flarecontainer");
svgElem.setAttribute("xmlns", svgns);
// link number/range
for (i = 0; i < document.querySelectorAll('input[type=number]').length; i++) {
document.querySelectorAll("input[type=range]")[i].oninput = new Function("document.querySelectorAll('input[type=number]')[" + i.toString() + "].value = document.querySelectorAll('input[type=range]')[" + i.toString() + "].value");
document.querySelectorAll("input[type=number]")[i].oninput = new Function("document.querySelectorAll('input[type=range]')[" + i.toString() + "].value = document.querySelectorAll('input[type=number]')[" + i.toString() + "].value");
}
function drawFlare(flareX, flareY, hotspotscale, streakscale, randomseed) {
// use random seed - thanks to http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html
Math.seedrandom(randomseed);
// clear old
svgElem.innerHTML = "";
// add defs
svgElem.innerHTML += `
<defs>
<filter id="blur" y="-200%" height="500%">
<feGaussianBlur in="SourceGraphic" stdDeviation="5" />
</filter>
<filter id="smallblur" y="-200%" height="500%">
<feGaussianBlur in="SourceGraphic" stdDeviation="2.5" />
</filter>
<radialGradient id="glow">
<stop offset="0%" stop-color="white" />
<stop offset="100%" stop-color="black" />
</radialGradient>
</defs>
`;
// black bg
background = document.createElementNS(svgns, "rect");
background.setAttribute("width", 800);
background.setAttribute("height", 450);
background.style.fill = "black";
svgElem.appendChild(background);
flareCenter = [flareX - 400, flareY - 225];
x = flareCenter[0];
y = flareCenter[1];
// multi-iris towards lens
for (i = 0; i < 55; i++) {
x += (0 - flareCenter[0]) / 20
y += (0 - flareCenter[1]) / 20;
if (Math.random() < 0.35) {
iris = document.createElementNS(svgns, "path");
iris.setAttribute("d", "M431.16 225L422.04 247.04L400 256.16L377.96 247.04L368.84 225L377.96 202.96L400 193.84L422.04 202.96L431.16 225Z");
iris.setAttribute("transform", "translate(" + x.toString() + ", " + y.toString() + ")\nscale(" + (Math.random() * (i / 30)).toString() + ")");
iris.setAttribute("opacity", Math.random() / 3);
iris.style.transformOrigin = "center";
iris.style.fill = "white";
iris.style.mixBlendMode = "screen";
iris.setAttribute("filter", "url(#smallblur)");
svgElem.appendChild(iris);
}
}
x = flareCenter[0];
y = flareCenter[1];
// multi-iris away from lens
for (i = 0; i < 25; i++) {
x -= (0 - flareCenter[0]) / 20
y -= (0 - flareCenter[1]) / 20;
if (Math.random() < 0.35) {
iris = document.createElementNS(svgns, "path");
iris.setAttribute("d", "M431.16 225L422.04 247.04L400 256.16L377.96 247.04L368.84 225L377.96 202.96L400 193.84L422.04 202.96L431.16 225Z");
iris.setAttribute("transform", "translate(" + x.toString() + ", " + y.toString() + ")\nscale(" + (Math.random() * (i / 30)).toString() + ")");
iris.setAttribute("opacity", Math.random() / 3);
iris.style.transformOrigin = "center";
iris.style.fill = "white";
iris.style.mixBlendMode = "screen";
iris.setAttribute("filter", "url(#smallblur)");
svgElem.appendChild(iris);
}
}
hotspot = document.createElementNS(svgns, "path");
hotspot.setAttribute("d", "M570.33 225L416.45 229.41L547.51 310.16L412.04 237.04L485.16 372.51L404.41 241.45L400 395.33L395.59 241.45L314.84 372.51L387.96 237.04L252.49 310.16L383.55 229.41L229.67 225L383.55 220.59L252.49 139.84L387.96 212.96L314.84 77.49L395.59 208.55L400 54.67L404.41 208.55L485.16 77.49L412.04 212.96L547.51 139.84L416.45 220.59L570.33 225Z");
hotspot.setAttribute("transform", "translate(" + flareCenter[0] + ", " + flareCenter[1] + ")\nscale(" + (0.35 * hotspotscale).toString() + ")");
hotspot.style.transformOrigin = "center";
hotspot.style.fill = "white";
hotspot.style.mixBlendMode = "screen";
hotspot.setAttribute("filter", "url(#smallblur)");
svgElem.appendChild(hotspot);
glow = document.createElementNS(svgns, "circle");
glow.setAttribute("cx", 400);
glow.setAttribute("cy", 225);
glow.setAttribute("r", 22.5 * hotspotscale);
glow.setAttribute("transform", "translate(" + flareCenter[0] + ", " + flareCenter[1] + ")");
glow.style.fill = "url(#glow)";
glow.setAttribute("filter", "url(#blur)");
glow.style.mixBlendMode = "screen";
svgElem.appendChild(glow);
halo = document.createElementNS(svgns, "circle");
halo.setAttribute("cx", 400);
halo.setAttribute("cy", 225);
halo.setAttribute("r", 50 * hotspotscale);
halo.setAttribute("transform", "translate(" + flareCenter[0] + ", " + flareCenter[1] + ")");
halo.setAttribute("opacity", 0.1);
halo.style.fill = "white";
halo.style.mixBlendMode = "screen";
halo.setAttribute("filter", "url(#blur)");
svgElem.appendChild(halo);
streak = document.createElementNS(svgns, "path");
streak.setAttribute("d", "M603 225L400 230L197 225L400 220L603 225Z");
streak.setAttribute("transform", "translate(" + flareCenter[0] + ", " + flareCenter[1] + ")\nscale(" + streakscale.toString() + ", 0.4)");
streak.style.transformOrigin = "center";
streak.style.fill = "white";
streak.style.mixBlendMode = "screen";
streak.setAttribute("filter", "url(#blur)");
svgElem.appendChild(streak);
}
function exportsvg() {
file = new Blob([svgElem.outerHTML], {type: "svg"});
downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(file);
downloadLink.download = "my_lens_flare.svg";
downloadLink.click();
}
document.getElementById("exportsvg").onclick = exportsvg;
function exportpng() {
svgData = new XMLSerializer().serializeToString(svgElem);
imgElem = document.createElement("img");
imgElem.src = "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgData)));
imgElem.onload = function() {
svgClientRect = svgElem.getBoundingClientRect();
canvas = document.createElement("canvas");
canvas.width = svgClientRect.width * 4.8;
canvas.height = svgClientRect.height * 4.8;
ctx = canvas.getContext("2d");
ctx.drawImage(imgElem, 0, 0, 3840, 2160);
downloadLink = document.createElement("a");
downloadLink.href = canvas.toDataURL("image/png");
downloadLink.download = "my_lens_flare.png";
downloadLink.click();
}
}
document.getElementById("exportpng").onclick = exportpng;
// check for form updating
function readFormData() {
x = parseFloat(document.querySelector('input[name=x]').value);
y = parseFloat(document.querySelector('input[name=y]').value);
novascale = parseFloat(document.querySelector('input[name=novascale]').value);
streakscale = parseFloat(document.querySelector('input[name=streakscale]').value);
seed = document.querySelector('input[name=randseed]').value;
drawFlare(x, y, novascale, streakscale, seed);
}
for (inputbox of document.getElementsByTagName("input")) {
inputbox.addEventListener("input", readFormData);
}
// drawFlare(200, 200, 1, 1, "hi");
readFormData();
/*
// animation just for fun
x_value = 0;
function animation() {
drawFlare(x_value, 100, 1, 1, "hi");
x_value++;
if (x_value <= 800) {
requestAnimationFrame(animation);
}
}
animation();
*/