Skip to content

Latest commit

 

History

History
173 lines (113 loc) · 4.5 KB

README.md

File metadata and controls

173 lines (113 loc) · 4.5 KB

Responsive Jigsaw Puzzle

A responsive javascript jigsaw plugin utilizing HTML5 Drag and Drop API.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

Javascript dependencies that need to be included:

<!-- Polyfill JS -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default"></script>

Installing

  1. Include the core plugin files on your page:
<!-- CSS -->
<link rel="stylesheet" href="../path/to/file/puzzle.css">

<!-- Javascript -->
<script src="../path/to/file/puzzle.js"></script>
  1. Create a variable and initialize the plugin.
var puzzle = new Puzzle({options});

Avaialbe Options

Property Description Default
el Container element which will hold the puzzle. This option MUST be passed a valid HTML element null
image URL for the image to be used. Must be a valid src value for the <img> tag :)
numcolumns Number of columns in the puzzle grid 3
numrows Number of rows in the puzzle grid 3
difficulty Determines how much of the puzzle is shuffled. Values include "easy","normal","hard" and "expert" "normal"
debug Outputs useful information for drag/drop events. Must be set as a boolean value false

Methods

Init

Usage:

init()

Initialize puzzle instance and attach to container element.

returns puzzle instance.

setOpts

Usage:

setOpts( Object[ {options} ] )

Redeclare options on current puzzle.

returns puzzle instance.

setDifficulty

Usage:

setDifficulty( String["easy" | "normal" | "hard" | "expert"] )

Set difficulty of a puzzle using one of the predefined options.

returns puzzle instance.

setGridSize

Usage:

setGridSize( Object[ { [numrows, numcolumns] } ])

Update the number of rows or columns for a puzzle.

returns puzzle instance.

setImage

Usage:

setImage( String[ image_url ] )

Update the puzzle image (Must be a valid src value for the <img> tag).

returns puzzle instance.

isSorted

Usage:

isSorted()

Check to see if the puzzle is completed.

returns true or false.

getTiles

Usage:

getTiles()

Grabs the current order of tiles in the puzzle.

returns multi-dimensional array of tiles, the first item is the original position of the tile and the second is the tile itself.

correctTiles

Usage:

correctTiles()

Counts the number of tiles in order and returns that number.

returns number value of correct tiles.

Callbacks

These events are exposed through the settings and accept callback functions:

  • dragstart - Fires when a tile is first dragged.
  • dragenter - Fires when a tile enters a dropzone.
  • drag - Fires while a tile is currently being dragged.
  • dragover - Fires while a tile is dragged over a dropzone.
  • dragleave - Fires when a tile is exits a dropzone.
  • dragend - Fires when a tile is no longer being dragged.
  • drop - Fires when a tile is dropped.
  • touchstart - Fires when a tile is touched.
  • touchmove - Fires while a tile is dragged over the document.
  • touchhover - Fires while a tile is dragged over a dropzone.
  • touchend - Fires when a tile is no longer being dragged.
  • correct - Fires when a tile is dropped into the correct slot.
  • finished - Fires when the entire puzzle is completed.

To use callbacks pass a custom function to one of the above events in your initial settings.

Each callback is passed a custom object that includes a reference to the puzzle instance, the event and the event target.

Running the tests

You can include the debug option to output useful information to the console.

Built With

Authors

  • Eric Harris - Initial work - Eric-Jr

License

This project is licensed under the MIT License - see the LICENSE.md file for details