# core/canvas2d/desktop_canvas (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/canvas2d/desktop_canvas/

Desktop version of the 2D drawing target. (paints with tiny-skia, header comment of core/canvas2d.rs)

Example: Draw another canvas's two pixels stretched without smoothing

```rust
use vanilla::Canvas2D;

let mut source = Canvas2D::new(2, 1);
source.set_fill_style("#ff0000");
source.fill_rect(0.0, 0.0, 1.0, 1.0);
source.set_fill_style("#0000ff");
source.fill_rect(1.0, 0.0, 1.0, 1.0);

let mut canvas = Canvas2D::new(8, 4);
canvas.set_image_smoothing_enabled(false);
canvas.draw_image_scaled(&source, 0.0, 0.0, 8.0, 4.0);
assert_eq!(canvas.get_image_data(1, 1, 1, 1).data, vec![255, 0, 0, 255]);
assert_eq!(canvas.get_image_data(6, 1, 1, 1).data, vec![0, 0, 255, 255]);
```

## Canvas2D (struct)

2D drawing target. (the original canvas and its 2D context)

Example: Fill a red rectangle, then save the state and fill translucent blue at a translated position

```rust
use vanilla::Canvas2D;

let mut canvas = Canvas2D::new(20, 10);
canvas.set_fill_style("#ff0000");
canvas.fill_rect(0.0, 0.0, 10.0, 10.0);
assert_eq!(canvas.get_image_data(5, 5, 1, 1).data, vec![255, 0, 0, 255]);

canvas.save();
canvas.translate(10.0, 0.0);
canvas.set_global_alpha(0.5);
canvas.set_fill_style("#0000ff");
canvas.fill_rect(0.0, 0.0, 10.0, 10.0);
canvas.restore();
assert_eq!(canvas.get_global_alpha(), 1.0);
assert_eq!(canvas.get_image_data(15, 5, 1, 1).data, vec![0, 0, 255, 128]);
```

- `pub fn new(width: u32, height: u32) -> Canvas2D` (constructor, since 0.4.0): Creates. (the original document.createElement("canvas") given width and height, transparent black)
  - `width` u32: Width. (pixels)
  - `height` u32: Height. (pixels)
  - returns Canvas2D: A new canvas filled with transparent black.
- `pub fn get_width(&self) -> u32` (method, since 0.4.0): Width.
  - returns u32: Width. (pixels)
- `pub fn get_height(&self) -> u32` (method, since 0.4.0): Height.
  - returns u32: Height. (pixels)
- `pub fn set_width(&mut self, width: u32)` (method, since 0.4.0): Sets the width. (clears the drawing and resets the state like the original, even for the same value)
  - `width` u32: New width. (pixels)
- `pub fn set_height(&mut self, height: u32)` (method, since 0.4.0): Sets the height. (clears the drawing and resets the state like the original, even for the same value)
  - `height` u32: New height. (pixels)
- `pub fn reset(&mut self)` (method, since 0.4.0): Resets. (the original reset: clears the drawing and resets the state and path)
- `pub fn save(&mut self)` (method, since 0.4.0): Pushes the state. (the original save)
- `pub fn restore(&mut self)` (method, since 0.4.0): Pops the state. (the original restore, does nothing if nothing is pushed)
- `pub fn scale(&mut self, x: f64, y: f64)` (method, since 0.4.0): Scales. (the original scale)
  - `x` f64: Horizontal scale.
  - `y` f64: Vertical scale.
- `pub fn rotate(&mut self, angle: f64)` (method, since 0.4.0): Rotates. (the original rotate, radians. Positive is clockwise on screen)
  - `angle` f64: Angle to rotate. (radians, clockwise on screen is +)
- `pub fn translate(&mut self, x: f64, y: f64)` (method, since 0.4.0): Translates. (the original translate)
  - `x` f64: Horizontal translation. (in current coordinate system units, pixels)
  - `y` f64: Vertical translation. (in current coordinate system units, pixels)
- `pub fn transform(&mut self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64)` (method, since 0.4.0): Multiplies the transform. (the original transform, ignored for non-finite values)
  - `a` f64: Horizontal scale. (multiplied by x and added to the new x)
  - `b` f64: Vertical skew. (multiplied by x and added to the new y)
  - `c` f64: Horizontal skew. (multiplied by y and added to the new x)
  - `d` f64: Vertical scale. (multiplied by y and added to the new y)
  - `e` f64: Horizontal translation. (in current coordinate system units, pixels)
  - `f` f64: Vertical translation. (in current coordinate system units, pixels)
- `pub fn set_transform(&mut self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64)` (method, since 0.4.0): Replaces the transform. (the original setTransform(a, b, c, d, e, f))
  - `a` f64: Horizontal scale. (multiplied by x and added to the new x)
  - `b` f64: Vertical skew. (multiplied by x and added to the new y)
  - `c` f64: Horizontal skew. (multiplied by y and added to the new x)
  - `d` f64: Vertical scale. (multiplied by y and added to the new y)
  - `e` f64: Horizontal translation. (pixels)
  - `f` f64: Vertical translation. (pixels)
- `pub fn set_transform_matrix(&mut self, matrix: CanvasMatrix)` (method, since 0.4.0): Replaces the transform. (the original setTransform(DOMMatrix))
  - `matrix` CanvasMatrix: New transform. (ignored if any value is non-finite)
- `pub fn reset_transform(&mut self)` (method, since 0.4.0): Clears the transform. (the original resetTransform)
- `pub fn get_transform(&self) -> CanvasMatrix` (method, since 0.4.0): Current transform. (the original getTransform)
  - returns CanvasMatrix: Current transform matrix. (initially the identity matrix)
- `pub fn set_global_alpha(&mut self, global_alpha: f64)` (method, since 0.4.0): Sets the global alpha. (the original globalAlpha, ignored if outside 0 ~ 1 or non-finite)
  - `global_alpha` f64: Global alpha. (0 is transparent, 1 is opaque, initially 1)
- `pub fn get_global_alpha(&self) -> f64` (method, since 0.4.0): Global alpha.
  - returns f64: Global alpha. (0 ~ 1, initially 1)
- `pub fn set_global_composite_operation(&mut self, operation_name: &str)` (method, since 0.4.0): Sets compositing. (the original globalCompositeOperation, unknown names are ignored)
  - `operation_name` &str: The original globalCompositeOperation name. (lowercase and hyphens like "multiply", case-sensitive)
- `pub fn get_global_composite_operation(&self) -> String` (method, since 0.4.0): Compositing.
  - returns String: Original globalCompositeOperation name. (initially "source-over")
- `pub fn set_image_smoothing_enabled(&mut self, is_enabled: bool)` (method, since 0.4.0): Sets image smoothing. (the original imageSmoothingEnabled, nearest pixel when off)
  - `is_enabled` bool: true to draw smoothly, false to draw with the nearest pixel. (initially true)
- `pub fn is_image_smoothing_enabled(&self) -> bool` (method, since 0.4.0): Whether images are smoothed.
  - returns bool: true if drawn smoothly. (initially true)
- `pub fn set_fill_style(&mut self, style: impl Into<CanvasStyle>)` (method, since 0.4.0): Sets the fill paint. (the original fillStyle)
  - `style` impl Into<CanvasStyle>: Fill paint. (Color, CSS color text like "#rrggbb", CanvasGradient, CanvasPattern, initially black)
- `pub fn get_fill_style(&self) -> &CanvasStyle` (method, since 0.4.0): Fill paint.
  - returns &CanvasStyle: Fill paint. (initially black)
- `pub fn set_stroke_style(&mut self, style: impl Into<CanvasStyle>)` (method, since 0.4.0): Sets the stroke paint. (the original strokeStyle)
  - `style` impl Into<CanvasStyle>: Stroke paint. (Color, CSS color text like "#rrggbb", CanvasGradient, CanvasPattern, initially black)
- `pub fn get_stroke_style(&self) -> &CanvasStyle` (method, since 0.4.0): Stroke paint.
  - returns &CanvasStyle: Stroke paint. (initially black)
- `pub fn create_linear_gradient(&self, start_x: f64, start_y: f64, end_x: f64, end_y: f64) -> CanvasGradient` (method, since 0.4.0): Linear gradient. (the original createLinearGradient)
  - `start_x` f64: Start point x.
  - `start_y` f64: Start point y.
  - `end_x` f64: End point x.
  - `end_y` f64: End point y.
  - returns CanvasGradient: A linear gradient with no color stops. (the coordinates follow the transform at paint time)
