add draw_circle

This commit is contained in:
histausse 2022-01-15 17:47:40 +01:00
parent 0cf23255fb
commit cc6a0c4bce
Signed by: histausse
GPG key ID: 67486F107F62E9E9
2 changed files with 13 additions and 16 deletions

9
src/drawing.rs Normal file
View file

@ -0,0 +1,9 @@
use std::f64;
pub fn draw_circle(center: (f64, f64), radius: f64, context: &web_sys::CanvasRenderingContext2d) {
let (x, y) = center;
context.move_to(x+radius, y);
context
.arc(x, y, radius, 0.0, f64::consts::PI * 2.0)
.unwrap();
}

View file

@ -1,6 +1,6 @@
use std::f64;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
pub mod drawing;
#[wasm_bindgen(start)]
pub fn start() {
@ -20,21 +20,9 @@ pub fn start() {
context.begin_path();
// arc(x, y, radius, startAngle, endAngle, counterclockwise)
// Draws an arc which is centered at (x, y) position with radius r starting at startAngle and ending at endAngle going in the given direction indicated by counterclockwise (defaulting to clockwise).
context
.arc(75.0, 75.0, 50.0, 0.0, f64::consts::PI * 2.0)
.unwrap();
context.move_to(110.0, 75.0);
context
.arc(75.0, 75.0, 35.0, 0.0, f64::consts::PI)
.unwrap();
context.move_to(65.0, 65.0);
context
.arc(60.0, 65.0, 5.0, 0.0, f64::consts::PI * 2.0)
.unwrap();
drawing::draw_circle((75.0, 75.0), 70.0, &context);
drawing::draw_circle((75.0, 75.0), 35.0, &context);
drawing::draw_circle((60.0, 65.0), 5.0, &context);
context.stroke();
}