Tempusxr Français
  1. Docs
  2. Experience Library
  3. Interactive

Interactive Image tracking

Tap the 3D objects: change a color, make a ball jump.

Open in A-FrameOpen in Three.jsBack to the Experience Library

Code

The code of this experience, split in the files you would keep: HTML, CSS and JavaScript. The live page keeps them in one file; copy each part into its own file and the page works the same.

<!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 src="page.js"></script>
  </body>
</html>
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',
  );
});
<!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>
    <link rel="stylesheet" href="page.css" />
  </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" src="page.js"></script>
  </body>
</html>
html,
body {
  margin: 0;
  height: 100%;
  background: #111;
}
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();

Open on your phone

Scan this code with the phone camera.

Open on this device