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

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

Soft shade.

## SoftShade extends Component

Soft shade.
- Used to darken the area behind text so it stays readable even on a bright background. Bakes the shape into one small canvas and draws it stretched to the node's content size.
- Since it is a soft gradient, no artifacts show even when baked at low resolution and stretched. (one baked pixel covers one bake unit, and there is a width/height cap)
- Rebakes when the node size, shape, color, intensity or side blur changes. The node's opacity applies to it as a single sheet.

Example: Lay a band shade behind text

```js
import { Engine, EngineConfiguration, Scene, Vector2, WorldNode, Paint, SoftShade, SoftShadeShape, Text } from "./build/vanilla.js";

class MainScene extends Scene {
	async load(engine) {
		await super.load(engine);
		const root = this.getRoot();
		const backgroundNode = new WorldNode();
		backgroundNode.setContentSize(Vector2.create(640, 360));
		backgroundNode.setLocalPosition(Vector2.create(320, 180));
		const backgroundPaint = backgroundNode.addComponent(Paint);
		backgroundPaint.setColor("#f0e6c8");
		root.addChild(backgroundNode);

		const shadeNode = new WorldNode();
		shadeNode.setContentSize(Vector2.create(520, 90));
		shadeNode.setLocalPosition(Vector2.create(320, 180));
		const softShade = shadeNode.addComponent(SoftShade);
		softShade.setShade(SoftShadeShape.band, "#000000", 0.6, 80);
		const text = shadeNode.addComponent(Text);
		text.setFontSize(36);
		text.setText("Stage Clear");
		root.addChild(shadeNode);
	}
}

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.
- `isDrawnInsideContent()` (method, since 0.4.0): Returns whether it draws only inside the content area. (draws the baked shade stretched to the content size)
  - false for subclasses that override draw, since where they draw is unknown.
  - returns boolean: True if drawing only inside the content area.
- `detach(node)` (method, since 0.4.0): Detached from a node. (releases the shared baked image)
  - `node` ComponentNode: The node being detached.
- `bakeShade(bakeWidth, bakeHeight, fadeRatio)` (method, since 0.4.0): Bake. (computes alpha per pixel; a new canvas on every bake, since the texture cache uploads once per canvas)
  - `bakeWidth` number: Width of the canvas to bake. (pixels)
  - `bakeHeight` number: Height of the canvas to bake. (pixels)
  - `fadeRatio` number: Ratio of the side fade width to the content width. (0 ~ 0.5, no fade if 0, used only by the band shape)
- `setShade(shape, color, opacity, fadeWidth = 0)` (method, since 0.4.0): Sets the shade. (shape, color, opacity at the darkest point, width over which the band's sides fade)
  - `shape` string: SoftShadeShape
  - `color` Color | string: Shade colour. (a Color or a colour string such as "#rrggbb", the alpha is not read)
  - `opacity` number: Opacity at the darkest spot. (0 ~ 1, not drawn if 0 or less)
  - `fadeWidth` number: Width over which the band's sides fade. (content size units, up to half the content width, no fade if 0)
- `setBakeResolution(bakeUnit, bakeMaximumSize)` (method, since 0.4.0): Sets the bake resolution. (unit covered by one baked pixel, width/height cap)
  - `bakeUnit` number: Content size units covered by one baked pixel. (1 if less than 1, initially 4)
  - `bakeMaximumSize` number: Upper limit of the baked canvas width and height. (pixels, 2 if less than 2, initially 256)
- `getShape()` (method, since 0.4.0): Returns the shape.
  - returns string: Shape. (SoftShadeShape)
- `getColor()` (method, since 0.4.0): Returns the color.
  - returns Color: Colour. (the colour given to setShade, the alpha is not used for baking)
- `getOpacity()` (method, since 0.4.0): Returns the opacity at the darkest point.
  - returns number: Opacity at the darkest spot.
- `getFadeWidth()` (method, since 0.4.0): Returns the width over which the band's sides fade.
  - returns number: Width over which the band's sides fade. (content size units)
- `getBakeUnit()` (method, since 0.4.0): Returns the unit covered by one baked pixel.
  - returns number: Content size units covered by one baked pixel.
- `getBakeMaximumSize()` (method, since 0.4.0): Returns the width/height cap of the baked canvas.
  - returns number: Upper limit of the baked canvas width and height. (pixels)
- `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)

## SoftShadeShape (enum)

Shape of the soft shade.

- `band` = `band`: Band darkest at the horizontal center line, gradually fading toward the top and bottom edges. (the sides fade gradually up to fadeWidth inward)
- `bottom` = `bottom`: Vertical gradient darkest at the bottom edge and 0 at the top edge.
