dyna3/engine-proto/ganja-test/ganja-test.jl

127 lines
2.7 KiB
Julia
Raw Normal View History

using Blink
2024-06-26 00:57:16 +00:00
using Colors
2024-06-25 23:30:19 +00:00
# === utilities ===
2024-06-26 07:41:21 +00:00
append_to_head!(w, type, content) = @js w begin
2024-06-25 23:30:19 +00:00
@var element = document.createElement($type)
element.appendChild(document.createTextNode($content))
document.head.appendChild(element)
end
2024-06-25 23:30:19 +00:00
style!(w, stylesheet) = append_to_head!(w, "style", stylesheet)
2024-06-25 23:30:19 +00:00
script!(w, code) = append_to_head!(w, "script", code)
function add_element!(vec)
2024-06-26 00:57:16 +00:00
# add element
2024-06-25 23:30:19 +00:00
full_vec = [0; vec; fill(0, 26)]
2024-06-26 00:57:16 +00:00
n = @js win elements.push(@new CGA3($full_vec))
# generate palette. this is Gadfly's `default_discrete_colors` palette,
# available under the MIT license
palette = distinguishable_colors(
n,
[LCHab(70, 60, 240)],
transform = c -> deuteranopic(c, 0.5),
lchoices = Float64[65, 70, 75, 80],
cchoices = Float64[0, 50, 60, 70],
hchoices = range(0, stop=330, length=24)
)
palette_packed = [RGB24(c).color for c in palette]
@js win palette = $palette_packed
2024-06-25 23:30:19 +00:00
end
# === build page ===
# create window and open developer console
win = Window()
opentools(win)
# set stylesheet
style!(win, """
2024-06-26 06:31:00 +00:00
body {
background-color: #ffe0f0;
}
2024-06-26 06:31:00 +00:00
/* needed to keep Ganja canvas from blowing up */
canvas {
min-width: 600px;
max-width: 600px;
min-height: 600px;
max-height: 600px;
}
2024-06-25 23:30:19 +00:00
""")
# load Ganja.js
loadjs!(win, "https://unpkg.com/ganja.js")
# create global functions and variables
script!(win, """
// create algebra
var CGA3 = Algebra(4, 1);
2024-06-26 00:57:16 +00:00
// initialize element list and palette
2024-06-25 23:30:19 +00:00
var elements = [];
2024-06-26 00:57:16 +00:00
var palette = [];
2024-06-25 23:30:19 +00:00
// declare visualization handle
var graph;
2024-06-26 00:57:16 +00:00
// create scene function
function scene() {
commands = [];
for (let n = 0; n < elements.length; ++n) {
commands.push(palette[n], elements[n]);
}
return commands;
}
2024-06-25 23:30:19 +00:00
function flip() {
2024-06-26 00:57:16 +00:00
let last = elements.length - 1;
for (let n = 0; n < last; ++n) {
2024-06-25 23:30:19 +00:00
// reflect
2024-06-26 00:57:16 +00:00
elements[n] = CGA3.Mul(CGA3.Mul(elements[last], elements[n]), elements[last]);
2024-06-25 23:30:19 +00:00
// de-noise
for (let k = 6; k < elements[n].length; ++k) {
elements[n][k] = 0;
}
}
2024-06-25 23:30:19 +00:00
requestAnimationFrame(graph.update.bind(graph, scene));
}
2024-06-25 23:30:19 +00:00
""")
2024-06-25 23:30:19 +00:00
# set up controls
body!(win, """
<p><button id="flip-button" onclick="flip()">Flip</button></p>
2024-06-26 00:57:16 +00:00
""", async = false)
2024-06-25 23:30:19 +00:00
# === set up visualization ===
2024-06-25 23:30:19 +00:00
# list elements. in the default view, e4 + e5 is the point at infinity
elements = sqrt(0.5) * BigFloat[
1 1 -1 -1 0;
1 -1 1 -1 0;
1 -1 -1 1 0;
0 0 0 0 -sqrt(6);
1 1 1 1 2
]
2024-06-25 23:30:19 +00:00
# load elements
for vec in eachcol(elements)
add_element!(vec)
end
2024-06-25 23:30:19 +00:00
# initialize visualization
@js win begin
graph = CGA3.graph(
scene,
Dict(
"conformal" => true,
"gl" => true,
"grid" => true
)
)
2024-06-26 00:57:16 +00:00
document.body.appendChild(graph)
2024-06-25 23:30:19 +00:00
end