File size: 3,142 Bytes
6c2bcb4
 
 
 
 
c976217
6c2bcb4
 
 
 
 
 
2f65818
6c2bcb4
 
 
 
 
 
 
 
 
 
c976217
6c2bcb4
 
 
 
 
 
 
 
 
 
c976217
 
6c2bcb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c976217
 
6c2bcb4
2f65818
 
 
 
 
 
 
 
 
 
 
c976217
2f65818
 
 
 
6c2bcb4
 
2f65818
 
6c2bcb4
 
 
 
 
c976217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f65818
 
 
 
6c2bcb4
 
 
 
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
import { renderToString } from "react-dom/server";

export const wrappers: Record<"js" | "html" | "css" | "miniHtml", (content?: string) => string> = {
	html(content) {
		return `<!DOCTYPE html>
<!-- generated with https://huggingface.co/spaces/failfast/2D-GameCreator by https://failfa.st -->
${renderToString(
	<html lang="en">
		<head>
			<meta charSet="utf-8" />
			<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
			<title>{content}</title>
			<script defer src="/script.js" type="module" />
			<link rel="stylesheet" href="/style.css" />
		</head>
		<body>
			<canvas id="canvas" />
		</body>
	</html>
)}`;
	},
	miniHtml() {
		return `
<!-- generated with https://huggingface.co/spaces/failfast/2D-GameCreator by https://failfa.st -->
${renderToString(
	<>
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />

		<canvas id="canvas" />
	</>
)}`;
	},
	css() {
		return `/**
 * generated with https://huggingface.co/spaces/failfast/2D-GameCreator
 * by https://failfa.st
 */

* {
	margin: 0;
	padding: 0;
	box-sizing: border-box;
}
html, body {
	height: 100%;
	width: 100%;
	overflow: hidden;
}
`;
	},
	js(content) {
		return `
/**
 * generated with https://huggingface.co/spaces/failfast/2D-GameCreator
 * by https://failfa.st
 */


/**
 * Global imports
 */
import Mousetrap from "https://cdn.skypack.dev/mousetrap@1.6.5";
import confetti from "https://cdn.skypack.dev/canvas-confetti@1.4.0";

/**
 * Helper to handle the resize of the window > canvas automatically
 */
function __2DGameCreator__ResizeHelper(){
	const _canvas = document.querySelector("canvas")
	_canvas.width = window.innerWidth;
	_canvas.height = window.innerHeight;

	function handleResize() {
		requestAnimationFrame(() => {
			_canvas.width = window.innerWidth;
			_canvas.height = window.innerHeight;
		});
	}
	handleResize();
	window.addEventListener("resize", handleResize, { passive: true });
}
__2DGameCreator__ResizeHelper()

function __2DGameCreator__downloadCanvasImage() {
    // Get the canvas element
    const canvas = document.getElementById('canvas');

    // Create a new 'a' element
    let link = document.createElement('a');

    // Set the download attribute with a file name
    link.download = 'canvas_image.png';

    // Get the data URL for the canvas
    link.href = canvas.toDataURL();

    // Trigger the click event on the link
    link.click();
}

// Uncomment if you want to take screenshots of the canvas
/*document.addEventListener("keydown", (e) => {
	if (e.isComposing || e.code === "KeyS") {
	  __2DGameCreator__downloadCanvasImage();
	  return;
	}
});*/

function createGameLoop(callback) {
    let lastTimestamp;

    function gameLoop(timestamp) {
        if (lastTimestamp) {
            const delta = timestamp - lastTimestamp;

            if (delta >= 1000 / 60) {
                lastTimestamp = timestamp;
                callback(delta);
            }
        } else {
            lastTimestamp = timestamp;
        }
        requestAnimationFrame(gameLoop);
    }

    return gameLoop;
}


/**
 * Generated 2D game
 */
${content}
`;
	},
};