# deep2universe/youtube-motion-tracking architecture

> Architecture diagram of the GitHub repository deep2universe/youtube-motion-tracking, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/deep2universe/youtube-motion-tracking
- Repository: https://github.com/deep2universe/youtube-motion-tracking

## Overview

1. **What this repo is**
   - This is a **Chrome Extension Manifest V3** project that augments the YouTube page itself. The runtime is split between a **content script injected into YouTube**, a **background service worker**, and extension UI surfaces like the **popup** and **options page**.

2. **Primary runtime boundary**
   - The main boundary is **“extension code” vs “YouTube page DOM”**. `src/content.js` is the central orchestrator that runs in the YouTube tab, reads the player/video state, injects overlays and styles, and coordinates pose tracking, animations, filters, and game mode.

3. **Page overlay and DOM integration**
   - `src/content.css` plus the `src/themes/*.css` files define the visual layer that is injected into the YouTube page. This includes Halloween UI theming, overlay containers, particle layers, and player-adjacent controls. The theme is additive: it changes YouTube’s look without replacing core YouTube functionality.

4. **Pose detection and motion analysis**
   - The AI pipeline centers on **TensorFlow.js MoveNet**. `src/detectUtils.js`, `src/motionDetector.js`, `src/motionDetectors.js`, `src/motionPatterns.js`, `src/motionEnum.js`, and `src/gameConfig.js` handle keypoint interpretation, motion classification, and movement pattern thresholds.
   - Data flow here is: **video frame / pose keypoints → motion interpretation → event triggers**. This is the foundation for both animations and game mode.

5. **Animation subsystem**
   - `src/anim.js` is the main rendering/animation engine, with `src/animEnum.js` defining the available effects. `src/jumpMarkers.js` and `src/ghostCharacter.js` support visual reactions and game feedback.
   - The README indicates a dual rendering strategy using **Canvas 2D and WebGL**, so the diagram should show overlays being drawn over the YouTube player, not inside the video itself.

6. **Effects composition**
   - `src/filterEnum.js` defines horror video filters, which are visually separate from motion-triggered animations but can be applied together. This means the architecture has **two visual effect paths**:
     - **motion-driven overlays/animations**
     - **video-wide filters / style transforms**
   - The system is designed so these layers can stack.

7. **Motion Game Mode**
   - `src/gameMode.js` and `src/gameModeEnum.js` implement a stateful “game” layer on top of motion tracking. Rather than animating the page directly, it translates detected movement into gameplay events like ghost jumps and scoring.
   - Conceptually, this is a separate subsystem that consumes motion events and emits game state updates, score persistence, and feedback effects.

8. **Extension UI and configuration**
   - `src/popup.html/js` and `src/options.html/js` are the user control surfaces. The popup appears to be the primary in-player control for selecting animations, filters, theme settings, and game mode; the options page handles persistent configuration.
   - These pages are likely connected to content-script state through Chrome extension messaging and storage APIs.

9. **Background/service worker role**
   - `src/background.js` is the MV3 service worker. The README says it monitors URLs, so its job is likely to keep extension state aligned with tab navigation and YouTube page changes, not to render UI or run detection.

10. **State and persistence**
   - Persistent user choices and game stats are part of the architecture even if the exact storage layer is not visible from the tree. The presence of popup/options pages, game scoring, and theme intensity controls implies a **Chrome storage-backed settings layer** used by the content script and UI.

11. **Build and delivery pipeline**
   - The repo is bundled with **Parcel** (`package.json`, `package-lock.json`), producing a distributable extension in `dist/`.
   - `build-for-store.sh` packages a release ZIP for Chrome Web Store-style distribution, while `manifest.json` in `src/` defines the extension entrypoints and permissions.

12. **Non-runtime support assets**
   - `debug_scripts/`, `dev_docs/`, and `.kiro/specs/` are support and planning artifacts rather than runtime subsystems. They document diagnosis, fixes, architecture decisions, and testing, which is useful for understanding evolution but should not be drawn as active components in the runtime diagram.

13. **External dependencies that matter architecturally**
   - **YouTube** is the host environment the extension hooks into.
   - **TensorFlow.js / MoveNet** is the AI inference dependency.
   - **Proton Engine** is used for particle-style visual effects.
   - **WebGL + Canvas 2D** are the rendering backends for overlays and effects.

14. **High-level data flow to diagram**
   - User action in popup/options or page state on YouTube → extension state changes → content script coordinates detection/rendering → pose/motion modules emit signals → animation/filter/game subsystems render results over the player → background service worker keeps tab/navigation state in sync.

Main entry points: [`src/content.js`](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/content.js) (Content script), [`src/motionDetector.js`](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/motionDetector.js) (Motion detector), [`src/manifest.json`](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/manifest.json) (Manifest), [`src/gameMode.js`](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/gameMode.js) (Game mode)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Extension runtime"]
  node_content["Content script<br/>orchestrator<br/>[content.js]"]
  node_background["Service worker<br/>mv3 worker<br/>[background.js]"]
  node_manifest["Manifest<br/>entrypoints<br/>[manifest.json]"]
