Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

Page: give Blitz its viewport in device pixels (a Retina window's interface sits in a quarter of it) (#120)

Luke, I am your fatherea741f6bef16
diff --git a/runtime/dom/src/host/elements.rs b/runtime/dom/src/host/elements.rs
index 3d32fef6..7560b8c0 100644
--- a/runtime/dom/src/host/elements.rs
+++ b/runtime/dom/src/host/elements.rs
@@ -60,8 +60,9 @@ pub fn elements(roots: &Roots, docs: &Docs, page: Entity, pick: impl Fn(&serde_j
use style::values::computed::Overflow;
let Some(r) = roots.0.iter().find(|r| r.page == page) else { return vec![] };
let Some(doc) = docs.0.get(&page) else { return vec![] };
- let vp = doc.viewport().window_size;
- let window = [0.0, 0.0, vp.0 as f32, vp.1 as f32];
+ // the window in CSS px, as the boxes below are: window_size is in device px (twice these on a Retina display)
+ let vp = doc.viewport().logical_size();
+ let window = [0.0, 0.0, vp.0, vp.1];
let bounds = |n: NodeId| doc.get_client_bounding_rect(n).map(|b| [b.x as f32, b.y as f32, b.width as f32, b.height as f32]);
let meets = |a: [f32; 4], b: [f32; 4]| a[0] < b[0] + b[2] && b[0] < a[0] + a[2] && a[1] < b[1] + b[3] && b[1] < a[1] + a[3];
let focus = doc.get_focussed_node_id();
diff --git a/systems/page/src/lib.rs b/systems/page/src/lib.rs
index ffd94602..5b2c19a5 100644
--- a/systems/page/src/lib.rs
+++ b/systems/page/src/lib.rs
@@ -53,6 +53,15 @@ pub struct Page {
pub scale: f32,
}
+impl Page {
+ /// The page's size in device pixels: its image's size, and the viewport Blitz is given (Blitz's `window_size` is
+ /// in device pixels: it divides by `hidpi_scale` for the CSS viewport). Rounded up, so the image is never short
+ /// of the page: at a fractional scale the CSS viewport can be a fraction of a CSS pixel wider than `size`.
+ pub fn px(&self) -> UVec2 {
+ (self.size.as_vec2() * self.scale).ceil().as_uvec2().max(UVec2::ONE)
+ }
+}
+
/// The time pages' CSS animations and transitions are at, in seconds: a render's frame time (`BlitzPage::at_frame`);
/// None, the App's clock (live play).
#[derive(Resource, Default)]
@@ -80,7 +89,8 @@ impl Docs {
/// A document's settings for a page: its viewport, styled on this thread (stylo's own pool would compete with Bevy's).
pub fn config(at: Page) -> DocumentConfig {
- let viewport = Viewport { window_size: (at.size.x, at.size.y), hidpi_scale: at.scale, ..default() };
+ let px = at.px();
+ let viewport = Viewport { window_size: (px.x, px.y), hidpi_scale: at.scale, ..default() };
let mut config = DocumentConfig { viewport: Some(viewport), style_threading: blitz_dom::StyleThreading::Sequential, ..default() };
// an <iframe> loads its document, parsed by blitz-html, through FrameNet (files under RAEA_PAGE_ROOT only;
// docs/blitz-bevy.feature B5c, off with RAEA_PAGE_IFRAMES=0)
@@ -95,7 +105,7 @@ pub fn config(at: Page) -> DocumentConfig {
/// offscreen target), so whatever draws or reads one page draws or reads the other.
fn size_images(mut commands: Commands, pages: Query<(Entity, &Page, Option<&PageImage>)>, mut images: ResMut<Assets<Image>>) {
for (e, page, have) in &pages {
- let px = (page.size.as_vec2() * page.scale).ceil().as_uvec2().max(UVec2::ONE);
+ let px = page.px();
if have.and_then(|h| images.get(&h.0)).is_some_and(|i| i.size() == px) {
continue;
}
@@ -152,9 +162,10 @@ pub fn resolve(mut docs: NonSendMut<Docs>, pages: Query<(Entity, &Page)>, time:
let Some(doc) = docs.0.get_mut(&e) else { continue };
let at = clock.0.unwrap_or_else(|| time.elapsed_secs_f64());
// a page resized or rescaled (the iPad sets its scale late): its viewport follows, or text lays out off its box
- if doc.viewport().window_size != (page.size.x, page.size.y) || doc.viewport().hidpi_scale != page.scale {
+ let px = page.px();
+ if doc.viewport().window_size != (px.x, px.y) || doc.viewport().hidpi_scale != page.scale {
let mut v = doc.viewport().clone();
- v.window_size = (page.size.x, page.size.y);
+ v.window_size = (px.x, px.y);
v.hidpi_scale = page.scale;
doc.set_viewport(v);
}
@@ -186,7 +197,7 @@ pub fn paint(mut docs: NonSendMut<Docs>, pages: Query<(Entity, &Page, &PageImage
let mut out = vec![];
for (e, page, image, straight) in &pages {
let Some(doc) = docs.0.get_mut(&e) else { continue };
- let px = (page.size.as_vec2() * page.scale).ceil().as_uvec2().max(UVec2::ONE);
+ let px = page.px();
// a scene the render world drew last frame, reset: its encoding keeps its capacity (ZeroHeap)
let mut scene = SPARE.lock().unwrap().pop().unwrap_or_default();
scene.reset();
diff --git a/systems/page/src/snapshot.rs b/systems/page/src/snapshot.rs
index 015aadce..23a55ca3 100644
--- a/systems/page/src/snapshot.rs
+++ b/systems/page/src/snapshot.rs
@@ -35,7 +35,9 @@ fn num(n: &mut String, v: f64) {
/// With `offsets`, `o` has each element's id, offsetLeft, offsetTop, offsetWidth, offsetHeight (untransformed, from
/// its offsetParent's padding edge) its offsetParent's id (OFFSET_BODY, OFFSET_NONE), and 1 when it's a non-atomic inline (its client* and scroll* are 0).
pub fn snapshot_by(doc: &BaseDocument, nodes: &Nodes, id: impl Fn(u64) -> Option<u64>, offsets: bool) -> String {
- let vp = doc.viewport().window_size;
+ // the viewport in CSS px and the device pixels to one of them: what innerWidth, matchMedia and devicePixelRatio
+ // answer. window_size is in device px, so on a Retina display it is twice the width the page is laid out at.
+ let (vp, dpr) = (doc.viewport().logical_size(), doc.viewport().scale());
let mut n = String::new();
let mut o = String::new();
for (page_id, node) in &nodes.by_id {
@@ -83,9 +85,9 @@ pub fn snapshot_by(doc: &BaseDocument, nodes: &Nodes, id: impl Fn(u64) -> Option
n.pop();
o.pop();
if offsets {
- format!("{{\"vw\":{},\"vh\":{},\"dpr\":1,\"n\":[{n}],\"o\":[{o}]}}", vp.0, vp.1)
+ format!("{{\"vw\":{},\"vh\":{},\"dpr\":{dpr},\"n\":[{n}],\"o\":[{o}]}}", vp.0, vp.1)
} else {
- format!("{{\"vw\":{},\"vh\":{},\"dpr\":1,\"n\":[{n}]}}", vp.0, vp.1)
+ format!("{{\"vw\":{},\"vh\":{},\"dpr\":{dpr},\"n\":[{n}]}}", vp.0, vp.1)
}
}
diff --git a/systems/page/tests/common/mod.rs b/systems/page/tests/common/mod.rs
new file mode 100644
index 00000000..2ac2fea5
--- /dev/null
+++ b/systems/page/tests/common/mod.rs
@@ -0,0 +1,12 @@
+//! What the page's tests build documents with.
+use blitz_dom::{LocalName, QualName, ns};
+
+/// An HTML element's name.
+pub fn q(local: &str) -> QualName {
+ QualName { prefix: None, ns: ns!(html), local: LocalName::from(local) }
+}
+
+/// An attribute, as `name="v"`.
+pub fn attr(local: &str, v: &str) -> blitz_dom::Attribute {
+ blitz_dom::Attribute { name: QualName { prefix: None, ns: ns!(), local: LocalName::from(local) }, value: v.into() }
+}
diff --git a/systems/page/tests/vars.rs b/systems/page/tests/vars.rs
index 43c2acb9..438f8d9c 100644
--- a/systems/page/tests/vars.rs
+++ b/systems/page/tests/vars.rs
@@ -1,13 +1,7 @@
//! Custom properties set in an element's style attribute reach its children's var() (p12-css/var-inherit).
+mod common;
use blitz_dom::{BaseDocument, DocumentConfig, LocalName, QualName, ns};
-
-fn q(local: &str) -> QualName {
- QualName { prefix: None, ns: ns!(html), local: LocalName::from(local) }
-}
-
-fn attr(local: &str, v: &str) -> blitz_dom::Attribute {
- blitz_dom::Attribute { name: QualName { prefix: None, ns: ns!(), local: LocalName::from(local) }, value: v.into() }
-}
+use common::{attr, q};
#[test]
fn inline_custom_properties_inherit() {
diff --git a/systems/page/tests/viewport.rs b/systems/page/tests/viewport.rs
new file mode 100644
index 00000000..afcc313a
--- /dev/null
+++ b/systems/page/tests/viewport.rs
@@ -0,0 +1,51 @@
+//! A page painted at a scale other than 1 (raea's window on a Retina display) is laid out at its size in CSS px:
+//! Blitz is given the viewport in device px, which it divides by the scale. Given the size in CSS px, a page at
+//! scale 2 was laid out in a quarter of its image (2026-11-12, issue 119).
+use bevy::math::UVec2;
+mod common;
+use blitz_dom::BaseDocument;
+use common::{attr, q};
+use raea_sys_page::{Page, config};
+
+/// The box of a `width: 100vw; height: 100vh` element in a page: the CSS viewport as layout sees it.
+fn full(at: Page) -> (f64, f64) {
+ let mut doc = BaseDocument::new(config(at));
+ let div = {
+ let mut m = doc.mutate();
+ let root = m.doc.root_node().id;
+ let html = m.create_element(q("html"), vec![]);
+ m.append_children(root, &[html]);
+ let body = m.create_element(q("body"), vec![attr("style", "margin:0")]);
+ m.append_children(html, &[body]);
+ let div = m.create_element(q("div"), vec![attr("style", "width:100vw;height:100vh")]);
+ m.append_children(body, &[div]);
+ div
+ };
+ doc.resolve(0.0);
+ let b = doc.get_client_bounding_rect(div).expect("the element has a box");
+ (b.width, b.height)
+}
+
+#[test]
+fn a_page_at_scale_2_is_laid_out_at_its_size_in_css_px() {
+ let at = Page { size: UVec2::new(400, 300), scale: 2.0 };
+ assert_eq!(at.px(), UVec2::new(800, 600));
+ let viewport = config(at).viewport.expect("a page's config has a viewport");
+ assert_eq!(viewport.window_size, (800, 600));
+ assert_eq!(viewport.logical_size(), (400.0, 300.0));
+ assert_eq!(full(at), (400.0, 300.0));
+}
+
+#[test]
+fn a_page_at_scale_1_is_as_it_was() {
+ let at = Page { size: UVec2::new(400, 300), scale: 1.0 };
+ assert_eq!(at.px(), UVec2::new(400, 300));
+ assert_eq!(config(at).viewport.expect("a page's config has a viewport").window_size, (400, 300));
+ assert_eq!(full(at), (400.0, 300.0));
+}
+
+#[test]
+fn at_a_fractional_scale_the_image_is_rounded_up() {
+ assert_eq!(Page { size: UVec2::new(1001, 601), scale: 1.25 }.px(), UVec2::new(1252, 752));
+ assert_eq!(Page { size: UVec2::new(0, 0), scale: 2.0 }.px(), UVec2::ONE);
+}