-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
118 lines (99 loc) · 3.64 KB
/
Copy pathscript.js
File metadata and controls
118 lines (99 loc) · 3.64 KB
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
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const bgx = document.getElementById('MyCanvas');
const bgColor = document.querySelector('input.bg_color');
const inputColor = document.querySelector('.input__color');
const tools = document.querySelectorAll('.button__tool');
const sizeButtons = document.querySelectorAll('.button__size');
const buttonClear = document.querySelector('.button__clear');
let brushSize = 10;
let isPaining = false;
let activeTool = "brush";
inputColor.addEventListener('change', ({ target }) => {
ctx.fillStyle = target.value;
});
bgColor.addEventListener('change', (event) => {
bgx.style.backgroundColor = event.target.value;
});
// Verifica se há uma cor salva no Local Storage
if (localStorage.getItem('canvasBgColor')) {
// Define a cor de fundo do canvas com base no valor armazenado no Local Storage
bgx.style.backgroundColor = localStorage.getItem('canvasBgColor');
}
bgColor.addEventListener('change', (event) => {
// Obtém o valor da cor selecionada
const selectedColor = event.target.value;
// Define a cor de fundo do canvas com a cor selecionada
bgx.style.backgroundColor = selectedColor;
// Armazena a cor selecionada no Local Storage
localStorage.setItem('canvasBgColor', selectedColor);
});
//função para verificar a ferramenta ativa e chamar a função apropriada
canvas.addEventListener('mousedown', (event) => {
const { clientX, clientY } = event;
isPaining = true;
if (activeTool == "brush") {
draw(clientX, clientY);
}
if (activeTool == "rubber") {
erase(clientX, clientY);
}
});
// Função para verificar se não está desenhando
canvas.addEventListener('mouseup', (event) => {
isPaining = false;
});
//Função para apagar ou desenhar enquanto mouse se move
window.addEventListener('mousemove', function (e) {
if (isPaining && activeTool == "brush") {
draw(e.x, e.y);
}
if (isPaining && activeTool == "rubber") {
erase(e.x, e.y);
}
});
//função para desenhar
const draw = (x, y) => {
ctx.globalCompositeOperation = "source-over"
ctx.beginPath()
ctx.arc(x - canvas.offsetLeft, y - canvas.offsetTop, brushSize / 2, 0, 90);
ctx.fill();
};
//borracha
const erase = (x, y) => {
ctx.globalCompositeOperation = "destination-out";
ctx.beginPath()
ctx.arc(x - canvas.offsetLeft, y - canvas.offsetTop, brushSize / 2, 0, 90);
ctx.fill();
};
//altera ferramenta para ser usada
tools.forEach((tool) => {
tool.addEventListener('click', ({ target }) => {
//#Closest vai buscar um elemento que está mais próximo do target (button-tools)
const selectedTool = target.closest("button");
const action = selectedTool.getAttribute("data-action");
if (action) {
tools.forEach((tool) => {
tool.classList.remove("active");
})
activeTool = action;
selectedTool.classList.add("active");
}
});
});
//Altera o tamanho do pincel
sizeButtons.forEach((button) => {
button.addEventListener('click', ({ target }) => {
//#Closest vai buscar um elemento que está mais próximo do target (button-buttons)
const selectedButton = target.closest("button");
const size = selectedButton.getAttribute("data-size");
sizeButtons.forEach((buttons) => {
buttons.classList.remove("active");
brushSize = size;
selectedButton.classList.add("active");
})
});
})
buttonClear.addEventListener('click', () => {
ctx.clearRect(0, 0, canvas.width,canvas.height);
});