Tempusxr English
  1. Documentation
  2. Bibliothèque d'expériences
  3. Cadre de tableau

Cadre de tableau Suivi d'images

Un cadre 3D et une plaque autour d'un tableau. Changez le style du cadre.

Ouvrir en A-FrameOuvrir en Three.jsRetour à la bibliothèque d'expériences

Code

Le code de cette expérience, séparé en fichiers comme vous les garderiez : HTML, CSS et JavaScript. La page en ligne les garde dans un seul fichier ; copiez chaque partie dans son fichier et la page marche pareil.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
    <title>Tempusxr picture frame</title>
    <!-- Plain HTML, no JavaScript framework: A-Frame, Tempusxr, the image tracker. Needs pnpm build. -->
    <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>
    <link rel="stylesheet" href="page.css" />
  </head>
  <body>
    <!--
      Open art.png on a screen (or print it) and point the camera at it: the picture gets a 3D frame
      and a nameplate. "Change frame" cycles wood, gold, black and white.
    -->
    <div id="ui" class="tx-overlay">
      <div data-tx-slot="top-left" class="pill" data-tx-show="found">Sunset hills</div>
      <div data-tx-slot="center" data-tx-show="scanning" class="pill">
        Point the camera at the painting
      </div>
      <div data-tx-slot="bottom" data-tx-show="found">
        <div class="plate">
          <strong>Sunset hills</strong><br />
          A. Martin, 2026
        </div>
        <button class="tx-cta" type="button" data-tx-action="style" style="margin-top: 10px">
          Change frame
        </button>
      </div>
    </div>

    <a-scene
      tempusxr="tracker: image; targets: art.txr; overlay: #ui"
      renderer="alpha: true"
      vr-mode-ui="enabled: false"
    >
      <a-camera
        position="0 0 0"
        look-controls="enabled: false"
        wasd-controls="enabled: false"
      ></a-camera>
      <a-light type="ambient" intensity="1.1"></a-light>
      <a-light type="directional" position="0.4 1 1" intensity="0.9"></a-light>

      <a-entity tempusxr-image-target="index: 0">
        <!-- The four sides of the frame are built from the real size of the image, see the script -->
        <a-entity id="frame"></a-entity>
        <!-- A faint glass shine over the painting -->
        <a-plane
          tempusxr-image-plane
          width="1"
          height="1.25"
          color="#ffffff"
          opacity="0.07"
          position="0 0 0.01"
        ></a-plane>
      </a-entity>
    </a-scene>

    <script src="page.js"></script>
  </body>
</html>
body {
  margin: 0;
}
.pill {
  padding: 6px 16px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-weight: 700;
}
.plate {
  padding: 10px 22px;
  border-radius: 10px;
  background: rgba(20, 20, 20, 0.7);
  border: 1px solid rgba(212, 175, 55, 0.7);
  color: #f6e7b4;
  text-align: center;
}
const styles = [
  { name: 'Wood', color: '#8b5a2b' },
  { name: 'Gold', color: '#d4af37' },
  { name: 'Black', color: '#1a1a1a' },
  { name: 'White', color: '#f2f2f2' },
];
const THICKNESS = 0.06; // frame width, in image widths
const DEPTH = 0.05;
let current = 0;
let overlay = null;

const paint = () => {
  for (const side of document.querySelectorAll('#frame a-box'))
    side.setAttribute('color', styles[current].color);
};

// Once the image is known, the frame fits it exactly, whatever its proportions.
document.querySelector('a-scene').addEventListener('tempusxr-ready', (event) => {
  overlay = event.detail.overlay;
  const { width: w, height: h } = event.detail.tracker.anchors[0].size;
  const t = THICKNESS;
  const sides = [
    { x: 0, y: h / 2 + t / 2, w: w + 2 * t, h: t },
    { x: 0, y: -h / 2 - t / 2, w: w + 2 * t, h: t },
    { x: -w / 2 - t / 2, y: 0, w: t, h: h },
    { x: w / 2 + t / 2, y: 0, w: t, h: h },
  ];
  const frame = document.getElementById('frame');
  for (const side of sides) {
    const box = document.createElement('a-box');
    box.setAttribute('position', `${side.x} ${side.y} ${DEPTH / 2}`);
    box.setAttribute('width', side.w);
    box.setAttribute('height', side.h);
    box.setAttribute('depth', DEPTH);
    frame.appendChild(box);
  }
  paint();
});

