-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcanvas.html
More file actions
410 lines (380 loc) · 16 KB
/
canvas.html
File metadata and controls
410 lines (380 loc) · 16 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE html>
<html>
<head>
<title>Pixeltable Canvas</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.1/dist/chart.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.min.js"></script>
<script>
// Initialize Mermaid with dark theme
mermaid.initialize({
startOnLoad: false,
theme: 'dark',
themeVariables: {
darkMode: true,
background: '#1a1a1a',
primaryColor: '#4ade80',
primaryTextColor: '#fff',
primaryBorderColor: '#333',
lineColor: '#666',
secondaryColor: '#60a5fa',
tertiaryColor: '#a78bfa'
}
});
</script>
<style>
body {
font-family: sans-serif;
padding: 20px;
background: #1a1a1a;
color: #fff;
margin: 0;
}
#status {
position: fixed;
top: 10px;
right: 10px;
padding: 10px 20px;
background: #2a2a2a;
border-radius: 5px;
font-size: 14px;
}
#canvas {
margin-top: 60px;
max-width: 1200px;
margin-left: auto;
margin-right: auto;
}
.item {
margin: 20px 0;
padding: 20px;
background: #2a2a2a;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.item img {
max-width: 100%;
height: auto;
border-radius: 4px;
display: block;
}
.item table {
border-collapse: collapse;
width: 100%;
margin-top: 10px;
}
.item th, .item td {
padding: 12px;
border: 1px solid #444;
text-align: left;
}
.item th {
background: #333;
font-weight: 600;
}
.item pre {
background: #1a1a1a;
padding: 15px;
border-radius: 4px;
overflow-x: auto;
font-family: 'Courier New', monospace;
}
h1 {
margin-top: 0;
text-align: center;
}
.image-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 15px;
margin-top: 10px;
}
.image-grid img {
width: 100%;
height: 200px;
object-fit: cover;
border-radius: 4px;
cursor: pointer;
transition: transform 0.2s;
}
.image-grid img:hover {
transform: scale(1.05);
}
.comparison {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.comparison-item {
text-align: center;
}
.comparison-item h3 {
margin-top: 0;
margin-bottom: 10px;
color: #888;
font-size: 14px;
}
video, audio {
max-width: 100%;
border-radius: 4px;
}
.chart-container {
position: relative;
height: 400px;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>Pixeltable Canvas</h1>
<div id="status">Connecting...</div>
<div id="canvas"></div>
<script>
// Connect to MCP canvas stream
// CHANGE THIS URL if your MCP is running on a different port
const eventSource = new EventSource('http://localhost:8000/canvas/stream');
const canvas = document.getElementById('canvas');
const status = document.getElementById('status');
eventSource.onopen = () => {
status.textContent = 'Connected ✓';
status.style.color = '#4ade80';
};
eventSource.onerror = () => {
status.textContent = 'Disconnected ✗';
status.style.color = '#f87171';
};
eventSource.onmessage = (event) => {
// Mark as connected when we receive any message
if (status.textContent !== 'Connected ✓') {
status.textContent = 'Connected ✓';
status.style.color = '#4ade80';
}
const message = JSON.parse(event.data);
if (message.type === 'connected') {
console.log('Canvas connected to MCP');
return;
}
const item = document.createElement('div');
item.className = 'item';
// Add title if provided
if (message.title) {
const title = document.createElement('h3');
title.textContent = message.title;
title.style.margin = '0 0 15px 0';
title.style.textAlign = 'center';
title.style.color = '#fff';
title.style.fontSize = '18px';
title.style.fontWeight = '600';
title.style.borderBottom = '2px solid #444';
title.style.paddingBottom = '10px';
item.appendChild(title);
}
switch(message.content_type) {
case 'image':
const img = document.createElement('img');
img.src = message.data;
img.onerror = () => {
// If image fails to load, show error
item.innerHTML = `<p style="color: #f87171;">Failed to load image: ${message.data}</p>`;
};
item.appendChild(img);
break;
case 'text':
const text = document.createElement('p');
text.textContent = message.data;
text.style.margin = '0';
text.style.fontSize = '16px';
text.style.lineHeight = '1.6';
item.appendChild(text);
break;
case 'html':
item.innerHTML = message.data;
break;
case 'table':
const table = document.createElement('table');
// Assume data is array of objects
if (Array.isArray(message.data) && message.data.length > 0) {
const headers = Object.keys(message.data[0]);
const thead = table.createTHead();
const headerRow = thead.insertRow();
headers.forEach(h => {
const th = document.createElement('th');
th.textContent = h;
headerRow.appendChild(th);
});
const tbody = table.createTBody();
message.data.forEach(row => {
const tr = tbody.insertRow();
headers.forEach(h => {
const td = tr.insertCell();
const value = row[h];
// Check if value is a URL (starts with http:// or file://)
if (typeof value === 'string' && (value.startsWith('http://') || value.startsWith('https://') || value.startsWith('file://'))) {
// Check if it's an audio file
if (value.match(/\.(mp3|wav|ogg|m4a)$/i)) {
const audioEl = document.createElement('audio');
audioEl.controls = true;
audioEl.src = value;
audioEl.style.width = '100%';
td.appendChild(audioEl);
}
// Check if it's an image file
else if (value.match(/\.(jpg|jpeg|png|gif|webp)$/i)) {
const imgEl = document.createElement('img');
imgEl.src = value;
imgEl.style.maxWidth = '200px';
imgEl.style.height = 'auto';
td.appendChild(imgEl);
}
// Check if it's a video file
else if (value.match(/\.(mp4|webm|ogg)$/i)) {
const videoEl = document.createElement('video');
videoEl.controls = true;
videoEl.src = value;
videoEl.style.maxWidth = '300px';
td.appendChild(videoEl);
}
else {
// Just a regular URL, show as link
const link = document.createElement('a');
link.href = value;
link.textContent = value;
link.target = '_blank';
td.appendChild(link);
}
} else {
// Regular text value
td.textContent = value;
}
});
});
item.appendChild(table);
} else {
item.innerHTML = '<p>Invalid table data</p>';
}
break;
case 'json':
const pre = document.createElement('pre');
pre.textContent = JSON.stringify(message.data, null, 2);
item.appendChild(pre);
break;
case 'chart':
// Chart.js visualization
// data format: { type: 'bar'|'line'|'pie'|'scatter', chartData: {...}, options: {...} }
const chartContainer = document.createElement('div');
chartContainer.className = 'chart-container';
const chartCanvas = document.createElement('canvas');
chartContainer.appendChild(chartCanvas);
item.appendChild(chartContainer);
new Chart(chartCanvas, {
type: message.data.type || 'bar',
data: message.data.chartData,
options: {
...message.data.options,
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
labels: { color: '#fff' }
}
},
scales: message.data.type !== 'pie' ? {
x: { ticks: { color: '#888' }, grid: { color: '#333' } },
y: { ticks: { color: '#888' }, grid: { color: '#333' } }
} : undefined
}
});
break;
case 'image_grid':
// Grid of images
// data format: [{ url: '...', caption: '...' }, ...]
const grid = document.createElement('div');
grid.className = 'image-grid';
message.data.forEach(imgData => {
const gridImg = document.createElement('img');
gridImg.src = imgData.url || imgData;
if (imgData.caption) {
gridImg.title = imgData.caption;
}
gridImg.onclick = () => {
// Show full size on click
const fullView = window.open('', '_blank');
fullView.document.write(`<img src="${gridImg.src}" style="max-width:100%; height:auto;">`);
};
grid.appendChild(gridImg);
});
item.appendChild(grid);
break;
case 'comparison':
// Side-by-side comparison
// data format: { left: { type: 'image'|'text', data: '...' }, right: {...}, leftLabel: '...', rightLabel: '...' }
const comparison = document.createElement('div');
comparison.className = 'comparison';
['left', 'right'].forEach(side => {
const compItem = document.createElement('div');
compItem.className = 'comparison-item';
const label = document.createElement('h3');
label.textContent = message.data[`${side}Label`] || side.toUpperCase();
compItem.appendChild(label);
const content = message.data[side];
if (content.type === 'image') {
const compImg = document.createElement('img');
compImg.src = content.data;
compItem.appendChild(compImg);
} else if (content.type === 'text') {
const compText = document.createElement('p');
compText.textContent = content.data;
compItem.appendChild(compText);
}
comparison.appendChild(compItem);
});
item.appendChild(comparison);
break;
case 'video':
// Video player
const video = document.createElement('video');
video.controls = true;
video.src = message.data;
item.appendChild(video);
break;
case 'audio':
// Audio player
const audio = document.createElement('audio');
audio.controls = true;
audio.src = message.data;
item.appendChild(audio);
break;
case 'mermaid':
// Mermaid diagram
// data format: string with mermaid syntax
const mermaidDiv = document.createElement('div');
mermaidDiv.className = 'mermaid';
mermaidDiv.textContent = message.data;
item.appendChild(mermaidDiv);
// Render the mermaid diagram
mermaid.run({
nodes: [mermaidDiv]
});
break;
default:
// Fallback: display as JSON
const fallback = document.createElement('pre');
fallback.textContent = JSON.stringify(message, null, 2);
item.appendChild(fallback);
}
// Add timestamp
const timestamp = document.createElement('div');
timestamp.style.fontSize = '12px';
timestamp.style.color = '#888';
timestamp.style.marginTop = '10px';
timestamp.textContent = new Date().toLocaleTimeString();
item.appendChild(timestamp);
// Insert at top (most recent first)
canvas.insertBefore(item, canvas.firstChild);
};
// Log connection info
console.log('Pixeltable Canvas initialized');
console.log('Connecting to: http://localhost:8000/canvas/stream');
console.log('Waiting for messages from Claude...');
</script>
</body>
</html>