2.0.5 • Published 13 days ago

@bucky24/react-canvas-map v2.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
13 days ago

react-canvas-map

This module provides a basic 2d grid-based map interface that works with react-canvas (https://github.com/Bucky24/react-canvas)

Installation

With NPM

npm install --save @bucky24/react-canvas-map

With Yarn

yarn add @bucky24/react-canvas-map

Dependencies

This module depends on @bucky24/react-canvas as a peer dependency, with a min version of 3.1.4

Usage

Map

The Map component is the main component that sets up and draws the map.

Children

The Map component expects a series of Layers as its children. Layers are drawn in order.

Parameters

ParamDescriptionRequired
xX coord where the map starts on the canvasYes
yY coord where the map starts on the canvasYes
widthWidth in pixels of the mapYes
heightHeight in pixels of the mapYes
cellSizeSize in pixels of each map cell (can be modified internally based on zoom level). Defaults to 25No
xOffInitial map horizontal offsetNo
yOffInitial map vertical offsetNo
moveTypeOne of the MoveType entries. Determines how the map can be panned. Defaults to MOUSENo
mapBackgroundMapBackground object, describes background for the mapNo
offMapBackgroundMapBackground object, describes background for area that is not take up by the mapNo
onMoveFunction, fires when mouse moves over a map cell. First param is cell x, second is cell y.No
onClickFunction, fires when move is clicked over a map cell. onClick(cellX, cellY, button, rawX, rawY) (button is a ButtonType from @bucky24/react-canvas).No
zoomNumber that indicates the zoom level of the map. Used as initial value if zoomType is not NONE. 100 is default zoom (100% zoom)No
zoomTypeOne of the ZoomType entries. Determines how the map is zoomed. Defaults to MOUSENo
minCellXThe cell x at which the map will start drawing cells. Defaults 0No
minCellYThe cell y at which the map will start drawing cells. Defaults 0No
maxCellXThe cell x at which the map will stop drawing cells (inclusive). Defaults 20No
maxCellYThe cell y at which the map will stop drawing cells (inclusive). Defaults 20No
hideGridBoolean, if true, the grid lines for cells are not drawnNo
typeMapType. Indicates type of map, defaults to MapType.STANDARDNo
centerXX coord of cell to center the map on. Ignored unless moveType is NONENo
centerYY coord of cell to center the map on. Ignored unless moveType is NONENo

Layer

The Layer component define a layer to be drawn to the map. Layers are drawn in order. It does not take any props, and expects zero or more Layer specific components (detailed below). This component must be placed as a child of a Map.

LayerImage

This component details an image that should be drawn on a given layer. It must be a child of a Layer.

Parameters

ParamDescriptionRequired
srcEither a URL or base64 encoded string with image dataYes
widthHow many cells the image takes up horizontally (can be fractional)Yes
heightHow many cells the image takes up vertically (can be fractional)Yes
xThe x position of the cell to draw the image atYes
yThe y position of the cell to draw the image atYes
xOffSetting this will shift the image horizontally from the cell x. Can be fractional.No
yOffSetting this will shift the image vertically from the cell y. Can be fractional.No
rotIndicates how much the image should be rotated, if at allNo
hAlignThe horizontal alignment of the image. One of HAlign. Defaults HAlign.LEFTNo
vAlignThe vertical alignment of the image. One of VAlign. Defaults VAlign.TOPNo

LayerText

This component details text that should be drawn on a given layer. It must be a child of a Layer.

Parameters

ParamDescriptionRequired
textThe text to drawYes
xThe x position of the cell to draw the text atYes
yThe y position of the cell to draw the text atYes
vAlignThe vertical alignment of the text. One of "top", "center", "bottom"No
hAlignThe horizontal alignment of the text. One of "left", "center", "right"No
fontThe font of the text to drawYes

LayerRaw

This compoment has been replaced by Cell

LayerPassthrough

This component can be useful if you want the efficiency of building the layer maually but still want to use the Layer component. Note that LayerPassthrough does not technically need to be the only child in the Layer, but, due to the way it is implemented, it must be the first (and any LayerRaw that comes after will overwrite its raw portion of the layer).

Parameters

ParamDescriptionRequired
layerInstance of MapLayerYes

Manually Building Layers

Manually building layers can be done by setting the layers property on the Map component. This may be slightly more efficient, but is less React-like.

MapLayer

The MapLayer object defines a layer of various objects that need to be drawn. MapLayers are drawn in order, so the first layer is drawn first, and the last layer is drawn last. You can use this to ensure that you get a proper z-depth for your map. MapLayers have the following keys:

ParamDescriptionRequired
imagesList of Image objectsNo
textList of Text objectsNo
rawRaw objectno

Note that this means a single layer can have multiple images, multiple text strings, or both.

Image

The Image object defines an image as well as information on how to draw or position the image.

ParamDescriptionRequired
srcEither a URL or base64 encoded string with image dataYes
cellWidthHow many cells the image takes up horizontally (can be fractional)Yes
cellHeightHow many cells the image takes up vertically (can be fractional)Yes
cellXThe x position of the cell to draw the image atYes
cellYThe y position of the cell to draw the image atYes
xOffSetting this will shift the image horizontally from the cell x. Can be fractional.No
yOffSetting this will shift the image vertically from the cell y. Can be fractional.No
rotIndicates how much the image should be rotated, if at allNo
hAlignThe horizontal alignment of the image. One of HAlign. Defaults HAlign.LEFTNo
vAlignThe vertical alignment of the image. One of VAlign. Defaults VAlign.TOPNo

Text

The Text object defines a string of text as well as information on how to draw the text.

ParamDescriptionRequired
textThe text to drawYes
cellXThe x position of the cell to draw the text atYes
cellYThe y position of the cell to draw the text atYes
vAlignThe vertical alignment of the text. One of "top", "center", "bottom"No
hAlignThe horizontal alignment of the text. One of "left", "center", "right"No
fontThe font of the text to drawYes

Raw

The Raw object determines how to draw a layer that is custom, allowing more complex things than just images and text to be drawn.

ParamDescriptionRequired
cellsList of CellItem objectsYes
drawFuncA callback function that will be called once per cell given in the cells list. This function will be given a DrawFuncParams object as a parameter. This function should return either a single React element, or an array of React elements. Note that these elements must be something that can be handled by the Clip element from @bucky24/react-canvas

Cell

A component that describes an item on the map

ParamDescriptionRequired
cellXThe x coord on the map of the itemYes
cellYThe y coord on the map of the itemYes
cellWidthThe width, in cells, of the itemYes
cellHeightThe height, in cells, of the itemYes
cbCallback function that is called with a CellDimsYes

CellDims

KeyDescription
xStarting X of cell
yStarting Y of cell
widthWidth of cell
heightHeight of cell
x2x + width
y2y + width

DrawFuncParams

An object that is passed into the drawFunc for raw drawing on layers. Note that any coords or widths here are already adjusted for map offset, map x/y, and zoom.

ParamDescription
xThe x coord on the canvas where this item begins
yThe y coord on the canvas where this item begins
widthThe width, in pixels, of the item
heightThe height, in pixels, of the item
idThe id of the item. Whatever was givenin the CellItem is passed through here.

MapBackground

The MapBackground object defines a background.

ParamDescriptionRequired
colorA hex color code to fill a solid color backgroundNo
imageAn image url or base 64 string to fill an image backgroundNo

Note that either color or image is expected to be set.

MoveType

MoveType is also exported from the module. It is an enum with the following types:

TypeDescription
MOUSEUse the mouse for panning the map
KEYBOARD_ARROWUse arrow keys for panning the map
NONENo map panning will be done by the component

ZoomType

ZoomType is also exported from the module. It is an enum with the following types:

TypeDescription
MOUSEUse the mouse wheel for zooming the map
FIXEDThe zoom parameter will be respected but the map cannot be dynamically zoomed by the user
NONENo zoom will be done by the component, and zoom of the map will be set to 100%

HAlign

HAlign type is exported from the module. It is an enum with the following types:

TypeDescription
LEFTAlign the item left
CENTERCenter the item horizontally
RIGHTAlign the item right

VAlign

VAlign type is exported from the module. It is an enum with the following types:

TypeDescription
TOPAlign the item top
CENTERCenter the item vertically
BOTTOMAlign the item bottom

MapType

MapType is expected from the module. It is an enum with the following types:

Note there may be some issues with the isometric grid, especially around displaying text.

TypeDescription
STANDARDA normal square grid
ISOMETRICAn isometric grid
2.0.5

13 days ago

2.0.4

14 days ago

2.0.3

2 months ago

2.0.2

2 months ago

2.0.1

2 months ago

2.0.0

2 months ago

1.1.0

1 year ago

0.7.6

2 years ago

0.7.5

2 years ago

0.7.7

2 years ago

0.8.1

2 years ago

0.7.2

2 years ago

0.8.0

2 years ago

0.7.4

2 years ago

0.7.3

2 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago