# Vanilla HTML & JS example

Base on the schema on [how-does-it-work](https://posetracker.gitbook.io/posetracker-api/use-posetracker-on-real-time-camera-webcam/how-does-it-work "mention") You need to call our URL <https://app.posetracker.com/pose_tracker/tracking> through a **iframe.**

**Base on this here is a tutorial to use PoseTracker in a web app :**

#### Create an HTML file

```
touch test.html
```

#### Integrate PoseTracker

Copy/past this in your test.html and <mark style="color:red;">**modify your API\_KEY**</mark> :

<pre><code>&#x3C;!DOCTYPE html>
&#x3C;html lang="en">
&#x3C;head>
    &#x3C;meta charset="UTF-8">
    &#x3C;meta name="viewport" content="width=device-width, initial-scale=1.0">
    &#x3C;title>PoseTracker Camera Access&#x3C;/title>
    &#x3C;style>
        #poseTrackerFrame {
            width: 350px;
            height: 350px;
            border: none;
        }
        #infoDisplay {
            margin-top: 20px;
        }
    &#x3C;/style>
&#x3C;/head>
&#x3C;body>
<strong>&#x3C;iframe 
</strong>  id="poseTrackerFrame"
  src="https://app.posetracker.com/pose_tracker/tracking?token=<a data-footnote-ref href="#user-content-fn-1">API_KEY</a>&#x26;exercise=squat&#x26;difficulty=easy&#x26;width=350&#x26;height=350&#x26;progression=true"
  allow="camera *;" 
/>
&#x3C;div id="infoDisplay">Allow camera and wait few secondes.&#x3C;/div>
&#x3C;div id="progression">Squat : 0 %&#x3C;/div>
&#x3C;script>
  window.addEventListener('message', function(event) {
    if (event.origin !== "https://app.posetracker.com") return;

    const data = JSON.parse(event.data);
    updateInfoDisplay(data);
  });

  function updateInfoDisplay(info) {
    const infoDisplay = document.getElementById('infoDisplay');
    const progress = document.getElementById('progression');

    // Sorry for that x)
    if (info.type === 'counter') {
      infoDisplay.textContent = 'Counter: ' + info.current_count;
    } else if (info.type === 'progression') {
      progress.textContent = "Squat : " + info.value + " %"
    } else if (info.type === 'posture') {
      infoDisplay.textContent = info.placementMessage
    } else if (info.type === 'initialization') {
      infoDisplay.textContent = info.message
    } else {
      infoDisplay.textContent = 'Info received: ' + JSON.stringify(info);
    }
  }
&#x3C;/script>
&#x3C;/body>
&#x3C;/html>
</code></pre>

#### How does it works ?

* First we have the iframe :

```
<iframe 
  id="poseTrackerFrame"
  src="https://app.posetracker.com/pose_tracker/tracking?token=API_KEY&exercise=squat&difficulty=easy&width=350&height=350&progression=true"
  allow="camera *;" 
/>
```

In src="<https://...?token=><mark style="color:red;">API\_KEY</mark>" you need to set your own API\_KEY

#### 🟧 Important point : D**ata exchange between PoseTracker and your web app 🟧**

PoseTracker page will send information with a poseMessage so you can handle then like this

```
window.addEventListener('message', function(event) {
    // First check if the message come from posetracker
    if (event.origin !== "https://app.posetracker.com") return;

    const data = JSON.parse(event.data);
    updateInfoDisplay(data);
  });
```

#### Then you can handle data received :

```
function updateInfoDisplay(info) {
    const infoDisplay = document.getElementById('infoDisplay');
    const progress = document.getElementById('progression');

    // Sorry for that x)
    if (info.type === 'counter') {
      infoDisplay.textContent = 'Counter: ' + info.current_count;
    } else if (info.type === 'progression') {
      progress.textContent = "Squat : " + info.value + " %"
    } else if (info.type === 'posture') {
      infoDisplay.textContent = info.placementMessage
    } else if (info.type === 'initialization') {
      infoDisplay.textContent = info.message
    } else {
      infoDisplay.textContent = 'Info received: ' + JSON.stringify(info);
    }
  }
```

You can find all the informations returned by [PoseTracker here.](https://posetracker.gitbook.io/posetracker-api/use-posetracker-on-real-time-camera-webcam/tracking-endpoint-message-to-handle)

#### Result

{% embed url="<https://youtu.be/12zcKJmqbx0>" %}

#### You can find source files here

This repo is kept as a reference implementation on GitHub.

We’ll clean and stabilize it over time.

It will remain available as sample code for integration.

{% embed url="<https://github.com/Movelytics/HtmlJSPoseTrackerDemo/tree/main>" %}

[^1]: Set your own <mark style="color:red;">**API\_KEY 🙏**</mark>