end

subgraph group_motion["Motion pipeline"]
  node_detect["Pose utils<br/>keypoint utils<br/>[detectUtils.js]"]
  node_motiondetector["Motion detector<br/>classifier<br/>[motionDetector.js]"]
  node_motiondetectors["Detector set<br/>pattern engines<br/>[motionDetectors.js]"]
  node_patterns["Motion patterns<br/>threshold rules<br/>[motionPatterns.js]"]
  node_motionenum["Motion types<br/>enum<br/>[motionEnum.js]"]
  node_gameconfig["Game config<br/>tuning<br/>[gameConfig.js]"]
  node_gamemode["Game mode<br/>state machine<br/>[gameMode.js]"]
  node_gameenum["Game states<br/>enum<br/>[gameModeEnum.js]"]
end

subgraph group_visuals["Visual effects"]
  node_anim["Animator<br/>render engine<br/>[anim.js]"]
  node_animenum["Effects enum<br/>[animEnum.js]"]
  node_filters["Video filters<br/>effect catalog<br/>[filterEnum.js]"]
  node_markers["Jump markers<br/>feedback layer<br/>[jumpMarkers.js]"]
  node_ghost["Ghost character<br/>feedback actor<br/>[ghostCharacter.js]"]
  node_contentcss["Overlay styles<br/>page styling<br/>[content.css]"]
  node_themecss["Theme layer<br/>css theme<br/>[youtube-theme.css]"]
end

subgraph group_ui["User surfaces"]
  node_popup["Popup UI<br/>extension popup<br/>[popup.html]"]
  node_popupjs["Popup logic<br/>ui controller<br/>[popup.js]"]
  node_options["Options page<br/>settings UI<br/>[options.html]"]
  node_optionsjs["Options logic<br/>ui controller<br/>[options.js]"]
end

node_youtube["YouTube<br/>host page"]
node_build["Store build<br/>packaging<br/>[build-for-store.sh]"]

node_manifest -->|"loads"| node_content
node_manifest -->|"loads"| node_background
node_manifest -->|"opens"| node_popup
node_manifest -->|"opens"| node_options
node_content -->|"augments"| node_youtube
node_background -.->|"syncs"| node_content
node_popupjs -.->|"controls"| node_content
node_optionsjs -.->|"persists"| node_content
node_content -->|"feeds"| node_detect
node_detect -->|"normalizes"| node_motiondetector
node_motiondetector -->|"delegates"| node_motiondetectors
node_motiondetectors -->|"matches"| node_patterns
node_motiondetector -->|"emits"| node_motionenum
node_motiondetector -.->|"tuned by"| node_gameconfig
node_motiondetector -->|"triggers"| node_gamemode
node_gamemode -->|"states"| node_gameenum
node_content -->|"drives"| node_anim
node_anim -->|"selects"| node_animenum
node_content -->|"applies"| node_filters
node_contentcss -->|"styles"| node_youtube
node_themecss -->|"themes"| node_youtube
node_gamemode -->|"feedback"| node_markers
node_gamemode -->|"feedback"| node_ghost
node_build -.->|"packages"| node_manifest

