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
autoUpdateControls whether the view is automatically updated in the next animation frame on changes, or whether you prefer to manually call
update() orrequestUpdate() after changes. Note that this istrueby default, except for Node.js, where manual updates make more sense.Type:
Boolean
elementThe underlying native element.
Read only.
Type:
HTMLCanvasElement
pixelRatioThe ratio between physical pixels and device-independent pixels (DIPs) of the underlying canvas / device. It is
1for normal displays, and2or more for high-resolution displays.Read only.
Type:
Number
resolutionThe resoltuion of the underlying canvas / device in pixel per inch (DPI). It is
72for normal displays, and144for high-resolution displays with a pixel-ratio of2.Read only.
Type:
Number
viewSizeThe size of the view. Changing the view’s size will resize it’s underlying element.
Type:
Size
boundsThe bounds of the currently visible area in project coordinates.
Read only.
Type:
Rectangle
sizeThe size of the visible area in project coordinates.
Read only.
Type:
Size
centerThe center of the visible area in project coordinates.
Type:
Point
zoomThe view’s zoom factor by which the project coordinates are magnified.
Type:
NumberSee also:
scaling
rotationThe current rotation angle of the view, as described by its
matrix.Type:
Number
scalingThe current scale factor of the view, as described by its
matrix.Type:
PointSee also:
zoom
matrixThe view’s transformation matrix, defining the view onto the project’s contents (position, zoom level, rotation, etc).
Type:
Matrix
Event Handlers
onFrameHandler function to be called on each frame of an animation. The function receives an event object which contains information about the frame event:
Type:
Function⟋nullOptions:
event.count: Number— the number of times the frame event was firedevent.time: Number— the total amount of time passed since the first frame event in secondsevent.delta: Number— the time passed in seconds since the last frame eventSee also:
item.onFrame
Example:Creating an animation:
// Create a rectangle shaped path with its top left point at: // {x: 50, y: 25} and a size of {width: 50, height: 50} var path = new Path.Rectangle(new Point(50, 25), new Size(50, 50)); path.fillColor = 'black'; view.onFrame = function(event) { // Every frame, rotate the path by 3 degrees: path.rotate(3); }onResizeHandler function that is called whenever a view is resized.
Type:
Function⟋null
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; }onMouseDownThe function to be called when the mouse button is pushed down on the view. The function receives a
MouseEventobject 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 withevent.stopPropagation() or by returningfalsefrom a handler.Type:
Function⟋nullSee also:
item.onMouseDown
onMouseDragThe function to be called when the mouse position changes while the mouse is being dragged over the view. The function receives a
MouseEventobject 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 withevent.stopPropagation() or by returningfalsefrom a handler.Type:
Function⟋nullSee also:
item.onMouseDrag
onMouseUpThe function to be called when the mouse button is released over the item. The function receives a
MouseEventobject which contains information about the mouse event.Type:
Function⟋nullSee also:
item.onMouseUp
onClickThe function to be called when the mouse clicks on the view. The function receives a
MouseEventobject 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 withevent.stopPropagation() or by returningfalsefrom a handler.Type:
Function⟋nullSee also:
item.onClick
onDoubleClickThe function to be called when the mouse double clicks on the view. The function receives a
MouseEventobject 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 withevent.stopPropagation() or by returningfalsefrom a handler.Type:
Function⟋nullSee also:
item.onDoubleClick
onMouseMoveThe function to be called repeatedly while the mouse moves over the view. The function receives a
MouseEventobject 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 withevent.stopPropagation() or by returningfalsefrom a handler.Type:
Function⟋nullSee also:
item.onMouseMove
onMouseEnterThe 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
MouseEventobject 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 withevent.stopPropagation() or by returningfalsefrom a handler.Type:
Function⟋nullSee also:
item.onMouseEnter
onMouseLeaveThe function to be called when the mouse moves out of the view. The function receives a
MouseEventobject 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 withevent.stopPropagation() or by returningfalsefrom a handler.Type:
Function⟋nullSee also:
view.onMouseLeave
Methods
remove()Removes this view from the project and frees the associated element.
update()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.
Returns:
Boolean—trueif the view was updated,falseotherwise
requestUpdate()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.
play()Makes all animation play by adding the view to the request animation loop.
pause()Makes all animation pause by removing the view from the request animation loop.
isVisible()Checks whether the view is currently visible within the current browser viewport.
Returns:
Boolean—trueif the view is visible,falseotherwise
isInserted()Checks whether the view is inserted into the browser DOM.
Returns:
Boolean—trueif the view is inserted,falseotherwise
Transform Functions
translate(delta)Translates (scrolls) the view by the given offset vector.
Parameters:
delta:Point— the offset to translate the view by
rotate(angle[, center])Rotates the view by a given angle around the given center point.
Angles are oriented clockwise and measured in degrees.
Parameters:
angle:Number— the rotation anglecenter:Point— optional, default:view.centerSee also:
matrix.rotate(angle[, center])
scale(scale[, center])Scales the view by the given value from its center point, or optionally from a supplied point.
Parameters:
scale:Number— the scale factorcenter:Point— optional, default:view.center
scale(hor, ver[, center])Scales the view by the given values from its center point, or optionally from a supplied point.
Parameters:
hor:Number— the horizontal scale factorver:Number— the vertical scale factorcenter:Point— optional, default:view.center
shear(shear[, center])Shears the view by the given value from its center point, or optionally by a supplied point.
Parameters:
shear:Point— the horizontal and vertical shear factors as a pointcenter:Point— optional, default:view.centerSee also:
matrix.shear(shear[, center])
shear(hor, ver[, center])Shears the view by the given values from its center point, or optionally by a supplied point.
Parameters:
hor:Number— the horizontal shear factorver:Number— the vertical shear factorcenter:Point— optional, default:view.centerSee also:
matrix.shear(hor, ver[, center])
skew(skew[, center])Skews the view by the given angles from its center point, or optionally by a supplied point.
Parameters:
skew:Point— the horizontal and vertical skew angles in degreescenter:Point— optional, default:view.centerSee also:
matrix.shear(skew[, center])
skew(hor, ver[, center])Skews the view by the given angles from its center point, or optionally by a supplied point.
Parameters:
hor:Number— the horizontal skew angle in degreesver:Number— the vertical sskew angle in degreescenter:Point— optional, default:view.centerSee also:
matrix.shear(hor, ver[, center])
transform(matrix)Transform the view.
Parameters:
matrix:Matrix— the matrix by which the view shall be transformed
projectToView(point)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.
Parameters:
point:Point— the point in project coordinates to be convertedReturns:
Point— the point converted into view coordinates
viewToProject(point)Converts the passed point from view coordinate space to project coordinate space.
Parameters:
point:Point— the point in view coordinates to be convertedReturns:
Point— the point converted into project coordinates
getEventPoint(event)Determines and returns the event location in project coordinate space.
Parameters:
event:Event— the native event object for which to determine the location.Returns:
Point— the event point in project coordinates.
Event Handling
on(type, function)Attach an event handler to the view.
Parameters:
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 aMouseEventorEventobject as its sole argumentReturns:
View— this view itself, so calls can be chained
Example:Create a rectangle shaped path with its top left point at: {x: 50, y: 25} and a size of {width: 50, height: 50}
var path = new Path.Rectangle(new Point(50, 25), new Size(50, 50)); path.fillColor = 'black'; var frameHandler = function(event) { // Every frame, rotate the path by 3 degrees: path.rotate(3); }; view.on('frame', frameHandler);on(param)Attach one or more event handlers to the view.
Parameters:
param:Object— an object literal containing one or more of the following properties:frame,resizeReturns:
View— this view itself, so calls can be chained
Example:Create a rectangle shaped path with its top left point at: {x: 50, y: 25} and a size of {width: 50, height: 50}
var path = new Path.Rectangle(new Point(50, 25), new Size(50, 50)); path.fillColor = 'black'; var frameHandler = function(event) { // Every frame, rotate the path by 3 degrees: path.rotate(3); }; view.on({ frame: frameHandler });off(type, function)Detach an event handler from the view.
Parameters:
type:String— the event type:‘frame’,‘resize’,‘mousedown’,‘mouseup’,‘mousedrag’,‘click’,‘doubleclick’,‘mousemove’,‘mouseenter’,‘mouseleave’function:Function— the function to be detachedReturns:
View— this view itself, so calls can be chained
Example:Create a rectangle shaped path with its top left point at: {x: 50, y: 25} and a size of {width: 50, height: 50}
var path = new Path.Rectangle(new Point(50, 25), new Size(50, 50)); path.fillColor = 'black'; var frameHandler = function(event) { // Every frame, rotate the path by 3 degrees: path.rotate(3); }; view.on({ frame: frameHandler }); // When the user presses the mouse, // detach the frame handler from the view: function onMouseDown(event) { view.off('frame'); }off(param)Detach one or more event handlers from the view.
Parameters:
param:Object— an object literal containing one or more of the following properties:frame,resizeReturns:
View— this view itself, so calls can be chained
emit(type, event)Emit an event on the view.
Parameters:
type:String— the event type:‘frame’,‘resize’,‘mousedown’,‘mouseup’,‘mousedrag’,‘click’,‘doubleclick’,‘mousemove’,‘mouseenter’,‘mouseleave’event:Object— an object literal containing properties describing the eventReturns:
Boolean—trueif the event had listeners,falseotherwise
responds(type)Check if the view has one or more event handlers of the specified type.
Parameters:
type:String— the event type:‘frame’,‘resize’,‘mousedown’,‘mouseup’,‘mousedrag’,‘click’,‘doubleclick’,‘mousemove’,‘mouseenter’,‘mouseleave’Returns:
Boolean—trueif the view has one or more event handlers of the specified type,falseotherwise
Was this helpful?