Tempusxr Français
Menu
  1. Docs
  2. Image tracking examples

Interactive

Touch the 3D content. Tap the cube to change its color, tap the ball to make it jump. The tap goes through the transparent canvas to the objects on the image.

Try it

Open the demos on a phone: the camera needs a secure page. Then point the camera at the picture below, shown on a second screen or printed.

Picture to point the camera at: poster.png

A-Frame

Add cursor="rayOrigin: mouse" and raycaster="objects: .clickable" to the scene, and the class clickable to what can be tapped. A tap sends a click event to the entity, as for any element.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
    <title>Interactive (A-Frame)</title>
    <!-- A-Frame, Tempusxr and the image tracker, as plain script tags. -->
    <script src="/node_modules/aframe/dist/aframe-v1.8.0.min.js"></script>
    <script src="/packages/sdk-aframe/dist/index.umd.cjs"></script>
    <script src="/packages/sdk-tracker-image/dist/index.umd.cjs"></script>
  </head>
  <body>
    <!-- Show poster.png, then tap the yellow cube (new color) or the blue ball (it jumps). -->
    <a-scene
      tempusxr="tracker: image; targets: /examples/poster-video/poster.txr"
      cursor="rayOrigin: mouse"
      raycaster="objects: .clickable"
      renderer="alpha: true"
      vr-mode-ui="enabled: false"
    >
      <a-camera></a-camera>
      <a-light type="ambient" intensity="0.9"></a-light>
      <a-light type="directional" position="0.5 1 1" intensity="0.8"></a-light>

      <a-entity tempusxr-image-target="index: 0">
        <a-box
          id="cube"
          class="clickable"
          position="-0.2 0 0.15"
          scale="0.25 0.25 0.25"
          color="#fecb02"
        ></a-box>
        <a-sphere
          id="ball"
          class="clickable"
          position="0.2 0 0.15"
          radius="0.13"
          color="#6ec1ff"
        ></a-sphere>
      </a-entity>
    </a-scene>

    <script>
      const colors = ['#fecb02', '#ff5e62', '#8b4bb5', '#3d7a68'];
      let next = 1;
      document.getElementById('cube').addEventListener('click', (event) => {
        event.target.setAttribute('color', colors[next++ % colors.length]);
      });
      document.getElementById('ball').addEventListener('click', (event) => {
        event.target.setAttribute(
          'animation__jump',
          'property: position; from: 0.2 0 0.15; to: 0.2 0 0.45; dir: alternate; dur: 250; loop: 2; easing: easeOutQuad',
        );
      });
    </script>
  </body>
</html>

Three.js

A tap is a ray from the camera through that point of the screen: raycaster.setFromCamera(pointer, ar.stage.camera), then intersectObjects() tells which object was hit.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
    <title>Interactive (Three.js)</title>
    <!-- Three.js with the SDK, as a page without a bundler loads it. -->
    <script type="importmap">
      {
        "imports": {
          "three": "/node_modules/three/build/three.module.js",
          "@tempusxr/sdk-core": "/packages/sdk-core/dist/index.js",
          "@tempusxr/sdk-three": "/packages/sdk-three/dist/index.js",
          "@tempusxr/sdk-tracker-image": "/packages/sdk-tracker-image/dist/index.js"
        }
      }
    </script>
    <style>
      html,
      body {
        margin: 0;
        height: 100%;
        background: #111;
      }
    </style>
  </head>
  <body>
    <!-- Show poster.png, then tap the yellow cube (new color) or the blue ball (it jumps). -->
    <div id="ar"></div>
    <script type="module">
      import * as THREE from 'three';
      import { create } from '@tempusxr/sdk-three';
      import '@tempusxr/sdk-tracker-image';

      const ar = create({ container: '#ar' });
      window.__ar = ar;
      const tracker = await ar.use('image', { targets: ['/examples/poster-video/poster.txr'] });

      const content = new THREE.Group();
      content.add(new THREE.AmbientLight(0xffffff, 1.2));
      const sun = new THREE.DirectionalLight(0xffffff, 1.5);
      sun.position.set(0.5, 1, 1);
      content.add(sun);
      const cube = new THREE.Mesh(
        new THREE.BoxGeometry(0.25, 0.25, 0.25),
        new THREE.MeshStandardMaterial({ color: '#fecb02' }),
      );
      cube.position.set(-0.2, 0, 0.15);
      const ball = new THREE.Mesh(
        new THREE.SphereGeometry(0.13, 32, 16),
        new THREE.MeshStandardMaterial({ color: '#6ec1ff' }),
      );
      ball.position.set(0.2, 0, 0.15);
      content.add(cube, ball);
      ar.attach(tracker.anchors[0], content);

      // A tap is a ray from the camera through that point of the screen.
      const raycaster = new THREE.Raycaster();
      const pointer = new THREE.Vector2();
      const canvas = ar.stage.renderer.domElement;
      const colors = ['#fecb02', '#ff5e62', '#8b4bb5', '#3d7a68'];
      let next = 1;
      let jumpStart = -1;
      canvas.addEventListener('pointerdown', (event) => {
        const box = canvas.getBoundingClientRect();
        pointer.set(
          ((event.clientX - box.left) / box.width) * 2 - 1,
          -((event.clientY - box.top) / box.height) * 2 + 1,
        );
        raycaster.setFromCamera(pointer, ar.stage.camera);
        const hit = raycaster.intersectObjects([cube, ball])[0]?.object;
        if (hit === cube) cube.material.color.set(colors[next++ % colors.length]);
        if (hit === ball) jumpStart = performance.now();
      });
      ar.session.onFrame(() => {
        const t = (performance.now() - jumpStart) / 500;
        ball.position.z = jumpStart > 0 && t < 1 ? 0.15 + Math.sin(t * Math.PI) * 0.3 : 0.15;
      });

      await ar.start();
    </script>
  </body>
</html>

What to remember