Skip to content

Commit

Permalink
fix: minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBLT committed Dec 13, 2023
1 parent 8ec41fc commit cbca3a9
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions lib/scratch-card.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class ScratchCard extends HTMLElement {
canvas = document.createElement('canvas');
span = document.createElement('span');
ctx = this.canvas.getContext('2d', { willReadFrequently: true });
pointer = {x: 0, y: 0};
canvas = document.createElement('canvas');
span = document.createElement('span');
ctx = this.canvas.getContext('2d', { willReadFrequently: true });
pointer = {x: 0, y: 0};
idleTimer = 0;

static observedAttributes = [
'code',
Expand Down Expand Up @@ -43,19 +44,24 @@ class ScratchCard extends HTMLElement {
}

get scratchSize() {
return parseInt(this.prop('scratch-size') ?? '8');
return parseInt(this.prop('scratch-size') ?? '10');
}

get noiseFactor() {
return parseInt(this.prop('noise-factor') ?? '20');
}

static {
onIdle(func) {

if (this.idleTimer) {
clearTimeout(this.idleTimer);
}

this.idleTimer = setTimeout(func, 500);
}

pointFromEvent(e) {
const bbox = this.canvas.getBoundingClientRect();
const bbox = this.getBoundingClientRect();

return {
x: e.clientX - bbox.left,
Expand All @@ -82,7 +88,6 @@ class ScratchCard extends HTMLElement {

for (const touch of e.targetTouches) {
Object.assign(this.pointer, this.pointFromEvent(touch));
break;
}

e.preventDefault();
Expand All @@ -97,8 +102,6 @@ class ScratchCard extends HTMLElement {
this.draw(p.x, p.y);

Object.assign(this.pointer, p);

break;
}

e.preventDefault();
Expand Down Expand Up @@ -148,25 +151,23 @@ class ScratchCard extends HTMLElement {

this.ctx.beginPath();
this.ctx.globalCompositeOperation = 'destination-out';

this.ctx.moveTo(
p.x + this.random(this.scratchSize),
p.y + this.random(this.scratchSize)
);

const randomizerCount = 15 * window.devicePixelRatio;

for (let i = 0; i < randomizerCount; i++ ) {
for (let i = 0; i < 20; i++ ) {
this.ctx.lineTo(
p.x + this.random(this.scratchSize),
p.y + this.random(this.scratchSize)
);
}

this.ctx.closePath();
this.ctx.fill();
this.ctx.fill('evenodd');

requestIdleCallback(() => this.updatePercentageScratched());
this.onIdle(() => this.updatePercentageScratched());
}
}

Expand Down Expand Up @@ -220,8 +221,6 @@ class ScratchCard extends HTMLElement {
this.ctx.fillStyle = gradient2;
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)
}


}

customElements.define('scratch-card', ScratchCard);

0 comments on commit cbca3a9

Please sign in to comment.