big_m/
big-m.rs

1use js_canvas_rendering_context_2d::*;
2
3fn main(){
4// Big M
5  draw();
6}
7
8#[no_mangle]
9pub extern "C" fn draw(){
10   let (x_offset, y_offset) = get_offsets();
11   let coeff = get_coeff();
12
13
14   local_move_to(5.,95.,x_offset,y_offset,coeff);
15   local_line_to(10.,15.,x_offset,y_offset,coeff);
16   local_line_to(50.,70.,x_offset,y_offset,coeff);
17   local_line_to(90.,15.,x_offset,y_offset,coeff);
18   local_line_to(95.,95.,x_offset,y_offset,coeff);
19
20   CanvasRenderingContext2D::set_line_width(15 * coeff.floor() as u32);
21   CanvasRenderingContext2D::stroke();
22}
23
24fn local_move_to(x:f32, y:f32,x_offset:f32, y_offset: f32, coeff: f32) {
25   let local_x = apply_transformation(x, coeff, x_offset);
26   let local_y = apply_transformation(y, coeff, y_offset);
27
28   CanvasRenderingContext2D::move_to(local_x, local_y);
29}
30
31fn local_line_to(x:f32, y:f32,x_offset:f32, y_offset: f32, coeff: f32) {
32   let local_x = apply_transformation(x, coeff, x_offset);
33   let local_y = apply_transformation(y, coeff, y_offset);
34
35   CanvasRenderingContext2D::line_to(local_x, local_y);
36}
37
38fn apply_transformation(source: f32, coeff: f32, offset: f32) -> f32 {
39   offset + (coeff * source)
40}
41
42fn get_offsets() -> (f32,f32){
43   let canvas_w = CanvasRenderingContext2D::get_canvas_width();
44   let canvas_h = CanvasRenderingContext2D::get_canvas_height();
45
46   let square_side = canvas_w.min(canvas_h);
47   let offset_x = (canvas_w - square_side) / 2.;
48   let offset_y = (canvas_h - square_side) / 2.;
49   (offset_x,offset_y)
50}
51
52fn get_coeff() -> f32{
53   let canvas_w = CanvasRenderingContext2D::get_canvas_width();
54   let canvas_h = CanvasRenderingContext2D::get_canvas_height();
55
56   let square_side = canvas_w.min(canvas_h);
57
58   square_side / 100.0
59}