1.2.0 • Published 5 months ago

formation-creator v1.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Formation Creator

Simple customizable drag and drop sports formation and tactic creator using svg.

Basic Usage

import React, { useState } from "react";
import FormationCreator, { fields, players } from "formation-creator";

const MyCreator = () => {
  const [players, setPlayers] = useState(
    players.soccer["7"]["2-1-2-1"].players
  );

  return (
    <FormationCreator
      field={fields.soccer["7"]}
      players={players}
      setPlayers={setPlayers}
    />
  );
};

This will create a basic field using the existing 7-a-side soccer templates for both fields and players. Players can be dragged and dropped into new spots on the field.

Callbacks

Use the onDrop callback executed when a player has been dropped into a new position to retrieve data on current positions of players. All players are passed back as the only argument.

import React, { useState } from "react";
import FormationCreator, { fields, players } from "formation-creator";

const MyCreator = () => {
  const [players, setPlayers] = useState(
    players.soccer["7"]["2-1-2-1"].players
  );

  return (
    <FormationCreator
      field={fields.soccer["7"]}
      players={players}
      setPlayers={setPlayers}
      onDrop={(updatedPlayers) => {
        console.log(updatedPlayers);
      }}
    />
  );
};

Draw Mode

Use draw mode to allow users to dynamically add lines to the field, showing direction of runs to be made, etc.

import React, { useState } from "react";
import FormationCreator, { fields, players } from "formation-creator";

const MyCreator = () => {
  const [players, setPlayers] = useState(
    players.soccer["7"]["2-1-2-1"].players
  );
  const [lines, setLines] = useState([]);

  return (
    <FormationCreator
      field={fields.soccer["7"]}
      players={players}
      setPlayers={setPlayers}
      lines={lines}
      setLines={setLines}
      drawMode={{
        lineColor: "red",
        withMarker: true,
        onDrawEnd: (newLines) => {
          console.log(newLines);
        },
      }}
    />
  );
};

Draw Mode Props

nametypedescription
lineColorstring/hexThe color in which lines will be drawn.
withMarkerbooleanWill an arrow marker show at the end of the line
onDrawEndfunctionCallback function executed when the line has been completed (onMouseUp). The array of all user created lines are passed back as the only argument.

Other Props

nametypedescription
isLockedbooleanIf true all dragging and dropping of players or creation of lines will be disabled.

Utility Functions

nameinputoutputdescription
flipFormationplayers arrayplayers arrayEach formation provided only gives the attacking players. To display the defense on the other side of the field in a reversed formation input the existing players. The output will be a new array of players, each of which can be dragged and dropped independently.

Custom Field Creation

You can use the available base fields from the json file, or create your own.

Field Props

nametypedescription
idstringUnique identifier for this field. Required if rendering multiple fields.
widthnumberWidth of the whole field
heightnumberHeight of the whole field
backgroundColorstring/hexColor of the field
lineColorstring/hexColor of the lines on the field
linesLine[]All the lines to draw on the field

Line Props

nametypedescription
typestring (optional)Leave blank for a straight line, use path to create a curved line.
x1numberStarting x coordinate of the line, as a percentage.
y1numberStarting y coordinate of the line, as a percentage.
x2numberEnding x coordinate of the line, as a percentage.
y2numberEnding y coordinate of the line, as a percentage. For path lines this is the radius of the curve.
sweepnumberThe direction of the sweep for curved path lines. 0 or 1
dashedstringDashing value for the line. Example: "5, 3" for 5 pixels of line followed by 3 pixels of blank space. Leave null for a solid line.

Custom Player Creation

There are available preset formations available in the players json file, or you can create your own.

Player Props

nametypedescription
namestringName of player to display. Display will default to using name.
numbernumberNumber of player to display. By default this will only display if no name is provided.
numberFirstbooleanIf true the player number will be the preferred display if it exists.
xnumberStarting x coordinate of the player, as a percentage.
ynumberStarting y coordinate of the player, as a percentage.
backgroundColorstring/hexBackground color of the player element. Default blue.
colorstring/hexText and outline color for the player element. Default white
1.2.0

5 months ago

1.1.0

5 months ago

1.0.0

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago