File size: 7,394 Bytes
fa31ba2
 
9ee0f7d
 
 
fa31ba2
 
9ee0f7d
 
 
 
 
 
 
 
 
 
 
 
fa31ba2
9ee0f7d
 
 
 
fa31ba2
 
 
9ee0f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa31ba2
9ee0f7d
fa31ba2
9ee0f7d
 
 
 
 
 
 
 
 
 
 
 
 
fa31ba2
9ee0f7d
 
 
 
fa31ba2
9ee0f7d
 
 
fa31ba2
9ee0f7d
 
 
 
 
fa31ba2
9ee0f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
fa31ba2
 
9ee0f7d
 
fa31ba2
 
9ee0f7d
fa31ba2
9ee0f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa31ba2
9ee0f7d
 
 
 
fa31ba2
9ee0f7d
 
 
 
 
 
 
 
fa31ba2
9ee0f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa31ba2
9ee0f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa31ba2
 
9ee0f7d
 
 
 
 
 
 
 
 
fa31ba2
9ee0f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa31ba2
9ee0f7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa31ba2
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
def whiteboard_iframe():
    board_html = r"""
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>سبورة الطالب</title>

  <style>
    * {
      box-sizing: border-box;
    }

    body {
      margin: 0;
      font-family: Arial, sans-serif;
      background: #f1f5f9;
      color: #0f172a;
      overflow: hidden;
    }

    .app {
      padding: 10px;
      height: 100vh;
      display: flex;
      flex-direction: column;
    }

    .toolbar {
      display: flex;
      flex-wrap: wrap;
      gap: 8px;
      align-items: center;
      background: #ffffff;
      border: 1px solid #cbd5e1;
      border-radius: 14px;
      padding: 10px;
      margin-bottom: 10px;
      box-shadow: 0 2px 8px rgba(15, 23, 42, 0.08);
    }

    button {
      border: 1px solid #94a3b8;
      background: #ffffff;
      color: #0f172a;
      padding: 9px 12px;
      border-radius: 10px;
      font-size: 14px;
      font-weight: bold;
      cursor: pointer;
    }

    button.active {
      background: #0f172a;
      color: #ffffff;
    }

    .tool-label {
      display: flex;
      align-items: center;
      gap: 6px;
      font-size: 13px;
      font-weight: bold;
    }

    input[type="range"] {
      width: 100px;
    }

    .board-shell {
      flex: 1;
      overflow: auto;
      background: #e2e8f0;
      border: 2px solid #334155;
      border-radius: 16px;
      position: relative;
      min-height: 560px;
    }

    canvas {
      display: block;
      background:
        linear-gradient(#e5e7eb 1px, transparent 1px),
        linear-gradient(90deg, #e5e7eb 1px, transparent 1px),
        #ffffff;
      background-size: 32px 32px;
      touch-action: none;
      cursor: crosshair;
    }

    .note {
      font-size: 12px;
      line-height: 1.5;
      color: #475569;
      background: #ffffff;
      padding: 8px;
      border-radius: 10px;
      border: 1px solid #cbd5e1;
      margin-top: 8px;
    }
  </style>
</head>

<body>
  <div class="app">
    <div class="toolbar">
      <button id="penBtn" class="active">✏️ قلم</button>
      <button id="eraserBtn">🧽 محاية</button>
      <button id="panBtn">🤚 تحريك</button>
      <button id="undoBtn">↩️ تراجع</button>
      <button id="clearBtn">🗑️ مسح</button>

      <label class="tool-label">
        الحجم
        <input id="sizeInput" type="range" min="1" max="10" value="3">
      </label>

      <label class="tool-label">
        اللون
        <input id="colorInput" type="color" value="#000000">
      </label>
    </div>

    <div id="boardShell" class="board-shell">
      <canvas id="board" width="1800" height="5200"></canvas>
    </div>

    <div class="note">
      اكتب بالقلم. استخدم "تحريك" للتمرير داخل السبورة. استخدم المحاية للمسح الجزئي.
    </div>
  </div>

  <script>
    const canvas = document.getElementById("board");
    const shell = document.getElementById("boardShell");
    const ctx = canvas.getContext("2d");

    const penBtn = document.getElementById("penBtn");
    const eraserBtn = document.getElementById("eraserBtn");
    const panBtn = document.getElementById("panBtn");
    const undoBtn = document.getElementById("undoBtn");
    const clearBtn = document.getElementById("clearBtn");
    const sizeInput = document.getElementById("sizeInput");
    const colorInput = document.getElementById("colorInput");

    let mode = "pen";
    let drawing = false;
    let lastX = 0;
    let lastY = 0;
    let history = [];

    ctx.lineCap = "round";
    ctx.lineJoin = "round";

    function setMode(nextMode) {
      mode = nextMode;

      penBtn.classList.toggle("active", mode === "pen");
      eraserBtn.classList.toggle("active", mode === "eraser");
      panBtn.classList.toggle("active", mode === "pan");

      if (mode === "pan") {
        canvas.style.touchAction = "auto";
        canvas.style.cursor = "grab";
      } else {
        canvas.style.touchAction = "none";
        canvas.style.cursor = "crosshair";
      }
    }

    function saveHistory() {
      try {
        if (history.length > 15) {
          history.shift();
        }

        history.push(canvas.toDataURL("image/png"));
      } catch (e) {
        console.log(e);
      }
    }

    function restoreFromDataUrl(dataUrl) {
      const img = new Image();

      img.onload = function () {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, 0, 0);
      };

      img.src = dataUrl;
    }

    function getPoint(event) {
      const rect = canvas.getBoundingClientRect();

      let clientX;
      let clientY;

      if (event.touches && event.touches.length > 0) {
        clientX = event.touches[0].clientX;
        clientY = event.touches[0].clientY;
      } else {
        clientX = event.clientX;
        clientY = event.clientY;
      }

      return {
        x: clientX - rect.left,
        y: clientY - rect.top
      };
    }

    function startDraw(event) {
      if (mode === "pan") {
        return;
      }

      event.preventDefault();

      saveHistory();

      drawing = true;

      const point = getPoint(event);
      lastX = point.x;
      lastY = point.y;
    }

    function draw(event) {
      if (!drawing || mode === "pan") {
        return;
      }

      event.preventDefault();

      const point = getPoint(event);

      ctx.beginPath();
      ctx.moveTo(lastX, lastY);
      ctx.lineTo(point.x, point.y);

      if (mode === "eraser") {
        ctx.globalCompositeOperation = "destination-out";
        ctx.lineWidth = Math.max(12, parseInt(sizeInput.value, 10) * 3);
      } else {
        ctx.globalCompositeOperation = "source-over";
        ctx.strokeStyle = colorInput.value;
        ctx.lineWidth = parseInt(sizeInput.value, 10);
      }

      ctx.stroke();

      lastX = point.x;
      lastY = point.y;
    }

    function stopDraw() {
      drawing = false;
      ctx.globalCompositeOperation = "source-over";
    }

    penBtn.addEventListener("click", function () {
      setMode("pen");
    });

    eraserBtn.addEventListener("click", function () {
      setMode("eraser");
    });

    panBtn.addEventListener("click", function () {
      setMode("pan");
    });

    undoBtn.addEventListener("click", function () {
      if (history.length > 0) {
        const last = history.pop();
        restoreFromDataUrl(last);
      }
    });

    clearBtn.addEventListener("click", function () {
      if (confirm("هل تريد مسح السبورة؟")) {
        saveHistory();
        ctx.clearRect(0, 0, canvas.width, canvas.height);
      }
    });

    canvas.addEventListener("mousedown", startDraw);
    canvas.addEventListener("mousemove", draw);
    window.addEventListener("mouseup", stopDraw);

    canvas.addEventListener("touchstart", startDraw, { passive: false });
    canvas.addEventListener("touchmove", draw, { passive: false });
    window.addEventListener("touchend", stopDraw);

    setMode("pen");
  </script>
</body>
</html>
"""

    escaped_board = html.escape(board_html, quote=True)

    return f"""
<iframe
    srcdoc="{escaped_board}"
    style="width:100%; height:780px; border:1px solid #cbd5e1; border-radius:14px; background:white;"
    title="السبورة">
</iframe>
"""