wasmblock/
canvas.rs

1use std;
2use std::os::raw::{c_char};
3use std::ffi::{CString};
4
5pub fn export_string<T:Into<std::vec::Vec<u8>>>(s:T) -> *const c_char{
6    let s = CString::new(s).unwrap();
7    let p = s.as_ptr();
8    std::mem::forget(s);
9    return p;
10}
11
12extern {
13    fn wasmblock_canvas_get_context(target: *const c_char) -> u32;
14    fn wasmblock_canvas_set_fill_style(ctx: u32, style: *const c_char);
15    fn wasmblock_canvas_set_fill_style_color(ctx: u32, r: u8, g: u8, b: u8, a: f32);
16    fn wasmblock_canvas_fill_rect(ctx: u32, x:f32,y:f32,width:f32,height:f32);
17    fn wasmblock_canvas_put_image_data(ctx: u32, pixelPtr:  *const u8, pixelLength: u32, x:i32,y:i32,width:i32,height:i32);
18}
19
20#[inline]
21pub fn get_context(target:&str) -> u32 {
22    unsafe {
23        return wasmblock_canvas_get_context(export_string(target));
24    }
25}
26
27#[inline]
28pub fn set_fill_style(ctx: u32, style:&str){
29    unsafe {
30        wasmblock_canvas_set_fill_style(ctx, export_string(style));
31    }
32}
33
34#[inline]
35pub fn set_fill_style_color(ctx: u32, r:u8, g:u8, b:u8, a:f32){
36    unsafe {
37        wasmblock_canvas_set_fill_style_color(ctx, r, g, b, a);
38    }
39}
40
41#[inline]
42pub fn fill_rect(ctx: u32, x:f32, y:f32, width:f32, height:f32){
43    unsafe {
44        wasmblock_canvas_fill_rect(ctx, x, y, width, height);
45    }
46}
47
48#[inline]
49pub fn put_image_data(ctx: u32, pixel: &Vec<u8>, x:i32,y:i32,width:i32,height:i32) {
50    unsafe {
51        let pix = pixel.clone();
52        let l = pix.len();
53        let p = pix.as_ptr();
54        std::mem::forget(pix);
55        wasmblock_canvas_put_image_data(ctx, p as *const u8, l as u32, x,y,width,height);
56    }
57}