Position mixin
This mixin defines the key attributes and functionality of Scrawl-canvas artefact objects.
We define an artefact as something that can be displayed in a Scrawl-canvas Canvas or Stack wrapper - both of which wrap DOM elements in the web page document - <canvas>, and other DOM elements (most commonly a <div> element), respectively.
- We call canvas based artefacts entity objects - these objects represent a shape, path or image drawn in the canvas.
- Entitys include: Block; Grid; Loom; Phrase for text; Picture for images, videos, etc; Shapes of various types; and Wheel.
- Other artefacts live in stack containers. They include nested Stack wrappers, Canvas wrappers (which can exist outside of a stack); and Element wrappers for other direct child elements.
Positioning
Artefacts break away from the normal flow mechanisms of the HTML document; instead they are explicitly positioned within their containers in a variety of ways:
- absolute positioning - where we give the artefact a coordinate, measured in pixels from the top left corner of the container (
[0, 0], [347, 26.4], etc).
- relative positioning - where we supply the artefact with percentage coordinates, with
['0%', '0%'] representing the top left corner of the container, and ['100%', '100%'] its bottom right corner.
- reference positioning - this is where an artefact will use another artefact’s current (not given) coordinate to determine its own position. We can reference by
pivot, or by mimic, or by path.
Scrawl-canvas uses Coordinate Arrays - [x, y] - for storing artefact coordinates. The Javascript types of the x and y parts of the coordinate can be either Numbers or Strings, and do not need to match each other: ['5%', 20] and [10, '15.4%'] are as valid as [10, 20] and ['5%', '15.4%'].
The artefact ‘rotation-reflection’ point
Every artefact has a start coordinate which, by default, is set at the artefact’s top-left corner - even for Wheels and Shapes!
- When we rotate an artefact, we rotate it around this coordinate. When we flip entity artefacts, we flip them around the artefact’s local horizontal or vertical axis drawn through this coordinate.
- Thus what we call a ‘start’ coordinate is in fact the artefact’s Rotation-Reflection point.
The R-R point does not need to be at the artefact’s top-left corner. We can move it by giving the artefact a handle coordinate - a distance measured from the artefact’s top-left corner. Just as for the start coordinate, we can set the handle coordinate using absolute, relative or reference values:
- absolute handles - where we give the artefact a distance, measured in pixels from its top left corner .
- relative handles - where we supply the artefact with percentage distances measured against the artefact’s own dimensions.
- reference handles.
- In all cases, the distancing effect is along the artefact’s local horizontal and vertical axes - if the artefact is rotated, then those axes will not be perpendicular to the containing Canvas or Stack axes.
Normally this is enough information to position an artefact in its container. However there may be times when we need to move the artefact’s R-R point a given distance along the container axes. We can achieve this by giving the artefact an offset coordinate.
- absolute offsets - where we give the artefact a distance, measured in pixels from its top left corner.
- relative offsets - where we supply the artefact with percentage distances measured against the container’s dimensions.
- reference offsets.
An artefact can have both a handle and an offset; handle distances move the R-R point along the artefact’s axes (which may be diagonal lines), while offsets move it along the container’s axes.
Artefact dimensions
Another use for Coordinate Arrays is to set the artefact’s dimensions - their width and height values. In this case we can think of the coordinate in terms of [w, h]. All artefacts, except Shape and Loom entitys, require dimension data.
Just as for positioning data, an artefact’s dimensions data can be supplied in absolute, relative or reference terms:
- absolute dimensions - where we give the artefact a width and height, measured in pixels from its top left corner.
- relative dimensions - where we supply the artefact with percentage widths and height values, measured against the container’s dimensions.
- reference dimensions.
Artefact pseudo-attributes
Scrawl-canvas uses the following pseudo-attributes to make working with Coordinates easier:
- startX and startY - for getting and setting the
start (R-R) Coordinate values
- handleX and handleY - for getting and setting the
handle Coordinate values
- offsetX and offsetY - for getting and setting the
offset Coordinate values
- width and height - for getting and setting the
dimensions Coordinate values
Scrawl-canvas also supports the following pseudo-values, which can be used when setting relative coordinates:
['left', 'top'] == ['0%', '0%']
['center', 'center'] == ['50%', '50%']
['bottom', 'right'] == ['100%', '100%']
scrawl.makeBlock({
name: 'my-block',
startX: 'center',
startY: 'center',
handleX: 'center',
handleY: 'center',
width: '50%',
height: '50%',
lineWidth: 10,
fillStyle: 'blue',
strokeStyle: 'red',
method: 'fillThenDraw',
});
Referencing other artefacts
Scrawl-canvas allows an artefact to reference other artefacts. The referenced objects supply data back to the first artefact, which it then uses to update its position and dimensions.
- This all happens automatically once the reference is set up, with updates occuring as part of the Scrawl-canvas Display cycle.
- To move a group of artefacts as a single unit, we can set up one as the reference artefact, with the rest using handles and offsets to supply distancing data from the reference. Updates to the reference artefact will now be transmitted automatically to all artefacts using it as their
pivot or mimic.
We create a reference by setting the value of one or more of the following attributes to a reference artefact’s name-String, or to the object itself:
pivot - once set, we can then use the referenced artefact’s start coordinate as this artefact’s start value. With appropriate flags set, we can add this artefact’s handle and offset coordinates to the start value.
mimic - extends the ‘pivot’ concept to include dimensions, alongside adding this artefact’s data to the referenced artefact’s data in various ways.
path - use a Shape entity’s path to determine this artefact’s position and rotation values.
We action a reference by setting this artefact’s lockTo Array attribute. Ther lock array is similar to coordinate Arrays: it is a two-element array of Strings in the form [x-lock, y-lock]. Five lock strings are supported:
start - use this artefact’s own start (and other) values
pivot - use the pivot reference artefact’s start (and other) values
mimic - use the mimic reference artefact’s start (and other) values
path - use the Shape entity’s path to calculate this artefact’s start (and rotation) values
mouse - use the mouse/touch cursor’s current position as the reference for this artefact’s start value
The lockTo Array supports the pseudo-attributes lockXTo and lockYTo to make working with it easier.
- Setting
lockTo sets both elements of the Array to the supplied String
lockXTo and lockYTo allow us to separate the x and y coordinates
For instance if we set the lock to ['start', 'mouse'] we can make the artefact track the mouse across the container along an (invisible) vertical line, whose position is determined by the artefact’s x coordinate value.
Artefact rotation
Scrawl-canvas artefacts can be rotated around their Rotation-Reflection point:
- Entity artefacts, being representations of 2-dimensional graphical shapes drawn on a <canvas> element, are restricted to rotating around the R-R point’s z-axis.
- Other artefacts, when part of a Stack container, can be rotated in 3 dimensions along the R-R point’s x-axis, y-axis and z-axis.
Scrawl canvas measures rotations in degrees, not radians. An artefact’s rotation values are stored in the following attributes:
- roll - for
z-axis rotations
- yaw - for
y-axis rotations
- pitch - for
x-axis rotations
These euler attributes are named after the Aircraft principal axes - mainly because I find it difficult to keep the mathematical representations in my head when thinking about rotations in 3 dimensions, but an image of an aircraft is a lot easier to visualize.
- Internally, Scrawl-canvas stores rotational data in Quaternion objects rather than in matrices.
Instead of 3D rotations, entity artefacts have two Boolean flags - flipReverse, flipUpend - which determine the orientation of the entity when it stamps itself on the display.
- a
reversed entity is effectively flipped 180° around a vertical line passing through that entity’s rotation-reflection (start) point - a face looking to the right will now look to the left
- an
upended entity is effectively flipped 180° around a horizontal line passing through that entity’s rotation-reflection (start) point - a normal face will now appear upside-down
We can emulate 3D rotations for Picture entity artefacts using the Loom entity - see Demo DOM-015 for an example of this in action.
Collision detection
Collision detection functionality - beyond mouse drag-and-drop - has been removed from Scrawl-canvas since v8.2.0