File size: 5,199 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
class Pages {
  constructor() {
    this.pages = [{}]; // Initially one empty page
    this.currentPage = 0;
    this.drawing = false; // Whether the user is currently drawing
    this.lastX = 0;
    this.lastY = 0;
    this.ctx = null; // Canvas context
    this.setupControls();
    this.setupCanvas();
    this.setupColorPicker();
    this.setupBrushSize();
  }

  getAllPages() {
    return this.pages; // Simply return all pages in the array
  }

  setPageContent(states) {
    const currentPageData = this.pages[this.currentPage];
    if (states.length > 0) {
      currentPageData.drawing = states[states.length - 1].imageData;  // Store only the latest state
    }
  }

  setupControls() {
    const prevBtn = document.getElementById('prevBtn');
    const nextBtn = document.getElementById('nextBtn');
    const pageInfo = document.getElementById('pageInfo');

    prevBtn.addEventListener('click', () => this.previousPage());
    nextBtn.addEventListener('click', () => this.nextPage());

    this.updatePageInfo();
  }

  setupCanvas() {
    const canvas = document.getElementById('whiteboard');
    this.ctx = canvas.getContext('2d');
    canvas.width = window.innerWidth - 40;
    canvas.height = window.innerHeight - 100;
    this.clearCanvas();

    // Event listeners for drawing
    canvas.addEventListener('mousedown', (e) => this.startDrawing(e));
    canvas.addEventListener('mousemove', (e) => this.draw(e));
    canvas.addEventListener('mouseup', () => this.stopDrawing());
    canvas.addEventListener('mouseout', () => this.stopDrawing());
  }

  getPageContent() {
    return this.pages[this.currentPage].drawing;  // Return the latest drawing state (imageData)
  }

  startDrawing(e) {
    this.drawing = true;
    const mousePos = getMousePos(this.ctx.canvas, e);
    this.lastX = mousePos.x;
    this.lastY = mousePos.y;
  }

  draw(e) {
    if (!this.drawing) return;
    const mousePos = getMousePos(this.ctx.canvas, e);
    this.ctx.beginPath();
    this.ctx.moveTo(this.lastX, this.lastY);
    this.ctx.lineTo(mousePos.x, mousePos.y);
    this.ctx.strokeStyle = this.pages[this.currentPage].color || '#000000'; // Default black if no color set
    this.ctx.lineWidth = this.pages[this.currentPage].brushSize || 2; // Default brush size
    this.ctx.stroke();
    this.lastX = mousePos.x;
    this.lastY = mousePos.y;
  }

  stopDrawing() {
    this.drawing = false;
  }

  setupColorPicker() {
    const colorPicker = document.getElementById('colorPicker');
    colorPicker.addEventListener('input', (event) => {
      this.pages[this.currentPage].color = event.target.value; // Store color for current page
    });
  }

  setupBrushSize() {
    const brushSizeInput = document.getElementById('brushSize');
    brushSizeInput.addEventListener('input', (event) => {
      this.pages[this.currentPage].brushSize = event.target.value; // Store brush size for current page
    });
  }

  getCurrentPage() {
    return this.currentPage;
  }

  getPageCount() {
    return this.pages.length;
  }

  addPage() {
    this.pages.push({}); // Add a new empty page
    this.currentPage = this.pages.length - 1;
    this.clearCanvas(); // Clear the canvas for the new page
    this.updatePageInfo();
  }

  previousPage() {
    if (this.currentPage > 0) {
      this.pages[this.currentPage].drawing = this.ctx.getImageData(0, 0, this.ctx.canvas.width, this.ctx.canvas.height); // Save current page's content
  
      this.currentPage--;
      this.loadPageData(); // Load the content of the previous page
      this.updatePageInfo();
    }
  }
  
  nextPage() {
    if (this.pages[this.currentPage]) {
      // Save content of current page to its storage
      this.pages[this.currentPage].drawing = this.ctx.getImageData(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
    }
  
    // Add new page and clear canvas
    if (this.pages[this.currentPage].drawing !== null) {
      // Add new page only if current page is not empty
      this.pages.push({ drawing: null });
      this.currentPage++;
    }
    
    this.clearCanvas(); // Clear the canvas for the new page
    this.loadPageData(); // Load the newly added page's data
    this.updatePageInfo();
  }

  updatePageInfo() {
    const pageInfo = document.getElementById('pageInfo');
    pageInfo.textContent = `Page ${this.currentPage + 1}`;
  }

  clearCanvas() {
    this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
  }

  loadPageData() {
    const pageData = this.pages[this.currentPage];
    
    // If there is saved content for this page, restore it
    if (pageData.drawing) {
      this.ctx.putImageData(pageData.drawing, 0, 0);
    } else {
      this.clearCanvas(); // If no content, clear canvas (empty page)
    }
    
    // You can add any additional settings here (color picker, brush size, etc.)
    if (pageData.color) {
      document.getElementById('colorPicker').value = pageData.color;
    }
    if (pageData.brushSize) {
      document.getElementById('brushSize').value = pageData.brushSize;
    }
  }
}

function getMousePos(canvas, evt) {
  const rect = canvas.getBoundingClientRect();
  return {
    x: evt.clientX - rect.left,
    y: evt.clientY - rect.top,
  };

}