File size: 1,260 Bytes
d7508be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class DrawingTools {
  constructor(canvas, ctx) {
    this.canvas = canvas;
    this.ctx = ctx;
    this.color = '#000000';
    this.brushSize = 2;
    this.isEraser = false;
    this.setupTools();
  }

  setupTools() {
    const colorPicker = document.getElementById('colorPicker');
    const brushSize = document.getElementById('brushSize');
    const brushSizeLabel = document.getElementById('brushSizeLabel');
    const eraserBtn = document.getElementById('eraserBtn');

    colorPicker.addEventListener('input', (e) => {
      this.color = e.target.value;
      this.isEraser = false;
      eraserBtn.classList.remove('active');
    });

    brushSize.addEventListener('input', (e) => {
      this.brushSize = e.target.value;
      brushSizeLabel.textContent = `${this.brushSize}px`;
    });

    eraserBtn.addEventListener('click', () => {
      this.isEraser = !this.isEraser;
      eraserBtn.classList.toggle('active');
    });
  }

  applyToolSettings() {
    if (this.isEraser) {
      this.ctx.globalCompositeOperation = 'destination-out';
      this.ctx.strokeStyle = 'rgba(0,0,0,1)';
    } else {
      this.ctx.globalCompositeOperation = 'source-over';
      this.ctx.strokeStyle = this.color;
    }
    this.ctx.lineWidth = this.brushSize;
  }
}