MCPcopy Create free account
hub / github.com/TheOrcDev/github-creature / captureWebGLToImage

Function captureWebGLToImage

components/download-card-button.tsx:16–53  ·  view source on GitHub ↗
(canvas: HTMLCanvasElement)

Source from the content-addressed store, hash-verified

14};
15
16function captureWebGLToImage(canvas: HTMLCanvasElement): string | null {
17 const gl = canvas.getContext("webgl2") || canvas.getContext("webgl");
18 if (!gl) {
19 try {
20 return canvas.toDataURL("image/png");
21 } catch {
22 return null;
23 }
24 }
25
26 const width = canvas.width;
27 const height = canvas.height;
28
29 const pixels = new Uint8Array(width * height * 4);
30 gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
31
32 const tempCanvas = document.createElement("canvas");
33 tempCanvas.width = width;
34 tempCanvas.height = height;
35 const ctx = tempCanvas.getContext("2d");
36 if (!ctx) return null;
37
38 const imageData = ctx.createImageData(width, height);
39
40 for (let y = 0; y < height; y++) {
41 for (let x = 0; x < width; x++) {
42 const srcIdx = (y * width + x) * 4;
43 const dstIdx = ((height - y - 1) * width + x) * 4;
44 imageData.data[dstIdx] = pixels[srcIdx];
45 imageData.data[dstIdx + 1] = pixels[srcIdx + 1];
46 imageData.data[dstIdx + 2] = pixels[srcIdx + 2];
47 imageData.data[dstIdx + 3] = pixels[srcIdx + 3];
48 }
49 }
50
51 ctx.putImageData(imageData, 0, 0);
52 return tempCanvas.toDataURL("image/png");
53}
54
55function captureWebGLCanvases(
56 container: HTMLElement

Callers 1

captureWebGLCanvasesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected