# Class: CropBox

## sceneObjs.CropBox()

The crop box

File -> Export as PNG/SVG

## Constructor

#### new CropBox()

##### Properties:

Name | Type | Description
--- | --- | ---
`p1` | Point | The top left corner of the crop box.
`p2` | Point | The top right corner of the crop box.
`p3` | Point | The bottom left corner of the crop box.
`p4` | Point | The bottom right corner of the crop box.
`format` | string | The format of the image to be exported.
`width` | number | The width of the image to be exported. Not effective when the format is 'svg'.
`rayCountLimit` | number | The maximum number of rays to be traced. This is to avoid infinite loop. If not set, the default value is determined by the simulator and depends on `format`.

Source:
  - sceneObjs/special/CropBox.js, line 36

### Extends

- BaseSceneObj

### Members

#### error

##### Properties:

Name | Type | Description
--- | --- | ---
`error` | string | null | The error message of the object.

Overrides:
  - BaseSceneObj#error
Source:
  - sceneObjs/BaseSceneObj.js, line 46

#### isOptical

Whether the object is optical (i.e. is a light source, interacts with rays, or detects rays).

Overrides:
  - BaseSceneObj#isOptical
Source:
  - sceneObjs/BaseSceneObj.js, line 111

#### scene

##### Properties:

Name | Type | Description
--- | --- | ---
`scene` | Scene | The scene the object belongs to.

Overrides:
  - BaseSceneObj#scene
Source:
  - sceneObjs/BaseSceneObj.js, line 44

#### serializableDefaults

The default values of the properties of the object which are to be serialized. The keys are the property names and the values are the default values. If some properties is default, they will not be serialized and will be deserialized to the default values.

Overrides:
  - BaseSceneObj#serializableDefaults
Source:
  - sceneObjs/BaseSceneObj.js, line 106

#### supportsSurfaceMerging

Whether the object supports surface merging. (This is currently only for glasses, where the surfaces of two glasses are merged to form a single surface if the surfaces overlap.)

Overrides:
  - BaseSceneObj#supportsSurfaceMerging
Source:
  - sceneObjs/BaseSceneObj.js, line 116

#### type

The type of the object.

Overrides:
  - BaseSceneObj#type
Source:
  - sceneObjs/BaseSceneObj.js, line 101

#### warning

##### Properties:

Name | Type | Description
--- | --- | ---
`warning` | string | null | The warning message of the object.

Overrides:
  - BaseSceneObj#warning
Source:
  - sceneObjs/BaseSceneObj.js, line 48

### Methods

#### arePropertiesDefault(propertyNames) → {boolean}

Check whether the given properties of the object are all the default values.

##### Parameters:

Name | Type | Description
--- | --- | ---
`propertyNames` | Array.<string> | The property names to be checked.

Overrides:
  - BaseSceneObj#arePropertiesDefault
Source:
  - sceneObjs/BaseSceneObj.js, line 86

##### Returns:

Whether the properties are all the default values.

Type
  boolean

#### checkMouseOver(mouse) → {DragContext|null}

Check whether the mouse is over the object, which is called when the user moves the mouse over the scene. This is used for deciding the highlighting of the object, and also for deciding that if the user starts dragging at this position, which part of the object should be dragged.

##### Parameters:

Name | Type | Description
--- | --- | ---
`mouse` | Mouse | The mouse object.

Overrides:
  - BaseSceneObj#checkMouseOver
Source:
  - sceneObjs/BaseSceneObj.js, line 206

##### Returns:

The drag context if the user starts dragging at this position, or null if the mouse is not over the object.

Type
  DragContext | null

#### checkRayIntersects(ray) → {Point|null}

Check whether the object intersects with the given ray. Called during the ray tracing when `ray` is to be tested whether it intersects with the object. Find whether they intersect and find the nearset intersection if so. Implemented only by optical elements that affects or detect rays.

##### Parameters:

Name | Type | Description
--- | --- | ---
`ray` | Ray | The ray.

Overrides:
  - BaseSceneObj#checkRayIntersects
