P5Beholder

The main class of the library. It should have a single instance per sketch, which is then prepared at the setup() function.

Constructor

(private) new P5Beholder()

Creates an instance of P5Beholder. It is automatically called during p5 loading. Initialization of the library is only compelte after prepare() is called on sketch setup (see P5Beholder#prepare).

Members

(static) _defaultConfig

An object holding camera, detection, feed and overlay default options for the Beholder detection system.

Example
// Structure of the default configuration object
{
  camera_params: {
    videoSize: 0, // The video size values map to the following [320 x 240, 640 x 480, 1280 x 720, 1920 x 1080]
    rearCamera: false, // Boolean value for defaulting to the rear facing camera. Only works on mobile
    torch: false, // Boolean value for if torch/flashlight is on. Only works for rear facing mobile cameras. Can only be set from init
  },
  detection_params: {
    minMarkerDistance: 10,
    minMarkerPerimeter: 0.2,
    maxMarkerPerimeter: 0.8,
    sizeAfterPerspectiveRemoval: 49,
  },
  feed_params: {
    contrast: 0,
    brightness: 0,
    grayscale: 0,
    flip: false,
  },
  overlay_params: {
    present: true, // Determines if the Beholder overlay will display or be invisible entirely via display: none
    hide: true, // Determines if the overlay should be hidden on the left of the screen or visible
  },
}

Methods

angleInRange(currentAngle, detectionTargetAngle, angleRange) → {true|false}

Checks if the current angle is within a defined range from a target angle. All angles are in radians.

Parameters:
NameTypeDescription
currentAnglenumber

Value to be checked.

detectionTargetAnglenumber

The target angle.

angleRangenumber

The range centers around the target angle. Should be less than PI.

Returns:
Type: 
true | false

cameraToCanvasVector(coord) → {p5.Vector}

Converts a position from camera to canvas using a p5.Vector.

Parameters:
NameTypeDescription
coordp5.Vector

p5.Vector with camera position.

Returns:
  • p5.Vector with canvas position.
Type: 
p5.Vector

cameraToCanvasX(x) → {number}

Converts X position from camera to canvas.

Parameters:
NameTypeDescription
xnumber

Camera X position.

Returns:
  • Canvas X position.
Type: 
number

cameraToCanvasXY(pX, pY) → {p5.Vector}

Converts a position from camera to canvas positions using 2 numbers.

Parameters:
NameTypeDescription
pXnumber

Camera-relative X value.

pYnumber

Camera-relative Y value.

Returns:
  • p5.Vector with canvas position.
Type: 
p5.Vector

cameraToCanvasY(y) → {number}

Converts Y position from camera to canvas.

Parameters:
NameTypeDescription
ynumber

Camera Y position.

Returns:
  • Canvas Y position.
Type: 
number

debugDrawMarker(markerIdopt)

Debug functionality for drawing a marker on the canvas.

Parameters:
NameTypeAttributesDefaultDescription
markerIdnumber<optional>
0

The id of the marker to draw.

markerPresenceToKey(markerIdopt, pKeyCodeopt)

Quick converter between marker presence and keyboard input. When a marker is detected, fires the keydown event. Whe the marker is undetected, fires the keyup event.

Parameters:
NameTypeAttributesDefaultDescription
markerIdnumber<optional>
0

The id of the marker to check.

pKeyCodenumber<optional>
32

The number with the keyboard code (from event.key).

markerPresenceToMouseClick(markerIdopt, non-null, clickXopt, clickYopt)

If a marker is present, the mouse button 0 goes down and, when undetected, goes up. The position of the resulting click can be defined, in pixels.

Parameters:
NameTypeAttributesDefaultDescription
markerIdnumber<optional>
0

The id of the marker to check presence.

clickXnumber<optional>
0

The X position where of the resulting click.

clickYnumber<optional>
0

The Y position where of the resulting click.

markerRotationInRange(markerId, detectionTargetAngle, angleRange) → {true|false}

Checks if a marker's rotation is within a specified range from a target angle. Useful for detecting things like a crank or spinner with a marker at its center. All angles are in radians.

Parameters:
NameTypeDescription
markerIdnumber

The id of the marker to be checked.

detectionTargetAnglenumber

The target angle.

angleRangenumber

The range centers around the target angle. Should be less than PI.

Returns:
Type: 
true | false

prepare(configopt, querySelectoropt)

Sets-up the p5beholder and sketch integration. Adds Beholder to the HTML page at the and initializes it with proper config file. Important: must be called once on setup(). The config file must use P5Beholder._defaultConfig as a base.

Parameters:
NameTypeAttributesDefaultDescription
config*<optional>
P5Beholder._defaultConfig

Controls how Beholder detection will run.

querySelectorstring<optional>
"#beholder_root"

Picks an HTML element to be the parent of the Beholder system elements (GUI, etc).

setMarkersTimeout(timeoutopt)

Changes the timeout value for all markers.

Parameters:
NameTypeAttributesDefaultDescription
timeoutnumber<optional>
50

New time in milliseconds.