All Projects → sebastianwachter → vue-scratchable

sebastianwachter / vue-scratchable

Licence: MIT License
A Vue.js wrapper component that turns everything into fun scratch cards.

Programming Languages

Vue
7211 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to vue-scratchable

Vue Info Card
Simple and beautiful card component with an elegant spark line, for VueJS.
Stars: ✭ 159 (+278.57%)
Mutual labels:  card, vue-components
gobo.icu
URL Shortener For Scratch
Stars: ✭ 14 (-66.67%)
Mutual labels:  scratch
vim-backscratch
Small scratches for Vim, feels nice
Stars: ✭ 20 (-52.38%)
Mutual labels:  scratch
hoc-element-table
📦 A Vue 3.x Table Component built on Webpack 5
Stars: ✭ 26 (-38.1%)
Mutual labels:  vue-components
vue-avatar-editor
Avatar editor for Vue.js. Demo on : https://fpluquet.github.io/vue-avatar-editor/
Stars: ✭ 85 (+102.38%)
Mutual labels:  vue-components
teaching-open
Scratch少儿编程教学平台,集成Scratch、ScratchJr、Python教学工具。包含课程、班级、作业、权限、赛事、社区等。
Stars: ✭ 202 (+380.95%)
Mutual labels:  scratch
vue-scrolly
Overlay scrollbar for Vue.js.
Stars: ✭ 24 (-42.86%)
Mutual labels:  vue-components
v-tostini
Toast plugin for Vue.js 2.x
Stars: ✭ 12 (-71.43%)
Mutual labels:  vue-components
gn-api-sdk-node
SDK em NodeJS integrada a API Gerencianet. Esta SDK está preparada para integração à API Pix e API Boletos da Gerencianet, que lhe permite realizar o gerenciamento de cobranças Pix com QR Code e Pix Copia e Cola, boleto/Bolix, carnê, cartão de crédito e muito mais.
Stars: ✭ 33 (-21.43%)
Mutual labels:  card
lovelace-climate-mode-entity-row
Climate mode entity for Lovelace
Stars: ✭ 49 (+16.67%)
Mutual labels:  card
ex pesa
Payment Library For Most Public Payment API's in Kenya and hopefully Africa. Let us get this moneybag
Stars: ✭ 19 (-54.76%)
Mutual labels:  card
ScratchVerifier
Verify Scratch accounts as genuine, for use in authorization or identification.
Stars: ✭ 17 (-59.52%)
Mutual labels:  scratch
vueplotlib
Declarative, interactive, linked 📊📈 components
Stars: ✭ 23 (-45.24%)
Mutual labels:  vue-components
vue-qrcode-component
Create QR codes with a simple Vue component
Stars: ✭ 97 (+130.95%)
Mutual labels:  vue-components
vue-eslint-editor
A code editor component to play ESLint.
Stars: ✭ 35 (-16.67%)
Mutual labels:  vue-components
vue-undraw
Vue unDraw Components: MIT licensed illustrations by unDraw (http://undraw.co) for your Vue projects
Stars: ✭ 31 (-26.19%)
Mutual labels:  vue-components
unkillable-scratch
Disallow the *scratch* buffer from being killed
Stars: ✭ 13 (-69.05%)
Mutual labels:  scratch
scratchie
👆 HTML5 canvas based scratch off panels
Stars: ✭ 18 (-57.14%)
Mutual labels:  scratchcard
vue
Vue.js Demos. jQWidgets Vue.js Components - Grids, Charts, Scheduling, Pivot Tables
Stars: ✭ 55 (+30.95%)
Mutual labels:  vue-components
v-page
A simple pagination bar, including length Menu, i18n support, based on Vue2.x
Stars: ✭ 85 (+102.38%)
Mutual labels:  vue-components

🦄 vue-scratchable 🏳️‍🌈🧽

Publishing status: Publishing status

A Vue.js wrapper component that turns everything into fun scratch cards. Includes touch support without additional dependencies.

It can also calculate percentage value of the scratchables's cleared area.

🎉 Installation

Install it with npm:

npm i vue-scratchable

Or directly in the browser:

<script src="https://unpkg.com/vue-scratchable@latest/dist/vue-scratchable.umd.min.js"></script>

Usage

Register the component globally:

import VueScratchable from 'vue-scratchable';

Vue.component('vue-scratchable', VueScratchable);

Or use it locally in your Single File Components:

import VueScratchable from 'vue-scratchable';

export default {
  /* ... */
  components: {
    VueScratchable,
  },
  /* ... */
};

In both cases you are now able to use it in the <template> of a component:

<vue-scratchable>
  <h1>Hello Scratchable World</h1>
</vue-scratchable>

🤔 Complete example as a Single File Component

This code is taken from this project's App.vue file to showcase the component's easy way of use.

<template>
  <div id="app">
    <h1>A beautiful parrot got trapped behind some paper.</h1>
    <h2>Scratch them free!</h2>
    <vue-scratchable
      v-slot="{ init }"
      :brushOptions="brush"
      :hideOptions="hide"
      getPercentageCleared
      @percentageUpdate="updatePoints"
    >
      <div class="wrapper">
        <img
          :src="require('./assets/mehmet-turgut-kirkgoz-vnayhPykN5Q-unsplash.jpg')"
          @load="init()"
        >
        <h3>{{ subline }}</h3>
      </div>
    </vue-scratchable>
    <p>You scratched {{ percentage }}% free.</p>
    <pre>Photo by <a href="https://unsplash.com/@tkirkgoz?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Mehmet Turgut Kirkgoz</a> on <a href="https://unsplash.com/t/animals?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></pre>
  </div>
</template>

<script>
import VueScratchable from 'vue-scratchable';
import paperPattern from './assets/natural-paper-texture.jpg';

export default {
  name: 'App',
  components: {
    VueScratchable,
  },
  computed: {
    subline() {
      return this.percentage < 100
        ? `🎉 There is still ${100 - this.percentage}% left for me to be free... 🎉`
        : '💚 Thank you for scratching me free! 💚';
    },
  },
  data() {
    return {
      percentage: 0,
      hide: {
        type: 'pattern',
        src: paperPattern,
        repeat: 'repeat',
      },
      brush: {
        size: 60,
        shape: 'round',
      },
    };
  },
  methods: {
    updatePoints(percentage) {
      this.percentage = percentage;
    },
  },
};
</script>

<style>
body {
  background-color: #333;
  margin: 0;
}

#app {
  font-family: 'Open Sans', sans-serif;
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  margin-top: 50px;
}

