-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·139 lines (109 loc) · 3.15 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Setup Canvas
const canvas =document.querySelector("canvas")
const ctx = canvas.getContext("2d");
// dimensions
canvas.width = document.body.offsetWidth
canvas.height = document.body.offsetHeight;
console.log(canvas);
// Event Listener Refresh
// click, mousedown, mouseup, mousemove
canvas.addEventListener('click', ( e ) => {
console.log(e);
});
// Points
let mousedown = false;
let lastX = 0;
let lastY = 0;
// Set defaults
ctx.strokeStyle = 'black';
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.lineWidth = 10;
// Draw function
function draw(e){
if(mousedown === false) return;
ctx.strokeStyle;
ctx.beginPath();
ctx.moveTo(lastX, lastY)
ctx.lineTo(e.offsetX, e.offsetY)
ctx.stroke()
lastX = e.offsetX
lastY = e.offsetY
}
// Canvas Event: Watch mouse MOVE, then draw
// line from last point (lastX, lastY) to next point(offsetX, offsetY)
canvas.addEventListener("mousemove", draw );
// Canvas Event: Mouse DOWN, get points
canvas.addEventListener("mousedown", function(e) {
// check-check
console.log(e);
// mousedown, start drawing
lastX = e.offsetX;
lastY = e.offsetY;
mousedown = true
})
// Canvas Event: Mouse UP stop drawing and tracking
canvas.addEventListener("mouseup", function(e) {
// check-check
console.log(e);
// comment on/off. What happens?
mousedown = false
});
// Get buttons
const blackButton = document.querySelector("#black");
const whiteButton = document.querySelector('#white');
const coralButton = document.querySelector('#coral');
const redButton = document.querySelector('#red');
const orangeButton = document.querySelector('#orange');
const yellowButton = document.querySelector('#yellow');
const greenButton = document.querySelector('#green');
const blueButton = document.querySelector('#blue');
const purpleButton = document.querySelector('#purple');
const pinkButton = document.querySelector('#pink');
const clearButton = document.querySelector('#erase');
// Erase
clearButton.addEventListener('click', () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
// Change color to black
blackButton.addEventListener('click', () => {
ctx.strokeStyle = '#161a1d';
} );
// Change color to white
whiteButton.addEventListener('click', () => {
ctx.strokeStyle = 'white';
} );
// Change color to red
redButton.addEventListener('click', () => {
ctx.strokeStyle = '#ff0c2c';
});
// Change color to orange
orangeButton.addEventListener('click', () => {
ctx.strokeStyle = '#F58e16';
});
// Change color to yellow
yellowButton.addEventListener('click', () => {
ctx.strokeStyle = '#FFFF00';
});
// Change color to green
greenButton.addEventListener('click', () => {
ctx.strokeStyle = '#29ca40';
});
// Change color to blue
blueButton.addEventListener('click', () => {
ctx.strokeStyle = '#0c43ff';
});
// Change color to purple
purpleButton.addEventListener('click', () => {
ctx.strokeStyle = '#8000ff';
});
// Change color to pink
pinkButton.addEventListener('click', () => {
ctx.strokeStyle = '#ff00ff';
});
// Download Button
downloadButton.addEventListener('click', () => {
console.log(canvas.toDataURL());
});
// .click()
// bad to use because you can simulate a click and hack people