# core/component/shadowimage (vanilla.js 0.4.0)

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

Image with a shadow.

## ShadowImage extends Component

Image with a shadow.
- Draws the image (or one piece of an atlas) inside the node as large as possible while keeping its ratio, centered.
- Like ShadowText, bakes the look of the shadow painted beneath the image's shape (opacity) into one canvas 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 draw scale (device DPR, view scale), image, color, shadow or node size changes. Widens the edge by the shadow blur amount.

Example: Icon with a shadow

```js
import { Engine, EngineConfiguration, Scene, Vector2, WorldNode, ShadowImage, ImageAsset } from "./build/vanilla.js";

class MainScene extends Scene {
	async load(engine) {
		await super.load(engine);
		const imageAsset = new ImageAsset();
		await imageAsset.load("assets/icons/icon.png");
		const iconNode = new WorldNode();
		iconNode.setContentSize(Vector2.create(128, 128));
		iconNode.setLocalPosition(Vector2.create(320, 180));
		const shadowImage = iconNode.addComponent(ShadowImage);
		shadowImage.setImage(imageAsset.image);
		shadowImage.setShadow("#000000", 0.6, 6, 8);
		const root = this.getRoot();
		root.addChild(iconNode);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.4.0): Constructor.
- `draw(graphic)` (method, since 0.4.0): Draw.
  - `graphic` Graphic: Graphics.
- `calculateDrawnRect(rect)` (method, since 0.4.0): Writes the drawn rectangle to rect. (Override: the image fits inside the content area keeping its ratio, and the shadow spills by its blur and offset)
  - The content area grows on all sides by twice the blur plus the bake padding, and downward (upward if negative) by the shadow offset.
    (Canvas shadowBlur is a Gaussian with standard deviation blur / 2 whose tail reaches 1.5 times; twice to be generous)
  - Reports the same rectangle whether or not there is an image. (Changing the image does not recalculate)
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if attached to a node.

  Example: Seeing where a shadow image draws

  ```js
  import { WorldNode, ShadowImage, Rect, Vector2 } from "./build/vanilla.js";
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(64, 64));
  const shadowImage = node.addComponent(ShadowImage);
  shadowImage.setShadow("#000000", 0.6, 6, 8);
  const drawnRect = Rect.zero();
  shadowImage.calculateDrawnRect(drawnRect);
  console.log(drawnRect.position.x, drawnRect.size.y);
  ```

- `detach(node)` (method, since 0.4.0): Detached from a node. (releases the shared baked image)
  - `node` ComponentNode: The node being detached.
- `readSourceRect()` (method, since 0.4.0): Reads the piece within the image. (the whole image if there is no piece)
  - returns { left: number, top: number, width: number, height: number }: Piece inside the image. (pixels, the whole image if there is no piece)
- `bakeImage(image, sourceRect, imageWidth, imageHeight, bakeScale)` (method, since 0.4.0): Bake. (painting the image once makes the canvas paint the shadow beneath it first)
  - Creates a new canvas on every bake, since the texture cache uploads once per canvas.
  - `image` HTMLImageElement | HTMLCanvasElement: Image.
  - `sourceRect` object: Piece inside the image. ({ left, top, width, height }, pixels)
  - `imageWidth` number: Width to draw. (content size units)
  - `imageHeight` number: Height to draw. (content size units)
  - `bakeScale` number: Bake scale. (Graphic.calculateTextBakeScale)
- `readScaledImage(image, sourceRect, scaledImageWidth, scaledImageHeight)` (method, since 0.4.0): Image scaled down to the draw size. (if there is an image color, a single-color shape of that color, without the shadow)
  - `image` HTMLImageElement | HTMLCanvasElement: Image.
  - `sourceRect` object: Piece inside the image. ({ left, top, width, height }, pixels)
  - `scaledImageWidth` number: Scaled width. (pixels)
  - `scaledImageHeight` number: Scaled height. (pixels)
  - returns HTMLCanvasElement | null: New canvas holding the scaled image. (null if the 2D context is unavailable)
- `setImage(image, imageRect = null)` (method, since 0.4.0): Sets the image. (the whole image if no piece is given)
  - `image` HTMLImageElement | HTMLCanvasElement | null: Image. (nothing is drawn if null)
  - `imageRect` Rect | null: Piece inside the image. (pixels, a copy is kept, the whole image if null)
- `getImage()` (method, since 0.4.0): Returns the image.
  - returns HTMLImageElement | HTMLCanvasElement | null: Image. (null if none)
- `setImagePath(imagePath)` (method, since 0.4.0): Sets the image path. (Only records it and does not load the image, UIDocument writes and reads this path instead of the image)
  - `imagePath` string: Image path.
- `getImagePath()` (method, since 0.4.0): Returns the image path. (empty string if not set)
  - returns string: Image path. (empty string if not set)
- `getImageRect()` (method, since 0.4.0): Returns the piece within the image. (the whole image if null)
  - returns Rect | null: Piece inside the image. (the whole image if null)
- `setImageColor(imageColor)` (method, since 0.4.0): Sets the color for painting the image in a single color. (image as is if null)
  - `imageColor` Color | string | null: Colour to paint the image in one tone. (a Color or a colour string such as "#rrggbb", the alpha is the paint opacity, null keeps the image as is)
- `getImageColor()` (method, since 0.4.0): Returns the color for painting the image in a single color.
  - returns Color | null: Color to fill the image with a single color. (the image as is if null)
- `setShadow(shadowColor, shadowOpacity, shadowOffset, shadowBlur = 0)` (method, since 0.4.0): Sets the shadow. (color, opacity, offset below the image, blur amount)
  - `shadowColor` Color | string: Shadow colour. (a Color or a colour string such as "#rrggbb", the alpha is not read)
  - `shadowOpacity` number: Shadow opacity. (0 ~ 1)
  - `shadowOffset` number: How far the shadow drops below the image. (content size units)
  - `shadowBlur` number: No blur if 0.
- `getBakeCanvas()` (method, since 0.4.0): Returns the baked canvas. (null if not baked yet)
  - returns HTMLCanvasElement | null: The baked canvas. (null if not baked yet)
