On this page

A-Frame P5 AR

A-Frame P5 AR is a library created by Craig Kapp that makes it easy to build Augmented Reality (AR) experiences algorithmically using the p5 graphical framework. It is a wrapper around the A-Frame 3D framework and two tracking libraries: AR.js, which follows printed markers, and MindAR, which follows your own pictures. You write a normal p5 sketch, and the 3D objects you create appear on top of the markers and pictures the camera can see.

Please note that this library is used for Augmented Reality experiences only. If you are interested in developing a Virtual Reality experience please visit the documentation page for A-Frame P5.

New to A-Frame P5 AR? Everything you need to start is in the example gallery: 49 ready-to-run examples, from a single box on a marker up to image tracking and an escape room for a whole class, plus printable markers and pictures to track.

Begin by downloading the example gallery

or try the live examples in your browser first, and see Getting Started for how to run them

Version 2.0 (latest stable release, 2026)

Version 1.0 (legacy release, 2024): version 1.0 documentation and example gallery

Version 0.4 (legacy release, 2021): example gallery

What's New in Version 2.0

Version 2.0 is built on the same core as A-Frame P5 version 2.4 (the VR library), so the objects, their options and interaction functions work the same way in both libraries.

Getting Started (A-Frame P5 AR version 2.0)

All A-Frame P5 AR classes live inside an AFrameP5AR object, so to make a box you write:

let b = new AFrameP5AR.Box({})

A sketch creates a World, tells it which markers (or pictures) to look for, and adds objects to them:

let world;
let marker;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image - don't call createCanvas)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker
   marker = world.addMarker({ type: 'hiro' });

   // put a green box on the marker - it appears whenever the camera can see the marker
   let b = new AFrameP5AR.Box({ x: 0, y: 0.5, z: 0, red: 0, green: 255, blue: 0 });
   marker.add(b);
}

function draw() {
   // draw in 2D on top of the camera image here, just like a normal p5 sketch
}

If you are using p5 version 2 (which the examples use), keep in mind that p5 2.x doesn't have a preload() function. To wait for an image to load, make setup an async function and use await: let tree = await world.loadImage('images/tree.png', 'tree');

Begin by downloading the example gallery for the A-Frame P5 AR library. It has a series of examples that start with the basics and build up to image tracking and a whole-class game. They are grouped into numbered topic folders (1_worldAndObjects, 2_interaction, and so on), and the examples in each folder are numbered in the order we suggest working through them. The gallery also has the printable markers (markers) and pictures (targets) that the examples look for. Each example folder contains the following:

Once you've downloaded and unzipped the example gallery, open one of the projects in your favorite text editor. Most of your work will take place in sketch.js; index.html can be used to load images and 3D models (you can also load images from your sketch using world.loadImage).

Browsers only let a page use the camera if it comes from a secure address, so open the examples from http://localhost (run a local web server, for example the "Live Server" extension in VS Code) or upload them to a web host that uses https://. To try them on a phone, upload them to an https web host and open the page in the phone's browser. Print the markers, or show them on another screen.

Moving from version 1.0

Live Examples

Here are live demos of each of the examples, in the order we suggest working through them. Download all of them as one zip file to run and change them yourself. Most of them look for the hiro marker (some also use the kanji marker or the numbered barcodes); the image tracking examples look for the cards in the targets folder (card A, card B and card C).

World & objects

Interaction

Dynamic textures

Markers

2D AR (drawing with p5 on top of the camera image)

Image tracking (uses the cards in the targets folder)

Classroom demo

Printable markers and pictures

Scene & Asset Management

All A-Frame P5 AR projects require some minimal HTML code to get started. Each page uses one kind of tracking, chosen by its <a-scene> tag: markers (AR.js) or pictures (MindAR). A boilerplate HTML document for marker tracking looks like this:

