js_canvas_rendering_context_2d/lib.rs
1extern "C" {
2 // property getters/setters
3 fn js_get_canvas_height()->f32;
4 fn js_get_canvas_width()->f32;
5 fn js_set_fill_style_rgba(r: u8, g: u8, b: u8, a: u8);
6 fn js_get_line_cap() -> u32;
7 fn js_set_line_cap(value: u32);
8 fn js_get_line_join() -> u32;
9 fn js_set_line_join(value: u32);
10 fn js_get_line_width() -> u32;
11 fn js_set_line_width(value: u32);
12 fn js_get_shadow_blur() ->u32;
13 fn js_set_shadow_blur(value: u32);
14 fn js_set_shadow_color_rgba(r: u8, g: u8, b: u8, a: u8);
15 fn js_set_stroke_style_rgba(r: u8, g: u8, b: u8, a: u8);
16 fn js_get_shadow_offset_x() ->i32;
17 fn js_set_shadow_offset_x(value: i32);
18 fn js_get_shadow_offset_y() ->i32;
19 fn js_set_shadow_offset_y(value: i32);
20 // functions
21 fn js_arc(
22 x: f32,
23 y: f32,
24 radius: f32,
25 start_angle: f32,
26 end_angle: f32,
27 counter_clockwise: bool,
28 );
29 fn js_arc_to(x1: f32, y1: f32, x2: f32, y2: f32, radius: f32);
30 fn js_begin_path();
31 fn js_clear_rect(x: f32, y: f32, width: f32, height: f32);
32 fn js_close_path();
33 fn js_fill();
34 fn js_fill_rect(x: f32, y: f32, width: f32, height: f32);
35 fn js_line_to(x: f32, y: f32);
36 fn js_move_to(x: f32, y: f32);
37 fn js_stroke();
38 fn js_stroke_rect(x: f32, y: f32, width: f32, height: f32);
39}
40
41pub struct CanvasRenderingContext2D {}
42
43impl CanvasRenderingContext2D {
44
45
46 pub fn get_canvas_height()->f32{
47 unsafe {
48 return js_get_canvas_height();
49 }
50 }
51 pub fn get_canvas_width()->f32{
52 unsafe {
53 return js_get_canvas_width();
54 }
55 }
56
57 /// setter for property that specifies the color to use for the fills (outlines) around shapes. The default is black.
58 /// see [CanvasRenderingContext2D.fillStyle](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillStyle)
59 ///
60 /// # Parameters
61 ///
62 /// * 'r' - value from 0 to 255 that represents how much red is in the color
63 /// * 'g' - value from 0 to 255 that represents how much green is in the color
64 /// * 'b' - value from 0 to 255 that represents how much blue is in the color
65 /// * 'a' - value from 0 to 255 that represents how opaque the color is
66 pub fn set_fill_style_rgba(r: u8, g: u8, b: u8, a: u8) {
67 unsafe {
68 js_set_fill_style_rgba(r, g, b, a);
69 }
70 }
71
72 /// getter for property that determines the shape used to join two line segments where they meet.
73 /// see [CanvasRenderingContext2D.lineJap](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJap)
74 pub fn get_line_join() -> LineJoin {
75 unsafe {
76 let raw_join_val = js_get_line_join();
77 return match raw_join_val {
78 1 => LineJoin::Bevel,
79 2 => LineJoin::Round,
80 3 => LineJoin::Miter,
81 _ => LineJoin::Bevel,
82 };
83 }
84 }
85
86 /// setter for property that determines the shape used to join two line segments where they meet.
87 /// see [CanvasRenderingContext2D.lineJoin](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJoin)
88 ///
89 /// # Parameters
90 ///
91 /// * 'value' - the type of line join. Valid options are Bevel, Round, Miter
92 pub fn set_line_join(value: LineJoin) {
93 let join_val = match value {
94 LineJoin::Bevel => 1,
95 LineJoin::Round => 2,
96 LineJoin::Miter => 3,
97 };
98
99 unsafe {
100 js_set_line_join(join_val);
101 }
102 }
103 /// getter for property that determines the shape used to draw the end points of lines.
104 /// see [CanvasRenderingContext2D.lineCap](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineCap)
105 pub fn get_line_cap() -> LineCap {
106 unsafe {
107 let raw_cap_val = js_get_line_cap();
108 return match raw_cap_val {
109 1 => LineCap::Butt,
110 2 => LineCap::Round,
111 3 => LineCap::Square,
112 _ => LineCap::Butt,
113 };
114 }
115 }
116
117 /// setter for property that determines the shape used to draw the end points of lines.
118 /// see [CanvasRenderingContext2D.lineCap](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineCap)
119 ///
120 /// # Parameters
121 ///
122 /// * 'value' - the type of line cap. Valid options are Butt, Round, Square
123 pub fn set_line_cap(value: LineCap) {
124 let cap_val = match value {
125 LineCap::Butt => 1,
126 LineCap::Round => 2,
127 LineCap::Square => 3,
128 };
129
130 unsafe {
131 js_set_line_cap(cap_val);
132 }
133 }
134
135 /// getter for property that sets the thickness of lines.
136 /// see [CanvasRenderingContext2D.lineWidth](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth)
137 pub fn get_line_width() -> u32 {
138 unsafe {
139 return js_get_line_width();
140 }
141 }
142
143 /// setter for property that sets the thickness of lines.
144 /// see [CanvasRenderingContext2D.lineWidth](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth)
145 ///
146 /// # Parameters
147 ///
148 /// * 'value' - the thickness of lines. Must be a non-zero positive number
149 pub fn set_line_width(value: u32) {
150 if value == 0 {
151 panic!("value must not be 0");
152 }
153 unsafe {
154 js_set_line_width(value);
155 }
156 }
157
158 /// getter for property that specifies the amount of blur applied to shadows. The default is 0 (no blur).
159 /// see [CanvasRenderingContext2D.shadowBlur](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowBlur)
160 pub fn get_shadow_blur() -> u32 {
161 unsafe {
162 return js_get_shadow_blur();
163 }
164 }
165
166
167 /// setter for property that specifies the amount of blur applied to shadows. The default is 0 (no blur).
168 /// see [CanvasRenderingContext2D.shadowBlur](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowBlur)
169 ///
170 /// # Parameters
171 ///
172 /// * 'value' - the the level of shadow blur. A zero value indicates no blur.
173 pub fn set_shadow_blur(value: u32) {
174 unsafe {
175 js_set_shadow_blur(value);
176 }
177 }
178
179 /// setter for property that specifies the color of shadows. The default is fully transparent black.
180 /// see [CanvasRenderingContext2D.shadowColor](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowColor)
181 ///
182 /// # Parameters
183 ///
184 /// * 'r' - value from 0 to 255 that represents how much red is in the color
185 /// * 'g' - value from 0 to 255 that represents how much green is in the color
186 /// * 'b' - value from 0 to 255 that represents how much blue is in the color
187 /// * 'a' - value from 0 to 255 that represents how opaque the color is
188 pub fn set_shadow_color_rgba(r: u8, g: u8, b: u8, a: u8) {
189 unsafe {
190 js_set_shadow_color_rgba(r, g, b, a);
191 }
192 }
193
194 /// getter for property that specifies the distance that shadows will be offset horizontally. The default is 0 (no horizontal offset).
195 /// see [CanvasRenderingContext2D.shadowOffsetX](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowOffsetX)
196 pub fn get_shadow_offset_x() -> i32 {
197 unsafe {
198 return js_get_shadow_offset_x();
199 }
200 }
201
202
203 /// setter for property that specifies the distance that shadows will be offset horizontally. The default is 0 (no horizontal offset).
204 /// see [CanvasRenderingContext2D.shadowOffsetX](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowOffsetX)
205 ///
206 /// # Parameters
207 ///
208 /// * 'value' - the the distance of horizontal offset. Positive values are to the right, and negative to the left.
209 pub fn set_shadow_offset_x(value: i32) {
210 unsafe {
211 js_set_shadow_offset_x(value);
212 }
213 }
214
215 /// getter for property that specifies the distance that shadows will be offset vertically. The default is 0 (no vertical offset).
216 /// see [CanvasRenderingContext2D.shadowOffsetY](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowOffsetY)
217 pub fn get_shadow_offset_y() -> i32 {
218 unsafe {
219 return js_get_shadow_offset_y();
220 }
221 }
222
223
224 /// setter for property that specifies the distance that shadows will be offset vertically. The default is 0 (no vertical offset).
225 /// see [CanvasRenderingContext2D.shadowOffsetY](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/shadowOffsetY)
226 ///
227 /// # Parameters
228 ///
229 /// * 'value' - the the distance of vertical offset. Positive values are down, and negative are up.
230 pub fn set_shadow_offset_y(value: i32) {
231 unsafe {
232 js_set_shadow_offset_y(value);
233 }
234 }
235
236 /// setter for property that specifies the color to use for the strokes (outlines) around shapes. The default is black.
237 /// see [CanvasRenderingContext2D.strokeStyle](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/strokeStyle)
238 ///
239 /// # Parameters
240 ///
241 /// * 'r' - value from 0 to 255 that represents how much red is in the color
242 /// * 'g' - value from 0 to 255 that represents how much green is in the color
243 /// * 'b' - value from 0 to 255 that represents how much blue is in the color
244 /// * 'a' - value from 0 to 255 that represents how opaque the color is
245 pub fn set_stroke_style_rgba(r: u8, g: u8, b: u8, a: u8) {
246 unsafe {
247 js_set_stroke_style_rgba(r, g, b, a);
248 }
249 }
250
251 /// adds a circular arc to the current sub-path.
252 /// see [CanvasRenderingContext2D.arc](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arc)
253 ///
254 /// # Parameters
255 ///
256 /// * 'x' - the horizontal coordinate of the arc's center
257 /// * 'y' - the vertical coordinate of the arc's center
258 /// * 'radius' - the arc's radius. Must be positive
259 /// * 'start_angle' - the angle at which the arc starts in radians, measured from the positive x-axis
260 /// * 'end_angle' - the angle at which the arc ends in radians, measured from the positive x-axis
261 /// * 'counter_clockwise' - if true, draws the arc counter-clockwise between the start and end angles
262 pub fn arc(
263 x: f32,
264 y: f32,
265 radius: f32,
266 start_angle: f32,
267 end_angle: f32,
268 counter_clockwise: bool,
269 ) {
270 if radius <= 0. {
271 panic!("radius must be positive");
272 }
273 unsafe { js_arc(x, y, radius, start_angle, end_angle, counter_clockwise) }
274 }
275
276 /// adds a circular arc to the current sub-path, using the given control points and radius.
277 /// The arc is automatically connected to the path's latest point with a straight line, if necessary for the specified parameters.
278 /// see [CanvasRenderingContext2D.arcTo](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arcTo)
279 ///
280 /// # Parameters
281 ///
282 /// * 'x1' - the x-axis coordinate of the first control point
283 /// * 'y1' - the y-axis coordinate of the first control point.
284 /// * 'x2' - the x-axis coordinate of the second control point
285 /// * 'y2' - the y-axis coordinate of the second control point.
286 /// * 'radius' - the arc's radius. Must be positive
287 pub fn arc_to(x1: f32, y1: f32, x2: f32, y2: f32, radius: f32) {
288 if radius <= 0. {
289 panic!("radius must be positive");
290 }
291 unsafe { js_arc_to(x1, y1, x2, y2, radius) }
292 }
293
294 /// starts a new path by emptying the list of sub-paths. Call this method when you want to create a new path.
295 /// see [CanvasRenderingContext2D.beginPath](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/beginPath)
296 ///
297 /// # Parameters (none)
298 pub fn begin_path() {
299 unsafe { js_begin_path() }
300 }
301
302 /// erases the pixels in a rectangular area by setting them to transparent black.
303 /// see [CanvasRenderingContext2D.clearRect](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clearRect)
304 ///
305 /// # Parameters
306 ///
307 /// * 'x' - the x-axis coordinate of the rectangle's starting point.
308 /// * 'y' - the y-axis coordinate of the rectangle's starting point.
309 /// * 'width' - the rectangle's width. Positive values are to the right, and negative to the left.
310 /// * 'height' - the rectangle's height. Positive values are down, and negative are up.
311 pub fn clear_rect(x: f32, y: f32, width: f32, height: f32) {
312 unsafe {
313 js_clear_rect(x, y, width, height);
314 }
315 }
316
317 /// attempts to add a straight line from the current point to the start of the current sub-path. If the shape has already been closed
318 /// or has only one point, this function does nothing.
319 ///
320 /// This method doesn't draw anything to the canvas directly. You can render the path using the stroke() or fill() methods.
321 /// see [CanvasRenderingContext2D.closePath](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/closePath)
322 ///
323 /// # Parameters (none)
324 pub fn close_path() {
325 unsafe {
326 js_close_path();
327 }
328 }
329
330 /// fills the current path with the current fillStyle
331 ///
332 /// see [CanvasRenderingContext2D.fill](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fill)
333 ///
334 /// # Parameters (none)
335 pub fn fill() {
336 unsafe {
337 js_fill();
338 }
339 }
340
341 /// draws a rectangle that is filled according to the current fillStyle.
342 ///
343 /// This method draws directly to the canvas without modifying the current path, so any subsequent fill() or stroke() calls will have no effect on it.
344 ///
345 /// see [CanvasRenderingContext2D.fillRect](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillRect)
346 ///
347 /// # Parameters
348 ///
349 /// * 'x' - the x-axis coordinate of the rectangle's starting point.
350 /// * 'y' - the y-axis coordinate of the rectangle's starting point.
351 /// * 'width' - the rectangle's width. Positive values are to the right, and negative to the left.
352 /// * 'height' - the rectangle's height. Positive values are down, and negative are up.
353 pub fn fill_rect(x: f32, y: f32, width: f32, height: f32) {
354 unsafe {
355 js_fill_rect(x, y, width, height);
356 }
357 }
358
359 /// adds a straight line to the current sub-path by connecting the sub-path's last point to the specified (x, y) coordinates.
360 /// see [CanvasRenderingContext2D.lineTo](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineTo)
361 ///
362 /// # Parameters
363 ///
364 /// * 'x' - the x-axis coordinate of the line's end point
365 /// * 'y' - the y-axis coordinate of the line's end point.
366 pub fn line_to(x: f32, y: f32) {
367 unsafe { js_line_to(x, y) }
368 }
369
370 /// begins a new sub-path at the point specified by the given (x, y) coordinates.
371 /// see [CanvasRenderingContext2D.moveTo](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/moveTo)
372 ///
373 /// # Parameters
374 ///
375 /// * 'x' - the x-axis coordinate of the point
376 /// * 'y' - the y-axis coordinate of the point.
377 pub fn move_to(x: f32, y: f32) {
378 unsafe { js_move_to(x, y) }
379 }
380
381 /// strokes (outlines) the current or given path with the current stroke style.
382 /// see [CanvasRenderingContext2D.stroke](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/stroke)
383 ///
384 /// # Parameters (none)
385 pub fn stroke() {
386 unsafe { js_stroke() }
387 }
388
389 /// draws a rectangle that is stroked (outlined) according to the current strokeStyle and other context settings.
390 /// see [CanvasRenderingContext2D.strokeRect](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/strokeRect)
391 ///
392 /// # Parameters
393 ///
394 /// * 'x' - the x-axis coordinate of the rectangle's starting point.
395 /// * 'y' - the y-axis coordinate of the rectangle's starting point.
396 /// * 'width' - the rectangle's width. Positive values are to the right, and negative to the left.
397 /// * 'height' - the rectangle's height. Positive values are down, and negative are up.
398 pub fn stroke_rect(x: f32, y: f32, width: f32, height: f32) {
399 unsafe { js_stroke_rect(x, y, width, height) }
400 }
401}
402
403#[derive(Copy, Clone)]
404pub enum LineCap {
405 Butt,
406 Round,
407 Square,
408}
409
410impl LineCap {
411 pub fn iterator() -> impl Iterator<Item = LineCap> {
412 [LineCap::Butt, LineCap::Round, LineCap::Square]
413 .iter()
414 .copied()
415 }
416}
417
418#[derive(Copy, Clone)]
419pub enum LineJoin {
420 Bevel,
421 Round,
422 Miter,
423}
424
425impl LineJoin {
426 pub fn iterator() -> impl Iterator<Item = LineJoin> {
427 [LineJoin::Bevel, LineJoin::Round, LineJoin::Miter]
428 .iter()
429 .copied()
430 }
431}