document.getElementById('ui').addEventListener('click', (event) => {
  if (!event.target.closest('[data-tx-action="style"]')) return;
  current = (current + 1) % styles.length;
  paint();
  overlay?.toast(styles[current].name, { color: styles[current].color });
});
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
    <title>Tempusxr picture frame (Three.js)</title>
    <!-- Three.js with the SDK, as a page without a bundler loads it. Needs pnpm build. -->
    <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>
    <!--
      Open art.png on a screen (or print it) and point the camera at it: the picture gets a 3D frame
      and a nameplate. "Change frame" cycles wood, gold, black and white.
    -->
    <div id="ui" class="tx-overlay">
      <div data-tx-slot="top-left" class="pill" data-tx-show="found">Sunset hills</div>
      <div data-tx-slot="center" data-tx-show="scanning" class="pill">
        Point the camera at the painting
      </div>
      <div data-tx-slot="bottom" data-tx-show="found">
        <div class="plate">
          <strong>Sunset hills</strong><br />
          A. Martin, 2026
        </div>
        <button class="tx-cta" type="button" data-tx-action="style" style="margin-top: 10px">
          Change frame
        </button>
      </div>
    </div>
    <div id="ar"></div>
    <script type="module" src="page.js"></script>
  </body>
</html>
html,
body {
  margin: 0;
  height: 100%;
  background: #111;
}
.pill {
  padding: 6px 16px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-weight: 700;
}
.plate {
  padding: 10px 22px;
  border-radius: 10px;
  background: rgba(20, 20, 20, 0.7);
  border: 1px solid rgba(212, 175, 55, 0.7);
  color: #f6e7b4;
  text-align: center;
}
import * as THREE from 'three';
import { create } from '@tempusxr/sdk-three';
import '@tempusxr/sdk-tracker-image';
const ar = create({
  container: '#ar',
  overlay: document.getElementById('ui'),
  cameraOptions: { idealWidth: 640, idealHeight: 480 },
});
window.__ar = ar;
const tracker = await ar.use('image', { targets: ['/examples/art-frame/art.txr'] });
const { width: w, height: h } = tracker.anchors[0]?.size ?? { width: 1, height: 1.25 };

const styles = [
  { name: 'Wood', color: '#8b5a2b' },
  { name: 'Gold', color: '#d4af37' },
  { name: 'Black', color: '#1a1a1a' },
  { name: 'White', color: '#f2f2f2' },
];
const THICKNESS = 0.06; // frame width, in image widths
const DEPTH = 0.05;
const material = new THREE.MeshStandardMaterial({ color: styles[0].color, roughness: 0.6 });

// The four sides of the frame are built from the real size of the image, whatever its proportions.
const t = THICKNESS;
const content = new THREE.Group();
for (const side of [
  { x: 0, y: h / 2 + t / 2, w: w + 2 * t, h: t },
  { x: 0, y: -h / 2 - t / 2, w: w + 2 * t, h: t },
  { x: -w / 2 - t / 2, y: 0, w: t, h },
  { x: w / 2 + t / 2, y: 0, w: t, h },
]) {
  const box = new THREE.Mesh(new THREE.BoxGeometry(side.w, side.h, DEPTH), material);
  box.position.set(side.x, side.y, DEPTH / 2);
  content.add(box);
}
// A faint glass shine over the painting.
const glass = new THREE.Mesh(
  new THREE.PlaneGeometry(w, h),
  new THREE.MeshBasicMaterial({ color: '#ffffff', transparent: true, opacity: 0.07 }),
);
glass.position.z = 0.01;
content.add(glass);
content.add(new THREE.AmbientLight(0xffffff, 1.3));
const sun = new THREE.DirectionalLight(0xffffff, 1.2);
sun.position.set(0.4, 1, 1);
content.add(sun);
ar.attach(tracker.anchors[0], content);

let current = 0;
document.getElementById('ui').addEventListener('click', (event) => {
  if (!event.target.closest('[data-tx-action="style"]')) return;
  current = (current + 1) % styles.length;
  material.color.set(styles[current].color);
  ar.ui?.toast(styles[current].name, { color: styles[current].color });
});
await ar.start();

Ouvrir sur votre téléphone

Scannez ce code avec l'appareil photo du téléphone.

Ouvrir sur cet appareil