MCPcopy Create free account
hub / github.com/endbasic/endbasic / new

Method new

web/src/canvas.rs:103–145  ·  view source on GitHub ↗

Creates a new canvas console backed by the `canvas` HTML element and that receives input events from `input`.

(
        canvas: HtmlCanvasElement,
        font: &'static Font,
        yielder: WebYielder,
    )

Source from the content-addressed store, hash-verified

101 /// Creates a new canvas console backed by the `canvas` HTML element and that receives input
102 /// events from `input`.
103 pub(crate) fn new(
104 canvas: HtmlCanvasElement,
105 font: &'static Font,
106 yielder: WebYielder,
107 ) -> io::Result<Self> {
108 let size_pixels = {
109 let width = match u16::try_from(canvas.width()) {
110 Ok(v) => v,
111 Err(_) => {
112 return Err(io::Error::new(
113 io::ErrorKind::InvalidInput,
114 format!("Canvas is too wide at {} pixels", canvas.width()),
115 ));
116 }
117 };
118 let height = match u16::try_from(canvas.height()) {
119 Ok(v) => v,
120 Err(_) => {
121 return Err(io::Error::new(
122 io::ErrorKind::InvalidInput,
123 format!("Canvas is too tall at {} pixels", canvas.height()),
124 ));
125 }
126 };
127 SizeInPixels::new(width, height)
128 };
129
130 let context = html_canvas_to_2d_context(canvas)?;
131 context.set_image_smoothing_enabled(false);
132 context.set_text_baseline("middle");
133
134 // The actual values are irrelevant but need to be different than the initial values we use
135 // below.
136 let fill_color = (10, 10, 10);
137 let stroke_color = (100, 100, 100);
138
139 let mut raster_ops = Self { context, font, yielder, size_pixels, fill_color, stroke_color };
140
141 raster_ops.set_fill_style_rgb((0, 0, 0));
142 raster_ops.set_stroke_style_rgb((255, 255, 255));
143
144 Ok(raster_ops)
145 }
146
147 /// Returns true if the point falls within the canvas bounds.
148 fn contains_pixel(&self, xy: PixelsXY) -> bool {

Callers

nothing calls this directly

Calls 3

set_fill_style_rgbMethod · 0.80
set_stroke_style_rgbMethod · 0.80

Tested by

no test coverage detected