A.Olokhtonov
2 years ago
9 changed files with 178 additions and 193 deletions
@ -1,26 +1,35 @@ |
|||||||
function tools_switch(state, tool) { |
function switch_tool(state, item) { |
||||||
|
const tool = item.getAttribute('data-tool'); |
||||||
|
|
||||||
if (state.tools.active_element) { |
if (state.tools.active_element) { |
||||||
state.tools.active_element.classList.remove('active'); |
state.tools.active_element.classList.remove('active'); |
||||||
} |
} |
||||||
|
|
||||||
state.tools.active = tool; |
state.tools.active = tool; |
||||||
state.tools.active_element = document.querySelector(`.tool[data-tool="${tool}"]`); |
state.tools.active_element = item; |
||||||
state.tools.active_element.classList.add('active'); |
state.tools.active_element.classList.add('active'); |
||||||
} |
} |
||||||
|
|
||||||
function init_tools(state, context) { |
function switch_color(state, item) { |
||||||
const pencil = document.querySelector('.tool[data-tool="pencil"]'); |
const color = item.getAttribute('data-color'); |
||||||
const ruler = document.querySelector('.tool[data-tool="ruler"]'); |
|
||||||
const eraser = document.querySelector('.tool[data-tool="eraser"]'); |
if (state.colors.active_element) { |
||||||
const undo = document.querySelector('.tool[data-tool="undo"]'); |
state.colors.active_element.classList.remove('active'); |
||||||
|
} |
||||||
pencil.addEventListener('click', () => tools_switch(state, 'pencil')); |
|
||||||
ruler.addEventListener('click', () => tools_switch(state, 'ruler')); |
state.colors.active = color_to_u32(color); |
||||||
eraser.addEventListener('click', () => tools_switch(state, 'eraser')); |
state.colors.active_element = item; |
||||||
undo.addEventListener('click', () => { |
state.colors.active_element.classList.add('active'); |
||||||
pop_stroke(state, context); |
} |
||||||
window.requestAnimationFrame(() => draw(state, context)); |
|
||||||
}); |
function init_tools(state) { |
||||||
|
const tools = document.querySelectorAll('.tools .tool'); |
||||||
tools_switch(state, 'pencil'); |
const colors = document.querySelectorAll('.pallete .color'); |
||||||
|
|
||||||
|
tools.forEach((item) => { item.addEventListener('click', () => switch_tool(state, item)); }); |
||||||
|
colors.forEach((item) => { item.addEventListener('click', () => switch_color(state, item)); }); |
||||||
|
|
||||||
|
// TODO: from localstorage
|
||||||
|
switch_tool(state, document.querySelector('.tool[data-tool="pencil"]')); |
||||||
|
switch_color(state, document.querySelector('.color[data-color="000000"]')); |
||||||
} |
} |
Loading…
Reference in new issue