mirror of
https://github.com/Astatin3/IntroToWebAuthoring.git
synced 2026-06-09 00:28:00 -06:00
Start work on views system
This commit is contained in:
+50
-6
@@ -1,10 +1,7 @@
|
||||
use wasm_bindgen::{Clamped, prelude::*};
|
||||
use web_sys::ImageData;
|
||||
|
||||
use crate::{
|
||||
log,
|
||||
render::{buffer::ImgBuffer, calc_resolution, rand::Rnd},
|
||||
};
|
||||
use crate::render::{RESOLUTION, buffer::ImgBuffer, calc_resolution, rand::Rnd};
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct Renderer {
|
||||
@@ -72,6 +69,10 @@ impl Renderer {
|
||||
.unwrap();
|
||||
self.ctx.put_image_data(&data, 0., 0.).unwrap();
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.img.data = vec![0; (self.img.width() * self.img.height() * 4) as usize];
|
||||
}
|
||||
}
|
||||
|
||||
impl Renderer {
|
||||
@@ -82,6 +83,47 @@ impl Renderer {
|
||||
)
|
||||
}
|
||||
|
||||
/// Draw a rectangle centered at (cx, cy) with the given width and height.
|
||||
pub fn rect_center_xywh(
|
||||
&mut self,
|
||||
cx: i32,
|
||||
cy: i32,
|
||||
width: i32,
|
||||
height: i32,
|
||||
color: (u8, u8, u8),
|
||||
) {
|
||||
self.rect_xyxy(
|
||||
cx - width / 2,
|
||||
cy - height / 2,
|
||||
cx + width / 2,
|
||||
cy + height / 2,
|
||||
color,
|
||||
);
|
||||
}
|
||||
|
||||
/// Draw a rectangle at (x, y) with the given width and height.
|
||||
pub fn rect_xywh(&mut self, x: i32, y: i32, width: i32, height: i32, color: (u8, u8, u8)) {
|
||||
self.rect_xyxy(x, y, x + width, y + height, color);
|
||||
}
|
||||
|
||||
/// Draw a rectangle at (x1, y1) with the given width and height.
|
||||
pub fn rect_xyxy(&mut self, x1: i32, y1: i32, x2: i32, y2: i32, color: (u8, u8, u8)) {
|
||||
let (leftx, topy) = self.undistort(x1 as f32, y1 as f32);
|
||||
let (rightx, bottomy) = self.undistort(x2 as f32, y2 as f32);
|
||||
|
||||
let leftx = leftx.max(0f32);
|
||||
let topy = topy.max(0f32);
|
||||
let rightx = rightx.min(self.canvas_width as f32);
|
||||
let bottomy = bottomy.min(self.canvas_height as f32);
|
||||
|
||||
for x in leftx as i32..rightx as i32 {
|
||||
for y in topy as i32..bottomy as i32 {
|
||||
self.img.set_pixel(x as u32, y as u32, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the square distance between two points on the screen, and convert to canvas coordinates for processing
|
||||
pub fn screen_dist_sq(&self, x: f32, y: f32, rx: f32, ry: f32) -> f32 {
|
||||
(rx - x).powf(2.) * self.distortion_y + (ry - y).powf(2.) * self.distortion_x
|
||||
}
|
||||
@@ -99,8 +141,10 @@ impl Renderer {
|
||||
let rightx = rightx.min(self.canvas_width as f32);
|
||||
let bottomy = bottomy.min(self.canvas_height as f32);
|
||||
|
||||
let e = (self.canvas_height as f32 / self.actual_height as f32)
|
||||
* (self.canvas_width as f32 / self.actual_width as f32);
|
||||
// let e = (self.canvas_height as f32 / self.actual_height as f32)
|
||||
// * (self.canvas_width as f32 / self.actual_width as f32);
|
||||
|
||||
let e = RESOLUTION.pow(2) as f32 / (self.actual_width * self.actual_height) as f32;
|
||||
|
||||
let r2 = (radius).powf(2.) * e;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user