-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
88 lines (82 loc) · 2.44 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const scratchCard = document.getElementById('scratch');
const canvas = scratchCard.querySelector('canvas')
const ctx = canvas.getContext('2d');
canvas.width = scratchCard.offsetWidth;
canvas.height = scratchCard.offsetHeight;
const gradient = ctx.createConicGradient(0, 0, canvas.width, canvas.height);
gradient.addColorStop(0, '#4285f4');
gradient.addColorStop(0, '#206dd9');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#fff';
ctx.font = '24px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText('Scratch here for #update', canvas.width /2, canvas.height/2);
let isDrawing = false;
let isMouseDown = false;
function getMousePos(e){
const rect = canvas.getBoundingClientRect();
return{
x: (e.clientX - rect.left) / (rect.right - rect.left) * canvas.width,
y: (e.clientY - rect.top) / (rect.bottom - rect.top) * canvas.height
};
}
function getTouchPos(e){
const rect = canvas.getBoundingClientRect();
const touch = e.touches[0];
return{
x: (touch.clientX - rect.left) / (rect.right - rect.left) * canvas.width,
y: (touch.clientY - rect.top) / (rect.bottom - rect.top) * canvas.height
};
}
canvas.addEventListener('mousedown', function(e){
isDrawing = true;
isMouseDown = true;
const pos = getMousePos(e);
scratch(pos.x, pos.y);
});
canvas.addEventListener('mousemove',function(e){
if(isDrawing && isMouseDown){
const pos = getMousePos(e);
scratch(pos.x, pos.y);
}
});
canvas.addEventListener('touchmove', function(e){
e.preventDefault();
if (isDrawing){
const pos = getMousePos(e);
scratch(pos.x. pos.y);
}
});
canvas.addEventListener('mouseup', function(e){
isDrawing = false;
isMouseDown = false;
});
canvas.addEventListener('mouseleave', function(e){
isDrawing = false;
});
canvas.addEventListener('mouseenter', function(e){
if (isMouseDown){
isDrawing = true;
}
});
document.addEventListener('mouseup', function(e){
isMouseDown = false;
});
canvas.addEventListener('touchstart', function(e){
e.preventDefault();
isDrawing = true;
const pos = getTouchPos(e);
scratch(pos.x, pos.y);
});
canvas.addEventListener('touchpad',function(e){
e.preventDefault();
isDrawing = false;
});
function scratch(x,y){
ctx.globalCompositeOperation = 'destination-out';
ctx.beginPath();
ctx.arc(x, y, 50, 0, 2*Math.PI);
ctx.fill();
}