.vue-scratchable-wrapper {
  background-color: white;
}

h3 {
  color: #2c3e50;
  text-align: center;
}

a {
  color: #2196f3;
}
</style>

⚙️ Configuration

🎰 Slot

Slot name Description
default The content to be scratched free.

💡 Props

Property Type Description
brushOptions Object Configuration object of the "scratcher". See below for possible options.
hideOptions Object Configuration object of the scratchable layer. See below for possible options.
getPercentageCleared Boolean Flag to enable the percentageUpdate event which emits the amount of cleared paint as percentage.
percentageStride Number A stride used while calculating the cleared percentage to reduce calculation time.

🖊️ Brush options

Property Type Default Description
size Number 20 Defines the lineWidth attribute.
shape String round Defines the lineJoin attribute.

🙈 Hide options

There are two different types of fill that can be applied to the scratchable area: a solid colour or a canvas pattern. These are differentiated by the type property:

🟩 Solid colour
Property Type Description
type String Can be 'color' or 'pattern'. If you want it to be a solid colour you should set it to 'color'.
value String The colour you want for the fill. Can be any type of colour: hex or rgba.

Example:

const hide = {
  type: 'color',
  value: '#333',
};
🏳️‍🌈 Pattern
Property Type Description
type String Can be 'color' or 'pattern'. If you want it to be a pattern you should set it to 'pattern'.
src String A link to an image (best case is a repeatable texture). Can be an external Link as well as an imported local asset.
repeat String Defines whether and in which direction the image should be repeated. Possible values are "repeat", "no-repeat", "repeat-x" and "repeat-y".

Example:

const hide = {
  type: 'pattern',
  src: 'https://mdn.mozillademos.org/files/222/Canvas_createpattern.png',
  repeat: 'repeat',
};

🎈 Events

Event name Parameter type Description
percentage-update Number If the getPercentageCleared flag is set the component will emit this event and pass a number calculated from the percentage amount of the cleared area.

✔️ Caveats

  1. Img tags with local assets

When using img tags with local imported assets you have to call the init method of the component corresponding to the img tag's @load event. The init method can be accessed through the component's scoped slot.

Example:

<vue-scratchable v-slot="{ init }">
  <img
    :src="require('./img.jpg')"
    @load="init()"
  >
</vue-scratchable>
  1. Using external images with patterns won't calculate percentage values.

Patterns from external sources can't be used together with the percentage calculation. If done anyways it will resolve in the following CORS error:

Error in v-on handler: "SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data."
  1. Percentage calculation is very taxing on performance

The cleared area percentage calculation has to take every pixel of the canvas into consideration and analyzes whether they are cleared or not. Since this calculation gets called on every draw step this needs a lot of processing power. Because of that this feature is disabled by default and needs to be explicitly activated.

That's also why the percentageStride property should be set wisely and adjusted to your needs. It defines how many pixels should be skipped while calculating. This obviously also decreases the percentage's value accuracy.

🛠️ Contribution

If you find a bug or want to contribute to the code or documentation, you can help by submitting an issue or a pull request.

🧾 License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].