click node_content "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/content.js"
click node_background "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/background.js"
click node_manifest "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/manifest.json"
click node_detect "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/detectUtils.js"
click node_motiondetector "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/motionDetector.js"
click node_motiondetectors "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/motionDetectors.js"
click node_patterns "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/motionPatterns.js"
click node_motionenum "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/motionEnum.js"
click node_gameconfig "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/gameConfig.js"
click node_gamemode "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/gameMode.js"
click node_gameenum "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/gameModeEnum.js"
click node_anim "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/anim.js"
click node_animenum "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/animEnum.js"
click node_filters "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/filterEnum.js"
click node_markers "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/jumpMarkers.js"
click node_ghost "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/ghostCharacter.js"
click node_contentcss "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/content.css"
click node_themecss "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/themes/youtube-theme.css"
click node_popup "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/popup.html"
click node_popupjs "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/popup.js"
click node_options "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/options.html"
click node_optionsjs "https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/options.js"
click node_build "https://github.com/deep2universe/youtube-motion-tracking/blob/master/build-for-store.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_content,node_background,node_manifest toneBlue
class node_detect,node_motiondetector,node_motiondetectors,node_patterns,node_motionenum,node_gameconfig,node_gamemode,node_gameenum toneAmber
class node_anim,node_animenum,node_filters,node_markers,node_ghost,node_contentcss,node_themecss toneMint
class node_popup,node_popupjs,node_options,node_optionsjs toneRose
class node_youtube,node_build toneNeutral
```

## Components

### Extension runtime

Core MV3 code that runs in the YouTube tab and in the service worker.

- [Content script](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/content.js): orchestrator · Runs in the YouTube tab, reads player state, injects overlays, and coordinates subsystems. · `src/content.js`
- [Service worker](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/background.js): mv3 worker · Background entrypoint that keeps extension state aligned with tab and URL changes. · `src/background.js`
- [Manifest](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/manifest.json): entrypoints · Defines extension permissions, pages, and runtime wiring. · `src/manifest.json`

### Motion pipeline

Pose detection, motion interpretation, and game-event generation.

- [Pose utils](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/detectUtils.js): keypoint utils · Normalizes pose keypoints and detection helpers for downstream motion logic. · `src/detectUtils.js`
- [Motion detector](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/motionDetector.js): classifier · Interprets keypoints and identifies motion signals from the video feed. · `src/motionDetector.js`
- [Detector set](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/motionDetectors.js): pattern engines · Collection of motion detectors used for specific movement types. · `src/motionDetectors.js`
- [Motion patterns](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/motionPatterns.js): threshold rules · Pattern definitions and thresholds used to recognize gestures and actions. · `src/motionPatterns.js`
- [Motion types](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/motionEnum.js): enum · Shared motion event vocabulary used across the pipeline. · `src/motionEnum.js`
- [Game config](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/gameConfig.js): tuning · Thresholds and tuning values for game-mode motion behavior. · `src/gameConfig.js`
- [Game mode](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/gameMode.js): state machine · Consumes motion events and turns them into gameplay, scoring, and reset logic. · `src/gameMode.js`
- [Game states](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/gameModeEnum.js): enum · Game-mode state and mode definitions. · `src/gameModeEnum.js`

### Visual effects

Overlay rendering, animations, filters, and themed styling layered on top of YouTube.

- [Animator](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/anim.js): render engine · Main animation engine that renders motion-reactive overlays and feedback. · `src/anim.js`
- [Effects enum](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/animEnum.js): enum · Available animation effect definitions. · `src/animEnum.js`
- [Video filters](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/filterEnum.js): effect catalog · Horror-style video-wide filters and style transforms that can stack with animations. · `src/filterEnum.js`
- [Jump markers](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/jumpMarkers.js): feedback layer · Visual markers used for jump and reaction feedback. · `src/jumpMarkers.js`
- [Ghost character](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/ghostCharacter.js): feedback actor · Game-feedback character used for motion-driven reactions. · `src/ghostCharacter.js`
- [Overlay styles](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/content.css): page styling · Base injected CSS for overlays, controls, and player-adjacent visuals. · `src/content.css`
- [Theme layer](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/themes/youtube-theme.css): css theme · YouTube theme variables, particles, colors, and additive Halloween styling. · `src/themes/youtube-theme.css`

### User surfaces

Popup and options pages for control and persistence.

- [Popup UI](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/popup.html): extension popup · Primary in-player control surface for animations, filters, themes, and game mode. · `src/popup.html`
- [Popup logic](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/popup.js): ui controller · Popup behavior and state synchronization logic. · `src/popup.js`
- [Options page](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/options.html): settings UI · Persistent configuration surface for extension preferences. · `src/options.html`
- [Options logic](https://github.com/deep2universe/youtube-motion-tracking/blob/master/src/options.js): ui controller · Options-page behavior and storage-backed settings handling. · `src/options.js`

### Other components

- **YouTube**: host page · The embedded host environment that the extension augments.
- [Store build](https://github.com/deep2universe/youtube-motion-tracking/blob/master/build-for-store.sh): packaging · Release packaging script used to produce distributable extension builds. · `build-for-store.sh`

## Connections

- Manifest → Content script: loads · Registers the content script as the main YouTube-tab runtime.
- Manifest → Service worker: loads · Registers the service worker entrypoint.
- Manifest → Popup UI: opens · Connects the popup surface to the extension action.
- Manifest → Options page: opens · Connects the options surface to extension settings.
- Content script → YouTube: augments · Reads player state and injects overlays into the host page.
- Service worker → Content script: syncs · Keeps tab and navigation state aligned with the active YouTube page.
- Popup logic → Content script: controls · Updates runtime modes and selected effects.
- Options logic → Content script: persists · Writes and restores configuration used by the content script.
- Content script → Pose utils: feeds · Supplies video and pose data to detection helpers.
- Pose utils → Motion detector: normalizes · Prepares keypoints for interpretation.
- Motion detector → Detector set: delegates · Uses specialized detectors for different movement types.
- Detector set → Motion patterns: matches · Applies pattern thresholds and gesture rules.
- Motion detector → Motion types: emits · Maps recognized actions into shared motion event types.
- Motion detector → Game config: tuned by · Uses game-specific thresholds and sensitivity settings.
- Motion detector → Game mode: triggers · Motion events become gameplay actions and resets.
- Game mode → Game states: states · Uses game-state definitions to manage progression.
- Content script → Animator: drives · Routes motion and UI state into the animation engine.
- Animator → Effects enum: selects · Chooses from available animation effects.
- Content script → Video filters: applies · Applies video-wide visual filters alongside animations.
- Overlay styles → YouTube: styles · Injects overlay and player-adjacent styling into the page.
- Theme layer → YouTube: themes · Adds additive YouTube theming and particle visuals.
- Game mode → Jump markers: feedback · Uses jump markers as motion-game feedback.
- Game mode → Ghost character: feedback · Uses the ghost character for game reactions.
- Store build → Manifest: packages · Bundles the extension using the manifest-defined entrypoints.
