# XRRay()

The `XRRay()` constructor creates a new `XRRay` object which is a geometric ray described by an origin point and a direction vector.

## Syntax

``````new XRRay()
new XRRay(origin)
new XRRay(origin, direction)
new XRRay(transform)
``````

### Parameters

`origin` Optional

A point object defining the 3-dimensional point in space that the ray originates from, in meters. All dimensions are optional, however, if provided, the origin's `w` property must be 1.0. The object is initialized to `{ x: 0.0, y: 0.0, z: 0.0, w: 1.0 }` by default.

`direction` Optional

A vector object defining the ray’s 3-dimensional directional vector. All dimensions are optional, however, if provided, the direction's `w` property must be 0.0. The object is initialized to: `{ x: 0.0, y: 0.0, z: -1.0, w: 0.0 }` by default.

`transform` Optional

An `XRRigidTransform` object representing the position and orientation of the ray.

### Return value

A newly-created `XRRay` object.

### Exceptions

A `TypeError` is thrown,

• if all of `direction`'s `x`, `y`, and `z` coordinates are zero.
• if `direction`'s `w` coordinate is not 0.0.
• if `origin`'s `w` coordinate is not 1.0.

## Example

### Creating `XRRay` objects

The `XRRay()` constructor allows to creating new rays by either providing an `origin` point and a `direction` vector, or by passing in an `XRRigidTransform` object.

``````// Default configuration
let ray1 = new XRRay();

// Specifying origin, leaving direction as default
let ray2 = new XRRay({y: 0.5});

// Specifying both, origin and direction
let origin = {x : 10.0, y : 10.0, z : 10.0, w : 1.0};
let direction = {x : 10.0, y : 0.0, z : 0.0, w : 0.0};
let ray3 = new XRRay(origin, direction);

// Using DOMPoint.fromPoint
let ray4 = new XRRay(DOMPoint.fromPoint(origin),
DOMPoint.fromPoint(direction));

// Using rigid transform
let rigidTransform = new XRRigidTransform(
DOMPoint.fromPoint(origin),
DOMPoint.fromPoint(direction));
let ray5 = new XRRay(rigidTransform);
``````

## Browser compatibility

BCD tables only load in the browser
{"ft_lang_label":"__label__en","ft_lang_prob":0.60368,"math_prob":0.9737173,"size":2021,"snap":"2021-31-2021-39","text_gpt3_token_len":587,"char_repetition_ratio":0.16559246,"word_repetition_ratio":0.057877813,"special_character_ratio":0.2701633,"punctuation_ratio":0.22327791,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96778584,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-25T06:54:37Z\",\"WARC-Record-ID\":\"<urn:uuid:9bd95520-65a4-4d11-9141-964a3e70947b>\",\"Content-Length\":\"54638\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fd4bd906-bddf-4f50-b1e2-f6d8fbb5cec4>\",\"WARC-Concurrent-To\":\"<urn:uuid:f08fe7ae-3ddf-4e5b-8954-9f1d1d939e94>\",\"WARC-IP-Address\":\"99.84.105.56\",\"WARC-Target-URI\":\"https://developer.mozilla.org/en-US/docs/Web/API/XRRay/XRRay\",\"WARC-Payload-Digest\":\"sha1:ZAOCN2HPA7S2QHWLE7UA5267XTUYF67N\",\"WARC-Block-Digest\":\"sha1:F4OBXGHNRMSOGHDEQ2FKRNYNJ6D6WNPU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057598.98_warc_CC-MAIN-20210925052020-20210925082020-00151.warc.gz\"}"}