Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created October 14, 2019 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/968b9521581ab4e17fb368d570138ceb to your computer and use it in GitHub Desktop.
Save rust-play/968b9521581ab4e17fb368d570138ceb to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#[wasm_bindgen]
pub extern fn handle_mouse_down(img_width: u32, img_height: u32, canvas_width: u32, canvas_height: u32, x: u32, y: u32, img_vec: Vec<u8>)
-> Vec<u8>
{
utils::set_panic_hook();
log!("INSIDE HANDLE_MOUSE_DOWN");
log!("inside handle_mouse_down and x: {:?}, and y: {:?}", x, y);
log!("1");
let mutex_var = &ID_TO_DATA;
log!("2");
let mut guard_var = mutex_var.lock().unwrap();
log!("3");
let mut pic = Picture::new(canvas_width, canvas_height, vec![0; 4 * canvas_width as usize * canvas_height as usize]);
let img_vec_guard: Vec<u8> = img_vec.clone();
let mut prev_storage: Vec<u8>;
let mut return_buffer = vec![0; 4 * canvas_width as usize * canvas_height as usize];
if guard_var.len() == 0 {
let z: u32 = x + y;
guard_var.insert(0, Data{
canvas_vec: vec![0; 4 * canvas_width as usize * canvas_height as usize],
img_vec: img_vec_guard
});
}else{
let (key, value) = guard_var.iter().last().unwrap();
prev_storage = value.canvas_vec.clone()
.iter()
.map(|n| u8::from(*n))
.collect();
pic = Picture::new(canvas_width, canvas_height, prev_storage);
pic.add_img_vec(img_width, img_height, canvas_width, canvas_height, x, y, img_vec);
return_buffer = pic.clone().buffer.into_raw();
guard_var.insert(0, Data{
canvas_vec: pic.buffer.into_raw(),
img_vec: img_vec_guard
});
}
std::mem::drop(guard_var);
log!("guard_var has been dropped~");
return return_buffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment