-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
297 lines (276 loc) · 10.4 KB
/
index.html
File metadata and controls
297 lines (276 loc) · 10.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-NNV3CN8D2N"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-NNV3CN8D2N');
</script>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pretext-masonry demo -- 10K cards</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: Inter, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background: #f0f0f0;
color: #1a1a1a;
}
#root { width: 100%; height: 100vh; }
.header {
background: #1a1a1a;
color: #fff;
padding: 16px 24px;
display: flex;
align-items: center;
justify-content: space-between;
position: sticky;
top: 0;
z-index: 100;
}
.header h1 { font-size: 18px; font-weight: 600; }
.header .stats { font-size: 13px; color: #aaa; }
.controls {
display: flex;
gap: 12px;
padding: 12px 24px;
background: #e8e8e8;
align-items: center;
flex-wrap: wrap;
}
.controls label { font-size: 13px; font-weight: 500; }
.controls select, .controls input {
padding: 4px 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 13px;
}
.fps-counter {
position: fixed;
bottom: 16px;
right: 16px;
background: rgba(0,0,0,0.8);
color: #0f0;
padding: 8px 12px;
border-radius: 6px;
font-family: monospace;
font-size: 14px;
z-index: 200;
}
.masonry-container {
height: calc(100vh - 100px);
overflow: auto;
padding: 16px;
}
.card {
border-radius: 8px;
padding: 16px;
overflow: hidden;
box-shadow: 0 1px 3px rgba(0,0,0,0.08);
transition: box-shadow 0.15s;
}
.card:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.15); }
.card h3 { font-size: 15px; font-weight: 600; margin-bottom: 8px; line-height: 1.3; }
.card p { font-size: 13px; line-height: 1.5; color: #444; }
.card .meta { font-size: 11px; color: #888; margin-top: 10px; }
</style>
</head>
<body>
<div id="root"></div>
<div class="fps-counter" id="fps">-- fps</div>
<script type="module">
// ---- FPS counter ----
let frames = 0;
let lastTime = performance.now();
function tick() {
frames++;
const now = performance.now();
if (now - lastTime >= 1000) {
document.getElementById('fps').textContent = frames + ' fps';
frames = 0;
lastTime = now;
}
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
// ---- Card data generation ----
const LOREM_WORDS = 'lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum'.split(' ');
const TITLES = [
'Quick thought', 'Idea', 'Reminder', 'Note to self', 'Reflection',
'Today I learned', 'Bookmark', 'Quote', 'Observation', 'Draft',
'Meeting notes', 'Recipe', 'Travel plan', 'Book excerpt', 'Poem',
'Code snippet idea', 'Design thought', 'Research note', 'Question',
'Hypothesis', 'Shower thought', 'Life lesson', 'Gratitude'
];
const COLORS = [
'#fff9c4', '#f8bbd0', '#c8e6c9', '#bbdefb', '#d1c4e9',
'#ffe0b2', '#b2dfdb', '#f0f4c3', '#ffccbc', '#e1bee7',
'#dcedc8', '#b3e5fc', '#fff3e0', '#e8eaf6', '#fce4ec',
];
function randomLorem(minWords, maxWords) {
const count = minWords + Math.floor(Math.random() * (maxWords - minWords));
let text = '';
for (let i = 0; i < count; i++) {
text += LOREM_WORDS[Math.floor(Math.random() * LOREM_WORDS.length)];
if (i < count - 1) text += ' ';
}
return text.charAt(0).toUpperCase() + text.slice(1) + '.';
}
function generateCards(count, startId = 0) {
const cards = [];
for (let i = 0; i < count; i++) {
const id = startId + i;
// Vary text length: some short (5-15 words), some medium (20-60), some long (80-200)
const r = Math.random();
let body;
if (r < 0.3) body = randomLorem(5, 15);
else if (r < 0.7) body = randomLorem(20, 60);
else body = randomLorem(80, 200);
cards.push({
id,
title: TITLES[id % TITLES.length],
body,
color: COLORS[id % COLORS.length],
});
}
return cards;
}
// ---- Masonry layout (standalone, no React for demo simplicity) ----
// This demo implements the same algorithm as the library to show the concept.
// In production, you'd import { Masonry } from '@shipitandpray/pretext-masonry'.
function estimateTextHeight(text, widthPx, fontSize, lineHeight) {
// Approximate: average char width ~ fontSize * 0.48
const avgCharWidth = fontSize * 0.48;
const charsPerLine = Math.max(1, Math.floor(widthPx / avgCharWidth));
// Account for word wrap (words don't break mid-word)
const words = text.split(/\s+/);
let lines = 1;
let lineLen = 0;
for (const word of words) {
if (lineLen + word.length + (lineLen > 0 ? 1 : 0) > charsPerLine) {
lines++;
lineLen = word.length;
} else {
lineLen += (lineLen > 0 ? 1 : 0) + word.length;
}
}
return lines * lineHeight;
}
function predictCardHeight(card, colWidth) {
const padding = 16 * 2; // top + bottom
const headerH = 20; // title line
const metaH = 18; // bottom meta
const gapH = 8 + 10; // margins between sections
const availW = colWidth - 32; // left + right padding
const bodyH = estimateTextHeight(card.body, availW, 13, 19.5);
return padding + headerH + gapH + bodyH + metaH;
}
let allCards = generateCards(500);
let colCount = 4;
let gap = 12;
let virtualizeEnabled = true;
const container = document.getElementById('root');
function getColCount() {
const w = window.innerWidth - 32; // padding
const target = 280;
return Math.max(1, Math.floor((w + gap) / (target + gap)));
}
function renderMasonry() {
colCount = getColCount();
const totalW = container.querySelector('.masonry-container')?.clientWidth || (window.innerWidth - 32);
const colWidth = (totalW - gap * (colCount - 1)) / colCount;
// Compute positions
const colHeights = new Array(colCount).fill(0);
const positions = [];
for (let i = 0; i < allCards.length; i++) {
const card = allCards[i];
const h = predictCardHeight(card, colWidth);
// Find shortest column
let sc = 0;
for (let c = 1; c < colCount; c++) {
if (colHeights[c] < colHeights[sc]) sc = c;
}
positions.push({
index: i, x: sc * (colWidth + gap), y: colHeights[sc],
width: colWidth, height: h, column: sc
});
colHeights[sc] += h + gap;
}
const totalHeight = Math.max(...colHeights) - gap;
// Render
const masonryEl = container.querySelector('.masonry-container');
if (!masonryEl) return;
const scrollTop = masonryEl.scrollTop;
const viewH = masonryEl.clientHeight;
const overscanPx = 500;
let html = `<div style="position:relative;height:${totalHeight}px;width:100%">`;
for (const pos of positions) {
// Virtualization: only render visible cards
if (virtualizeEnabled) {
if (pos.y + pos.height < scrollTop - overscanPx) continue;
if (pos.y > scrollTop + viewH + overscanPx) continue;
}
const card = allCards[pos.index];
html += `<div class="card" style="position:absolute;transform:translate(${pos.x}px,${pos.y}px);width:${pos.width}px;background:${card.color}">`;
html += `<h3>${card.title} #${card.id}</h3>`;
html += `<p>${card.body}</p>`;
html += `<div class="meta">Card ${card.id} · Col ${pos.column}</div>`;
html += `</div>`;
}
html += '</div>';
const inner = masonryEl.querySelector('.masonry-inner');
if (inner) inner.innerHTML = html;
}
function fullRender() {
container.innerHTML = `
<div class="header">
<h1>pretext-masonry</h1>
<span class="stats">${allCards.length.toLocaleString()} cards · ${colCount} columns · virtualized: ${virtualizeEnabled ? 'on' : 'off'}</span>
</div>
<div class="controls">
<label>Cards:
<select id="card-count">
<option value="100">100</option>
<option value="500" selected>500</option>
<option value="1000">1,000</option>
<option value="5000">5,000</option>
<option value="10000">10,000</option>
</select>
</label>
<label>Virtualize:
<input type="checkbox" id="virtualize" checked />
</label>
<label>Gap:
<input type="range" id="gap-slider" min="0" max="32" value="${gap}" style="width:80px" />
<span id="gap-val">${gap}px</span>
</label>
</div>
<div class="masonry-container">
<div class="masonry-inner"></div>
</div>
`;
const masonryEl = container.querySelector('.masonry-container');
masonryEl.addEventListener('scroll', renderMasonry, { passive: true });
document.getElementById('card-count').addEventListener('change', (e) => {
const count = parseInt(e.target.value);
allCards = generateCards(count);
fullRender();
});
document.getElementById('virtualize').addEventListener('change', (e) => {
virtualizeEnabled = e.target.checked;
fullRender();
});
document.getElementById('gap-slider').addEventListener('input', (e) => {
gap = parseInt(e.target.value);
document.getElementById('gap-val').textContent = gap + 'px';
renderMasonry();
});
renderMasonry();
}
fullRender();
window.addEventListener('resize', () => { colCount = getColCount(); fullRender(); });
</script>
</body>
</html>