dsh-department 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,621 @@
1
+ <!doctype html>
2
+ <html lang="ru">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>Пиксель-редактор отдела</title>
7
+ <style>
8
+ :root { --bg: #0b0f18; --panel: #141a28; --line: #26324a; --text: #e6edf7; --muted: #8b98ad; }
9
+ * { box-sizing: border-box; }
10
+ body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; }
11
+ .wrap { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 16px; padding: 16px; }
12
+ .panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
13
+ h1 { font-size: 16px; margin: 0 0 4px; }
14
+ .muted { color: var(--muted); font-size: 12px; }
15
+ .row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
16
+ button { background: #1b2438; color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; cursor: pointer; font-size: 13px; }
17
+ button.active { border-color: #4b8bff; color: #cfe3ff; }
18
+ button:hover { border-color: #3d4a63; }
19
+ select, input[type="number"], input[type="color"] { background: #1b2438; color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 4px 6px; }
20
+ input[type="color"] { width: 42px; height: 32px; padding: 2px; }
21
+ .swatches { display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px; }
22
+ .swatch { width: 100%; aspect-ratio: 1; border-radius: 4px; border: 1px solid #0008; cursor: pointer; }
23
+ .swatch.active { outline: 2px solid #fff; }
24
+ canvas { display: block; image-rendering: pixelated; background: #222; border-radius: 8px; cursor: crosshair; touch-action: none; }
25
+ .canvas-wrap { background: #10151f; border: 1px solid var(--line); border-radius: 12px; padding: 12px; overflow: auto; display: flex; justify-content: center; }
26
+ .status { font-size: 12px; color: var(--muted); }
27
+ .hint { font-size: 11px; color: var(--muted); }
28
+ </style>
29
+ </head>
30
+ <body>
31
+ <div class="wrap">
32
+ <div class="panel">
33
+ <h1>Пиксель-редактор</h1>
34
+ <div class="muted">Рисуй спрайт, сохраняй PNG или JSON-сетку.</div>
35
+
36
+ <div class="row">
37
+ <label class="muted">Зум</label>
38
+ <button id="zoomOut">−</button>
39
+ <span class="muted" id="zoomLabel">8×</span>
40
+ <button id="zoomIn">+</button>
41
+ <button id="zoomFit">Вписать</button>
42
+ </div>
43
+
44
+ <div class="row">
45
+ <label class="muted">Размер</label>
46
+ <select id="size">
47
+ <option value="16x16">16×16</option><option value="32x32" selected>32×32</option><option value="48x48">48×48</option><option value="64x64">64×64</option><option value="96x96">96×96</option><option value="16x48">16×48 персонаж</option><option value="32x64">32×64 персонаж</option>
48
+ </select>
49
+ <button id="gridToggle" class="active">Сетка</button>
50
+ </div>
51
+
52
+ <div class="row">
53
+ <button id="pencil" class="active">Карандаш</button>
54
+ <button id="eraser">Ластик</button>
55
+ <button id="fill">Заливка</button>
56
+ <button id="picker">Пипетка</button>
57
+ </div>
58
+ <div class="row">
59
+ <button id="line">Линия</button>
60
+ <button id="rect">Прямоуг.</button>
61
+ <button id="circle">Круг</button>
62
+ <button id="select">Выделение</button>
63
+ </div>
64
+ <div class="row">
65
+ <label class="muted"><input type="checkbox" id="template"> шаблон персонажа</label>
66
+ </div>
67
+ <div class="row">
68
+ <label class="muted"><input type="checkbox" id="filled"> залитый</label>
69
+ <input type="color" id="color" value="#7fdc9a">
70
+ </div>
71
+
72
+ <div class="swatches" id="swatches"></div>
73
+
74
+ <div class="row">
75
+ <button id="copy">Копировать</button>
76
+ <button id="cut">Вырезать</button>
77
+ <button id="paste">Вставить</button>
78
+ <button id="del">Удалить</button>
79
+ <button id="deselect">Снять</button>
80
+ </div>
81
+
82
+ <div class="row">
83
+ <button id="undo">Отменить</button>
84
+ <button id="redo">Вернуть</button>
85
+ <button id="savePng">PNG</button>
86
+ <button id="saveJson">JSON</button>
87
+ <button id="clear">Очистить</button>
88
+ </div>
89
+
90
+ <div class="row">
91
+ <input class="dept-input" id="spriteName" value="sprite" placeholder="имя спрайта" style="min-width:110px">
92
+ <button id="saveOffice">Сохранить в проект</button>
93
+ </div>
94
+ <div class="row">
95
+ <label class="muted">Трафарет</label>
96
+ <input type="file" id="trace" accept="image/png,image/jpeg,image/webp" style="max-width:150px">
97
+ <input type="range" id="traceOpacity" min="5" max="80" value="35" title="прозрачность трафарета">
98
+ </div>
99
+ <div class="row">
100
+ <input class="dept-input" id="note" placeholder="что дорисовать художнику" style="min-width:180px">
101
+ <button id="handoff">Передать художнику</button>
102
+ </div>
103
+ <div class="muted" id="officeStatus"></div>
104
+
105
+ <div class="status" id="status">32×32</div>
106
+ <div class="hint">Ctrl+C / Ctrl+X / Ctrl+V, Delete — по выделению. Вставка ставится в левый верхний угол выделения.</div>
107
+ </div>
108
+
109
+ <div class="canvas-wrap"><canvas id="canvas"></canvas></div>
110
+ </div>
111
+
112
+ <script>
113
+ const PALETTE = [
114
+ '#1b2434', '#2a3448', '#3a4560', '#5b6a90',
115
+ '#8a5a2b', '#a8713a', '#c9b48c', '#d9c9a3',
116
+ '#3f8a4f', '#4fa05e', '#7fdc9a', '#8fd3ff',
117
+ '#4b8bff', '#9b7bff', '#c25a4a', '#f0c9a0'
118
+ ];
119
+
120
+ const canvas = document.getElementById('canvas');
121
+ const ctx = canvas.getContext('2d');
122
+ let gridW = 32;
123
+ let gridH = 32;
124
+ let zoom = 12;
125
+ function fitZoom() {
126
+ return Math.max(1, Math.min(32, Math.floor(520 / Math.max(gridW, gridH))));
127
+ }
128
+ function setZoom(value) {
129
+ zoom = Math.max(1, Math.min(40, value));
130
+ const label = document.getElementById('zoomLabel');
131
+ if (label) label.textContent = zoom + '×';
132
+ render();
133
+ }
134
+ let pixels = [];
135
+ let tool = 'pencil';
136
+ let color = '#7fdc9a';
137
+ let showGrid = true;
138
+ let undoStack = [];
139
+ let redoStack = [];
140
+ let drawing = false;
141
+ let startPoint = null;
142
+ let drawSnapshot = null;
143
+ let selection = null;
144
+ let clipboard = null;
145
+ let showTemplate = false;
146
+ let traceImage = null;
147
+ let traceOpacity = 0.35;
148
+ const params = new URLSearchParams(location.search);
149
+ const ctxRoot = params.get('root') || '';
150
+ const ctxTask = params.get('taskId') || '';
151
+
152
+ function blank() {
153
+ return Array.from({ length: gridH }, () => Array(gridW).fill(null));
154
+ }
155
+
156
+ function reset() {
157
+ pixels = blank();
158
+ setZoom(fitZoom());
159
+ selection = null;
160
+ undoStack = [];
161
+ redoStack = [];
162
+ render();
163
+ }
164
+
165
+ function snapshot() {
166
+ undoStack.push(pixels.map((row) => row.slice()));
167
+ if (undoStack.length > 80) undoStack.shift();
168
+ redoStack = [];
169
+ }
170
+
171
+ function render() {
172
+ canvas.width = gridW * zoom;
173
+ canvas.height = gridH * zoom;
174
+ ctx.imageSmoothingEnabled = false;
175
+ ctx.fillStyle = '#222';
176
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
177
+ if (traceImage) {
178
+ ctx.save();
179
+ ctx.globalAlpha = traceOpacity;
180
+ ctx.imageSmoothingEnabled = false;
181
+ ctx.drawImage(traceImage, 0, 0, canvas.width, canvas.height);
182
+ ctx.restore();
183
+ }
184
+ for (let y = 0; y < gridH; y += 1) {
185
+ for (let x = 0; x < gridW; x += 1) {
186
+ const value = pixels[y][x];
187
+ if (!value) continue;
188
+ ctx.fillStyle = value;
189
+ ctx.fillRect(x * zoom, y * zoom, zoom, zoom);
190
+ }
191
+ }
192
+ if (showGrid && zoom >= 8) {
193
+ ctx.strokeStyle = 'rgba(255,255,255,0.08)';
194
+ ctx.lineWidth = 1;
195
+ for (let i = 0; i <= Math.max(gridW, gridH); i += 1) {
196
+ ctx.beginPath(); ctx.moveTo(i * zoom + 0.5, 0); ctx.lineTo(i * zoom + 0.5, canvas.height); ctx.stroke();
197
+ ctx.beginPath(); ctx.moveTo(0, i * zoom + 0.5); ctx.lineTo(canvas.width, i * zoom + 0.5); ctx.stroke();
198
+ }
199
+ }
200
+ if (showTemplate) {
201
+ const cx = Math.floor(gridW / 2);
202
+ const bottom = gridH;
203
+ const zones = [
204
+ { x: cx - 8, y: bottom - 15, w: 16, h: 15, color: '#4b8bff33', label: 'ноги' },
205
+ { x: cx - 8, y: bottom - 34, w: 16, h: 18, color: '#7fdc9a33', label: 'туловище' },
206
+ { x: cx - 6, y: bottom - 45, w: 12, h: 12, color: '#f0c9a033', label: 'голова' },
207
+ { x: cx - 7, y: bottom - 48, w: 14, h: 5, color: '#c25a4a33', label: 'волосы' },
208
+ ];
209
+ ctx.save();
210
+ ctx.font = Math.max(9, zoom) + 'px sans-serif';
211
+ for (const zone of zones) {
212
+ ctx.fillStyle = zone.color;
213
+ ctx.fillRect(zone.x * zoom, zone.y * zoom, zone.w * zoom, zone.h * zoom);
214
+ ctx.strokeStyle = '#ffffff66';
215
+ ctx.strokeRect(zone.x * zoom, zone.y * zoom, zone.w * zoom, zone.h * zoom);
216
+ ctx.fillStyle = '#ffffffaa';
217
+ ctx.fillText(zone.label, zone.x * zoom + 2, zone.y * zoom + Math.max(10, zoom) + 2);
218
+ }
219
+ ctx.restore();
220
+ }
221
+ if (selection) {
222
+ const { x0, y0, x1, y1 } = normalized(selection);
223
+ ctx.save();
224
+ ctx.strokeStyle = '#ffffff';
225
+ ctx.lineWidth = 2;
226
+ ctx.setLineDash([6, 4]);
227
+ ctx.strokeRect(x0 * zoom + 1, y0 * zoom + 1, (x1 - x0 + 1) * zoom - 2, (y1 - y0 + 1) * zoom - 2);
228
+ ctx.restore();
229
+ }
230
+ document.getElementById('status').textContent = gridW + '×' + gridH + ' · ' + tool + (selection ? ' · выделение' : '');
231
+ }
232
+
233
+ function normalized(sel) {
234
+ return {
235
+ x0: Math.min(sel.x0, sel.x1), y0: Math.min(sel.y0, sel.y1),
236
+ x1: Math.max(sel.x0, sel.x1), y1: Math.max(sel.y0, sel.y1),
237
+ };
238
+ }
239
+
240
+ function cell(event) {
241
+ const rect = canvas.getBoundingClientRect();
242
+ const x = Math.floor((event.clientX - rect.left) / zoom);
243
+ const y = Math.floor((event.clientY - rect.top) / zoom);
244
+ if (x < 0 || y < 0 || x >= gridW || y >= gridH) return null;
245
+ return { x, y };
246
+ }
247
+
248
+ function put(x, y, value) {
249
+ if (x < 0 || y < 0 || x >= gridW || y >= gridH) return;
250
+ pixels[y][x] = value;
251
+ }
252
+
253
+ function linePixels(x0, y0, x1, y1, paint) {
254
+ let dx = Math.abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
255
+ let dy = -Math.abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
256
+ let err = dx + dy;
257
+ for (;;) {
258
+ paint(x0, y0);
259
+ if (x0 === x1 && y0 === y1) break;
260
+ const e2 = 2 * err;
261
+ if (e2 >= dy) { err += dy; x0 += sx; }
262
+ if (e2 <= dx) { err += dx; y0 += sy; }
263
+ }
264
+ }
265
+
266
+ function rectPixels(x0, y0, x1, y1, filled, paint) {
267
+ const { x0: ax, y0: ay, x1: bx, y1: by } = normalized({ x0, y0, x1, y1 });
268
+ if (filled) {
269
+ for (let y = ay; y <= by; y += 1) for (let x = ax; x <= bx; x += 1) paint(x, y);
270
+ return;
271
+ }
272
+ for (let x = ax; x <= bx; x += 1) { paint(x, ay); paint(x, by); }
273
+ for (let y = ay; y <= by; y += 1) { paint(ax, y); paint(bx, y); }
274
+ }
275
+
276
+ function circlePixels(x0, y0, x1, y1, filled, paint) {
277
+ const cx = (x0 + x1) / 2;
278
+ const cy = (y0 + y1) / 2;
279
+ const rx = Math.abs(x1 - x0) / 2;
280
+ const ry = Math.abs(y1 - y0) / 2;
281
+ for (let y = 0; y < gridH; y += 1) {
282
+ for (let x = 0; x < gridW; x += 1) {
283
+ const dx = rx === 0 ? (x - cx) : (x - cx) / rx;
284
+ const dy = ry === 0 ? (y - cy) : (y - cy) / ry;
285
+ const d = dx * dx + dy * dy;
286
+ if (filled ? d <= 1 : Math.abs(d - 1) < Math.max(0.15, 1 / Math.max(rx, ry, 1)) * 1.4) paint(x, y);
287
+ }
288
+ }
289
+ }
290
+
291
+ function flood(x, y, from, to) {
292
+ if (from === to) return;
293
+ const stack = [[x, y]];
294
+ while (stack.length) {
295
+ const [cx, cy] = stack.pop();
296
+ if (cx < 0 || cy < 0 || cx >= gridW || cy >= gridH) continue;
297
+ if (pixels[cy][cx] !== from) continue;
298
+ pixels[cy][cx] = to;
299
+ stack.push([cx + 1, cy], [cx - 1, cy], [cx, cy + 1], [cx, cy - 1]);
300
+ }
301
+ }
302
+
303
+ function paintCell(x, y) {
304
+ if (tool === 'eraser') put(x, y, null);
305
+ else if (tool === 'fill') flood(x, y, pixels[y][x], color);
306
+ else if (tool === 'picker') {
307
+ if (pixels[y][x]) { color = pixels[y][x]; document.getElementById('color').value = color; }
308
+ } else put(x, y, color);
309
+ }
310
+
311
+ canvas.addEventListener('pointerdown', (event) => {
312
+ const point = cell(event);
313
+ if (!point) return;
314
+ if (tool === 'select') {
315
+ startPoint = point;
316
+ selection = { x0: point.x, y0: point.y, x1: point.x, y1: point.y };
317
+ drawing = true;
318
+ render();
319
+ return;
320
+ }
321
+ snapshot();
322
+ drawing = true;
323
+ startPoint = point;
324
+ if (tool === 'line' || tool === 'rect' || tool === 'circle') {
325
+ drawSnapshot = pixels.map((row) => row.slice());
326
+ render();
327
+ return;
328
+ }
329
+ paintCell(point.x, point.y);
330
+ render();
331
+ });
332
+
333
+ canvas.addEventListener('pointermove', (event) => {
334
+ if (!drawing) return;
335
+ const point = cell(event);
336
+ if (!point) return;
337
+ if (tool === 'select') {
338
+ selection.x1 = point.x;
339
+ selection.y1 = point.y;
340
+ render();
341
+ return;
342
+ }
343
+ if (tool === 'line' || tool === 'rect' || tool === 'circle') {
344
+ pixels = drawSnapshot.map((row) => row.slice());
345
+ const filled = document.getElementById('filled').checked;
346
+ const paint = (x, y) => put(x, y, color);
347
+ if (tool === 'line') linePixels(startPoint.x, startPoint.y, point.x, point.y, paint);
348
+ else if (tool === 'rect') rectPixels(startPoint.x, startPoint.y, point.x, point.y, filled, paint);
349
+ else circlePixels(startPoint.x, startPoint.y, point.x, point.y, filled, paint);
350
+ render();
351
+ return;
352
+ }
353
+ paintCell(point.x, point.y);
354
+ render();
355
+ });
356
+
357
+ window.addEventListener('pointerup', () => {
358
+ drawing = false;
359
+ startPoint = null;
360
+ drawSnapshot = null;
361
+ });
362
+
363
+ function mark(id, ids) {
364
+ for (const name of ids) document.getElementById(name).className = name === id ? 'active' : '';
365
+ }
366
+
367
+ const basic = ['pencil', 'eraser', 'fill', 'picker'];
368
+ const shapes = ['line', 'rect', 'circle', 'select'];
369
+ document.getElementById('pencil').onclick = () => { tool = 'pencil'; mark('pencil', basic.concat(shapes)); };
370
+ document.getElementById('eraser').onclick = () => { tool = 'eraser'; mark('eraser', basic.concat(shapes)); };
371
+ document.getElementById('fill').onclick = () => { tool = 'fill'; mark('fill', basic.concat(shapes)); };
372
+ document.getElementById('picker').onclick = () => { tool = 'picker'; mark('picker', basic.concat(shapes)); };
373
+ document.getElementById('line').onclick = () => { tool = 'line'; mark('line', basic.concat(shapes)); };
374
+ document.getElementById('rect').onclick = () => { tool = 'rect'; mark('rect', basic.concat(shapes)); };
375
+ document.getElementById('circle').onclick = () => { tool = 'circle'; mark('circle', basic.concat(shapes)); };
376
+ document.getElementById('select').onclick = () => { tool = 'select'; mark('select', basic.concat(shapes)); };
377
+
378
+ document.getElementById('color').oninput = (event) => { color = event.target.value; };
379
+
380
+ document.getElementById('size').onchange = (event) => {
381
+ const [w, h] = String(event.target.value).split('x').map(Number);
382
+ gridW = w;
383
+ gridH = h;
384
+ reset();
385
+ };
386
+
387
+ document.getElementById('template').onchange = (event) => {
388
+ showTemplate = event.target.checked;
389
+ render();
390
+ };
391
+
392
+ document.getElementById('gridToggle').onclick = (event) => {
393
+ showGrid = !showGrid;
394
+ event.target.className = showGrid ? 'active' : '';
395
+ render();
396
+ };
397
+
398
+ document.getElementById('undo').onclick = () => {
399
+ const previous = undoStack.pop();
400
+ if (!previous) return;
401
+ redoStack.push(pixels.map((row) => row.slice()));
402
+ pixels = previous;
403
+ render();
404
+ };
405
+ document.getElementById('redo').onclick = () => {
406
+ const next = redoStack.pop();
407
+ if (!next) return;
408
+ undoStack.push(pixels.map((row) => row.slice()));
409
+ pixels = next;
410
+ render();
411
+ };
412
+ document.getElementById('clear').onclick = () => { snapshot(); reset(); };
413
+
414
+ function withSelection(action) {
415
+ if (!selection) return;
416
+ snapshot();
417
+ action(normalized(selection));
418
+ render();
419
+ }
420
+
421
+ function copySelection(cut) {
422
+ withSelection(({ x0, y0, x1, y1 }) => {
423
+ const width = x1 - x0 + 1;
424
+ const height = y1 - y0 + 1;
425
+ const data = [];
426
+ for (let y = 0; y < height; y += 1) {
427
+ const row = [];
428
+ for (let x = 0; x < width; x += 1) row.push(pixels[y0 + y][x0 + x]);
429
+ data.push(row);
430
+ }
431
+ clipboard = { width, height, data, x: x0, y: y0 };
432
+ if (cut) {
433
+ for (let y = y0; y <= y1; y += 1) for (let x = x0; x <= x1; x += 1) pixels[y][x] = null;
434
+ }
435
+ });
436
+ }
437
+
438
+ function pasteSelection() {
439
+ if (!clipboard) return;
440
+ const at = selection ? normalized(selection) : { x0: clipboard.x, y0: clipboard.y };
441
+ snapshot();
442
+ for (let y = 0; y < clipboard.height; y += 1) {
443
+ for (let x = 0; x < clipboard.width; x += 1) {
444
+ put(at.x0 + x, at.y0 + y, clipboard.data[y][x]);
445
+ }
446
+ }
447
+ selection = { x0: at.x0, y0: at.y0, x1: at.x0 + clipboard.width - 1, y1: at.y0 + clipboard.height - 1 };
448
+ render();
449
+ }
450
+
451
+ document.getElementById('copy').onclick = () => copySelection(false);
452
+ document.getElementById('cut').onclick = () => copySelection(true);
453
+ document.getElementById('paste').onclick = pasteSelection;
454
+ document.getElementById('del').onclick = () => withSelection(({ x0, y0, x1, y1 }) => {
455
+ for (let y = y0; y <= y1; y += 1) for (let x = x0; x <= x1; x += 1) pixels[y][x] = null;
456
+ });
457
+ document.getElementById('deselect').onclick = () => { selection = null; render(); };
458
+
459
+ window.addEventListener('keydown', (event) => {
460
+ if (event.ctrlKey && event.key.toLowerCase() === 'c') { event.preventDefault(); copySelection(false); }
461
+ else if (event.ctrlKey && event.key.toLowerCase() === 'x') { event.preventDefault(); copySelection(true); }
462
+ else if (event.ctrlKey && event.key.toLowerCase() === 'v') { event.preventDefault(); pasteSelection(); }
463
+ else if (event.key === 'Delete' || event.key === 'Backspace') { event.preventDefault(); document.getElementById('del').click(); }
464
+ else if (event.key === 'Escape') { selection = null; render(); }
465
+ });
466
+
467
+ document.getElementById('savePng').onclick = () => downloadPng();
468
+ document.getElementById('saveJson').onclick = () => downloadJson();
469
+
470
+ function downloadPng() {
471
+ const out = document.createElement('canvas');
472
+ out.width = gridW;
473
+ out.height = gridH;
474
+ const octx = out.getContext('2d');
475
+ for (let y = 0; y < gridH; y += 1) {
476
+ for (let x = 0; x < gridW; x += 1) {
477
+ if (!pixels[y][x]) continue;
478
+ octx.fillStyle = pixels[y][x];
479
+ octx.fillRect(x, y, 1, 1);
480
+ }
481
+ }
482
+ out.toBlob((blob) => {
483
+ const url = URL.createObjectURL(blob);
484
+ const link = document.createElement('a');
485
+ link.href = url;
486
+ link.download = 'sprite-' + gridW + 'x' + gridH + '.png';
487
+ link.click();
488
+ URL.revokeObjectURL(url);
489
+ });
490
+ }
491
+
492
+ function downloadJson() {
493
+ const symbols = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
494
+ const map = new Map();
495
+ let next = 0;
496
+ const rows = [];
497
+ for (let y = 0; y < gridH; y += 1) {
498
+ let line = '';
499
+ for (let x = 0; x < gridW; x += 1) {
500
+ const value = pixels[y][x];
501
+ if (!value) { line += '.'; continue; }
502
+ if (!map.has(value)) map.set(value, symbols[next++] || '?');
503
+ line += map.get(value);
504
+ }
505
+ rows.push(line);
506
+ }
507
+ const palette = {};
508
+ for (const [value, symbol] of map) palette[symbol] = value;
509
+ const data = JSON.stringify({ rows, palette }, null, 2);
510
+ const blob = new Blob([data], { type: 'application/json' });
511
+ const url = URL.createObjectURL(blob);
512
+ const link = document.createElement('a');
513
+ link.href = url;
514
+ link.download = 'sprite-' + gridW + 'x' + gridH + '.json';
515
+ link.click();
516
+ URL.revokeObjectURL(url);
517
+ }
518
+
519
+ document.getElementById('zoomIn').onclick = () => setZoom(zoom + 2);
520
+ document.getElementById('zoomOut').onclick = () => setZoom(zoom - 2);
521
+ document.getElementById('zoomFit').onclick = () => setZoom(fitZoom());
522
+
523
+ canvas.addEventListener('wheel', (event) => {
524
+ if (!event.ctrlKey && !event.metaKey) return;
525
+ event.preventDefault();
526
+ setZoom(zoom + (event.deltaY < 0 ? 1 : -1));
527
+ }, { passive: false });
528
+
529
+ window.addEventListener('keydown', (event) => {
530
+ if (event.key === '+' || event.key === '=') { event.preventDefault(); setZoom(zoom + 1); }
531
+ if (event.key === '-') { event.preventDefault(); setZoom(zoom - 1); }
532
+ if (event.key === '0') { event.preventDefault(); setZoom(fitZoom()); }
533
+ });
534
+
535
+ function buildGrid() {
536
+ const symbols = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
537
+ const map = new Map();
538
+ let next = 0;
539
+ const rows = [];
540
+ for (let y = 0; y < gridSize; y += 1) {
541
+ let line = '';
542
+ for (let x = 0; x < gridSize; x += 1) {
543
+ const value = pixels[y][x];
544
+ if (!value) { line += '.'; continue; }
545
+ if (!map.has(value)) map.set(value, symbols[next++] || '?');
546
+ line += map.get(value);
547
+ }
548
+ rows.push(line);
549
+ }
550
+ const palette = {};
551
+ for (const [value, symbol] of map) palette[symbol] = value;
552
+ return { rows, palette };
553
+ }
554
+
555
+ async function saveOffice() {
556
+ const name = document.getElementById('spriteName').value.trim() || 'sprite';
557
+ const status = document.getElementById('officeStatus');
558
+ try {
559
+ const grid = buildGrid();
560
+ const response = await fetch('/api/department/editor/save', {
561
+ method: 'POST',
562
+ headers: { 'content-type': 'application/json' },
563
+ body: JSON.stringify({ root: ctxRoot, taskId: ctxTask, name, ...grid }),
564
+ });
565
+ const data = await response.json().catch(() => ({}));
566
+ if (!response.ok) throw new Error(data.error || response.statusText);
567
+ status.textContent = 'Сохранено: ' + data.path;
568
+ } catch (error) {
569
+ status.textContent = 'Ошибка: ' + error.message;
570
+ }
571
+ }
572
+
573
+ async function handoff() {
574
+ const status = document.getElementById('officeStatus');
575
+ const note = document.getElementById('note').value.trim();
576
+ try {
577
+ const response = await fetch('/api/department/editor/handoff', {
578
+ method: 'POST',
579
+ headers: { 'content-type': 'application/json' },
580
+ body: JSON.stringify({ root: ctxRoot, taskId: ctxTask, name: document.getElementById('spriteName').value.trim(), note }),
581
+ });
582
+ const data = await response.json().catch(() => ({}));
583
+ if (!response.ok) throw new Error(data.error || response.statusText);
584
+ status.textContent = 'Передано художнику: корректировка ' + (data.correctionId || '');
585
+ } catch (error) {
586
+ status.textContent = 'Ошибка: ' + error.message;
587
+ }
588
+ }
589
+
590
+ document.getElementById('saveOffice').onclick = saveOffice;
591
+ document.getElementById('handoff').onclick = handoff;
592
+ document.getElementById('trace').onchange = (event) => {
593
+ const file = event.target.files?.[0];
594
+ if (!file) return;
595
+ const img = new Image();
596
+ img.onload = () => { traceImage = img; render(); };
597
+ img.src = URL.createObjectURL(file);
598
+ };
599
+ document.getElementById('traceOpacity').oninput = (event) => {
600
+ traceOpacity = Number(event.target.value) / 100;
601
+ render();
602
+ };
603
+
604
+ const swatches = document.getElementById('swatches');
605
+ for (const value of PALETTE) {
606
+ const cellEl = document.createElement('div');
607
+ cellEl.className = 'swatch';
608
+ cellEl.style.background = value;
609
+ cellEl.onclick = () => {
610
+ color = value;
611
+ document.getElementById('color').value = value;
612
+ for (const child of swatches.children) child.className = 'swatch';
613
+ cellEl.className = 'swatch active';
614
+ };
615
+ swatches.append(cellEl);
616
+ }
617
+
618
+ reset();
619
+ </script>
620
+ </body>
621
+ </html>