1.0.4 • Published 7 months ago

claw-machine-js v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

ClawJs is a physic-based claw machine that provides various features for customization.

Demo

Check out the live demo here: Live Demo

Installation

npm i claw-machine-js

Features

  • Fully customizable claw machine parameters (size, speed, etc.)
  • Support for dynamic ball configurations
  • TypeScript support for better developer experience

Usage

import { ClawMachineCommands } from 'clawjs/dist/ClawMachine'
import { IInitialBall } from 'clawjs/dist/interfaces/InitialBall'

const initialState: IInitialBall[] = [
	{
		text: 'test',
		ballColor: 'gray',
		ballTextColor: 'yellow',
		icon: react,
	},
]

<ClawMachine alreadyDroppedBalls={[]}
    addToDroppedBalls={(balls) => console.log(balls)}
    ballData={initialState}
/>

Claw Machine Parameters

Prop NameTypeDefaultRequiredDescription
ballDataArray<Object>-YesAn array of ball configurations. Each ball object should have text, ballColor, ballTextColor, and optionally icon.
alreadyDroppedBallsArray-YesList of balls that have already been dropped from the machine.
addToDroppedBallsFunction-YesCallback function triggered when a ball is dropped.
widthNumber600NoThe width of the claw machine in pixels.
heightNumber400NoThe height of the claw machine in pixels.
ballRadiusNumber20YesThe radius of each ball in pixels.
gravityNumber0.2NoThe gravity effect on the balls. Higher values make balls fall faster.
frictionNumber0.99NoThe air resistance that slows the balls’ movement.
groundFrictionNumber0.8NoThe friction between the balls and the ground surface.
clawSizeNumber30NoThe size of the claw's opening in pixels.
clawWidthNumber10NoThe thickness of the claw's arms.
dividerLineWidthNumber70NoThe width of the divider line in pixels.
dividerLineHeightNumber140NoThe height of the divider line in pixels.
dividerLineThicknessNumber20NoThe thickness of the divider line in pixels.
dividerLineFillColorStringgrayNoThe color of the divider line.
dividerLineBorderColorStringgrayNoThe border color of the divider line.
clawColorString"gray"NoThe color of the claw.
clawBoltColorString"black"NoThe color of the claw's bolts.
clawStartPositionXNumber200NoInitial horizontal position of the claw when it is raised.
clawStartPositionYNumber40NoInitial vertical position of the claw when it is raised.
clawStartOpenAngleNumber0NoInitial open Angle of the Claw.
clawSpeedXNumber2NoThe speed of the claw moving horizontally (X-axis).
clawSpeedYNumber1.1NoThe speed of the claw moving vertically (Y-axis).
clawOpenSpeedNumber1NoThe speed of the claw opening.

Initial Ball Parameters

Prop NameTypeDefaultRequiredDescription
textString-YesText displayed inside the ball if no icon is provided.
iconStringball textNoThe file path or URL to an icon image displayed inside the ball.
radiusNumberglobal ball radiusNoRadius of the ball, in pixels.
startXNumberrandomNoInitial X-coordinate position of the ball on the canvas.
startYNumberrandomNoInitial Y-coordinate position of the ball on the canvas.
startXMomentumNumberrandomNoInitial horizontal momentum (velocity) of the ball.
startYMomentumNumberrandomNoInitial vertical momentum (velocity) of the ball.
imageHeightNumberball sizeNoHeight of the image inside the ball.
imageWidthNumberball sizeNoWidth of the image inside the ball.
ballColorStringredNoColor of the ball.
ballTextFontSizeNumberball sizeNoFont size of the text inside the ball.
ballTextColorStringwhiteNoText color of the text inside the ball.
ballTextAligncenter or left or rightcenterNoVertical alignment of the text inside the ball.
ballTextBaselinemiddle or top or bottommiddleNoHorizontal alignment of the text inside the ball.

Claw Control Functions

It is also possible to control the claw with a number of functions.

import { ClawMachineCommands } from 'clawjs/dist/ClawMachine'
import { IInitialBall } from 'clawjs/dist/interfaces/InitialBall'

const clawMachineRef = useRef<ClawMachineCommands>(null)
const initialState: IInitialBall[] = [
	{
		text: 'test',
		ballColor: 'gray',
		ballTextColor: 'yellow',
		icon: react,
	},
]

const handleMoveClawRight = async () => {
	if (clawMachineRef.current) {
		clawMachineRef.current.moveClawRight()
	}
}

<ClawMachine alreadyDroppedBalls={[]}
    ref={clawMachineRef}
    addToDroppedBalls={(balls) => console.log(balls)}
    ballData={initialState}
/>
<button onMouseDown={() => handleMoveClawRight()}>Move Right</button>

Functions

moveClaw

  • Return Type: Promise<void>
  • Parameters:
    • x (number): The desired x-coordinate for the claw.
    • y (number): The desired y-coordinate for the claw.
    • angle (number): The desired angle for the claw's open/closed state.
    • immediateReturn (boolean, optional): If true, the claw closes if it touches a ball and throws it to the exit area.
  • Description: Moves the claw to a specified position. Resolves the promise once the position is reached.

moveClawRight

  • Return Type: void
  • Parameters: None
  • Description: Moves the claw to the right at a set speed (dx) until stopMoving() is called.

moveClawLeft

  • Return Type: void
  • Parameters: None
  • Description: Moves the claw to the left at a set speed (dx) until stopMoving() is called.

stopMoving

  • Return Type: void
  • Parameters: None
  • Description: Stops any ongoing movement initiated by moveClawRight() or moveClawLeft().

moveClawDown

  • Return Type: Promise<void>
  • Parameters:
    • clawDropDelay (number, optional): The delay in milliseconds between the claw reaching the drop position and its reset. Defaults to 500ms.
  • Description: Moves the claw downward and attempts to grab a ball. Resolves the promise once the claw operation completes.

Known Problems

Balls slip out of the claw after they are grabbed

Currently, sometimes balls are able to leave the claw. This is especially the case if:

  • The balls are very small
  • The horizontal speed of the claw is very high
  • Many balls are grabbed at once

The easiest way is to prevent the cases described above.

License

ClawJs is licensed under the MIT License. See the LICENSE file for details.