Repository: penpot/penpot
File: render-wasm/src/render.rs (Rust)

Code before:
                SurfaceId::Strokes,
                SurfaceId::DropShadows,
                SurfaceId::InnerShadows,
            ],
            |s| {
                s.canvas().scale((scale, scale));
            },
        );

        let (sx, sy, ex, ey) = tiles::get_tiles_for_viewbox(self.viewbox);
        debug::render_debug_tiles_for_viewbox(self, sx, sy, ex, ey);
        /*
        // TODO: Instead of rendering only the visible area
        // we could apply an offset to the viewbox to render
        // more tiles.
        sx - interest_delta
        sy - interest_delta
        ex + interest_delta
        ey + interest_delta
        */
        let tile_center = ((ex - sx) / 2, (ey - sy) / 2);
        self.pending_tiles = vec![];
        self.surfaces.cache_clear_visited();
        for y in sy..=ey {
            for x in sx..=ex {
                let tile = (x, y);
                let distance = tiles::manhattan_distance(tile, tile_center);
                self.pending_tiles.push((x, y, distance));
                self.surfaces.cache_visit(tile);
            }
        }
        // reorder by distance to the center.
        self.pending_tiles.sort_by(|a, b| b.2.cmp(&a.2));
        self.current_tile = None;
        self.render_in_progress = true;
        self.apply_drawing_to_render_canvas(None);
        self.process_animation_frame(tree, modifiers, timestamp)?;
        Ok(())
    }

    pub fn request_animation_frame(&mut self) -> i32 {
        run_script_int!("requestAnimationFrame(_process_animation_frame)")
    }

    pub fn cancel_animation_frame(&mut self, frame_id: i32) {
        run_script!(format!("cancelAnimationFrame({})", frame_id))
    }

    pub fn process_animation_frame(
        &mut self,
        tree: &mut HashMap<Uuid, Shape>,

Review comment (suggestion):
I guess we want to sort in ascending order, to process closer tiles first, right?

```suggestion
        self.pending_tiles.sort_by(|a, b| a.2.cmp(&b.2));
```

Code after:

        let (sx, sy, ex, ey) = tiles::get_tiles_for_viewbox(self.viewbox);
        debug::render_debug_tiles_for_viewbox(self, sx, sy, ex, ey);
        /*
        // TODO: Instead of rendering only the visible area
        // we could apply an offset to the viewbox to render
        // more tiles.
        sx - interest_delta
        sy - interest_delta
        ex + interest_delta
        ey + interest_delta
        */
        let tile_center = ((ex - sx) / 2, (ey - sy) / 2);
        self.pending_tiles = vec![];
        self.surfaces.cache_clear_visited();
        for y in sy..=ey {
            for x in sx..=ex {
                let tile = (x, y);
                let distance = tiles::manhattan_distance(tile, tile_center);
                self.pending_tiles.push((x, y, distance));
                self.surfaces.cache_visit(tile);
            }
        }
        self.pending_nodes = vec![];
        // reorder by distance to the center.
        self.pending_tiles.sort_by(|a, b| b.2.cmp(&a.2));
        self.current_tile = None;
        self.render_in_progress = true;
        self.apply_drawing_to_render_canvas(None);
        self.process_animation_frame(tree, modifiers, timestamp)?;
        Ok(())
    }

    pub fn request_animation_frame(&mut self) -> i32 {
        run_script_int!("requestAnimationFrame(_process_animation_frame)")
    }

    pub fn cancel_animation_frame(&mut self, frame_id: i32) {
        run_script!(format!("cancelAnimationFrame({})", frame_id))
    }

    pub fn process_animation_frame(
        &mut self,
        tree: &mut HashMap<Uuid, Shape>,
        modifiers: &HashMap<Uuid, Matrix>,
        timestamp: i32,
    ) -> Result<(), String> {
        if self.render_in_progress {
            self.render_shape_tree(tree, modifiers, timestamp)?;
            self.flush();