Source:
  - sceneObjs/BaseSceneObj.js, line 238

##### Returns:

- The intersection (closest to the beginning of the ray) if they intersect.

Type
  Point | null

#### draw(canvasRenderer, isAboveLight, isHovered)

Draw the object on the canvas. Called once before the simulator renders the light with `isAboveLight === false` and once after with `isAboveLight === true`. Due to historical reason, some objects use this function to do some initialization. This should be avoided in the future.

##### Parameters:

Name | Type | Description
--- | --- | ---
`canvasRenderer` | CanvasRenderer | The canvas renderer.
`isAboveLight` | boolean | Whether the rendering layer is above the light layer.
`isHovered` | boolean | Whether the object is hovered by the mouse, which determines the style of the object to be drawn, e.g., with lighlighted color.

Overrides:
  - BaseSceneObj#draw
Source:
  - sceneObjs/BaseSceneObj.js, line 144

#### getError() → {string|null}

Get the error message of the object.

Overrides:
  - BaseSceneObj#getError
Source:
  - sceneObjs/BaseSceneObj.js, line 264

##### Returns:

The error message.

Type
  string | null

#### getWarning() → {string|null}

Get the warning message of the object.

Overrides:
  - BaseSceneObj#getWarning
Source:
  - sceneObjs/BaseSceneObj.js, line 272

##### Returns:

The warning message.

Type
  string | null

#### getZIndex() → {number}

Get the z-index of the object for the sequence of drawing. Called before the simulator starts to draw the scene.

Overrides:
  - BaseSceneObj#getZIndex
Source:
  - sceneObjs/BaseSceneObj.js, line 132

##### Returns:

The z-index. The smaller the number is, the earlier `draw` is called.

Type
  number

#### move(diffX, diffY)

Move the object by the given displacement. Called when the user use arrow keys to move the object.

##### Parameters:

Name | Type | Description
--- | --- | ---
`diffX` | number | The x-coordinate displacement.
`diffY` | number | The y-coordinate displacement.

Overrides:
  - BaseSceneObj#move
Source:
  - sceneObjs/BaseSceneObj.js, line 154

#### onConstructMouseDown(mouse, ctrl, shift) → {ConstructReturn}

Mouse down event when the object is being constructed by the user.

##### Parameters:

Name | Type | Description
--- | --- | ---
`mouse` | Mouse | The mouse object.
`ctrl` | boolean | Whether the control key is pressed.
`shift` | boolean | Whether the shift key is pressed.

Overrides:
  - BaseSceneObj#onConstructMouseDown
Source:
  - sceneObjs/BaseSceneObj.js, line 165

##### Returns:

The return value.

Type
  ConstructReturn

#### onConstructMouseMove(mouse, ctrl, shift) → {ConstructReturn}

Mouse move event when the object is being constructed by the user.

##### Parameters:

Name | Type | Description
--- | --- | ---
`mouse` | Mouse | The mouse object.
`ctrl` | boolean | Whether the control key is pressed.
`shift` | boolean | Whether the shift key is pressed.

Overrides:
  - BaseSceneObj#onConstructMouseMove
Source:
  - sceneObjs/BaseSceneObj.js, line 176

##### Returns:

The return value.

Type
  ConstructReturn

#### onConstructMouseUp(mouse, ctrl, shift) → {ConstructReturn}

Mouse up event when the object is being constructed by the user.

##### Parameters:

Name | Type | Description
--- | --- | ---
`mouse` | Mouse | The mouse object.
`ctrl` | boolean | Whether the control key is pressed.
`shift` | boolean | Whether the shift key is pressed.

Overrides:
  - BaseSceneObj#onConstructMouseUp
Source:
  - sceneObjs/BaseSceneObj.js, line 187

##### Returns:

The return value.

Type
  ConstructReturn

#### onConstructUndo() → {ConstructReturn}

Undo event when the object is being constructed by the user.

Overrides:
  - BaseSceneObj#onConstructUndo
Source:
  - sceneObjs/BaseSceneObj.js, line 195

##### Returns:

The return value.

