项目作者: phenax

项目描述 :
Pattern lock library for the web using canvas
高级语言: JavaScript
项目地址: git://github.com/phenax/pattern-lock-js.git
创建时间: 2017-03-28T13:24:38Z
项目社区:https://github.com/phenax/pattern-lock-js

开源协议:Apache License 2.0

下载


PatternLockJS

A pattern lock library for the web. Demo

@phenax/pattern-lock-js.svg?style=flat-square" alt="npm (scoped)">
@phenax/pattern-lock-js.svg?style=flat-square" alt="npm bundle size (minified + gzip)">
@phenax/pattern-lock-js.svg?style=flat-square" alt="NpmLicense">
Buy Me A Coffee donate button

Installation

Install the library with

  1. yarn add @phenax/pattern-lock-js

Import the library with

  1. import PatternLock from '@phenax/pattern-lock-js';

Get started

  1. const lock = PatternLock({
  2. $canvas: document.querySelector('#patternLock'),
  3. width: 300,
  4. height: 430,
  5. grid: [ 3, 3 ],
  6. });

Customize the theme

  1. lock.setTheme('dark');
  2. lock.setTheme('light');
  3. // Or pass a custom theme
  4. lock.setTheme({
  5. default: {
  6. colors: {
  7. accent: '#1abc9c', // Accent color for node
  8. primary: '#ffffff', // Primary node and line color
  9. bg: '#2c3e50', // Canvas background color
  10. },
  11. dimens: {
  12. node_radius: 20, // Radius of the outer ring of a node
  13. line_width: 6, // Thickness of the line joining nodes
  14. node_core: 8, // Radius of the inner circle of a node
  15. node_ring: 1, // Outer ring thickness
  16. }
  17. },
  18. success: {
  19. colors: {
  20. accent: '#51e980', // Green accent on successful match
  21. }
  22. },
  23. failure: {
  24. colors: {
  25. accent: '#e74c3c', // Red accent on an unsuccessful match
  26. }
  27. },
  28. customState: { // Your custom state
  29. dimens: {
  30. node_radius: 25, // Increases the node radius
  31. }
  32. },
  33. });

Manually change the state

  1. lock.setThemeState('success'); // Switch state to successful
  2. lock.setThemeState('customState'); // Switch to your custom state

You can even change the grid size dynamically

  1. lock.setGrid(4, 4); // 4x4 grid instead of the default 3x3

Callback for when the pattern is complete

  1. lock.onComplete(({ hash }) => (myRealHash === hash) ? success() : failure());

Or you can use the matchHash helper to check if the hash matches your set of correct passwords

  1. // If the pattern drawn is a Right L or a Diagonal L,
  2. // then turn the pattern green
  3. // else turn it red
  4. lock.matchHash([ 'LTU2MTIyNjM0Ng==', 'MTk1OTMwNzY2NQ==' ])
  5. .onSuccess(() => lock.setThemeState('success'))
  6. .onFailure(() => lock.setThemeState('failure'));

Destroy to enable scrolling on touch

  1. lock.destroy();