<!doctype html>
<html lang="en-us">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My AR Project</title>

    <!-- home screen icon (without this, A-Frame adds one that loads from aframe.io) -->
    <link rel="apple-touch-icon" href="data:,">
    <!-- an empty tab icon (without this, the browser asks for /favicon.ico and logs a "not found" error) -->
    <link rel="icon" href="data:,">

    <!--
        'integrity' makes the browser check that each library is exactly the file that was tested - if the file on
        the server is ever changed, the browser won't run it. If you change a library's version, remove its
        integrity and crossorigin attributes (or replace the integrity value with the new file's hash).
    -->

    <!-- A-Frame core library -->
    <!-- documentation: https://aframe.io/docs/1.7.0/ -->
    <script src="https://aframe.io/releases/1.7.1/aframe.min.js" integrity="sha384-kg1qQ4+5YryZ2JLdFNHm5qU40bu61EV2Dy8B6oDcP+Wsl7VIZ5GoYNPszSNx77NG" crossorigin="anonymous"></script>

    <!-- AR.js marker tracking (load it after A-Frame) -->
    <!-- documentation: https://ar-js-org.github.io/AR.js-Docs/ -->
    <script src="https://cdn.jsdelivr.net/npm/@ar-js-org/ar.js@3.4.8/aframe/build/aframe-ar.js" integrity="sha384-kMj13PP2WxCked2mWrDVWumIvqdYCYjifhPfsyYBF1av1wP+g7P/hp6kWVGtWrrD" crossorigin="anonymous"></script>

    <!-- p5 core library -->
    <!-- documentation: https://p5js.org/reference/ -->
    <script src="https://cdn.jsdelivr.net/npm/p5@2.3.3/lib/p5.min.js" integrity="sha384-SomRAPLG739U4EgE9jikBcMmCGPR2HQ/7rMNU5hTKbngPGnPT38K28CUR5P0oVdI" crossorigin="anonymous"></script>

    <!-- AFrameP5 AR library (load it after A-Frame and the tracking library) -->
    <script src="https://craignyu.github.io/aframep5-ar/libraries/aframep5_ar-v2.0.js"></script>

    <!-- Your p5 Sketch -->
    <script src="sketch.js"></script>

    <!-- custom styles -->
    <style>
        body {
            padding: 0;
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>

<body>

    <!--
        The A-Frame scene. Make sure it has an id of 'ARScene'.
        'arjs' turns on marker tracking with the webcam. 'mono_and_matrix' and '3x3' let it find both picture
        markers (hiro, kanji, your own) and the numbered barcode markers. Add markers in sketch.js with world.addMarker().
    -->
    <a-scene id="ARScene" embedded arjs="sourceType: webcam; detectionMode: mono_and_matrix; matrixCodeType: 3x3; debugUIEnabled: false">
        <!--
            The 'a-assets' container loads artwork you want to use as textures and models. Give each one a unique id -
            that's the name you use for 'asset' in sketch.js.
        -->
        <a-assets id="ARAssets">
        </a-assets>

        <!-- the camera (leave this as it is) -->
        <a-entity camera></a-entity>

    </a-scene>

</body>

</html>

The scripts have to be loaded in this order: A-Frame, then the tracking library, then p5, then A-Frame P5 AR, then your sketch. The integrity attribute makes the browser check that each library is exactly the file that was tested: if you change a library's version, remove its integrity and crossorigin attributes too.

For image tracking, load MindAR instead of AR.js, and use a mindar-image scene with an <a-camera>. imageTargetSrc is the .mind file with the pictures to look for (see Image Tracking); leave autoStart: false, because the library starts tracking once setup() has added your image targets:

<!-- in the <head>, instead of AR.js: -->
<!-- MindAR image tracking (load it after A-Frame) -->
<!-- documentation: https://hiukim.github.io/mind-ar-js-doc/ -->
<script src="https://cdn.jsdelivr.net/npm/mind-ar@1.2.5/dist/mindar-image-aframe.prod.js" integrity="sha384-kcf/qSrCvp+E61nlypb+jERqjCMK7gpiAp/DLO+HRdq1hcccAtLHyUmK6l8bFMal" crossorigin="anonymous"></script>

<!-- in the <body>: -->
<!--
    The A-Frame scene. Make sure it has an id of 'ARScene'.
    'mindar-image' turns on image tracking. imageTargetSrc is the .mind file with the pictures to look for
    (see targets/README.txt to make your own). Add image targets in sketch.js with world.addImageTarget().
    Leave autoStart: false - the library starts tracking once setup() has added your image targets.
-->
<a-scene id="ARScene" mindar-image="imageTargetSrc: ../../targets/targets.mind; autoStart: false; uiScanning: no">
    <!--
        The 'a-assets' container loads artwork you want to use as textures and models. Give each one a unique id -
        that's the name you use for 'asset' in sketch.js.
    -->
    <a-assets id="ARAssets">
    </a-assets>

    <!-- the camera (leave this as it is) -->
    <a-camera></a-camera>

</a-scene>

If you are planning on using external artwork or 3D models in your scene they can be included in your HTML document. All assets must be given unique ids which will be referenced whenever you wish to use this texture or model. This can be done using an <a-assets> tag, which needs to be nested inside of the <a-scene> tag:

<a-assets id="ARAssets">
   <img id="brick" src="images/tiles/brick_red.png">
   <a-asset-item id="dog" src="images/shiba/gltfmodel.txt"></a-asset-item>
</a-assets>

Images can also be loaded from your sketch using world.loadImage, and videos using world.loadVideo (see the loading textures and video texture examples). Load videos this way rather than in <a-assets>, which can stop the scene from loading on iPhones.

Tracking Markers

The hiro marker: the word Hiro inside a thick black square borderMarker tracking uses fiducial markers: square pictures with a thick black border that the camera can find quickly and reliably. The picture inside the border tells markers apart, and also which way up the marker is. Your sketch tells the world which markers to look for, and anything you add to a marker appears on top of it whenever the camera can see it:

let hiro = world.addMarker({ type: 'hiro' });
hiro.add(new AFrameP5AR.Box({ y: 0.5 }));

There are three kinds of marker:

To make a marker from your own picture:

  1. Make a square image for the middle of your marker, without the black border (the generator adds it). Simple, bold, high-contrast shapes work best. Avoid designs that look the same when they're turned (e.g. a plain circle): the camera needs to be able to tell which way up the marker is. markers/blank_ar_template.png in the gallery shows the size and the border.
  2. Open the AR.js marker generator and upload your image. Leave the pattern ratio at 0.50.
  3. Download the marker (the PNG you print) and the pattern file (the .patt file your code uses), and put the .patt file in your project.
  4. In sketch.js: let myMarker = world.addMarker({ type: 'pattern', url: 'myMarker.patt' }); (the url is relative to index.html).

Tips for reliable tracking:

Image Tracking

Image tracking follows ordinary pictures (posters, cards, book covers, album art) instead of black and white markers. Pictures you track are called image targets. They work just like markers: anything you add to one appears on top of the picture.

let card = world.addImageTarget({ index: 0 });
card.add(new AFrameP5AR.Box({ y: 0.25, width: 0.5, height: 0.5, depth: 0.5 }));

The pictures to look for are compiled into one .mind file, which index.html names in its <a-scene> tag (see Scene & Asset Management). index is the picture's position in that file. The examples use targets/targets.mind, which holds three cards: index 0 is card A, 1 is card B and 2 is card C.

To make your own .mind file:

  1. Choose your pictures. Good targets have lots of detail, sharp corners and contrast all over the picture. Plain areas, smooth gradients, repeating patterns and pictures that are mostly text track badly.
  2. Open the MindAR image target compiler, add all of your pictures at once (one .mind file holds all of them) and click Start. The compiler shows the feature points it found on each picture: more points, spread across the whole picture, means better tracking.
  3. Download the result and put it in your project, then point imageTargetSrc in index.html at it (or use the World option new AFrameP5AR.World('ARScene', { imageTargets: 'targets.mind' })).
  4. In sketch.js, the index of each picture is the order you added them to the compiler (the first one is 0).

Things to know:

Coordinate System

A-Frame renders a three dimensional world: every point can be expressed in terms of an x, y and z value. In AR, each marker and each image target has its own coordinate system, which moves and turns with it:

Objects you add to the world itself (world.add), rather than to a marker, use the camera's coordinate system: the camera is at 0, 0, 0 and looks along the negative z axis, so z: -6 is in front of it, and these objects stay in the same place on the screen (see the static container example). With image tracking, world coordinates are measured in pixels of the camera image; use the target's own coordinates instead (target.distanceTo, target.getLocalPositionOf and hit.pointLocal).

For 2D drawing, getScreenPosition(), getScreenCorners() and hit.screen give positions in p5 coordinates, the same as mouseX and mouseY.

Testing on Phones

Library Documentation

What follows is a technical overview of the various classes that can be used within a p5 sketch to construct and manipulate 3D objects in AR. Please download the A-Frame P5 AR Examples ZIP archive for a series of comprehensive examples.

Class: World

Connects a p5 sketch to the AR scene. Creating a World is required in order to use all other classes in this library. It needs an <a-scene> tag in the body of the HTML document (see Scene & Asset Management), and it decides from that tag whether the page tracks markers or pictures. Create it in setup(): it also creates a p5 canvas that covers the whole window, on top of the camera image, for 2D drawing (so don't call createCanvas yourself).

Constructor

new AFrameP5AR.World(id, options)

ParameterTypeDefaultDescription
id string 'ARScene' The id of the <a-scene> tag in index.html. It can be left out: new AFrameP5AR.World({ mirror: true }) also works.
options object {} Settings for the world, all optional (see the next table).

Options:

OptionTypeDefaultDescription
overlay boolean true create a p5 canvas that covers the whole window, on top of the camera image, for 2D drawing. Taps and clicks go straight through it to the 3D objects underneath, and p5's mousePressed and the other input functions still work. false leaves the canvas to you.
pixelDensity number the screen's, up to 2 the pixel density of that canvas. It is limited to 2 by default, which keeps phones fast.
mirror boolean false mirror the camera image, for front-facing cameras and laptop webcams (so moving left moves left on the screen). Screen positions and taps stay lined up. See setMirror to change it later.
readyFunction function undefined called once when the camera is running and tracking has started
errorFunction function undefined called if the camera can't be started, for example because the user said no, or the page isn't on https:// or localhost
autoStart boolean true image tracking only: start tracking as soon as setup() has finished. Set it to false and call world.start() yourself, for example after a "tap to start" button. (Marker tracking always starts by itself.)
imageTargets string the one in index.html image tracking only: the .mind file with your pictures, instead of the imageTargetSrc in index.html
maxTrack number 1 image tracking only: how many pictures can be tracked at the same time
filterMinCF, filterBeta number MindAR's image tracking only: smoothing. A lower filterMinCF (e.g. 0.0001) means less jitter but more lag; a higher filterBeta (e.g. 0.01) means less lag.
missTolerance, warmupTolerance number MindAR's image tracking only: how many frames a picture has to be missing before MindAR counts it as lost, and seen before it counts as found. (For your own code, foundDelay and lostDelay on the target are usually easier.)
Example

Create the world in your sketch.js file. Declare your world variable in the global scope, so all functions can use it.

// sketch.js file
let world;

function setup() {
   // marker tracking or image tracking is chosen by the <a-scene> tag in index.html
   world = new AFrameP5AR.World('ARScene', {
      // say when the camera is ready
      readyFunction: function() {
         console.log("ready!");
      },
      // and when it couldn't start
      errorFunction: function() {
         console.log("no camera - is the page on https:// or localhost?");
      }
   });

   let marker = world.addMarker({ type: 'hiro' });
   marker.add(new AFrameP5AR.Sphere({ y: 0.25, radius: 0.25 }));
}

function draw() {
   // the World's canvas is a normal p5 canvas: clear it each frame so the camera image shows through
   world.clearDrawingCanvas();
   fill(255);
   text("Point the camera at the hiro marker", 20, 30);
}

In your HTML document make sure to include an <a-scene> tag with the same id (the full templates are in Scene & Asset Management).

<!-- index.html file -->
<body>
   <a-scene id="ARScene" embedded arjs="sourceType: webcam; detectionMode: mono_and_matrix; matrixCodeType: 3x3; debugUIEnabled: false">
      <a-entity camera></a-entity>
   </a-scene>
</body>
Methods
MethodCategoryDescription
addMarker(options: object): Marker Targets starts looking for a marker and returns it (marker tracking pages only). Add things to it with marker.add(). Adding the same marker again gives back the one that already exists, but add markers once, in setup(). See Markers & Image Targets for the options. Example: let hiro = world.addMarker({ type: 'hiro' });
addImageTarget(options: object | number): ImageTarget Targets starts looking for a picture and returns it (image tracking pages only). index is the picture's position in the .mind file. Add image targets in setup(): adding one after tracking has started restarts tracking. See Markers & Image Targets for the options. Example: let card = world.addImageTarget({ index: 0 }); (or just world.addImageTarget(0))
getTargets(): Array Targets returns an Array of all of the markers and image targets that have been added (and not removed)
removeTarget(target: Marker | ImageTarget) Targets stops using a marker or image target: it is hidden, and its foundFunction and lostFunction won't be called any more
getMarker(id: string): Marker Targets for sketches written for version 1.0: uses an <a-marker> tag with this id that is written in index.html. New sketches should use addMarker.
isReady(): boolean Tracking returns true once the camera is running and tracking has started
start() Tracking image tracking only: starts tracking. Only needed if the world was created with autoStart: false. Marker tracking always starts by itself.
stop() Tracking image tracking only: stops tracking and turns the camera off. Call start() to turn it on again.
pause() Tracking image tracking only: pauses tracking; the camera image freezes until resume()
resume() Tracking image tracking only: carries on after pause()
restartTracking() Tracking image tracking only: stops tracking and starts it again (addImageTarget does this for you when a target is added late)
add(entity: Entity) Scene adds an entity to the world itself rather than to a marker. The camera never moves in AR, so things added here stay in the same place on the screen, for example a heads-up display (see Coordinate System and the static container example)
addChild(entity: Entity) Scene alias for add
remove(entity: Entity) Scene removes an entity that was added to the world with add
removeChild(entity: Entity) Scene alias for remove
removeDefaultWorldLighting() Scene removes the default lights added by A-Frame (adding your own Light also turns them off)
clearDrawingCanvas() Camera image erases the 2D drawing canvas so the camera image shows through (the same as p5's clear()). Call it at the start of draw() when you draw in 2D.
setMirror(mirror: boolean) Camera image mirrors the camera image (true) or not (false), the same as the mirror option
isMirrored(): boolean Camera image returns whether the camera image is mirrored
setVideoOpacity(opacity: number) Camera image sets how see-through the camera image is (0 = invisible, 1 = normal), for example to draw a game over a faded camera image
hideVideo() Camera image hides the camera image (tracking keeps going)
showVideo() Camera image shows the camera image again
getVideoElement(): HTMLVideoElement Camera image returns the <video> element that shows the camera image
getVideoRect(): object Camera image returns where the camera image is on the screen, in p5 coordinates (the image is usually bigger than the window; the parts that don't fit are cut off), as an object with:
  • x, y (numbers): its top left corner
  • width, height (numbers): its size
loadImage(filename: string, id: string, callback?: function) Textures loads an image so it can be used as a texture (asset) without adding an <img> tag to your HTML file. Returns a Promise that gives back the id, so in an async function setup() you can wait for it: let tree = await world.loadImage('images/tree.png', 'tree'); then use asset: tree. The optional callback runs when the image has loaded.
loadVideo(filename: string, id: string, callback?: function) Textures loads a video so it can be used as a texture: asset: await world.loadVideo('clip.mp4', 'clip'). Phones only play videos by themselves if they are muted, so the video is always muted, and it loops. It starts playing straight away where the browser allows it, otherwise on the first tap on the screen (iPhones). Returns a Promise that gives back the id once the video has loaded (or after 3 seconds, so setup doesn't wait for a tap). To pause and play it later: document.getElementById('clip').pause() / .play(). Use short, small MP4 files. See the video texture example.
createDynamicTextureFromCreateGraphics(graphics: p5.Graphics): string Textures turns a buffer made with p5's createGraphics into a texture that updates every frame, and returns its id, to use as an entity's asset (with dynamicTexture: true). See the dynamic textures example.
setClickOnRelease(enabled: boolean, tolerance?: number) Interaction makes clicks and taps wait until the mouse button is released or the finger is lifted, so that dragging (for example to spin something) doesn't also click it.
  • enabled (boolean, default true): true: an entity's clickFunction runs when the button is released, and not at all if the pointer moved more than tolerance pixels. false: clicks happen as soon as the button is pressed (the default if you never call this)
  • tolerance (number, optional, default 5): how many pixels the pointer can move and still count as a click
With this on, clickFunction and upFunction both happen when the button is released (clickFunction first). See the tap vs. drag example.
noLoop() Interaction calls p5's noLoop function, which stops draw from running (the AR scene keeps going). Use this instead of p5's own noLoop.
loop() Interaction calls p5's loop function to start draw again after noLoop

Properties: world.mode is 'marker' or 'image' (the kind of tracking the page uses), world.scene is the <a-scene> element and world.canvas is the p5 canvas the world created.


Class: Marker and Class: ImageTarget

Markers (created with world.addMarker) and image targets (created with world.addImageTarget) work the same way: anything you add to one appears on top of it whenever the camera can see it, in its own coordinate system (1 unit wide, with +y pointing up out of the marker or picture). Don't create them with new: use the world's methods.

Options (markers and image targets)

world.addMarker(options) or world.addImageTarget(options)

OptionTypeDefaultDescription
foundFunction function undefined called when the marker or picture is found (comes into view). It receives the marker or image target.
Example:
let hiro = world.addMarker({
   type: 'hiro',
   foundFunction: function(m) {
      console.log("found it!");
   },
   lostFunction: function(m) {
      console.log("lost it");
   },
   // ignore flickers: it has to be seen for 10 frames in a row to count as found
   foundDelay: 10
});
see the found and lost example.
lostFunction function undefined called when the marker or picture is lost (goes out of view). It receives the marker or image target.
foundDelay number 1 how many frames in a row it has to be seen before it counts as found. A bigger number (e.g. 15) ignores flickers.
lostDelay number 1 how many frames in a row it has to be missing before it counts as lost. A bigger number keeps things from disappearing when tracking drops for a moment.
customData object {} an object for storing your own information about the marker or picture

Options for markers only:

OptionTypeDefaultDescription
type string 'hiro' which kind of marker:
  • 'hiro' (the default): the hiro marker
  • 'kanji': the kanji marker
  • 'barcode': one of the 64 numbered barcode markers (set value)
  • 'pattern': a marker made from your own picture (set url)
See Tracking Markers.
value number 0 for barcode markers: the marker's number, from 0 to 63. Example: world.addMarker({ type: 'barcode', value: 5 })
url string undefined for pattern markers: the .patt file made with the marker generator, relative to index.html. Example: world.addMarker({ type: 'pattern', url: 'myMarker.patt' })
smooth boolean true smooth out jittery tracking. It goes with the options smoothCount (default 5: how many frames to average), smoothTolerance (default 0.01) and smoothThreshold (default 2).
minConfidence number 0.6 how sure the tracker has to be that it sees the marker, from 0 to 1. A higher number means fewer false finds but a marker that is harder to find.

Options for image targets only:

OptionTypeDefaultDescription
index number 0 which picture in the .mind file (0 = the first one)
alignAxes boolean true true makes pictures work like markers: 1 unit wide, lying flat in the x / z plane with +y pointing up out of the picture. false keeps MindAR's own axes, with the picture in the x / y plane.
Example
// sketch.js file

let world;
let hiro, kanji;

function setup() {
   world = new AFrameP5AR.World('ARScene');

   // two markers, each with its own object
   hiro = world.addMarker({ type: 'hiro' });
   hiro.add(new AFrameP5AR.Box({ y: 0.25, width: 0.5, height: 0.5, depth: 0.5, red: 255, green: 0, blue: 0 }));

   kanji = world.addMarker({ type: 'kanji' });
   kanji.add(new AFrameP5AR.Sphere({ y: 0.25, radius: 0.25, red: 0, green: 0, blue: 255 }));
}

function draw() {
   world.clearDrawingCanvas();

   // draw a line between the two markers when the camera can see both of them
   if (hiro.isVisible() && kanji.isVisible()) {
      let a = hiro.getScreenPosition();
      let b = kanji.getScreenPosition();
      stroke(255, 255, 0);
      strokeWeight(5);
      line(a.x, a.y, b.x, b.y);

      // and say how far apart they are, in marker widths
      noStroke();
      fill(255);
      textSize(24);
      text(nf(hiro.distanceTo(kanji), 1, 1) + " marker widths apart", (a.x + b.x) / 2, (a.y + b.y) / 2);
   }
}
Methods (markers and image targets)
MethodCategoryDescription
add(entity: Entity) Hierarchy puts an entity on the marker or picture, in its coordinate system. Alias for addChild.
addChild(entity: Entity) Hierarchy puts an entity on the marker or picture
remove(entity: Entity) Hierarchy takes an entity off the marker or picture. Alias for removeChild.
removeChild(entity: Entity) Hierarchy takes an entity off the marker or picture
getChildren(): Array Hierarchy returns an Array of the entities on the marker or picture
isVisible(): boolean Tracking returns whether the camera can see the marker or picture right now (on this frame)
isFound(): boolean Tracking returns whether it counts as found: the same as isVisible, but it waits for foundDelay and lostDelay frames before changing, so it doesn't flicker
getVisibleFrames(): number Tracking returns how many frames in a row it has been seen (0 if it can't be seen)
getHiddenFrames(): number Tracking returns how many frames in a row it has been missing (0 if it can be seen)
getLastSeen(): number Tracking returns how many milliseconds ago it was last seen (-1 if it hasn't been seen yet). See the hold when lost example.
setFoundFunction(f: function) Tracking sets the foundFunction (also called onFound)
setLostFunction(f: function) Tracking sets the lostFunction (also called onLost)
getScreenPosition(): object Screen returns where the center of the marker or picture is on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY). When it can't be seen, this is where it was last seen
  • visible (boolean): whether the camera can see it right now
See the 2D augmented reality example.
getScreenCorners(): Array Screen returns where its four corners are on the screen, as an Array of four objects with x and y, in this order: top left, top right, bottom right, bottom left ("top" is the top edge of the printed marker or picture). Useful for drawing a 2D shape exactly over the marker.
getScreenSize(): object Screen returns how big it looks on the screen, in pixels, as an object with:
  • width, height (numbers): its size
The size grows as the camera gets closer, so it is a handy way to tell how near the marker is.
getScreenRotation(): number Screen returns how far it is turned on the screen, in degrees (0 = upright, positive = clockwise), so a marker can be turned like a dial. See the marker as a dial example.
distanceTo(other: Marker | ImageTarget | Entity): number Position returns how far away another marker, picture or entity is, in this marker's units (1 = the width of the marker). See the marker proximity example.
getLocalPositionOf(other: Marker | ImageTarget | Entity): object Position returns where another marker, picture or entity is in this marker's coordinate system, as an object with:
  • x, y, z (numbers): its position
getWorldPosition(): object Position returns its position in world coordinates (see Coordinate System), as an object with x, y and z
getWorldRotation(): object Position returns its rotation in world coordinates, in degrees, as an object with x, y and z
getWorldPose(): object Position returns its position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
getTargetSize(): object Position returns its size in its own units, as an object with:
  • width (number): always 1
  • depth (number): 1 for markers; for image targets, the picture's height divided by its width (1.25 for the example cards)
See the model on a card example.
getType(): string Information returns 'marker' or 'image'
getMarkerType(): string Information markers only: returns the marker's type ('hiro', 'kanji', 'barcode' or 'pattern'). A barcode marker's number is in marker.value.
getIndex(): number Information image targets only: returns the picture's index in the .mind file

Properties: customData holds your own information (see the options), and foundFunction, lostFunction, foundDelay and lostDelay can be changed at any time.

Entity Classes

Each kind of object you can add to a marker, a picture or the world has its own section below. Every entity takes the options and methods in its "common to all entities" tables (they are the same in every section, so you can read them in whichever class you're using), plus the ones listed for that class. Sizes and positions are in the units of whatever the entity is added to: on a marker, 1 unit is the width of the marker.

Class: Box (entity)

Used to construct a six-faced box entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Box(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Box(options)

OptionTypeDefaultDescription
width number 1 width of the entity (along the x-axis)
height number 1 height of the entity (along the y-axis)
depth number 1 depth of the entity (along the z-axis)
Example
// sketch.js file

let world;
let marker;
let box;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // construct a new box, half as wide as the marker, sitting on top of it
   box = new AFrameP5AR.Box({
      x: 0, y: 0.25, z: 0,
      width: 0.5, height: 0.5, depth: 0.5,
      red: 255, green: 0, blue: 0
   });

   // add the box to the marker - it appears whenever the camera can see the marker
   marker.add(box);
}

function draw() {
   // slowly spin the box
   box.spinY(1);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getDepth(): number Geometry returns the current depth of the entity
getHeight(): number Geometry returns the current height of the entity
getWidth(): number Geometry returns the current width of the entity
setDepth(depth: number) Geometry sets the depth of the entity
setHeight(height: number) Geometry sets the height of the entity
setWidth(width: number) Geometry sets the width of the entity

Class: Plane (entity)

Used to construct a flat two dimensional plane entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Plane(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Plane(options)

OptionTypeDefaultDescription
width number 1 width of the entity (along the x-axis)
height number 1 height of the entity (along the y-axis)
Example
// sketch.js file

let world;
let marker;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // a plane lies in the x / y plane, so turn it -90 degrees to lay it flat on the marker
   let plane = new AFrameP5AR.Plane({
      x: 0, y: 0.01, z: 0,
      width: 1, height: 1,
      rotationX: -90,
      red: 0, green: 128, blue: 255,
      opacity: 0.75
   });
   marker.add(plane);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getHeight(): number Geometry returns the current height of the entity
getWidth(): number Geometry returns the current width of the entity
setHeight(height: number) Geometry sets the height of the entity
setWidth(width: number) Geometry sets the width of the entity

Class: Circle (entity)

Used to construct a flat two dimensional circular entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Circle(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Circle(options)

OptionTypeDefaultDescription
radius number 1 radius of the entity
segments integer 32 the number of segments to use when drawing the circle (higher numbers will yield a "smoother" looking circle)
thetaStart integer 0 start angle for first segment. can be used to define a partial circle.
thetaLength integer 360 the central angle (in degrees). defaults to 360, which makes for a complete circle.
Example
// sketch.js file

let world;
let marker;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // a circle lies in the x / y plane, so turn it -90 degrees to lay it flat on the marker
   let circle = new AFrameP5AR.Circle({
      x: 0, y: 0.01, z: 0,
      radius: 0.5,
      rotationX: -90,
      red: 255, green: 200, blue: 0
   });
   marker.add(circle);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getRadius(): number Geometry returns the current radius of the entity
getSegments(): number Geometry returns the number of segments being used to draw the entity
getThetaStart(): number Geometry returns the current thetaStart value for the entity
getThetaLength(): number Geometry returns the current thetaLength value for the entity
setRadius(radius: number) Geometry sets the radius of the entity
changeRadius(amount: number) Geometry updates the radius by the value provided (i.e. entity.changeRadius(0.1); will increase the radius by 0.1.
setSegments(segments: number) Geometry sets the number of segments being used to draw the entity
setThetaStart(thetaStart: number) Geometry sets the the thetaStart value for the entity
setThetaLength(thetaLength: number) Geometry sets the the thetaLength value for the entity

Class: Ring (entity)

Used to construct a flat two dimensional ring entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Ring(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Ring(options)

OptionTypeDefaultDescription
radiusInner number 0.5 inner radius of the ring
radiusOuter number 1 outer radius of the ring
segmentsTheta integer 32 number of segments. a higher number means the ring will be more round.
segmentsPhi integer 8 number of triangles within each face defined by segmentsTheta.
thetaStart integer 0 start angle for first segment.
thetaLength integer 360 the central angle (in degrees)
Example
// sketch.js file

let world;
let marker;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // a ring that stands up on the marker (rings lie in the x / y plane)
   let ring = new AFrameP5AR.Ring({
      x: 0, y: 0.5, z: 0,
      radiusInner: 0.3, radiusOuter: 0.45,
      side: 'double',
      red: 255, green: 0, blue: 255
   });
   marker.add(ring);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getRadiusInner(): number Geometry returns the current inner radius of the entity
getRadiusOuter(): number Geometry returns the current outer radius of the entity
getSegmentsTheta(): number Geometry returns the current segmentsTheta value for the entity
getSegmentsPhi(): number Geometry returns the current segmentsPhi value for the entity
getThetaStart(): number Geometry returns the current thetaStart value for the entity
getThetaLength(): number Geometry returns the current thetaLength value for the entity
setRadiusInner(radiusInner: number) Geometry sets the inner radius of the entity
setRadiusOuter(radiusOuter: number) Geometry sets the outer radius of the entity
setSegmentsTheta(segmentsTheta: number) Geometry sets the current segmentsTheta value for the entity
setSegmentsPhi(segmentsPhi: number) Geometry sets the current segmentsPhi value for the entity
setThetaStart(thetaStart: number) Geometry sets the the thetaStart value for the entity
setThetaLength(thetaLength: number) Geometry sets the the thetaLength value for the entity

Class: Sphere (entity)

Used to construct a three dimensional spherical entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Sphere(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Sphere(options)

OptionTypeDefaultDescription
radius number 1 radius of the sphere
segmentsWidth integer 18 number of horizontal segments
segmentsHeight integer 36 number horizontal segments
phiStart integer 0 horizontal starting angle
phiLength integer 360 horizontal sweep angle size
thetaStart integer 0 start angle for first segment.
thetaLength integer 360 the central angle (in degrees)
Example
// sketch.js file

let world;
let marker;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // a ball resting on the marker
   let ball = new AFrameP5AR.Sphere({
      x: 0, y: 0.25, z: 0,
      radius: 0.25,
      red: 0, green: 255, blue: 0
   });
   marker.add(ball);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getRadius(): number Geometry returns the current radius of the entity
getSegmentsWidth(): number Geometry returns the current segmentsWidth value for the entity
getSegmentsHeight(): number Geometry returns the current segmentsHeight value for the entity
getPhiStart(): number Geometry returns the current phiStart value for the entity
getPhiLength(): number Geometry returns the current phiLength value for the entity
getThetaStart(): number Geometry returns the current thetaStart value for the entity
getThetaLength(): number Geometry returns the current thetaLength value for the entity
setRadius(radius: number) Geometry sets the radius of the entity
changeRadius(amount: number) Geometry updates the radius by the value provided (i.e. entity.changeRadius(0.1); will increase the radius by 0.1.
setSegmentsWidth(segmentsWidth: number) Geometry sets the current segmentsWidth value for the entity
setSegmentsHeight(segmentsHeight: number) Geometry sets the current segmentsHeight value for the entity
setPhiStart(phiStart: number) Geometry sets the the phiStart value for the entity
setPhiLength(phiLength: number) Geometry sets the the phiLength value for the entity
setThetaStart(thetaStart: number) Geometry sets the the thetaStart value for the entity
setThetaLength(thetaLength: number) Geometry sets the the thetaLength value for the entity

Class: Cone (entity)

Used to construct a three dimensional conical entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Cone(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Cone(options)

OptionTypeDefaultDescription
height number 1 height of the entity (along the y-axis)
openEnded boolean false whether the cone is open ended or not
radiusBottom number 1 radius of the bottom of the cone
radiusTop number 1 radius of the top of the cone
segmentsRadial integer 36 number of segmented faces around the circumference of the cone.
segmentsHeight integer 18 Number of rows of faces along the height of the cone.
thetaStart integer 0 start angle in degrees
thetaLength integer 360 the central angle (in degrees)
Example
// sketch.js file

let world;
let marker;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // a cone standing on the marker
   let cone = new AFrameP5AR.Cone({
      x: 0, y: 0.4, z: 0,
      height: 0.8,
      radiusBottom: 0.3, radiusTop: 0,
      red: 255, green: 128, blue: 0
   });
   marker.add(cone);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getHeight(): number Geometry returns the current height of the entity
getOpenEnded(): boolean Geometry returns whether the entity is open ended or not
getRadiusBottom(): number Geometry returns the current bottom radius of the entity
getRadiusTop(): number Geometry returns the current top radius of the entity
getSegmentsRadial(): number Geometry returns the current segmentsRadial value for the entity
getSegmentsHeight(): number Geometry returns the current segmentsHeight value for the entity
getThetaStart(): number Geometry returns the current thetaStart value for the entity
getThetaLength(): number Geometry returns the current thetaLength value for the entity
setHeight(height: number) Geometry sets the height of the entity
setOpenEnded(openEnded: boolean) Geometry sets whether the entity is open ended or not
setRadiusBottom(radiusBottom: number) Geometry sets the radiusBottom value for the entity
setRadiusTop(radiusTop: number) Geometry sets the radiusTop value for the entity
setSegmentsRadial(segmentsRadial: number) Geometry sets the current segmentsRadial value for the entity
setSegmentsHeight(segmentsHeight: number) Geometry sets the current segmentsHeight value for the entity
setThetaStart(thetaStart: number) Geometry sets the the thetaStart value for the entity
setThetaLength(thetaLength: number) Geometry sets the the thetaLength value for the entity

Class: Cylinder (entity)

Used to construct a three dimensional cylindrical entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Cylinder(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Cylinder(options)

OptionTypeDefaultDescription
radius number 1 radius of the entity
height number 1 height of the entity (along the y-axis)
openEnded boolean false whether the cone is open ended or not
segmentsRadial integer 36 number of segmented faces around the circumference of the cylinder.
segmentsHeight integer 18 Number of rows of faces along the height of the cylinder.
thetaStart integer 0 start angle in degrees
thetaLength integer 360 the central angle (in degrees)
Example
// sketch.js file

let world;
let marker;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // a column standing on the marker
   let column = new AFrameP5AR.Cylinder({
      x: 0, y: 0.5, z: 0,
      radius: 0.2, height: 1,
      red: 200, green: 200, blue: 200
   });
   marker.add(column);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getRadius(): number Geometry returns the current radius of the entity
getHeight(): number Geometry returns the current height of the entity
getOpenEnded(): boolean Geometry returns whether the entity is open ended or not
getSegmentsRadial(): number Geometry returns the current segmentsRadial value for the entity
getSegmentsHeight(): number Geometry returns the current segmentsHeight value for the entity
getThetaStart(): number Geometry returns the current thetaStart value for the entity
getThetaLength(): number Geometry returns the current thetaLength value for the entity
setRadius(radius: number) Geometry sets the radius of the entity
changeRadius(amount: number) Geometry updates the radius by the value provided (i.e. entity.changeRadius(0.1); will increase the radius by 0.1.
setHeight(height: number) Geometry sets the height of the entity
setOpenEnded(openEnded: boolean) Geometry sets whether the entity is open ended or not
setSegmentsRadial(segmentsRadial: number) Geometry sets the current segmentsRadial value for the entity
setSegmentsHeight(segmentsHeight: number) Geometry sets the current segmentsHeight value for the entity
setThetaStart(thetaStart: number) Geometry sets the the thetaStart value for the entity
setThetaLength(thetaLength: number) Geometry sets the the thetaLength value for the entity

Class: Dodecahedron (entity)

Used to construct a three dimensional 12-sided entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Dodecahedron(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Dodecahedron(options)

OptionTypeDefaultDescription
radius number 1 radius of the entity
Example
// sketch.js file

let world;
let marker;
let shape;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   shape = new AFrameP5AR.Dodecahedron({
      x: 0, y: 0.4, z: 0,
      radius: 0.3,
      red: 0, green: 200, blue: 255
   });
   marker.add(shape);
}

function draw() {
   shape.spinY(1);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getRadius(): number Geometry returns the current radius of the entity
setRadius(radius: number) Geometry sets the radius of the entity
changeRadius(amount: number) Geometry updates the radius by the value provided (i.e. entity.changeRadius(0.1); will increase the radius by 0.1.

Class: Octahedron (entity)

Used to construct a three dimensional 8-sided entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Octahedron(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Octahedron(options)

OptionTypeDefaultDescription
radius number 1 radius of the entity
Example
// sketch.js file

let world;
let marker;
let shape;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   shape = new AFrameP5AR.Octahedron({
      x: 0, y: 0.4, z: 0,
      radius: 0.3,
      red: 255, green: 0, blue: 128
   });
   marker.add(shape);
}

function draw() {
   shape.spinY(1);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getRadius(): number Geometry returns the current radius of the entity
setRadius(radius: number) Geometry sets the radius of the entity
changeRadius(amount: number) Geometry updates the radius by the value provided (i.e. entity.changeRadius(0.1); will increase the radius by 0.1.

Class: Tetrahedron (entity)

Used to construct a three dimensional 4-sided entity.

Constructor Parameters (common to all entities)

new AFrameP5AR.Tetrahedron(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Tetrahedron(options)

OptionTypeDefaultDescription
radius number 1 radius of the entity
Example
// sketch.js file

let world;
let marker;
let shape;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   shape = new AFrameP5AR.Tetrahedron({
      x: 0, y: 0.4, z: 0,
      radius: 0.3,
      red: 128, green: 255, blue: 0
   });
   marker.add(shape);
}

function draw() {
   shape.spinY(1);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getRadius(): number Geometry returns the current radius of the entity
setRadius(radius: number) Geometry sets the radius of the entity
changeRadius(amount: number) Geometry updates the radius by the value provided (i.e. entity.changeRadius(0.1); will increase the radius by 0.1.

Class: Torus (entity)

A torus is a surface of revolution generated by revolving a circle in three-dimensional space about an axis coplanar with the circle.

Constructor Parameters (common to all entities)

new AFrameP5AR.Torus(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Torus(options)

OptionTypeDefaultDescription
radius number 1 radius of the entity
radiusTubular number 1 radius of the tubular portion of the entity
segmentsRadial integer 36 number of segmented faces around the circumference of the torus.
segmentsTubular integer 18 number of segments along the circumference of the tube face. a higher number means the tube will be more round.
arc integer 360 central angle of the entity
Example
// sketch.js file

let world;
let marker;
let donut;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   donut = new AFrameP5AR.Torus({
      x: 0, y: 0.4, z: 0,
      radius: 0.3, radiusTubular: 0.05,
      red: 255, green: 100, blue: 150
   });
   marker.add(donut);
}

function draw() {
   donut.spinY(1);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getRadius(): number Geometry returns the current radius of the entity
getRadiusTubular(): number Geometry returns the current radiusTubular of the entity
getSegmentsRadial(): number Geometry returns the current segmentsRadial value for the entity
getSegmentsTubular(): number Geometry returns the current segmentsTubular value for the entity
getArc(): number Geometry returns the current arc value for the entity
setRadius(radius: number) Geometry sets the radius value for the entity
changeRadius(amount: number) Geometry updates the radius by the value provided (i.e. entity.changeRadius(0.1); will increase the radius by 0.1.
setRadiusTubular(radiusTubular: number) Geometry sets the radiusTublar value for the entity
setSegmentsRadial(segmentsRadial: number) Geometry sets the current segmentsRadial value for the entity
setSegmentsTubular(segmentsTubular: number) Geometry sets the current segmentsTubular value for the entity
setArc(arc: number) Geometry sets the current arc value for the entity

Class: TorusKnot (entity)

A torus three dimensional "pretzel" shape.

Constructor Parameters (common to all entities)

new AFrameP5AR.TorusKnot(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.TorusKnot(options)

OptionTypeDefaultDescription
radius number 1 radius of the entity
radiusTubular number 1 radius of the tubular portion of the entity
segmentsRadial integer 36 number of segmented faces around the circumference of the torus.
segmentsTubular integer 18 number of segments along the circumference of the tube face. a higher number means the tube will be more round.
p integer 2 a number that helps define the pretzel shape. the particular shape of the pretzel is defined by a pair of coprime integers, p and q. If p and q are not coprime the result will be a torus link.
q integer 3 a number that helps define the pretzel shape. the particular shape of the pretzel is defined by a pair of coprime integers, p and q. If p and q are not coprime the result will be a torus link.
Example
// sketch.js file

let world;
let marker;
let knot;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   knot = new AFrameP5AR.TorusKnot({
      x: 0, y: 0.4, z: 0,
      radius: 0.25, radiusTubular: 0.04,
      p: 2, q: 3,
      red: 255, green: 215, blue: 0
   });
   marker.add(knot);
}

function draw() {
   knot.spinY(1);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)
MethodCategoryDescription
getRadius(): number Geometry returns the current radius of the entity
getRadiusTubular(): number Geometry returns the current radiusTubular of the entity
getSegmentsRadial(): number Geometry returns the current segmentsRadial value for the entity
getSegmentsTubular(): number Geometry returns the current segmentsTubular value for the entity
getP(): number Geometry returns the current p value for the entity
getQ(): number Geometry returns the current q value for the entity
setRadius(radius: number) Geometry sets the radius value for the entity
changeRadius(amount: number) Geometry updates the radius by the value provided (i.e. entity.changeRadius(0.1); will increase the radius by 0.1.
setRadiusTubular(radiusTubular: number) Geometry sets the radiusTublar value for the entity
setSegmentsRadial(segmentsRadial: number) Geometry sets the current segmentsRadial value for the entity
setSegmentsTubular(segmentsTubular: number) Geometry sets the current segmentsTubular value for the entity
setP(p: number) Geometry sets the current p value for the entity
setQ(q: number) Geometry sets the current q value for the entity

Class: Container3D (entity)

An empty "container" that can be used to hold other entities. Has no visual representation of its own.

A container can have the same interaction functions as any other entity (clickFunction, upFunction, enterFunction, leaveFunction, overFunction, dragFunction and tickFunction). Since the container has no shape of its own, tapping or pointing at any entity inside it counts as tapping the container, and hit describes the spot on the entity that was hit. Moving the pointer from one entity to another inside the same container does not run leaveFunction and enterFunction. If an entity inside the container has its own interaction function as well, both run: the entity's first, then the container's. Give the container its interaction functions when you create it (in its options): a clickFunction added later won't make the things inside it tappable.

Constructor Parameters (common to all entities)

new AFrameP5AR.Container3D(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.Container3D(options)

No entity-specific constructor parameters

Example
// sketch.js file

let world;
let marker;
let container;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // a container on the marker, holding two boxes
   container = new AFrameP5AR.Container3D({
      x: 0, y: 0.25, z: 0
   });
   container.addChild(new AFrameP5AR.Box({ x: -0.3, width: 0.3, height: 0.3, depth: 0.3, red: 255, green: 0, blue: 0 }));
   container.addChild(new AFrameP5AR.Box({ x: 0.3, width: 0.3, height: 0.3, depth: 0.3, red: 0, green: 0, blue: 255 }));
   marker.add(container);
}

// turning the container turns everything inside it
function draw() {
   container.spinY(1);
}
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)

No entity-specific methods


Class: OBJ (entity)

A Wavefront 3D model (.OBJ file format)

Constructor Parameters (common to all entities)

new AFrameP5AR.OBJ(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.OBJ(options)

OptionTypeDefaultDescription
asset string undefined the id of the element in your HTML file that points to the OBJ file you wish to load. this element must be a a-asset-item tag included under the a-assets tag. see the example program below for more information.
mtl string undefined the id of the material file that should be used to texture the OBJ geometry file. this element must be a a-asset-item tag included under the a-assets tag. see the example program below for more information.
Example
// sketch.js file

let world;
let marker;
let robot;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // add a Wavefront (OBJ) model
   // reference the ids of both the OBJ and the MTL file here - they are
   // loaded in your 'index.html' file inside of the 'a-assets' tag
   robot = new AFrameP5AR.OBJ({
      asset: 'robot_obj',
      mtl: 'robot_mtl',
      x: 0, y: 0.35, z: 0,
      scaleX: 0.25, scaleY: 0.25, scaleZ: 0.25
   });
   marker.add(robot);

   // models come in all sizes, so try a few scale values to make one fit on a marker
}
<!-- index.html file -->
<a-assets id="ARAssets">
   <a-asset-item id="robot_obj" src="images/robot/model.obj"></a-asset-item>
   <a-asset-item id="robot_mtl" src="images/robot/materials.mtl"></a-asset-item>
</a-assets>
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)

No entity-specific methods


Class: GLTF (entity)

A Graphics Language Transmission Format 3D model (.GLTF file format)

Constructor Parameters (common to all entities)

new AFrameP5AR.GLTF(options)

OptionTypeDefaultDescription
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
transparent boolean false whether the texture associated with this entity supports transparency (i.e. a PNG file with an alpha channel)
alphaTest number 0 pixels less opaque than this value (0.0 to 1.0) are not drawn at all. Use 0.5 for cut-out pictures such as trees (PNGs with see-through parts), otherwise the see-through parts can hide whatever is behind them. 0 turns it off.
shader string 'standard' how the entity is lit:
  • 'standard' (the default): lights and shadows shade it realistically
  • 'flat': it shows its color or texture at full brightness, whatever the lighting
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
roughness number 0.5 how 'rough' the material appears. valid range = 0.0 to 1.0
metalness number 0 how 'metallic' the material appears. valid range = 0.0 to 1.0
red integer 255 how red the entity appears. valid range = 0 to 255
green integer 255 how green the entity appears. valid range = 0 to 255
blue integer 255 how blue the entity appears. valid range = 0 to 255
repeatX integer 1 how many times to repeat the texture associated with the entity along the x-axis of each face of the object. valid range = any positive number
repeatY integer 1 how many times to repeat the texture associated with the entity along the y-axis of each face of the object. valid range = any positive number
asset string 'None' the id (string) of the element that should be used as a texture for this entity. textures can be set up in the 'index.html' file inside of the a-assets tag, or loaded from your sketch with world.loadImage or world.loadVideo.
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
dynamicTexture boolean false sets whether the texture used on the entity is being drawn from a dynamically drawn canvas element. see these examples for more information: Dynamic textures, Multiple dynamic textures and Paint on a marker
dynamicTextureHeight number 0 tells the entity the height of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
dynamicTextureWidth number 0 tells the entity the width of a dynamic texture. this is used when computing intersection points with the entity and allows you to know the 2D position on the texture where the user is touching. this property must be used along with dynamicTexture and dynamicTextureHeight For more information see this example: Paint on a marker
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Constructor Parameters (specific to this class)

new AFrameP5AR.GLTF(options)

OptionTypeDefaultDescription
asset string undefined the id of the element in your HTML file that points to the GLTF file you wish to load. this element must be a a-asset-item tag included under the a-assets tag. see the example program below for more information.
Example
// sketch.js file

let world;
let marker;
let dog;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // add a GLTF model
   // reference the id of the GLTF file here - it is loaded in your
   // 'index.html' file inside of the 'a-assets' tag
   dog = new AFrameP5AR.GLTF({
      asset: 'dog',
      x: 0, y: 0.25, z: 0,
      scaleX: 0.25, scaleY: 0.25, scaleZ: 0.25
   });
   marker.add(dog);
}

function draw() {
   dog.spinY(1);
}
<!-- index.html file -->
<a-assets id="ARAssets">
   <a-asset-item id="dog" src="images/shiba/gltfmodel.txt"></a-asset-item>
</a-assets>
Methods (common to all entities)
MethodCategoryDescription
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
lookAtUser(upright?: boolean) Rotation turns the entity so that it faces the camera, for example a sign that should always be readable. Call it in a tickFunction (or in draw) to keep it facing the camera. On a marker, use lookAtUser(): lookAtUser(true) keeps the entity level with the camera's "up", not the marker's, so things on a marker lying on a table tip over. See the look at user example.
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getAsset(): string Material returns the current id of the asset being used to texture this entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getMetalness(): number Material returns the current metalness setting for the texture of the entity (range = 0.0 to 1.0)
getOpacity(): number Material returns the current opacity of the entity
getRed(): number Material returns the current red color of the entity
setRoughness(roughness: number) Material sets the roughness for the texture of the entity (range = 0.0 to 1.0)
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getTransparent(): boolean Material returns the current transparency of the entity
setAsset(asset: string) Material sets the id for the asset to texture this entity
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setMetalness(metalness: number) Material sets the metalness for the texture of the entity (range = 0.0 to 1.0)
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setRed(red: number) Material sets the red color of the entity
getRoughness(): number Material returns the current roughness setting for the texture of the entity (range = 0.0 to 1.0)
setTransparent(transparent: boolean) Material sets whether the entity supports transparent textures
getAlphaTest(): number Material returns the entity's alphaTest value
setAlphaTest(alphaTest: number) Material sets the alphaTest value (see the alphaTest constructor option)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).
Methods (specific to this class)

No entity-specific methods


Class: Text (entity)

A 2D entity that renders arbitrary text.

Constructor Parameters (specific to this class)

new AFrameP5AR.Text(options)

OptionTypeDefaultDescription
text string "" the text to display
textAlign string "center" how the lines of text line up:
  • 'center' (the default)
  • 'left'
  • 'right'
font string "roboto" the font for the text:
  • 'roboto' (the default): or the name of another of A-Frame’s stock fonts, such as 'exo2bold' or 'monoid'
  • 'https://...': the URL of a font file
red integer 128 how red the entity appears. valid range = 0 to 255
green integer 128 how green the entity appears. valid range = 0 to 255
blue integer 128 how blue the entity appears. valid range = 0 to 255
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
opacity number 1.0 how transparent this entity is (0.0 = fully transparent, 1.0 = fully opaque)
x number 0 x-position of the entity
y number 0 y-position of the entity
z number 0 z-position of the entity
side string 'front' which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside (e.g. to stand inside a sphere)
  • 'double': both sides (e.g. a plane you can see from behind)
scaleX number 1.0 the scale of the object in the x-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleY number 1.0 the scale of the object in the y-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
scaleZ number 1.0 the scale of the object in the z-axis. 1.0 means normal size - numbers smaller than 1.0 represent a smaller object (i.e. 0.5 means 50% of normal size). numbers larger than 1.0 represent a larger object (i.e. 2.0 means 200% of normal size).
rotationX number 0 the rotation of the object along the x-axis.
rotationY number 0 the rotation of the object along the y-axis.
rotationZ number 0 the rotation of the object along the z-axis.
visible boolean true determines whether the entity is visible in the scene.
customData object undefined a custom object that can be used by the entity to keep track of user-specified data.
Example:
let b = new AFrameP5AR.Box({
   customData: {
      numClicks: 0
   },
   clickFunction: function(e) { 
      e.customData.numClicks++;
      console.log("This element has been clicked " + e.customData.numClicks + " times");
   }
});
clickFunction function undefined a custom function that will run every time the user clicks on the entity. this function receives a reference to the entity that was clicked, and a second argument with information about the click (see below).
Example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e) { 
      e.setRed(random(255)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   clickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});


the second argument, often called hit, describes the click (or tap):
  • pointLocal : where the click landed, in the clicked entity's own coordinates (x, y and z). This is usually the most useful one: for example, where on a board the user tapped, wherever the marker is on the screen
  • screen : where on the screen the click landed (x and y, the same as mouseX / mouseY)
  • point3d : where the click landed in world coordinates (x, y and z; see Coordinate System)
  • distance : how far that point is from the camera, in world units
  • uv : the raw texture coordinates of the click (x and y, from 0 to 1)
  • point2d : for entities that use a dynamic texture, where on the texture the click landed (x and y, in pixels)
  • hand : always 'mouse' in AR (a mouse click or a tap)
example:
let b = new AFrameP5AR.Box({
   clickFunction: function(e, hit) {
      console.log("clicked at " + hit.pointLocal.x + ", " + hit.pointLocal.y + ", " + hit.pointLocal.z + " on the box");
   }
});
see the click data example.
upFunction function undefined a custom function that will run every time the user releases the mouse button (or lifts their finger) after the entity has been clicked. this function should be implemented to accept a single argument which is a reference to the entity that was clicked.
Example:
let b = new AFrameP5AR.Box({
   upFunction: function(e) { 
      e.spinX(random(360)); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   upFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
enterFunction function undefined a custom function that will run every time the mouse hovers over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   enterFunction: function(e) { 
      e.setScale(2,2,2); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   enterFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
leaveFunction function undefined a custom function that will run every time the mouse stops hovering over the entity. this function should be implemented to accept a single argument which is a reference to the entity. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   leaveFunction: function(e) { 
      e.setScale(1,1,1); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   leaveFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
overFunction function undefined a custom function that will run continually whenever the mouse is hovering over the entity. this function should be implemented to accept two arguments - the first is a reference to the entity that was clicked, and the second is an object that contains information about the interaction that just occured. Hovering needs a mouse: phones and tablets can't hover, so use clickFunction for things that should work on a touch screen.
Example:
let b = new AFrameP5AR.Box({
   overFunction: function(e, intersectionInfo) { 
      console.log(intersectionInfo);
   }
});
intersectionInfo is an object that contains info about how the user is interacting with this entity. it contains the following properties:
  • distance : how far the point is from the camera
  • point3d : an object with three properties (x, y & z) describing where the user is touching the entity
  • point2d : an object with two properites (x & y) describing where the user is touching the entity in 2D space (useful for determining where on a dynamic canvas the user is touching)
  • uv : an object with two properies (x & y) describing the raw textural offset (used to compute point2d)
  • pointLocal : the same point in the entity's own coordinates (x, y and z)
  • screen : where the pointer is on the screen (x and y, the same as mouseX / mouseY)
For an example please see the Paint on a marker sample program.

as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   overFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
dragFunction function undefined a custom function that runs every frame while the user holds the mouse button down (or keeps a finger on the screen) after pressing on this entity and keeps pointing at it - for example, to draw on a dynamic texture. A drag that starts somewhere else and moves onto the entity doesn't count. It takes the same two arguments as overFunction (the entity and hit), plus two more properties in hit:
  • previousPoint2d : where the user was pointing on the dynamic texture on the previous frame (x and y, in pixels). It is null when a new drag starts, and again when the pointer comes back onto the entity during a drag, so you can draw a line from it to point2d without joining separate strokes.
  • previousPoint3d : the same thing in the world (x, y and z)
Example:
let canvas = new AFrameP5AR.Plane({
   asset: texture, dynamicTexture: true, dynamicTextureWidth: 512, dynamicTextureHeight: 512,
   dragFunction: function(e, hit) {
      if (hit.previousPoint2d) {
         buffer.line(hit.previousPoint2d.x, hit.previousPoint2d.y, hit.point2d.x, hit.point2d.y);
      }
   }
});
To do something at the start of each drag (for example, stamp a dot), check for hit.previousPoint2d === null inside dragFunction. If an entity has both a clickFunction and a dragFunction, call world.setClickOnRelease(true) so that a drag doesn't count as a click as well (see the tap vs. drag example).
tickFunction function undefined a custom function that will run once per frame (like draw does for the main sketch). this function should be implemented to accept a single argument which is a reference to the entity. it keeps running while the entity's marker is out of view.
Example:
let b = new AFrameP5AR.Box({
   tickFunction: function(e) { 
      e.setRed( random(255) ); 
   }
});
as with the other interaction handler functions, this this function can access the customData property of the entity if desired. example:
let b = new AFrameP5AR.Box({
   customData: {
      currentRed: 0,
      colorChangeSpeed: 1
   },
   tickFunction: function(e) { 
      e.customData.currentRed += e.customData.colorChangeSpeed;
      if (e.customData.currentRed >= 255 || e.customData.currentRed <= 0) {
         e.customData.colorChangeSpeed *= -1;
      }
      e.setRed( e.customData.currentRed ); 
   }
});
Example
// sketch.js file

let world;
let marker;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // a label floating above the marker
   let label = new AFrameP5AR.Text({
      text: 'Hello, AR!',
      x: 0, y: 0.5, z: 0,
      red: 255, green: 255, blue: 255,
      scaleX: 2, scaleY: 2, scaleZ: 2
   });
   marker.add(label);
}
Methods (specific to this class)
MethodCategoryDescription
setText(text: string) Text sets the text to display for this entity
getText(): string Text returns the current text being displayed by this entity
setFont(font: string) Text sets the font for the text:
  • 'roboto' (the default): or the name of another of A-Frame’s stock fonts, such as 'exo2bold' or 'monoid'
  • 'https://...': the URL of a font file
getFont(): string Text returns the font for the text: the name of one of A-Frame’s stock fonts or the URL of a font file
setTextAlign(textAlign: string) Text sets how the lines of text line up:
  • 'center' (the default)
  • 'left'
  • 'right'
getTextAlign(): string Text returns how the lines of text line up:
  • 'center' (the default)
  • 'left'
  • 'right'
lookAt(otherEntity: Entity) Rotation turns the text so that it faces another entity (to make text face the user, use lookAtUser())
constrainPosition(xMin: number, xMax: number, yMin: number, yMax: number, zMin: number, zMax: number) Position ensures that the entity is constrained within the desired boundaries along all three axes. if an entity exceeds any of the constraints it is nudged back into position accordingly.
getX(): number Position returns the current x position of the entity. this is the entity's LOCAL position in relation to its container.
getY(): number Position returns the current y position of the entity. this is the entity's LOCAL position in relation to its container.
getZ(): number Position returns the current z position of the entity. this is the entity's LOCAL position in relation to its container.
nudge(x: number, y: number, z: number) Position nudges the element based on its current position. accepts three numeric values (i.e. myEntity.nudge(1,0,0); will nudge the entity 1 unit from its current position along the x-axis. this is the entity's LOCAL position in relation to its container.
setPosition(x: number, y: number, z: number) Position sets the position of the entity. this is the entity's LOCAL position in relation to its container.
setX(x: number) Position sets the x position of the entity. this is the entity's LOCAL position in relation to its container.
setY(y: number) Position sets the y position of the entity. this is the entity's LOCAL position in relation to its container.
setZ(z: number) Position sets the z position of the entity. this is the entity's LOCAL position in relation to its container.
getWorldPosition(): object Position returns the entity's position in world coordinates (where the camera is at 0, 0, 0), not its local position within its marker or container. To compare entities on different markers, distanceTo and getLocalPositionOf (below) are usually easier. (With image tracking, world coordinates are in pixels; see Coordinate System.)
getRotationX(): number Rotation returns the current x rotation of the entity.
getRotationY(): number Rotation returns the current y rotation of the entity.
getRotationZ(): number Rotation returns the current z rotation of the entity.
rotateX(x: number) Rotation sets the x rotation of the entity
rotateY(y: number) Rotation sets the y rotation of the entity
rotateZ(z: number) Rotation sets the z rotation of the entity
setRotation(x: number, y: number, z: number) Rotation sets the rotation of the entity.
spinX(x: number) Rotation spins the entity along the x axis based on its current rotation. i.e. myEntity.spinX(1); // spin by 1 degree
spinY(y: number) Rotation spins the entity along the y axis based on its current rotation. i.e. myEntity.spinY(1); // spin by 1 degree
spinZ(z: number) Rotation spins the entity along the z axis based on its current rotation. i.e. myEntity.spinZ(1); // spin by 1 degree
getVisibility(): boolean Visibility returns the current visibility state of the entity
hide() Visibility hides the entity by setting its visible property to false
show() Visibility shows the entity by setting its visible property to true
toggleVisibility() Visibility hides the entity if it is current visible and shows the entity if it is current hidden
getScale(): object Scale returns the scale of the entity as an object (i.e. {x:0, y:0, z:0}
getScaleX(): number Scale returns the x scale of the entity
getScaleY(): number Scale returns the y scale of the entity
getScaleZ(): number Scale returns the z scale of the entity
setScale(x: number, y: number, z: number) Scale sets the scale of the object in all axes
setScaleX(x: number) Scale sets the x scale of the object
setScaleY(y: number) Scale sets the y scale of the object
setScaleZ(z: number) Scale sets the z scale of the object
getRed(): number Material returns the current red color of the entity
getBlue(): number Material returns the current blue color of the entity
getGreen(): number Material returns the current green color of the entity
getOpacity(): number Material returns the current opacity of the entity
getSide(): string Material returns which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
setColor(red: number, green: number, blue: number) Material sets the color of the entity
setRed(red: number) Material sets the red color of the entity
setBlue(blue: number) Material sets the blue color of the entity
setGreen(green: number) Material sets the green color of the entity
setOpacity(opacity: number) Material sets the opacity of the entity (range = 0.0 to 1.0)
setSide(side: string) Material sets which side of the entity's surfaces is drawn:
  • 'front' (the default): the outside
  • 'back': the inside
  • 'double': both sides
getRepeatX(): number Material returns how many times the texture repeats across the entity horizontally
getRepeatY(): number Material returns how many times the texture repeats across the entity vertically
setRepeatX(times: number) Material sets how many times the texture repeats across the entity horizontally
setRepeatY(times: number) Material sets how many times the texture repeats across the entity vertically
add(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity. alias for addChild.
addChild(entity: Entity) Hierarchy adds an entity as a child to this entity. this places the new entity into the coordinate space of this entity.
getChildren(): Array Hierarchy returns an Array of all children of this entity
removeChild(entity: Entity) Hierarchy removes a child entity from this entity.
getScreenPosition(): object AR returns where the entity appears on the screen, as an object with:
  • x, y (numbers): the position in p5 coordinates (the same as mouseX / mouseY), for drawing 2D things on top of it
  • visible (boolean): false if it can't be seen right now (for example, its marker is lost, it is hidden, or it is behind the camera)
getWorldPose(): object AR returns the entity's position, rotation and scale in world coordinates, as an object with:
  • x, y, z (numbers): the position
  • rotationX, rotationY, rotationZ (numbers): the rotation, in degrees
  • scale (number): the scale
copyPoseFrom(other: Marker | ImageTarget | Entity, options?: object): boolean AR moves, turns and scales the entity so that it matches a marker, an image target or another entity, wherever this entity is (for example, something added to the world that follows a marker). Call it every frame to keep following, or once to leave the entity where the marker is right now (it stays there when the marker is lost).
  • position (boolean, default true): copy the position
  • rotation (boolean, default true): copy the rotation
  • scale (boolean, default true): copy the scale
Returns false and does nothing if the marker or image can't be seen. Example: sign.copyPoseFrom(marker, { scale: false }); (see the hold when lost example).
distanceTo(other: Marker | ImageTarget | Entity): number AR returns how far away another entity, marker or image target is, in the same units as this entity's x, y and z (so on a marker, 1 = the width of the marker)
getLocalPositionOf(other: Marker | ImageTarget | Entity): object AR returns where another entity, marker or image target is, in this entity's own coordinates, as an object with:
  • x, y, z (numbers): its position
For example, to add something to a container exactly where a marker is (see the spawn objects inside another object example).

Class: Light (entity)

Adds a light to the world. If you don't add any lights, A-Frame adds a default soft light so that everything is visible; adding your own light turns those default lights off (you can also remove them yourself with world.removeDefaultWorldLighting()). See the lighting example.

Constructor Parameters (specific to this class)

new AFrameP5AR.Light(options)

Lights also accept the position, rotation and visibility options listed under "Constructor Parameters (common to all entities)" (for example in the Box section), and have the common position, rotation and visibility methods.

OptionTypeDefaultDescription
type string 'directional' the kind of light:
  • 'ambient': lights everything evenly
  • 'directional' (the default): like sunlight, coming from one direction
  • 'hemisphere': a sky color from above and a ground color from below
  • 'point': like a light bulb, shining in all directions from one spot
  • 'spot': a cone of light
color string '#fff' the color of the light (any CSS color, e.g. '#ff0000' or 'rgb(255,0,0)')
intensity number 1.0 how bright the light is. A-Frame 1.7 changed how light brightness is measured; A-Frame P5 adjusts for this so that 1.0 looks the same on every version.
groundColor string '#fff' 'hemisphere' lights only: the color that comes from below
distance number 0 'point' and 'spot' lights only: how far the light reaches (0 = no limit). Note: since A-Frame 1.7, lights with a distance fade out a little differently than they did in older versions, so scenes that use distance may look slightly different.
decay number 1.0 'point' and 'spot' lights only: how quickly the light fades with distance (only used when distance is not 0)
angle number 60 'spot' lights only: how wide the cone of light is, in degrees
penumbra number 0.0 'spot' lights only: how soft the edge of the cone is (0 = sharp, 1 = very soft)
Example
// sketch.js file

let world;
let marker;
let lamp;

function setup() {
   // create the AR world (it also makes a p5 canvas on top of the camera image)
   world = new AFrameP5AR.World('ARScene');

   // look for the hiro marker - a marker is 1 unit wide
   marker = world.addMarker({ type: 'hiro' });

   // a dim ambient light so nothing is completely black (adding a light turns the default lights off)
   marker.add(new AFrameP5AR.Light({ type: 'ambient', intensity: 0.2 }));

   // a point light (like a light bulb) that circles above the marker
   lamp = new AFrameP5AR.Light({ type: 'point', x: 0, y: 1, z: 0, intensity: 1.0 });
   marker.add(lamp);

   marker.add(new AFrameP5AR.Box({ x: 0, y: 0.25, z: 0, width: 0.5, height: 0.5, depth: 0.5 }));
}

function draw() {
   // lights can be moved like any other entity
   lamp.setPosition(sin(frameCount * 0.02) * 0.75, 1, cos(frameCount * 0.02) * 0.75);
}

License (MIT)

Copyright 2016-2026 Craig Kapp

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.