Ray-caster: switch from draw effect to animation loop

This wastes a lot of CPU time, as explained on lines 253--258 of
`main.rs`, but it's better than the previous version, which could block
graphics updates system-wide for seconds on end.
This commit is contained in:
Aaron Fenyes 2024-08-26 16:06:37 -07:00
parent bf140efaf7
commit 5e9c5db231

View File

@ -10,7 +10,7 @@
extern crate js_sys;
use core::array;
use nalgebra::DVector;
use sycamore::{prelude::*, rt::{JsCast, JsValue}};
use sycamore::{prelude::*, motion::create_raf, rt::{JsCast, JsValue}};
use web_sys::{console, WebGl2RenderingContext, WebGlProgram, WebGlShader, WebGlUniformLocation};
mod engine;
@ -207,7 +207,7 @@ fn main() {
bind_vertex_attrib(&ctx, position_index, 3, &positions);
// set up a repainting routine
create_effect(move || {
let (_, start_updating_display, _) = create_raf(move || {
// update the construction
sphere_vec.clear();
sphere_vec.push(engine::sphere(0.5, 0.5, -5.0 + ctrl_x.get(), radius_x.get()));
@ -249,6 +249,14 @@ fn main() {
// draw the scene
ctx.draw_arrays(WebGl2RenderingContext::TRIANGLES, 0, VERTEX_CNT as i32);
});
/*
this wastes CPU time by running an animation loop, which updates the
display even when nothing has changed. there should be a way to make
Sycamore do single-frame updates in response to changes, but i
haven't found it yet
*/
start_updating_display();
});
view! {