Vedavyas1235 commited on
Commit
4e6eca5
·
1 Parent(s): bbb0d62

Composite DOM text overlays onto canvas frame for CPU-only servers

Browse files
Files changed (1) hide show
  1. index.js +69 -6
index.js CHANGED
@@ -222,14 +222,77 @@ app.post('/api/render', async (req, res) => {
222
 
223
  const framePath = path.join(tempDir, `frame_${String(i).padStart(5, '0')}.jpg`);
224
 
225
- // Read canvas pixels directly from JS memory — this works reliably on CPU-only servers
226
- // where page.screenshot() may not capture the canvas compositor layer.
 
227
  const canvasDataUrl = await page.evaluate(() => {
228
- const c = document.querySelector('canvas');
229
- if (c && c.width > 0 && c.height > 0) {
230
- return c.toDataURL('image/jpeg', 1.0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  }
232
- return null;
 
 
 
 
 
 
233
  });
234
 
235
  if (canvasDataUrl) {
 
222
 
223
  const framePath = path.join(tempDir, `frame_${String(i).padStart(5, '0')}.jpg`);
224
 
225
+ // Composite canvas pixels + DOM text overlays together inside the browser.
226
+ // This is needed on CPU-only servers where page.screenshot() cannot capture
227
+ // the canvas compositor layer, so we build the final frame manually.
228
  const canvasDataUrl = await page.evaluate(() => {
229
+ const animCanvas = document.querySelector('canvas');
230
+ if (!animCanvas || animCanvas.width === 0 || animCanvas.height === 0) return null;
231
+
232
+ // Create an offscreen composite canvas
233
+ const composite = document.createElement('canvas');
234
+ composite.width = animCanvas.width;
235
+ composite.height = animCanvas.height;
236
+ const ctx = composite.getContext('2d');
237
+
238
+ // Layer 1: Draw the animated canvas
239
+ ctx.drawImage(animCanvas, 0, 0);
240
+
241
+ // Layer 2: Draw all DOM text/overlay elements on top
242
+ const scaleX = animCanvas.width / window.innerWidth;
243
+ const scaleY = animCanvas.height / window.innerHeight;
244
+
245
+ function drawElement(el) {
246
+ const rect = el.getBoundingClientRect();
247
+ if (rect.width === 0 || rect.height === 0) return;
248
+ const style = window.getComputedStyle(el);
249
+ if (style.display === 'none' || style.visibility === 'hidden' || parseFloat(style.opacity) === 0) return;
250
+
251
+ // Only draw leaf text nodes
252
+ const text = el.childNodes.length === 1 && el.childNodes[0].nodeType === 3
253
+ ? el.childNodes[0].textContent.trim()
254
+ : null;
255
+
256
+ if (text) {
257
+ ctx.save();
258
+ ctx.globalAlpha = parseFloat(style.opacity) || 1;
259
+ // Map font from CSS
260
+ const fontSize = parseFloat(style.fontSize) * scaleX;
261
+ const fontFamily = style.fontFamily || 'Arial';
262
+ const fontWeight = style.fontWeight || 'normal';
263
+ ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
264
+ ctx.fillStyle = style.color || 'white';
265
+ ctx.letterSpacing = style.letterSpacing || '0px';
266
+ ctx.textAlign = 'center';
267
+ ctx.textBaseline = 'middle';
268
+ const cx = (rect.left + rect.width / 2) * scaleX;
269
+ const cy = (rect.top + rect.height / 2) * scaleY;
270
+ // Draw letter by letter to honour letter-spacing
271
+ const spacing = parseFloat(style.letterSpacing) || 0;
272
+ if (spacing > 0) {
273
+ let x = cx - ctx.measureText(text).width / 2;
274
+ for (const ch of text) {
275
+ ctx.fillText(ch, x + ctx.measureText(ch).width / 2, cy);
276
+ x += ctx.measureText(ch).width + spacing * scaleX;
277
+ }
278
+ } else {
279
+ ctx.fillText(text, cx, cy);
280
+ }
281
+ ctx.restore();
282
+ }
283
+
284
+ // Recurse into children
285
+ for (const child of el.children) {
286
+ drawElement(child);
287
+ }
288
  }
289
+
290
+ // Walk all elements that are overlaid on top of the canvas
291
+ document.querySelectorAll('body > *:not(canvas), .overlay, .scene, [class]').forEach(el => {
292
+ if (el !== animCanvas) drawElement(el);
293
+ });
294
+
295
+ return composite.toDataURL('image/jpeg', 1.0);
296
  });
297
 
298
  if (canvasDataUrl) {