# Getting started

vangle turns a static HTML page into a reactive document. Readers drag numbers
in the text or inside formulas, and everything that depends on them updates as they drag.

It's a modern take on Bret Victor's [Tangle](https://worrydream.com/Tangle/).
You describe the document with a handful of custom HTML elements. You don't write an
`update()` function, and there's no build step and no dependencies.

## Install

Copy `tangle.js` and `tangle.css` into your site, then add two lines to
the page's `<head>`, or to your blog's layout template:

```html
<link rel="stylesheet" href="/tangle.css">
<script type="module" src="/tangle.js"></script>
```

Or load both from jsDelivr instead of copying them:

```html
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/koaning/vangle@v0.1.0/tangle.min.css">
<script type="module" src="https://cdn.jsdelivr.net/gh/koaning/vangle@v0.1.0/tangle.min.js"></script>
```

That's all. KaTeX is only downloaded on pages that contain a [`<t-math>`](t-math.md) formula.

## Your first reactive paragraph

<t-scope class="example" data-show-source="open">
  <p>When you eat <t-num name="cookies" min="0" max="30">3</t-num> cookies, you consume
    <t-out expr="calories">150</t-out> calories. That's
    <t-out expr="calories / 2000" format="percent">8%</t-out> of a recommended daily intake.</p>
  <t-let name="calories" expr="cookies * 50"></t-let>
</t-scope>

Here's what each element does:

- `<t-num name="cookies">` declares a variable called `cookies` and shows it as a
  draggable number. Its text, `3`, is the starting value.
- `<t-let name="calories" expr="cookies * 50">` defines a computed variable. It's invisible.
- `<t-out expr="…">` shows the result of an expression and keeps it up to date.

Try dragging the number. You can also click it to type a value, or focus it and press the arrow keys.

## How it works

### Variables

Every value in the document is a named *variable*. A variable is declared by
[`<t-num>`](t-num.md),
[`<t-var>`](t-var.md),
[`<t-choice>`](t-choice.md),
[`<t-select>`](t-select.md) or
[`<t-text>`](t-text.md) (values the reader can change), or by
[`<t-let>`](t-let.md) and [`<t-call>`](t-call.md) (values computed from other variables).
Names follow JavaScript identifier rules: `rate`, `x2`, `total_cost`.

### Reactivity

Expressions are plain JavaScript (see [Expressions & formats](expressions.md)).
While an expression runs, vangle records which variables it reads. When one of them changes,
only the outputs, computed values and formulas that depend on it update. Changes are batched,
so a drag that moves several values redraws each output once.

### Order doesn't matter

You can use a variable before the element that declares it: in the example above,
`calories` is used before the `<t-let>` that defines it. This lets you
put declarations wherever reads best, for example next to the formula that uses them.

### Scopes

By default, every element on the page shares one set of variables. Wrap part of the page in
[`<t-scope>`](t-scope.md) to give it its own. Each example in these docs
sits in its own scope, so they don't interfere with each other.

### Without JavaScript

The markup degrades gracefully. A `<t-num>` shows its starting value, and a
`<t-out>` shows whatever fallback text you put inside it (the `150` above).
RSS readers and no-JS browsers still get a readable sentence.

## The elements

| Element | Use it for |
|---|---|
| [`<t-num>`](t-num.md) | A number the reader can drag, type or step with the keyboard. |
| [`<t-var>`](t-var.md) | Declaring a variable without showing it, for example one used only in a formula. |
| [`<t-let>`](t-let.md) | A named value computed from other variables. |
| [`<t-out>`](t-out.md) | Showing the live result of an expression in the text. |
| [`<t-choice>`](t-choice.md) | A word the reader clicks to cycle through options, or a toggle. |
| [`<t-select>`](t-select.md) | A word the reader clicks to pick from a searchable menu of options. |
| [`<t-text>`](t-text.md) | A piece of text the reader clicks to rewrite. |
| [`<t-call>`](t-call.md) | A function call whose arguments the reader can change, with the result as a variable. |
| [`<t-tag>`](t-tag.md) | An HTML tag whose attributes the reader can change, with the live element below it. |
| [`<t-math>`](t-math.md) | A KaTeX formula with draggable parameters and live results. |
| [`<t-graph>`](t-graph.md) | A network of nodes and edges that changes with the variables, with the picked and hovered node as variables. |
| [`<t-paint>`](t-paint.md) | A canvas the reader draws on, with the drawing as a variable. |
| [`<t-sketch>`](t-sketch.md) | An infinite whiteboard of vector strokes, with the board as a JSON variable. |
| [`<t-scope>`](t-scope.md) | Giving part of the page its own variables. |

For charts and other custom logic, the same variables are available from
[JavaScript](javascript.md). See [Updating a chart](charts.md) for a walkthrough.
