kitao/pyxel · error
canvas dimensions are too large
Error message
canvas dimensions are too large
What it means
`Canvas::new` allocates a width*height buffer using checked arithmetic; if width*height overflows u32 (or the allocation model), `try_new` returns None and `new` panics with 'canvas dimensions are too large'. It is a guard against impossible/oversized allocations.
Source
Thrown at crates/pyxel-core/src/canvas.rs:34
pub trait ToIndex {
fn to_index(&self) -> usize;
}
#[derive(Clone)]
pub struct Canvas<T: Copy + PartialEq + Default + ToIndex> {
pub self_rect: RectArea,
pub clip_rect: RectArea,
pub camera_x: i32,
pub camera_y: i32,
pub alpha: f32,
pub data: Vec<T>,
}
impl<T: Copy + PartialEq + Default + ToIndex> Canvas<T> {
// Constructors
pub fn new(width: u32, height: u32) -> Self {
Self::try_new(width, height).expect("canvas dimensions are too large")
}
pub(crate) fn try_new(width: u32, height: u32) -> Option<Self> {
let len = width.checked_mul(height)? as usize;
Some(Self {
self_rect: RectArea::new(0, 0, width, height),
clip_rect: RectArea::new(0, 0, width, height),
camera_x: 0,
camera_y: 0,
alpha: 1.0,
data: vec![T::default(); len],
})
}
// Public accessors
pub const fn width(&self) -> u32 {
self.self_rect.width()View on GitHub (pinned to 50f9bd7778)
Solutions
- Reduce width/height so their product fits (use Canvas::try_new and handle None to fail gracefully).
- Validate dimensions against maximum allowed size before constructing.
- Check for logic errors that pass wrong units (bytes vs pixels) or untrusted values.
Example fix
// before
let canvas = Canvas::new(w, h);
// after
let canvas = Canvas::try_new(w, h).expect("requested canvas size too large"); Defensive patterns
Strategy: validation
Validate before calling
fn can_create_canvas(w: u32, h: u32) -> bool {
w.checked_mul(h).is_some()
} Try / catch
// panic cannot be caught; use the fallible constructor
match Canvas::try_new(w, h) {
Some(c) => c,
None => panic!("canvas {}x{} too large", w, h), // handle gracefully instead
} Prevention
- Prefer Canvas::try_new over Canvas::new in code paths with dynamic dimensions.
- Clamp width/height to known-good maxima before constructing.
- Sanitize dimensions derived from user input or external files.
When it happens
Trigger: Calling Canvas::new(width, height) where width.checked_mul(height) overflows (e.g. very large or zero-derived u32 values), or any caller path where try_new yields None.
Common situations: Computing canvas size from user input or resource files without bounds checking; multiplication overflow from malformed data; accidentally passing byte counts or bit-shifted values instead of pixel dimensions.
Related errors
- image dimensions are too large
- tilemap dimensions are too large
- Pyxel not initialized
- pyxel.flip is not supported on Pyxel Web
- Failed to open file '{path}'
AI-assisted analysis of kitao/pyxel@50f9bd7778 (2026-09-03).
Data as JSON: /api/errors/8044987ce9022ac0.
Report an issue: GitHub.