# core/component/shadowimage (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/component/shadowimage/

Image with a shadow. (ShadowImage in vanilla.js core/component/shadowimage.js)
- Draws the image (or a piece within the image) inside the node as large as possible while keeping its ratio, centered.
- Like ShadowText, bakes the look of the shadow laid beneath the image's shape (opacity) into one sheet and draws that single sheet.
  The node's opacity applies to the image and shadow as one sheet.
- If an image color is given, paints the image as a single-color shape of that color. (e.g. a wrap that places the original image on top at the same spot so only the shadow remains)
- Rebakes when the bake scale, image, color, shadow or node size changes. Shrinking on desktop blends evenly by overlapping area.
  If a bake of the same image (the Image's instance identifier and piece) and look already exists, it is shared. (sharedbakecache.rs, the original numbers each image and uses that as the key)
- The image path (image_path), like Sprite's, is the value the UI document (UIDocument) writes and reads in place of the image. It is not used for drawing.
- Rebaking reads the image's CPU pixels, so the given image keeps its pixels even after being uploaded to the GPU. If they were already released, they are read back from the texture.
- On the web, as in the original, it shrinks with the canvas drawImage (imageSmoothingQuality high), paints the color with source-in, then lays the canvas shadow (shadowBlur) and bakes. (bake_shadow_image in platform/web/systemfont.rs)
  Desktop shrinks by overlapping area and computes the shadow with three box blurs.
- The original getBakeCanvas is get_bake_image, which returns the baked Image instead of the baked canvas.

## ShadowImage (struct) (Default, Component)

Image with a shadow component. (bakes the look of the image with a shadow of its shape beneath it into one sheet and draws it)
- Places the image (or a piece within the image) inside the node's content_size as large as possible while keeping its ratio, centered.
- The node's opacity applies to the image and shadow as one sheet. If an image color is given, paints the image as a single-color shape of that color.
- Initially no image, no image color, a black shadow, drop 0, blur 0.

Example: Lay a blurred shadow beneath an icon

```rust
use vanilla::{Color, Image, NodeTree, ShadowImage, Vector2};

let icon = Image::create_from_pixels(32, 32, vec![255u8; 32 * 32 * 4]);
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let icon_id = node_tree.create_child(root_id, "Icon");
node_tree[icon_id].set_content_size(Vector2::create(64.0, 64.0));
let shadow_image = node_tree.add_component(icon_id, ShadowImage::new()).unwrap();
shadow_image.set_image(Some(icon.clone()), None);
shadow_image.set_shadow(Color::black(), 0.5, 2.0, 4.0);

let shadow_image = node_tree[icon_id].get_component::<ShadowImage>().unwrap();
assert!(shadow_image.get_image().is_some());
assert!(icon.is_pixels_retained());
assert!(shadow_image.get_bake_image().is_none());
```

- `pub fn new() -> ShadowImage` (constructor, since 0.4.0): Constructor.
  - returns ShadowImage: New component with no image and a black shadow (drop 0, blur 0).
- `pub fn draw_shadow_image(&mut self, transform: &NodeTransform, graphic: &mut Graphic)` (method, since 0.4.0): Draw.
  - Does not draw if there is no image, or if the piece or node size is 0 or less.
  - Rebakes if the bake scale, image, piece, color, shadow or node size differs from last time. If the image's CPU pixels have not been read back yet, it does not draw this time and rebakes next time.
  - The baked sheet is wider by the shadow margin, so it is drawn pulled up and left from the image position by the margin.
  - `transform` &NodeTransform: Node transform to draw on. (reads content_size)
  - `graphic` &mut Graphic: Graphic to draw to. (with the node transform applied, used for the bake scale and texture readback)
- `pub fn set_image(&mut self, image: Option<Rc<Image>>, image_rect: Option<Rect>)` (method, since 0.4.0): Sets the image. (the whole image if no piece is given)
  - Rebaking reads the pixels of the given image, so it keeps its CPU pixels even after being uploaded to the GPU. (Image::set_pixels_retained(true))
  - `image` Option<Rc<Image>>: Image to draw. (does not draw if None)
  - `image_rect` Option<Rect>: Piece within the image. (pixels, relative to the image's top left, the whole image if None)

  Example: Draw only the right cell of a sheet

  ```rust
  use vanilla::{Image, Rect, ShadowImage};
  
  let sheet = Image::create_from_pixels(64, 32, vec![255u8; 64 * 32 * 4]);
  let mut shadow_image = ShadowImage::new();
  shadow_image.set_image(Some(sheet), Some(Rect::create(32.0, 0.0, 32.0, 32.0)));
  assert_eq!(shadow_image.get_image_rect(), Some(Rect::create(32.0, 0.0, 32.0, 32.0)));
  
  shadow_image.set_image(None, None);
  assert!(shadow_image.get_image().is_none());
  assert_eq!(shadow_image.get_image_rect(), None);
  ```

- `pub fn get_image(&self) -> Option<&Rc<Image>>` (method, since 0.4.0): Returns the image.
  - returns Option<&Rc<Image>>: The image being drawn. None if not set.
- `pub fn get_image_rect(&self) -> Option<Rect>` (method, since 0.4.0): Returns the piece within the image. (the whole image if None)
  - returns Option<Rect>: Piece within the image. (pixels, the whole image if None)
- `pub fn set_image_color(&mut self, image_color: Option<Color>)` (method, since 0.4.0): Sets the color to paint the image in a single color. (the image as is if None)
  - `image_color` Option<Color>: Color to paint the image shape with. (alpha is multiplied by the image alpha, the image colors as is if None, initially None)
- `pub fn get_image_color(&self) -> Option<Color>` (method, since 0.4.0): Returns the color for painting the image in a single color.
  - returns Option<Color>: Color to paint the image shape with. None if the image colors are kept as is.
- `pub fn set_image_path(&mut self, image_path: &str)` (method, since 0.4.0): Sets the image path. (written and read by the UI document)
  - `image_path` &str: Image path. (only recorded, the image is not loaded)
- `pub fn get_image_path(&self) -> &str` (method, since 0.4.0): Returns the image path. (empty string if not set. The UI document writes it separately as imagePath like Sprite, so it is excluded from reflection saved properties)
  - returns &str: Image path. (empty string if not set)
- `pub fn set_shadow(&mut self, shadow_color: impl Into<Color>, shadow_opacity: f64, shadow_offset: f64, shadow_blur: f64)` (method, since 0.4.0): Shadow settings. (color, opacity, downward offset, blur amount; same as the original setShadow(color, opacity, downward offset, blur amount))
  - `shadow_color` impl Into<Color>: Shadow color. (a Color or a string like "#rrggbb"; the alpha of the color is not read; black at first)
  - `shadow_opacity` f64: Shadow opacity. (0 to 1, 1 at first)
  - `shadow_offset` f64: How far the shadow drops down. (node coordinates, initially 0)
  - `shadow_blur` f64: How far the shadow blurs. (node coordinates, 0 if less than 0, initially 0)
- `pub fn get_bake_image(&self) -> Option<&Rc<Image>>` (method, since 0.4.0): Returns the baked image. (None if not baked yet, the original getBakeCanvas)
  - returns Option<&Rc<Image>>: The last baked image. (one sheet baked at the bake scale with the shadow margin around it) None if not baked yet.
