# emoji

Emoji or small pictures that float up from a like button, burst out of a success state or rain over a section.

count emoji pop in with a slight overshoot and fly for duration, then fade and shrink over the last third of their life. float sends them up from the top of the element in a gentle stream, swaying and rocking, like reactions on a live stream; burst throws them out of the box in every direction, slowed by the air; fountain shoots them up in a cone and lets them fall back; rain lets them fall through the element’s part of the viewport over its width. Every emoji keeps moving with the page as it scrolls. The list is drawn once with the system emoji font, so the emoji look native on every device; pictures of the list are drawn the same way. The element stays as it is.

Module `particles`, category Celebration and particles. Import `@dioramajs/particles/emoji`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/particles/src/emoji.ts)

A live stream with a like button that sends hearts floating up over the picture on every tap.

```html title="index.html"
<div class="ej-stream">
  <div class="ej-screen" role="img" aria-label="A live stream of a rooftop rehearsal at dusk">
    <span class="ej-live">Live</span>
    <span class="ej-viewers">2.4K watching</span>
  </div>
  <div class="ej-bar">
    <p class="ej-title">Rooftop rehearsal, set two</p>
    <button type="button" class="ej-like" aria-pressed="false" data-dio="emoji" data-dio-origin="pointer">❤️ <span class="ej-count">318</span></button>
  </div>
</div>
```

## Usage

Put it on: any element: a like or reaction button, a form, a section.

`start()` registers `emoji`, so the attribute alone puts it on an element.

```html
<button type="button" class="like" data-dio="emoji" aria-pressed="false">Like</button>
```

In page transitions: The emoji fade with the page as it leaves.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `trigger` (rebuilds) | `data-dio-trigger` | `'click' \| 'view' \| 'event' \| 'manual' \| 'interval'` | `'click'` | click; view once half the element is on screen; a DOM event; manual through fire; interval, an emission every interval seconds while the element is on screen. |
| `motion` (rebuilds) | `data-dio-motion` | `'float' \| 'burst' \| 'rain' \| 'fountain'` | `'float'` | float rises from the element, for likes and reactions; burst flies out of it all round, for a success state; fountain shoots up and falls back, for a win or a reward; rain falls over the element from its top, for a seasonal section or a thank-you page. |
| `emoji` (rebuilds) | `data-dio-emoji` | `string` | `'❤️'` | Emoji, symbols or words separated by spaces; a run of emoji without spaces counts as one each. A URL or a file name ending in .svg, .png, .webp and the like is a picture. Up to 16 different items; each emoji picks one at random. |
| `event` (rebuilds) | `data-dio-event` | `string` | `''` | Name of the DOM event on the element for the event trigger; its detail may pick one emoji of the list and a count up to count. |
| `fire` | `data-dio-fire` | `number` 0 to 10 | `0` | Every increase emits once with the manual trigger. |
| `once` | `data-dio-once` | `boolean` | `false` | At most one emission per page. |
| `origin` (rebuilds) | `data-dio-origin` | `'center' \| 'pointer'` | `'center'` | center: out of the box; pointer: from the click or the pointer. Rain ignores it. |
| `count` | `data-dio-count` | `number` 0 to 300 | `8` | Emoji per emission; 0 emits none. At most low 12, medium 24. |
| `scale` | `data-dio-scale` | `number` 0.25 to 5 | `1` | Size of the emoji over a base of 32 px, give or take a fifth. |
| `speed` | `data-dio-speed` | `number` 0 to 3 | `1` | Speed of the motion as a factor; 0 pops the emoji in place. |
| `spread` | `data-dio-spread` | `number` 0 to 360° | `40` | Width of the cone for float and fountain; burst flies all round. |
| `sway` | `data-dio-sway` | `number` 0 to 80 px | `12` | Side to side sway; 0 flies straight. |
| `spin` | `data-dio-spin` | `number` 0 to 4 | `1` | Rocking and spin as a factor; 0 keeps the emoji upright. |
| `duration` | `data-dio-duration` | `number` 0.4 to 8 s | `2.4` | Life of an emoji. |
| `interval` | `data-dio-interval` | `number` 0 to 10 s | `1.5` | Time between two emissions of the interval trigger; 0 stops them. |
| `seed` (rebuilds) | `data-dio-seed` | `number` 1 to 100 | `1` | Seed of the randomness. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `motion`: `float`, `burst`, `rain`, `fountain`
  - `float` rises from the element, for likes and reactions.
  - `burst` flies out of it all round, for a success state.
  - `fountain` shoots up and falls back, for a win or a reward.
  - `rain` falls over the element from its top, for a seasonal section or a thank-you page.
