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

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

Single-line text with a shadow.

## ShadowText extends Component

Single-line text with a shadow.
- Laying a shadow with two text nodes (the same text in black below, the text on top) makes the two show through separately, so when the node is faded the shadow beneath shows through the faded text.
  This component bakes the look of the text placed over the shadow into one canvas and draws that single sheet, so the node's opacity applies to the text and shadow as one sheet.
- Horizontal alignment and placement use the same calculation as Graphic.drawTextEntry. Vertically it is the node's center.
- If the text exceeds the node width, it shrinks from the initial size down to the minimum ratio (setFitMinimumRatio) to fit.
- Rebakes when the draw scale (device DPR, view scale), text, style or node width changes, or when a new web font chunk is downloaded.
- The shadow can be painted with the canvas shadow so that it blurs. The baked canvas edge is widened by the blur amount.
- Italic, underline, strikethrough (setItalic, setUnderline, setStrikethrough): the line position and thickness are the same as Text's middle baseline, and the lines are baked together with the text so the shadow lies beneath them too.
- Text direction is "ltr", "rtl", "auto" (by the first strong character of the text).

Example: Title text with a shadow

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

class MainScene extends Scene {
	async load(engine) {
		await super.load(engine);
		const fontAsset = new FontAsset();
		await fontAsset.loadFont("DOSGothic", "examples/assets/DOSGothic.woff");
		const titleNode = new WorldNode();
		titleNode.setContentSize(Vector2.create(400, 60));
		titleNode.setLocalPosition(Vector2.create(320, 180));
		const shadowText = titleNode.addComponent(ShadowText);
		shadowText.setFontFace(fontAsset.fontFace);
		shadowText.setTextStyle(40, true, "#ffffff");
		shadowText.setShadow("#000000", 0.8, 3, 4);
		shadowText.setText("Vanilla Engine");
		const root = this.getRoot();
		root.addChild(titleNode);
	}
}

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 aligned text grown by the shadow's blur and offset; used by off-screen culling and subtree draw bounds)
  - Horizontally, the text width measured at the initial font size (fitting only makes it narrower) is placed by alignment and grown by half the font size plus twice the blur on both sides.
    "start" and "end" become left or right depending on the direction at draw time, so both are covered.
  - Vertically, from the middle of the content height up and down by 1.5 times the font size plus twice the blur, and further down by the shadow offset. (Underline and strikethrough fit inside)
    (Canvas shadowBlur is a Gaussian with standard deviation blur / 2 whose tail reaches 1.5 times; twice, matching the bake padding)
  - Size 0 when there is no text. False if it cannot be measured. (Not culled)
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if written. (False if it cannot be measured)

  Example: Seeing where shadow text draws

  ```js
  import { WorldNode, ShadowText, Rect, Vector2 } from "./build/vanilla.js";
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(120, 30));
  const shadowText = node.addComponent(ShadowText);
  shadowText.setText("SCORE");
  shadowText.setShadow("#000000", 0.8, 3, 4);
  const drawnRect = Rect.zero();
  const isKnown = shadowText.calculateDrawnRect(drawnRect);
  console.log(isKnown, drawnRect.size.y > 30);
  ```

- `detach(node)` (method, since 0.4.0): Detached from a node. (releases the shared baked image)
  - `node` ComponentNode: The node being detached.
- `bakeText(graphic, text, fitWidth, bakeScale, textDirection, fontFace)` (method, since 0.4.0): Bake. (painting the text 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. (the previous one is collected, and its texture is collected with it)
  - `graphic` Graphic: Graphics. (measures the text width at the initial size)
  - `text` string: Text to bake.
  - `fitWidth` number: Width to fit. (no fitting if 0 or less)
  - `bakeScale` number: Bake scale. (Graphic.calculateTextBakeScale)
  - `textDirection` string: Bake direction. ("ltr" or "rtl")
  - `fontFace` FontFace | null: Font. (system font if null)
- `measureTextWidth(graphic)` (method, since 0.4.0): Measures the text width. (at the initial size, same measurement as Text, in content-size units)
  - `graphic` Graphic: Graphics. (uses its text measure cache)
  - returns number: Text width. (content size units, 0 if the text is empty)
- `setText(text)` (method, since 0.4.0): Sets the text.
  - `text` string: Single line of text to show.
- `getText()` (method, since 0.4.0): Returns the text.
  - returns string: Text.
- `setFontFace(fontFace)` (method, since 0.4.0): Sets the font. (system font if null)
  - `fontFace` FontFace | null: Font. (system font if null)
- `getFontFace()` (method, since 0.4.0): Returns the font.
  - returns FontFace | null: Font. (system font if null)
- `setTextStyle(fontSize, isBold, textColor)` (method, since 0.4.0): Sets the text style. (initial size, weight, color)
  - `fontSize` number: Initial font size. (content size units, baked smaller when it overflows)
  - `isBold` boolean: Whether bold.
  - `textColor` Color | string: Text colour. (a Color or a colour string such as "#rrggbb", the alpha is the text opacity)
- `getFontSize()` (method, since 0.4.0): Returns the initial font size.
  - returns number: Initial font size.
- `setTextAlign(textAlign)` (method, since 0.4.0): Sets the horizontal alignment.
  - `textAlign` string: "left", "center", "right", "start", "end"
- `getTextAlign()` (method, since 0.4.0): Returns the horizontal alignment.
  - returns string: Horizontal alignment. ("left", "center", "right", "start", "end")
- `setTextDirection(textDirection)` (method, since 0.4.0): Sets the text direction. ("ltr", "rtl", "auto")
  - `textDirection` string: Text direction. ("ltr", "rtl", "auto", other values bake as "ltr")
- `getTextDirection()` (method, since 0.4.0): Returns the text direction.
  - returns string: Text direction. ("ltr", "rtl", "auto")
- `resolveBakeTextDirection()` (method, since 0.4.0): Actual baking direction. ("rtl" is right to left, "auto" uses the first strong character of the text, anything else is "ltr")
  - returns string: "ltr" or "rtl".
- `isBold()` (method, since 0.4.0): Whether bold.
  - returns boolean: True if bold.
- `setItalic(isItalic)` (method, since 0.4.0): Sets italic.
  - `isItalic` boolean: Whether italic. (italic only when true)
- `isItalic()` (method, since 0.4.0): Whether italic.
  - returns boolean: True if italic.
- `setUnderline(isUnderline)` (method, since 0.4.0): Sets underline.
  - `isUnderline` boolean: Whether to underline. (only when true)
- `isUnderline()` (method, since 0.4.0): Whether underlined.
  - returns boolean: True if underlined.
- `setStrikethrough(isStrikethrough)` (method, since 0.4.0): Sets strikethrough.
  - `isStrikethrough` boolean: Whether to strike through. (only when true)
- `isStrikethrough()` (method, since 0.4.0): Whether strikethrough.
  - returns boolean: True if struck through.
- `setShadow(shadowColor, shadowOpacity, shadowOffset, shadowBlur = 0)` (method, since 0.4.0): Sets the shadow. (color, opacity, offset below the text, 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 text. (content size units)
  - `shadowBlur` number: No blur if 0.
- `setFitMinimumRatio(fitMinimumRatio)` (method, since 0.4.0): Sets the minimum ratio to shrink to on overflow. (no shrinking if 1)
  - `fitMinimumRatio` number: Smallest ratio to shrink to when overflowing. (clamped to 0.01 ~ 1, no shrinking if 1)
- `getFitMinimumRatio()` (method, since 0.4.0): Returns the minimum ratio to shrink to on overflow.
  - returns number: Smallest ratio to shrink to when overflowing.
- `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)
