Giter VIP home page Giter VIP logo

scratch-it's Introduction

scratch-it

A light weight javascript library for simulating a scratch off lottery ticket. It works on mobile and is responsive.

Usage

A new ScratchIt instance may be instantiated with:

ScratchIt( containerEl, overlayImgUrl, brushImgUrl, onReveal, revealPercent );
Argument Required Description
containerEl Required A DOM Element that will have an HTML5 <canvas> element appended within. Typically this element will also contain an image or have a background image that is meant to be reavealed once the overlay has been scratched off.
overlayImgUrl Required A URL to an image that will act as the overlay image. This is the image the user will interact with and scratch off.
brushImgUrl Required A URL to an image that will act as a "brush". This can be used to give different textures to the edges where the user has scratched off.
onReveal Optional A user defined callback function that is executed after some percentage of the overlay image has been scratched off. Default: none.
revealPercent Optional The percentage of the overlay image that must be scratched off before the onReveal handler is called. Must be a number between 0 and 100. Default: 50

Testing For Support

The library comes with a simple isSupported() method for testing whether the browser supports the browser technologies necessary to function.

if(ScratchIt.isSupported()){
  // Library is supported
}

Example Code

<div id="scratch"><img src="hiddenMessage.jpg" /></div>

<script src="ScratchIt.min.js"></script>
<script>
  var containerEl = document.getElementById('scratch'),
      overlayImgUrl = 'overlay.jpg',
      brushImgUrl = 'brush.png',
      revealPercent = 50;

  function onReveal(){
    // TODO: Your code here
  }

  if(ScratchIt.isSupported()){
    ScratchIt(containerEl, overlayImgUrl, brushImgUrl, onReveal, revealPercent);
  }
</script>

Demo

Visit the Demo Page to see this library in action.

Browser Support

This library has been tested to work with:

  • Chrome
  • Safari
  • Firefox
  • Internet Explorer 9+
  • Edge 16+
  • Opera 51+
  • Android 4.4, 6.0, 7.0, 8.0
  • iOS 10+

Other browsers may be supported as well, but have not been tested. This library requires the browser to support HTML5 Canvas and RequestAnimationFrame

Caveats

If your image assets are hosted on another domain (like a CDN), this host will need to have CORS enabled. CORS is required so that HTML5 canvas may have permission to read the contents of an image from an outside domain. Please note that Internet Explorer 9 does not have the ability to properly fetch images this way and will not work with images hosted on another domain.

scratch-it's People

Contributors

porcupine021 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.