Type
  ConstructReturn

#### onDrag(mouse, dragContext, ctrl, shift)

The event when the user drags the object, which is fired on every step during the dragging. The object should be updated according to `DragContext` which is returned by `checkMouseOver`. `dragContext` can be modified during the dragging.

##### Parameters:

Name | Type | Description
--- | --- | ---
`mouse` | Mouse | The mouse object.
`dragContext` | DragContext | The drag context.
`ctrl` | boolean | Whether the control key is pressed.
`shift` | boolean | Whether the shift key is pressed.

Overrides:
  - BaseSceneObj#onDrag
Source:
  - sceneObjs/BaseSceneObj.js, line 217

#### onRayIncident(ray, rayIndex, incidentPoint, surfaceMergingObjs) → {SimulationReturn|null}

The event when a ray is incident on the object. Called during the ray tracing when `ray` has been calculated to be incident on the object at the `incidentPoint`. Perform the interaction between `ray` and the object. Implemented only by optical elements that affects or detect rays. If `ray` is absorbed by the object, return `{ isAbsorbed: true }`. If there is a primary outgoing ray, directly modify `ray` to be the outgoing ray. This includes the case when the object is a detector that does not modify the direction of the ray. If there are secondary rays to be emitted, return `{ newRays: [ray1, ray2, ...] }`. Note that if there are more than one secondary rays, image detection does not work in the current version, and `rayN.gap` should be set to `true`. But for future support, the secondary ray which is to be of the same continuous bunch or rays should have consistent index in the `newRays` array. Sometimes keeping tracks of all the rays may result in infinite loop (such as in a glass). Depending on the situation, some rays with brightness below a certain threshold (such as 0.01) may be truncated. In this case, the brightness of the truncated rays should be returned as `truncation`.

##### Parameters:

Name | Type | Description
--- | --- | ---
`ray` | Ray | The ray.
`rayIndex` | number | The index of the ray in the array of all rays currently in the scene in the simulator. In particular, in a bunch of continuous rays, the rays are ordered by the time they are emitted, and the index is the order of emission. This can be used to downsample the rays and increase the individual brightness if it is too low.
`incidentPoint` | Point | The point where the ray is incident on the object, which is the intersection point found by `checkRayIntersects`.
`surfaceMergingObjs` | Array.<BaseSceneObj> | The objects that are merged with the current object. If two or more objects with `supportsSurfaceMerging === true` has overlapping surfaces (often acheived by using the grid), and if a ray is incident on those surfaces together, only one of the object will be have `onRayIncident` being called, and the other objects will be in `surfaceMergingObjs`. In this case, this function must also deal with the combination of the surface before doing the actual interaction. Note that treating them as two very close surfaces may not give the correct result due to an essential discontinuity of ray optics (which is smoothed out at the scale of the wavelength in reality).

Overrides:
  - BaseSceneObj#onRayIncident
Source:
  - sceneObjs/BaseSceneObj.js, line 256

##### Returns:

The return value.

Type
  SimulationReturn | null

#### onSimulationStart() → {SimulationReturn|null}

The event when the simulation starts. If this object is a light source, it should emit rays here by setting `newRays`. If the object is a detector, it may do some initialization here.

Overrides:
  - BaseSceneObj#onSimulationStart
Source:
  - sceneObjs/BaseSceneObj.js, line 226

##### Returns:

The return value.

Type
  SimulationReturn | null

#### populateObjBar(objBar)

Populate the object bar. Called when the user selects the object (it is selected automatically when the user creates it, so the construction may not be completed at this stage).

##### Parameters:

Name | Type | Description
--- | --- | ---
`objBar` | ObjBar | The object bar to be populated.

Overrides:
  - BaseSceneObj#populateObjBar
Source:
  - sceneObjs/BaseSceneObj.js, line 123

#### serialize() → {Object}

Serializes the object to a JSON object.

Overrides:
  - BaseSceneObj#serialize
Source:
  - sceneObjs/BaseSceneObj.js, line 66

##### Returns:

The serialized JSON object.

Type
  Object