- `trigger`: `click`, `view`, `event`, `manual`, `interval`
  - `view` once half the element is on screen.
  - `manual` through fire.
  - `interval` an emission every interval seconds while the element is on screen.
- `origin`: `center`, `pointer`

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`presence`](/docs/api/inputs#presence), [`width`](/docs/api/inputs#input-width), [`x`](/docs/api/inputs#input-x), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. Only while emoji fly; none at rest. The interval trigger pauses while the element is off screen. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it is off and the element shows as plain HTML. Off: neither the module nor the engine loads for it, and the page is unchanged.

Runs on every device. A tap emits like a click.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { emoji: { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 4.5 KB the first time a page uses it. GPU: One instanced draw for a pool of 300 emoji. Memory: A glyph atlas of at most 1024 x 1024 px and the pool’s attributes, about 15 KB.

- low: At most 12 emoji an emission.
- medium: At most 24 emoji an emission.

See [the quality plugin](/docs/effects/quality).

## Recipes

### Reactions from other viewers

A stream or a webinar gets reactions from its audience over a websocket. Put one effect on the reaction bar with the `event` trigger and the list of reactions, and dispatch an event for every message. Its `detail` names the emoji to send and how many, up to `count`; an emoji missing from the list sends a random one of it.

A reaction bar on a dark stream page with thumbs, hearts, laughs and surprised faces floating up from it as other viewers react.

```html title="index.html"
<div class="ej-feed">
  <p class="ej-note">Reactions from other viewers float up from the bar.</p>
  <div class="ej-picks" data-dio="emoji" data-dio-trigger="event" data-dio-event="reaction" data-dio-emoji="👍 ❤️ 😂 😮" data-dio-count="3" data-dio-spread="60">
    <button type="button" data-react="👍" aria-label="Thumbs up">👍</button>
    <button type="button" data-react="❤️" aria-label="Love">❤️</button>
    <button type="button" data-react="😂" aria-label="Laughing">😂</button>
    <button type="button" data-react="😮" aria-label="Surprised">😮</button>
  </div>
</div>
```

```html title="panel.html"
<p>Other viewers react every 0.7 s. Tap a reaction to send three of it.</p>
```

```ts title="script.ts"
// Stands in for the reactions channel of a stream: other viewers react now and then, and every
// message becomes one event on the bar. A tap on a reaction goes the same way, three of it.
export default function mount(example: HTMLElement): () => void {
  const feed = example.querySelector<HTMLElement>('.ej-picks')
  if (!feed) return () => {}
  const react = (emoji: string, count: number) =>
    feed.dispatchEvent(
      new CustomEvent('reaction', { detail: { emoji, count } }),
    )
  const onPick = (e: Event) => {
    const emoji = (e.target as Element).closest<HTMLElement>(
      '[data-react]',
    )?.dataset.react
    if (emoji) react(emoji, 3)
  }
  const others = ['👍', '❤️', '❤️', '😂', '😮']
  const timer = window.setInterval(() => {
    react(others[Math.floor(Math.random() * others.length)] ?? '❤️', 1)
  }, 700)
  feed.addEventListener('click', onPick)
  return () => {
    clearInterval(timer)
    feed.removeEventListener('click', onPick)
  }
}
```

```ts title="src/reactions.ts"
const bar = document.querySelector('.reactions') as HTMLElement
const socket = new WebSocket('wss://example.com/live/reactions')

socket.addEventListener('message', (message) => {
  const { emoji } = JSON.parse(message.data) as { emoji: string }
  bar.dispatchEvent(
    new CustomEvent('reaction', { detail: { emoji, count: 1 } }),
  )
})
```

### A burst when the payment goes through

A click on Pay is too early: the payment can still fail. Use the `event` trigger with `burst`, and dispatch the event once your server confirms. Say what happened in the page too; the emoji are decoration.

```html
<section
  class="checkout"
  data-dio="emoji"
  data-dio-trigger="event"
  data-dio-event="paid"
  data-dio-motion="burst"
  data-dio-emoji="🎉 🥳 ✨"
  data-dio-count="24"
>
  <button type="submit">Pay $42</button>
  <p role="status"></p>
</section>
```

### Seasonal rain over a section

The `interval` trigger emits every `interval` seconds while the section is on screen and pauses while it is not. With `rain`, the emoji fall through the section over its width, and a few at a time make a steady rain.

```html
<section
  class="sale"
  data-dio="emoji"
  data-dio-trigger="interval"
  data-dio-motion="rain"
  data-dio-emoji="🎃🦇🍬"
  data-dio-count="5"
  data-dio-interval="0.8"
  data-dio-duration="3"
>
  <h2>Halloween sale</h2>
</section>
```

A run of emoji with no spaces counts as one emoji each, so `🎃🦇🍬` is a list of three.

### Your own icons

A word of the list that is a URL or a file name ending in `.svg`, `.png`, `.webp` and the like is drawn as a picture. Keep them small and square; a picture from another origin needs CORS headers, or it is left out with a warning in development.

```html
<button
  type="button"
  data-dio="emoji"
  data-dio-motion="fountain"
  data-dio-emoji="/icons/coin.svg /icons/star.svg"
  data-dio-count="16"
>
  Claim reward
</button>
```

## Notes

The effect never cancels the click: a like still counts and a form still submits. When the site swaps the button for a “Liked” label or hides it in the same click, the emoji fly on from where the button was and then stop.

The emoji are drawn once with the system emoji font, so they look like the visitor’s own emoji on Apple, Windows and Android devices. They draw on the canvas over the page and never take a click. Up to 16 different emoji or pictures make one list.

Every emoji moves with the page as it scrolls. Frames run only while emoji fly; once their element is scrolled far away they stop at once. All of them are one instanced draw from a pool of 300, and the quality tier caps `count` at 12 on the low tier and 24 on the medium one.

Under reduced motion neither the module nor the engine loads for it, and the element stays as it is.

`trigger`, `motion`, `emoji`, `event`, `origin` and `seed` rebuild the effect when they change. `scale`, `sway` and `spin` change the emoji in the air too; counts, speeds and times apply to the next emission.

## Related

- [confetti](/docs/effects/confetti.md): A burst of confetti from an element, a shower or fireworks to celebrate a sign-up, an order or a donation.
- [drops](/docs/effects/drops.md): Water drops on glass over a hero or the whole page: rain that beads and runs, or meltwater left by a thaw.
- [snow](/docs/effects/snow.md): Snow, cherry petals or autumn leaves drifting down over a section or the whole page, for a seasonal campaign.
- [sparkles](/docs/effects/sparkles.md): Small twinkling sparkles around a badge, a "new" label, a premium button or a product highlight.
- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [beam](/docs/effects/beam.md): A light beam that runs around the rounded border of a feature card, an input field or a highlight box.
- [beat](/docs/effects/beat.md): A cover, logo, card or play button that pulses with the music of a page player: it scales, shakes and glows, or sends out rings, on the beat.
- [coin](/docs/effects/coin.md): A coin, medal or token struck from a logo or a photo: it turns, catches the light and flips over.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
