Skip to content

Commit e915a25

Browse files
committedJun 4, 2022
feat: accept HTMLElement or string on the constructor
1 parent 4ef5f51 commit e915a25

File tree

6 files changed

+11
-6
lines changed

6 files changed

+11
-6
lines changed
 

‎build/scratchcard.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎build/scratchcard.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<script type="text/javascript">
1919
window.addEventListener('load', function () {
2020
var scContainer = document.getElementById('js--sc--container');
21-
var sc = new ScratchCard('#js--sc--container', {
21+
var sc = new ScratchCard(scContainer, {
2222
enabledPercentUpdate: true,
2323
scratchType: SCRATCH_TYPE.LINE,
2424
// brushSrc: './images/brush.png',

‎package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ and pick in the folder **./build** the file **scratchard.min.js**
2323
import {ScratchCard, SCRATCH_TYPE} from 'scratchcard-js'
2424

2525
const scContainer = document.getElementById('js--sc--container')
26+
27+
// You also can pass HTML Element or a string for querySelector
28+
// new ScratchCard(htmlElement...
29+
2630
const sc = new ScratchCard('#js--sc--container', {
2731
scratchType: SCRATCH_TYPE.SPRAY,
2832
containerWidth: scContainer.offsetWidth,

‎src/ScratchCard.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ScratchCard {
1818
public zone: {top: number, left: number};
1919
public percent: number;
2020

21-
constructor (selector: string, config: SC_CONFIG) {
21+
constructor (selector: string|HTMLElement, config: SC_CONFIG) {
2222
const self = this;
2323
const defaults = {
2424
scratchType: SCRATCH_TYPE.LINE,
@@ -40,7 +40,8 @@ class ScratchCard {
4040

4141
this.config = {...defaults, ...config};
4242
this.scratchType = this.config.scratchType;
43-
this.container = <HTMLElement> document.querySelector(selector);
43+
this.container = selector instanceof HTMLElement ?
44+
selector : document.querySelector(String(selector));
4445
this.position = [0, 0]; // init position
4546
this.readyToClear = false;
4647
this.percent = 0;

0 commit comments

Comments
 (0)
Failed to load comments.