- `pub fn create_radial_gradient(&self, start_x: f64, start_y: f64, start_radius: f64, end_x: f64, end_y: f64, end_radius: f64) -> Option<CanvasGradient>` (method, since 0.4.0): Radial gradient. (the original createRadialGradient, None if a radius is negative, like the original's exception)
  - `start_x` f64: Start circle center x.
  - `start_y` f64: Start circle center y.
  - `start_radius` f64: Start circle radius. (None if negative)
  - `end_x` f64: End circle center x.
  - `end_y` f64: End circle center y.
  - `end_radius` f64: End circle radius. (None if negative)
  - returns Option<CanvasGradient>: A radial gradient with no color stops. None if a radius is negative.
- `pub fn create_conic_gradient(&self, start_angle: f64, center_x: f64, center_y: f64) -> CanvasGradient` (method, since 0.4.0): Conic gradient. (the original createConicGradient, the start angle is in radians clockwise from the right)
  - `start_angle` f64: Start angle. (radians, 0 is to the right, clockwise on screen is +)
  - `center_x` f64: Center x.
  - `center_y` f64: Center y.
  - returns CanvasGradient: A conic gradient with no color stops.
- `pub fn create_pattern<'a>(&self, source: impl Into<CanvasImageSource<'a>>, repetition: CanvasPatternRepetition) -> Option<CanvasPattern>` (method, since 0.4.0): Pattern. (the original createPattern, None if the image pixels cannot be read or the size is 0)
  - `source` impl Into<CanvasImageSource<'a>>: Pattern image. (engine Image, `Rc<Image>`, another Canvas2D, its current pixels are copied)
  - `repetition` CanvasPatternRepetition: Repetition.
  - returns Option<CanvasPattern>: The pattern. None if the image width or height is 0 or its pixels cannot be read.
- `pub fn set_shadow_color(&mut self, color: impl Into<Color>)` (method, since 0.4.0): Sets the shadow color. (the original shadowColor)
  - `color` impl Into<Color>: Shadow color. (Color or CSS color text, initially transparent black so there is no shadow)
- `pub fn get_shadow_color(&self) -> Color` (method, since 0.4.0): Shadow color.
  - returns Color: Shadow color. (initially transparent black)
- `pub fn set_shadow_blur(&mut self, shadow_blur: f64)` (method, since 0.4.0): Sets the shadow blur. (the original shadowBlur, ignored if negative or non-finite)
  - `shadow_blur` f64: Blur amount. (0 means no blur, initially 0)
- `pub fn get_shadow_blur(&self) -> f64` (method, since 0.4.0): Shadow blur.
  - returns f64: Shadow blur. (initially 0)
- `pub fn set_shadow_offset_x(&mut self, offset_x: f64)` (method, since 0.4.0): Sets the shadow horizontal offset. (the original shadowOffsetX, not affected by the transform)
  - `offset_x` f64: Horizontal offset. (canvas pixels, ignored if non-finite, initially 0)
- `pub fn get_shadow_offset_x(&self) -> f64` (method, since 0.4.0): Shadow horizontal offset.
  - returns f64: Shadow horizontal offset. (initially 0)
- `pub fn set_shadow_offset_y(&mut self, offset_y: f64)` (method, since 0.4.0): Sets the shadow vertical offset. (the original shadowOffsetY, not affected by the transform)
  - `offset_y` f64: Vertical offset. (canvas pixels, ignored if non-finite, initially 0)
- `pub fn get_shadow_offset_y(&self) -> f64` (method, since 0.4.0): Shadow vertical offset.
  - returns f64: Shadow vertical offset. (initially 0)
- `pub fn set_line_width(&mut self, line_width: f64)` (method, since 0.4.0): Sets the line width. (the original lineWidth, ignored if 0 or less or non-finite)
  - `line_width` f64: Line width. (initially 1)
- `pub fn get_line_width(&self) -> f64` (method, since 0.4.0): Line width.
  - returns f64: Line width. (initially 1)
- `pub fn set_line_cap(&mut self, line_cap: CanvasLineCap)` (method, since 0.4.0): Sets the line cap. (the original lineCap)
  - `line_cap` CanvasLineCap: Line cap. (initially Butt)
- `pub fn get_line_cap(&self) -> CanvasLineCap` (method, since 0.4.0): Line cap.
  - returns CanvasLineCap: Line cap. (initially Butt)
- `pub fn set_line_join(&mut self, line_join: CanvasLineJoin)` (method, since 0.4.0): Sets the line join. (the original lineJoin)
  - `line_join` CanvasLineJoin: Line join. (initially Miter)
- `pub fn get_line_join(&self) -> CanvasLineJoin` (method, since 0.4.0): Line join.
  - returns CanvasLineJoin: Line join. (initially Miter)
- `pub fn set_miter_limit(&mut self, miter_limit: f64)` (method, since 0.4.0): Sets the miter limit. (the original miterLimit, ignored if 0 or less or non-finite)
  - `miter_limit` f64: Miter limit. (ratio of the miter length to the line width, initially 10)
- `pub fn get_miter_limit(&self) -> f64` (method, since 0.4.0): Miter limit.
  - returns f64: Miter limit. (initially 10)
- `pub fn set_line_dash(&mut self, dash_list: &[f64])` (method, since 0.4.0): Sets the line dash. (the original setLineDash, ignored if any value is negative or non-finite, doubled if the count is odd)
  - `dash_list` &[f64]: List alternating dash lengths and gap lengths. (a solid line if empty)
- `pub fn get_line_dash(&self) -> Vec<f64>` (method, since 0.4.0): Line dash. (the original getLineDash)
  - returns Vec<f64>: Dash list. (doubled if an odd count was given, initially empty)
- `pub fn set_line_dash_offset(&mut self, line_dash_offset: f64)` (method, since 0.4.0): Sets the line dash offset. (the original lineDashOffset)
  - `line_dash_offset` f64: Where the dashes start. (ignored if non-finite, initially 0)
- `pub fn get_line_dash_offset(&self) -> f64` (method, since 0.4.0): Line dash offset.
  - returns f64: Line dash offset. (initially 0)
- `pub fn set_font(&mut self, font: &str)` (method, since 0.4.0): Sets the font. (the original font, CSS font shorthand. Invalid strings are ignored like Chrome; read by the engine glyph cache)
  - The size is resolved to pixels at this point. Relative sizes (em, %, larger and the like) are relative to 10px sans-serif, like a canvas not attached to the document.
  - CSS-wide keywords (inherit and the like), a line height alone, and strings without a size or a family are invalid. System font keywords (caption and the like) are accepted.
  - `font` &str: Font. (CSS font shorthand like "bold 16px sans-serif", initially "10px sans-serif")

  Example: An accepted font reads back in Chrome's serialized form, and an invalid string keeps the previous value

  ```rust
  use vanilla::Canvas2D;
  
  let mut canvas = Canvas2D::new(8, 8);
  canvas.set_font("BOLD 12pt 'Noto Sans', SANS-SERIF");
  assert_eq!(canvas.get_font(), "bold 16px \"Noto Sans\", sans-serif");
  canvas.set_font("italic 2em/1.5 serif");
  assert_eq!(canvas.get_font(), "italic 20px serif");
  canvas.set_font("inherit");
  assert_eq!(canvas.get_font(), "italic 20px serif");
  ```

- `pub fn get_font(&self) -> String` (method, since 0.4.0): Font.
  - returns String: Font. (serialized like Chrome's font getter: "italic ", "bold " or the weight number, "small-caps ", the size in px, the family list. Initially "10px sans-serif")
- `pub fn set_text_align(&mut self, text_align: CanvasTextAlign)` (method, since 0.4.0): Sets the horizontal text alignment. (the original textAlign)
  - `text_align` CanvasTextAlign: Horizontal text alignment. (initially Start)
- `pub fn get_text_align(&self) -> CanvasTextAlign` (method, since 0.4.0): Horizontal text alignment.
  - returns CanvasTextAlign: Horizontal text alignment. (initially Start)
- `pub fn set_text_baseline(&mut self, text_baseline: CanvasTextBaseline)` (method, since 0.4.0): Sets the text baseline. (the original textBaseline)
  - `text_baseline` CanvasTextBaseline: Text baseline. (initially Alphabetic)
- `pub fn get_text_baseline(&self) -> CanvasTextBaseline` (method, since 0.4.0): Text baseline.
  - returns CanvasTextBaseline: Text baseline. (initially Alphabetic)
- `pub fn clear_rect(&mut self, x: f64, y: f64, width: f64, height: f64)` (method, since 0.4.0): Clears a rectangle. (the original clearRect: makes the area transparent black, follows the clip but ignores shadow, alpha and compositing)
  - `x` f64: Top-left x.
  - `y` f64: Top-left y.
  - `width` f64: Width. (leftward from x if negative)
  - `height` f64: Height. (upward from y if negative)
- `pub fn fill_rect(&mut self, x: f64, y: f64, width: f64, height: f64)` (method, since 0.4.0): Fills a rectangle. (the original fillRect, does not change the current path)
  - `x` f64: Top-left x.
  - `y` f64: Top-left y.
  - `width` f64: Width. (leftward from x if negative)
  - `height` f64: Height. (upward from y if negative)
- `pub fn stroke_rect(&mut self, x: f64, y: f64, width: f64, height: f64)` (method, since 0.4.0): Strokes a rectangle. (the original strokeRect)
  - `x` f64: Top-left x.
  - `y` f64: Top-left y.
  - `width` f64: Width. (leftward from x if negative)
  - `height` f64: Height. (upward from y if negative)
- `pub fn begin_path(&mut self)` (method, since 0.4.0): Clears the path. (the original beginPath)
- `pub fn close_path(&mut self)` (method, since 0.4.0): Closes the subpath. (the original closePath)
- `pub fn move_to(&mut self, x: f64, y: f64)` (method, since 0.4.0): New subpath. (the original moveTo)
  - `x` f64: Start point x of the new subpath.
  - `y` f64: Start point y of the new subpath.
- `pub fn line_to(&mut self, x: f64, y: f64)` (method, since 0.4.0): Straight line. (the original lineTo, starts a subpath at that point if there is none)
  - `x` f64: End point x.
  - `y` f64: End point y.
- `pub fn quadratic_curve_to(&mut self, control_x: f64, control_y: f64, x: f64, y: f64)` (method, since 0.4.0): Quadratic curve. (the original quadraticCurveTo)
  - `control_x` f64: Control point x.
  - `control_y` f64: Control point y.
  - `x` f64: End point x.
  - `y` f64: End point y.
- `pub fn bezier_curve_to(&mut self, first_control_x: f64, first_control_y: f64, second_control_x: f64, second_control_y: f64, x: f64, y: f64)` (method, since 0.4.0): Cubic curve. (the original bezierCurveTo)
  - `first_control_x` f64: First control point x.
  - `first_control_y` f64: First control point y.
  - `second_control_x` f64: Second control point x.
  - `second_control_y` f64: Second control point y.
  - `x` f64: End point x.
  - `y` f64: End point y.
- `pub fn arc(&mut self, x: f64, y: f64, radius: f64, start_angle: f64, end_angle: f64, is_counterclockwise: bool) -> bool` (method, since 0.4.0): Arc. (the original arc, does nothing and returns false if the radius is negative, like the original's exception)
  - `x` f64: Center x.
  - `y` f64: Center y.
  - `radius` f64: Radius. (false if negative)
  - `start_angle` f64: Start angle. (radians, 0 is to the right, clockwise on screen is +)
  - `end_angle` f64: End angle. (radians)
  - `is_counterclockwise` bool: true to go counterclockwise.
  - returns bool: false if the radius is negative (where the original throws), otherwise true. (if any value is non-finite, adds nothing and returns true like the original)
- `pub fn arc_to(&mut self, first_x: f64, first_y: f64, second_x: f64, second_y: f64, radius: f64) -> bool` (method, since 0.4.0): Arc between two tangents. (the original arcTo, false if the radius is negative)
  - `first_x` f64: x of the point where the two tangents meet. (the first tangent runs from the current point to this point)
  - `first_y` f64: y of the point where the two tangents meet.
  - `second_x` f64: x of a point the second tangent passes through. (the second tangent runs from the meeting point to this point)
  - `second_y` f64: y of a point the second tangent passes through.
  - `radius` f64: Radius. (false if negative)
  - returns bool: false if the radius is negative (where the original throws), otherwise true. (if any value is non-finite, adds nothing and returns true like the original)
- `pub fn ellipse(&mut self, x: f64, y: f64, radius_x: f64, radius_y: f64, rotation: f64, start_angle: f64, end_angle: f64, is_counterclockwise: bool) -> bool` (method, since 0.4.0): Elliptical arc. (the original ellipse, false if a radius is negative)
  - `x` f64: Center x.
  - `y` f64: Center y.
  - `radius_x` f64: Horizontal radius. (before rotation, false if negative)
  - `radius_y` f64: Vertical radius. (before rotation, false if negative)
  - `rotation` f64: Angle to rotate the ellipse. (radians, clockwise on screen is +)
  - `start_angle` f64: Start angle. (radians, 0 is to the right in the ellipse coordinate system)
  - `end_angle` f64: End angle. (radians)
  - `is_counterclockwise` bool: true to go counterclockwise.
  - returns bool: false if the radius is negative (where the original throws), otherwise true. (if any value is non-finite, adds nothing and returns true like the original)
- `pub fn rect(&mut self, x: f64, y: f64, width: f64, height: f64)` (method, since 0.4.0): Rectangle subpath. (the original rect, adds a closed subpath and starts a new subpath at (x, y))
  - `x` f64: Top-left x.
  - `y` f64: Top-left y.
  - `width` f64: Width. (leftward from x if negative)
  - `height` f64: Height. (upward from y if negative)
- `pub fn round_rect<R: Into<CanvasRadius> + Copy>(&mut self, x: f32, y: f32, width: f32, height: f32, radius_list: &[R]) -> bool` (method, since 0.4.0): Rounded rectangle segment. (the original roundRect, radii are numbers (f32) or elliptical corners ((x, y), CanvasRadius::Elliptical))
  - `x` f32: Top-left x.
  - `y` f32: Top-left y.
  - `width` f32: Width. (leftward from x if negative)
  - `height` f32: Height. (upward from y if negative)
  - `radius_list` &[R]: Corner radii, 1 ~ 4 of them. (as in the original: 1 for all four corners, 2 for top-left and bottom-right / top-right and bottom-left, 3 for top-left / top-right and bottom-left / bottom-right, 4 for top-left, top-right, bottom-right, bottom-left)
  - returns bool: false if there are not 1 ~ 4 radii or a radius is negative (where the original throws), otherwise true.
- `pub fn fill(&mut self, fill_rule: CanvasFillRule)` (method, since 0.4.0): Fills the current path. (the original fill(fillRule))
  - `fill_rule` CanvasFillRule: Fill rule.
- `pub fn fill_path(&mut self, path: &Path2D, fill_rule: CanvasFillRule)` (method, since 0.4.0): Fills a path. (the original fill(path, fillRule), the path is transformed by the current transform when painted)
  - `path` &Path2D: Path to fill.
  - `fill_rule` CanvasFillRule: Fill rule.
- `pub fn stroke(&mut self)` (method, since 0.4.0): Strokes the current path. (the original stroke(), the line width is affected by the current transform)
- `pub fn stroke_path(&mut self, path: &Path2D)` (method, since 0.4.0): Strokes a path. (the original stroke(path))
  - `path` &Path2D: Path to stroke. (stroked after transforming with the current transform)
- `pub fn clip(&mut self, fill_rule: CanvasFillRule)` (method, since 0.4.0): Clips to the current path. (the original clip(fillRule), only the area overlapping the existing clip remains)
  - `fill_rule` CanvasFillRule: Fill rule.
- `pub fn clip_path(&mut self, path: &Path2D, fill_rule: CanvasFillRule)` (method, since 0.4.0): Clips to a path. (the original clip(path, fillRule))
  - `path` &Path2D: Path to clip with. (clipped after transforming with the current transform)
  - `fill_rule` CanvasFillRule: Fill rule.
- `pub fn is_point_in_path(&self, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool` (method, since 0.4.0): Whether a point is inside the current path. (the original isPointInPath(x, y, fillRule), the point is in canvas coordinates not affected by the transform)
  - `x` f64: Point x. (canvas coordinates not affected by the transform)
  - `y` f64: Point y. (canvas coordinates not affected by the transform)
  - `fill_rule` CanvasFillRule: Fill rule.
  - returns bool: true if the point is inside.
- `pub fn is_point_in_path2d(&self, path: &Path2D, x: f64, y: f64, fill_rule: CanvasFillRule) -> bool` (method, since 0.4.0): Whether a point is inside a path. (the original isPointInPath(path, x, y, fillRule), the path is transformed by the current transform when tested)
  - `path` &Path2D: Path to test.
  - `x` f64: Point x. (canvas coordinates not affected by the transform)
  - `y` f64: Point y. (canvas coordinates not affected by the transform)
  - `fill_rule` CanvasFillRule: Fill rule.
  - returns bool: true if the point is inside.
- `pub fn measure_text(&self, text: &str) -> TextMetrics` (method, since 0.4.0): Measures text. (the original measureText, measured with the engine glyph cache. 0 if there are no fonts at all)
  - `text` &str: Text to measure.
  - returns TextMetrics: The measured values. (width, font ascent and descent, the box actually painted)
- `pub fn fill_text(&mut self, text: &str, x: f64, y: f64, maximum_width: Option<f64>)` (method, since 0.4.0): Fills text. (the original fillText(text, x, y, maxWidth), squeezes the text horizontally if it is wider than maxWidth)
  - `text` &str: Text to fill.
  - `x` f64: Reference point x. (the position set by the horizontal text alignment)
  - `y` f64: Reference point y. (the position set by the text baseline)
  - `maximum_width` Option<f64>: Maximum width. (not squeezed if None)
- `pub fn stroke_text(&mut self, text: &str, x: f64, y: f64, maximum_width: Option<f64>)` (method, since 0.4.0): Strokes text. (the original strokeText, the line is stroked at lineWidth thickness centered on the outline)
  - `text` &str: Text to stroke.
  - `x` f64: Reference point x. (the position set by the horizontal text alignment)
  - `y` f64: Reference point y. (the position set by the text baseline)
  - `maximum_width` Option<f64>: Maximum width. (not squeezed if None)
- `pub fn draw_image<'a>(&mut self, source: impl Into<CanvasImageSource<'a>>, destination_x: f64, destination_y: f64)` (method, since 0.4.0): Draws an image. (the original drawImage(image, dx, dy))
  - `source` impl Into<CanvasImageSource<'a>>: Image to draw. (engine Image, `Rc<Image>`, another Canvas2D)
  - `destination_x` f64: Left x of the draw position.
  - `destination_y` f64: Top y of the draw position.
- `pub fn draw_image_scaled<'a>(&mut self, source: impl Into<CanvasImageSource<'a>>, destination_x: f64, destination_y: f64, destination_width: f64, destination_height: f64)` (method, since 0.4.0): Draws a stretched image. (the original drawImage(image, dx, dy, dw, dh))
  - `source` impl Into<CanvasImageSource<'a>>: Image to draw. (engine Image, `Rc<Image>`, another Canvas2D)
  - `destination_x` f64: Left x of the draw position.
  - `destination_y` f64: Top y of the draw position.
  - `destination_width` f64: Width to draw. (leftward from destination_x if negative)
  - `destination_height` f64: Height to draw. (upward from destination_y if negative)
- `pub fn draw_image_region<'a>(&mut self, source: impl Into<CanvasImageSource<'a>>, source_x: f64, source_y: f64, source_width: f64, source_height: f64, destination_x: f64, destination_y: f64, destination_width: f64, destination_height: f64)` (method, since 0.4.0): Draws part of an image. (the original drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh), negative widths and heights are flipped to fit like the original)
  - `source` impl Into<CanvasImageSource<'a>>: Image to draw. (engine Image, `Rc<Image>`, another Canvas2D)
  - `source_x` f64: Left of the source area. (image pixels)
  - `source_y` f64: Top of the source area. (image pixels)
  - `source_width` f64: Width of the source area. (image pixels, leftward from source_x if negative)
  - `source_height` f64: Height of the source area. (image pixels, upward from source_y if negative)
  - `destination_x` f64: Left x of the draw position.
  - `destination_y` f64: Top y of the draw position.
  - `destination_width` f64: Width to draw. (leftward from destination_x if negative)
  - `destination_height` f64: Height to draw. (upward from destination_y if negative)
- `pub fn create_image_data(&self, width: u32, height: u32) -> ImageData` (method, since 0.4.0): Empty pixel data. (the original createImageData)
  - `width` u32: Width. (pixels)
  - `height` u32: Height. (pixels)
  - returns ImageData: Pixel data with every byte 0.
- `pub fn get_image_data(&self, source_x: i32, source_y: i32, width: u32, height: u32) -> ImageData` (method, since 0.4.0): Reads pixels. (the original getImageData, non-premultiplied RGBA. Transparent black outside the canvas)
  - `source_x` i32: Left of the area to read. (canvas pixels, not affected by the transform)
  - `source_y` i32: Top of the area to read. (canvas pixels, not affected by the transform)
  - `width` u32: Width to read. (pixels)
  - `height` u32: Height to read. (pixels)
  - returns ImageData: Non-premultiplied RGBA pixel data. (transparent black outside the canvas)
- `pub fn put_image_data(&mut self, image_data: &ImageData, destination_x: i32, destination_y: i32)` (method, since 0.4.0): Writes pixels. (the original putImageData: replaces them as is, without clip, alpha, compositing or shadow)
  - Straight RGBA is premultiplied as (color x alpha + 127) / 255, like Chrome.
  - Nothing is written when the width or height is 0 or exceeds i32, or when data is not width x height x 4 bytes. (pixel data the original new ImageData does not accept)
  - `image_data` &ImageData: Pixel data to write. (non-premultiplied RGBA)
  - `destination_x` i32: Left of the write position. (canvas pixels, not affected by the transform)
  - `destination_y` i32: Top of the write position. (canvas pixels, not affected by the transform)
- `pub fn put_image_data_region(&mut self, image_data: &ImageData, destination_x: i32, destination_y: i32, dirty_x: i32, dirty_y: i32, dirty_width: i32, dirty_height: i32)` (method, since 0.4.0): Writes only one rectangle of the pixel data. (the dirtyX, dirtyY, dirtyWidth, dirtyHeight form of the original putImageData, computed like Chrome's base_rendering_context_2d.cc)
  - Nothing is written when width x height exceeds i32. A negative width or height flips the rectangle by moving its start by that amount (0 if the start was negative), and parts outside the pixel data and the canvas are clipped.
  - `image_data` &ImageData: Pixel data to write. (non-premultiplied RGBA)
  - `destination_x` i32: Where the left of the pixel data goes. (canvas pixels, not affected by the transform)
  - `destination_y` i32: Where the top of the pixel data goes. (canvas pixels, not affected by the transform)
  - `dirty_x` i32: Left of the rectangle to write. (pixel data pixels)
  - `dirty_y` i32: Top of the rectangle to write. (pixel data pixels)
  - `dirty_width` i32: Width of the rectangle to write. (leftward if negative)
  - `dirty_height` i32: Height of the rectangle to write. (upward if negative)

  Example: Writing only the bottom-right pixel of 2 x 2 pixel data

  ```rust
  use vanilla::{Canvas2D, ImageData};
  
  let mut image_data = ImageData::new(2, 2);
  image_data.data.copy_from_slice(&[255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255, 255, 255, 255, 255]);
  
  let mut canvas = Canvas2D::new(4, 4);
  canvas.put_image_data_region(&image_data, 1, 1, 2, 2, -1, -1);
  assert_eq!(canvas.get_image_data(2, 2, 1, 1).data, vec![255, 255, 255, 255]);
  assert_eq!(canvas.get_image_data(1, 1, 1, 1).data, vec![0, 0, 0, 0]);
  ```

- `pub fn to_image(&self) -> Rc<Image>` (method, since 0.4.0): To an engine image. (used for Graphic, UIImageView. The same Image if nothing was drawn since)
  - returns Rc<Image>: An engine image of this canvas's pixels. (the same Rc if nothing was drawn since)
