Constructor
new Canvas(id, options)
Parameters:
Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
id |
string | the html id of the element where to place the canvas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
options |
object |
Properties
|
Members
_figurePadding :Vector
Type:
_imageAdjuster
_maxPiecesCount :Vector
Type:
_painter :Painter
Type:
_puzzle :Puzzle
Type:
_puzzleDiameter :Vector
Type:
estimatedPuzzleDiameter :Vector
The estimated puzzle diameter calculated using the the max pieces count.
Type:
figurePadding :Vector
Type:
figures :Object.<string, Figure>
Type:
- Object.<string, Figure>
figuresCount :Number
Type:
- Number
pieceDiameter :Vector
Type:
pieceRadius :Vector
Type:
puzzle :Puzzle
The puzzle rendered by this canvas
Type:
puzzleDiameter :Vector
The puzzle diameter, using the
configured puzzle diameter or the estimated one, if the first is not available.
Type:
settings :Settings
Type:
templates :Object.<string, Template>
Type:
- Object.<string, Template>
valid :boolean
Returns the current validation status
Type:
- boolean
Methods
_annotatePiecePosition(piece)
Parameters:
Name | Type | Description |
---|---|---|
piece |
Piece |
_baseImageMetadataFor(piece) → {ImageMetadata}
Parameters:
Name | Type | Description |
---|---|---|
piece |
Piece |
Returns:
- Type
- ImageMetadata
_bindGroupToPiece(group, piece)
Configures updates from piece into group
Parameters:
Name | Type | Description |
---|---|---|
group |
Group | |
piece |
Piece |
_bindPieceToGroup(piece, group)
* Configures updates from group into piece
Parameters:
Name | Type | Description |
---|---|---|
piece |
Piece | |
group |
Group |
_newPiece(structureLike, size, metadata)
Parameters:
Name | Type | Description |
---|---|---|
structureLike |
StructureLike | the piece structure |
size |
Size | |
metadata |
CanvasMetadata |
adjustImagesToPiece(axis)
Configures canvas to adjust images axis to pieces's axis
**Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
Parameters:
Name | Type | Description |
---|---|---|
axis |
Axis |
adjustImagesToPieceHeight()
Configures canvas to adjust images height to pieces's height
**Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
adjustImagesToPieceWidth()
Configures canvas to adjust images width to pieces's width
**Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
adjustImagesToPuzzle(axis)
Configures canvas to adjust images axis to puzzle's axis.
**Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
Parameters:
Name | Type | Description |
---|---|---|
axis |
Axis |
adjustImagesToPuzzleHeight()
Configures canvas to adjust images height to puzzle's height
**Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
adjustImagesToPuzzleWidth()
Configures canvas to adjust images width to puzzle's width
**Warning**: this method requires {@code maxPiecesCount} or {@code puzzleDiameter} to be set.
attachAbsolutePositionValidator()
Sets a validator that will report when puzzle pieces are in their expected absolute
positions, overriding any previously configured validator
attachConnectionRequirement(requirement)
Attaches a connection requirement function that will be used to check whether
two close and matching pieces can be actually connected. By default no connection
requirement is imposed which means that any close and matching pieces will be
connected.
Parameters:
Name | Type | Description |
---|---|---|
requirement |
ConnectionRequirement |
attachRelativePositionValidator()
Sets a validator that will report when puzzle pieces are in their expected relative
positions, overriding any previously configured validator
attachRelativeRefsValidator(expected)
Sets a validator that will report when puzzle are at the expected given
relative refs
Parameters:
Name | Type | Description |
---|---|---|
expected |
attachSolvedValidator()
Sets a validator that will report when puzzle has been solved,
overriding any previously configured validator
attachValidator(validator)
Sets a validator for the canvas' puzzle. Only one validator
can be attached, so subsequent calls of this method will override the previously
attached validator
Parameters:
Name | Type | Description |
---|---|---|
validator |
Validator |
autogenerate(options)
Automatically creates and renders pieces given some configuration paramters
Parameters:
Name | Type | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
object |
Properties
|
autogenerateWithManufacturer(manufacturer)
Parameters:
Name | Type | Description |
---|---|---|
manufacturer |
Manufacturer |
clear()
Clears the canvas, clearing the rendering backend and discarding all the created templates, figures, and pieces
clearConnectionRequirements()
Removes the connection requirement, if any.
defineTemplate(name, template)
Creates a name piece template, that can be later instantiated using Canvas#sketchPieceUsingTemplate
Parameters:
Name | Type | Description |
---|---|---|
name |
string | |
template |
Template |
draw()
Draws this canvas for the first time
getFigure(piece) → {Figure}
Answers the visual representation for the given piece.
This method uses piece's id.
Parameters:
Name | Type | Description |
---|---|---|
piece |
Piece |
Returns:
- Type
- Figure
getFigureById(id) → {Figure}
Answers the visual representation for the given piece id.
Parameters:
Name | Type | Description |
---|---|---|
id |
string |
Returns:
- Type
- Figure
imageMetadataFor(piece) → {ImageMetadata}
Parameters:
Name | Type | Description |
---|---|---|
piece |
Piece |
Returns:
- Type
- ImageMetadata
onConnect(f)
Registers a listener for connect events
Parameters:
Name | Type | Description |
---|---|---|
f |
CanvasConnectionListener |
onDisconnect(f)
Registers a listener for disconnect events
Parameters:
Name | Type | Description |
---|---|---|
f |
CanvasConnectionListener |
onTranslate(f)
Parameters:
Name | Type | Description |
---|---|---|
f |
CanvasTranslationListener |
onValid(f)
Parameters:
Name | Type | Description |
---|---|---|
f |
ValidationListener |
redraw()
Re-draws this canvas. This method is useful when the canvas Figures have
being modified and you need changes to become visible
refill()
Refreshes image metadata.
Use this method in order adjuster updates and image changes after initial draw
to make effect.
reframeWithinDimensions()
Translates all the pieces - preserving their relative positions - so that
they all can be visible, if possible. If they are already fully visible,
this method does nothing.
In order to prevent unexpected translations, this method will fail
if canvas is not `fixed`.
registerKeyboardGestures(gestures)
Registers keyboard gestures. `gestures` must be an object with one or more entries with the following format:
```
{ keyStrokeNumber: (puzzle) => ...change drag mode... }
```
For example, if you want to configure your canvas to force pieces to be moved together using the `alt` key, you can do the following:
```
canvas.registerKeyboardGestures({ 18: (puzzle) => puzzle.forceConnectionWhileDragging() })
```
If no gestures are given, then the following gestures are configured:
- `16` (`shift`): drags blocks of pieces as a whole, regardless of the movement direction
- `17` (`ctrl`): drags pieces individually, regardless of the movement direction
Parameters:
Name | Type | Description |
---|---|---|
gestures |
object |
renderPiece(piece)
Renders a previously created piece object
Parameters:
Name | Type | Description |
---|---|---|
piece |
Piece |
renderPieces(pieces)
Renders many previously created piece objects
Parameters:
Name | Type | Description |
---|---|---|
pieces |
Array.<Piece> |
renderPuzzle(puzzle)
Renders a previously created puzzle object. This method
overrides this canvas' Canvas#pieceDiameter and Canvas#proximity
Parameters:
Name | Type | Description |
---|---|---|
puzzle |
Puzzle |
resize(width, height)
Sets the new width and height of the canvas
Parameters:
Name | Type | Description |
---|---|---|
width |
number | |
height |
number |
scale(factor)
Scales the canvas contents to the given factor
Parameters:
Name | Type | Description |
---|---|---|
factor |
Vector | number |
shuffle(farness)
Parameters:
Name | Type | Default | Description |
---|---|---|---|
farness |
number | 1 | from 0 to 1, how far pieces will be placed from x = pieceDiameter.x, y = pieceDiameter.y |
shuffleColumns(farness)
**Warning**: this method requires {@code maxPiecesCount} to be set.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
farness |
number | 1 |
shuffleGrid(farness)
**Warning**: this method requires {@code maxPiecesCount} to be set.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
farness |
number | 1 |
shuffleLine(farness)
**Warning**: this method requires {@code maxPiecesCount} to be set.
**Warning**: this method requires puzzle to have an even number of columns
Parameters:
Name | Type | Default | Description |
---|---|---|---|
farness |
number | 1 |
shuffleWith(farness, shuffler)
Parameters:
Name | Type | Description |
---|---|---|
farness |
number | |
shuffler |
Shuffler |
sketchPiece(options)
Creates and renders a piece using a template, that is ready to be rendered by calling Canvas#draw
Parameters:
Name | Type | Description |
---|---|---|
options |
Template |
sketchPieceUsingTemplate(id, templateName)
Creates a new Piece with given id using a named template
defined with Canvas#defineTemplate
Parameters:
Name | Type | Description |
---|---|---|
id |
string | |
templateName |
string |