After some trial and error, I managed to get it functioning by adjusting the event listener to attach to the document itself rather than the canvas element.
document.addEventListener('mousedown', (event) => {
console.log('mousedown called');
event.preventDefault();
this._setScratchPosition();
document.addEventListener('mousemove', this.scratching);
this.callbackFun();
Alternatively, you could also use:
window.addEventListener('mousedown', (event) => {
console.log('mousedown called');
event.preventDefault();
this._setScratchPosition();
window.addEventListener('mousemove', this.scratching);
this.callbackFun();
});
If you prefer a more specific approach, you can declare a new variable like so:
public c: any;
and then add:
this.c = document.getElementById('myCanvas');
this.c.addEventListener('scratch.move', () => {
this.percent = Number(this.getPercent().toFixed(2));
console.log('scratch.move called');
if (this.percent > 5) {
this.togglePrizeLabel();
}
});
By following these adjustments, everything now functions as intended. It is important to note that direct referencing of a canvas object may not work as expected, as discussed in this Stack Overflow thread.