View
The View object wraps an HTML element and handles drawing and user interaction through mouse and keyboard for it. It offer means to scroll the view, find the currently visible bounds in project coordinates, or the center, both useful for constructing artwork that should appear centered on screen.
Properties
Controls whether the view is automatically updated in the next animation frame on changes, or whether you prefer to manually call update() or requestUpdate() after changes. Note that this is true
by default, except for Node.js, where manual updates make more sense.
- Boolean
Type:
The underlying native element.
Read only.
- HTMLCanvasElement
Type:
The ratio between physical pixels and device-independent pixels (DIPs) of the underlying canvas / device. It is 1
for normal displays, and 2
or more for high-resolution displays.
Read only.
- Number
Type:
The resoltuion of the underlying canvas / device in pixel per inch (DPI). It is 72
for normal displays, and 144
for high-resolution displays with a pixel-ratio of 2
.
Read only.
- Number
Type:
The size of the view. Changing the view’s size will resize it’s underlying element.
- Size
Type:
The view’s zoom factor by which the project coordinates are magnified.
- Number
Type:
- scaling
See also:
The view’s transformation matrix, defining the view onto the project’s contents (position, zoom level, rotation, etc).
- Matrix
Type:
Event Handlers
Handler function to be called on each frame of an animation. The function receives an event object which contains information about the frame event:
- Function⟋null
Type:
- event.count: Number — the number of times the frame event was fired
- event.time: Number — the total amount of time passed since the first frame event in seconds
- event.delta: Number — the time passed in seconds since the last frame event
Options:
- item.onFrame
See also:
Example:Creating an animation:
Handler function that is called whenever a view is resized.
- Function⟋null
Type:
Example:Repositioning items when a view is resized:
// Create a circle shaped path in the center of the view:
var path = new Path.Circle(view.bounds.center, 30);
path.fillColor = 'red';
view.onResize = function(event) {
// Whenever the view is resized, move the path to its center:
path.position = view.center;
}
The function to be called when the mouse button is pushed down on the view. The function receives a MouseEvent object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy, reaching the view at the end, unless they are stopped before with event.stopPropagation() or by returning false
from a handler.
- Function⟋null
Type:
- item.onMouseDown
See also:
The function to be called when the mouse position changes while the mouse is being dragged over the view. The function receives a MouseEvent object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy, reaching the view at the end, unless they are stopped before with event.stopPropagation() or by returning false
from a handler.
- Function⟋null
Type:
- item.onMouseDrag
See also:
The function to be called when the mouse button is released over the item. The function receives a MouseEvent object which contains information about the mouse event.
- Function⟋null
Type:
- item.onMouseUp
See also:
The function to be called when the mouse clicks on the view. The function receives a MouseEvent object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy, reaching the view at the end, unless they are stopped before with event.stopPropagation() or by returning false
from a handler.
- Function⟋null
Type:
- item.onClick
See also:
The function to be called when the mouse double clicks on the view. The function receives a MouseEvent object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy, reaching the view at the end, unless they are stopped before with event.stopPropagation() or by returning false
from a handler.
- Function⟋null
Type:
- item.onDoubleClick
See also:
The function to be called repeatedly while the mouse moves over the view. The function receives a MouseEvent object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy, reaching the view at the end, unless they are stopped before with event.stopPropagation() or by returning false
from a handler.
- Function⟋null
Type:
- item.onMouseMove
See also:
The function to be called when the mouse moves over the view. This function will only be called again, once the mouse moved outside of the view first. The function receives a MouseEvent object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy, reaching the view at the end, unless they are stopped before with event.stopPropagation() or by returning false
from a handler.
- Function⟋null
Type:
- item.onMouseEnter
See also:
The function to be called when the mouse moves out of the view. The function receives a MouseEvent object which contains information about the mouse event. Note that such mouse events bubble up the scene graph hierarchy, reaching the view at the end, unless they are stopped before with event.stopPropagation() or by returning false
from a handler.
- Function⟋null
Type:
- view.onMouseLeave
See also:
Methods
Removes this view from the project and frees the associated element.
Updates the view if there are changes. Note that when using built-in event hanlders for interaction, animation and load events, this method is invoked for you automatically at the end.
- Boolean — true if the view was updated, false otherwise
Returns:
Requests an update of the view if there are changes through the browser’s requestAnimationFrame() mechanism for smooth animation. Note that when using built-in event handlers for interaction, animation and load events, updates are automatically invoked for you automatically at the end.
Makes all animation play by adding the view to the request animation loop.
Makes all animation pause by removing the view from the request animation loop.
Checks whether the view is currently visible within the current browser viewport.
- Boolean — true if the view is visible, false otherwise
Returns:
Checks whether the view is inserted into the browser DOM.
- Boolean — true if the view is inserted, false otherwise
Returns:
Transform Functions
Translates (scrolls) the view by the given offset vector.
- delta: Point — the offset to translate the view by
Parameters:
Rotates the view by a given angle around the given center point.
Angles are oriented clockwise and measured in degrees.
- angle: Number — the rotation angle
- center: Point — optional, default: view.center
Parameters:
- matrix.rotate(angle[, center])
See also:
Scales the view by the given value from its center point, or optionally from a supplied point.
- scale: Number — the scale factor
- center: Point — optional, default: view.center
Parameters:
Scales the view by the given values from its center point, or optionally from a supplied point.
- hor: Number — the horizontal scale factor
- ver: Number — the vertical scale factor
- center: Point — optional, default: view.center
Parameters:
Shears the view by the given value from its center point, or optionally by a supplied point.
- shear: Point — the horizontal and vertical shear factors as a point
- center: Point — optional, default: view.center
Parameters:
- matrix.shear(shear[, center])
See also:
Shears the view by the given values from its center point, or optionally by a supplied point.
- hor: Number — the horizontal shear factor
- ver: Number — the vertical shear factor
- center: Point — optional, default: view.center
Parameters:
- matrix.shear(hor, ver[, center])
See also:
Skews the view by the given angles from its center point, or optionally by a supplied point.
- skew: Point — the horizontal and vertical skew angles in degrees
- center: Point — optional, default: view.center
Parameters:
- matrix.shear(skew[, center])
See also:
Skews the view by the given angles from its center point, or optionally by a supplied point.
- hor: Number — the horizontal skew angle in degrees
- ver: Number — the vertical sskew angle in degrees
- center: Point — optional, default: view.center
Parameters:
- matrix.shear(hor, ver[, center])
See also:
Transform the view.
- matrix: Matrix — the matrix by which the view shall be transformed
Parameters:
Converts the passed point from project coordinate space to view coordinate space, which is measured in browser pixels in relation to the position of the view element.
- point: Point — the point in project coordinates to be converted
Parameters:
- Point — the point converted into view coordinates
Returns:
Event Handling
Attach an event handler to the view.
- type: String — the type of event: ‘frame’, ‘resize’, ‘mousedown’, ‘mouseup’, ‘mousedrag’, ‘click’, ‘doubleclick’, ‘mousemove’, ‘mouseenter’, ‘mouseleave’
- function: Function — the function to be called when the event occurs, receiving a MouseEvent or Event object as its sole argument
Parameters:
- View — this view itself, so calls can be chained
Returns:
Example:Create a rectangle shaped path with its top left point at: {x: 50, y: 25} and a size of {width: 50, height: 50}
Attach one or more event handlers to the view.
- param: Object — an object literal containing one or more of the following properties: frame, resize
Parameters:
- View — this view itself, so calls can be chained
Returns:
Example:Create a rectangle shaped path with its top left point at: {x: 50, y: 25} and a size of {width: 50, height: 50}
Detach an event handler from the view.
- type: String — the event type: ‘frame’, ‘resize’, ‘mousedown’, ‘mouseup’, ‘mousedrag’, ‘click’, ‘doubleclick’, ‘mousemove’, ‘mouseenter’, ‘mouseleave’
- function: Function — the function to be detached
Parameters:
- View — this view itself, so calls can be chained
Returns:
Example:Create a rectangle shaped path with its top left point at: {x: 50, y: 25} and a size of {width: 50, height: 50}
Detach one or more event handlers from the view.
- param: Object — an object literal containing one or more of the following properties: frame, resize
Parameters:
- View — this view itself, so calls can be chained
Returns:
Emit an event on the view.
- type: String — the event type: ‘frame’, ‘resize’, ‘mousedown’, ‘mouseup’, ‘mousedrag’, ‘click’, ‘doubleclick’, ‘mousemove’, ‘mouseenter’, ‘mouseleave’
- event: Object — an object literal containing properties describing the event
Parameters:
- Boolean — true if the event had listeners, false otherwise
Returns:
Check if the view has one or more event handlers of the specified type.
- type: String — the event type: ‘frame’, ‘resize’, ‘mousedown’, ‘mouseup’, ‘mousedrag’, ‘click’, ‘doubleclick’, ‘mousemove’, ‘mouseenter’, ‘mouseleave’
Parameters:
- Boolean — true if the view has one or more event handlers of the specified type, false otherwise