> ## Content Index
> Fetch the complete content index at: https://evil-penguin.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# MLX42 in the web Tutorial
- URL: https://evil-penguin.com/mlx42-in-the-web-tutorial/
- Published: 2025-06-30T16:17:53.000Z
- Updated: 2026-11-16T23:01:38.000Z
- Description: Tutorial explaining all steps to publish your mlx42 code to the web for free.
- Author: evilpenguin
- Tags: JavaScript, GitHub, MLX

MLX42 makes it possible to compile your C ready to deploy in a static webpage with WebAssembly by using [https://emscripten.org/](https://emscripten.org/?ref=evil-penguin.com)  
  
Let's start with an awesome Makefile that I tailored for you:  
  
NAME = pulga\_web  
  
CC = cc  
CFLAGS = -O3 -Wall -Wextra -Werror -Iincludes -IMLX42/include  
  
SRCS = $(*wildcard* srcs/\*.c)  
  
OBJS = $(SRCS:.c=.o)  
  
MLX\_NATIVE\_LIB = MLX42/build/libmlx42.a  
MLX\_WEB\_LIB = MLX42/build\_web/libmlx42\_web.a  
WEB = web/demo.html  
MLX\_FLAGS = -LMLX42/build -lmlx42 -ldl -lglfw -lm -lpthread  
  
*all*: $(MLX\_NATIVE\_LIB) $(NAME)  
  
$(NAME): $(OBJS)  
 $(CC) $(CFLAGS) $(OBJS) -o $(NAME) $(MLX\_FLAGS)  
  
$(MLX\_NATIVE\_LIB):  
 @cd MLX42 && cmake -B build  
 @cd MLX42 && cmake --build build -j4  
 @-mv MLX42/build/libmlx42.a $(MLX\_NATIVE\_LIB)  
  
$(MLX\_WEB\_LIB):  
 @cd MLX42 && emcmake cmake -B build\_web  
 @cd MLX42 && cmake --build build\_web --parallel  
 @-mv MLX42/build\_web/libmlx42.a $(MLX\_WEB\_LIB)  
  
*clean*:  
 rm -f $(OBJS)  
  
*fclean*: clean  
 rm -f $(NAME)  
  
*web*: $(WEB)  
  
$(WEB): $(SRCS) $(MLX\_WEB\_LIB)  
 mkdir -p web  
 emcc -DWEB -O3 -I include -I MLX42/include -pthread $(SRCS) \\  
 \-o $(WEB) \\  
 $(MLX\_WEB\_LIB) \\  
 \-s USE\_GLFW=3 -s USE\_WEBGL2=1 -s FULL\_ES3=1 -s WASM=1 \\  
 \-s NO\_EXIT\_RUNTIME=1 -s EXPORTED\_RUNTIME\_METHODS='\["ccall", "cwrap"\]' \\  
 \-s ALLOW\_MEMORY\_GROWTH  
  
*re*: fclean all  
  
*.PHONY*: all clean fclean re $(WEB)  
  
This Makefile comes with a cool feature which allows you to compile your project natively (for quick testing on the usual MLX window) and also for the web, when you are ready to deploy it in a website.  
  
make # normal make, compiles your project with libmlx42  
make web # web compilation with emcc , uses libml42\_web  
The Makefile currently produces all the files you will need for your web deploymentWEB = web/demo.html  
Later I will show you how to tailor this, for now I'll demonstrate how this works with an example I prepared for you:  
  
  
#include <MLX42/MLX42.h>  
#include <time.h>  
#include <stdlib.h>  
#include <stdio.h>  
  
#define *WIDTH* 800  
#define *HEIGHT* 600  
  
#ifdef *WEB*  
\# include <emscripten.h>  
\# include <emscripten/html5.h>  
#endif  
  
static *mlx\_t* \*mlx;  
static *mlx\_image\_t* \*image;  
  
static *int32\_t* ft\_pixel(*int32\_t* r, *int32\_t* g, *int32\_t* b, *int32\_t* a)  
{  
 return (r << 24 | g << 16 | b << 8 | a);  
}  
  
static *void* ft\_randomize(*void*\* param)  
{  
 (*void*)param;  
 for (*uint32\_t* i = 0; i < image->width; ++i)  
 {  
 for (*uint32\_t* y = 0; y < image->height; ++y)  
 {  
*uint32\_t* color = ft\_pixel(  
 rand() % 0xFF, // R  
 rand() % 0xFF, // G  
 rand() % 0xFF, // B  
 rand() % 0xFF // A  
 );  
 mlx\_put\_pixel(image, i, y, color);  
 }  
 }  
}  
  
static *void* ft\_hook(*void*\* param)  
{  
*mlx\_t*\* mlx = param;  
  
 if (mlx\_is\_key\_down(mlx, MLX\_KEY\_ESCAPE))  
 mlx\_close\_window(mlx);  
 if (mlx\_is\_key\_down(mlx, MLX\_KEY\_UP))  
 image->instances\[0\].y -= 5;  
 if (mlx\_is\_key\_down(mlx, MLX\_KEY\_DOWN))  
 image->instances\[0\].y += 5;  
 if (mlx\_is\_key\_down(mlx, MLX\_KEY\_LEFT))  
 image->instances\[0\].x -= 5;  
 if (mlx\_is\_key\_down(mlx, MLX\_KEY\_RIGHT))  
 image->instances\[0\].x += 5;  
}  
  
#ifdef *WEB*  
static *void* *emscripten\_main\_loop*(*void*)  
{  
*mlx\_loop*(mlx);  
}  
#endif  
  
*int* main(*int* argc, *char* \*\*argv) {  
 (*void*)argc;(*void*)argv;  
 if (!(mlx = mlx\_init(*WIDTH*, *HEIGHT*, "MLX42", *true*)))  
 {  
 puts(mlx\_strerror(mlx\_errno));  
 return(*EXIT\_FAILURE*);  
 }  
 if (!(image = mlx\_new\_image(mlx, 128, 128)))  
 {  
 mlx\_close\_window(mlx);  
 puts(mlx\_strerror(mlx\_errno));  
 return(*EXIT\_FAILURE*);  
 }  
 if (mlx\_image\_to\_window(mlx, image, 0, 0) == -1)  
 {  
 mlx\_close\_window(mlx);  
 puts(mlx\_strerror(mlx\_errno));  
 return(*EXIT\_FAILURE*);  
 }  
  
 mlx\_loop\_hook(mlx, ft\_randomize, mlx);  
 mlx\_loop\_hook(mlx, ft\_hook, mlx);  
  
#ifdef *WEB*  
*emscripten\_set\_main\_loop*(emscripten\_main\_loop, 0, true);  
#else  
 mlx\_loop(mlx);  
#endif  
 mlx\_terminate(mlx);  
  
 return *EXIT\_SUCCESS*;  
}  
This should be your file structure:includes/  
srcs  
└── main.c  
MLX42  
├── build  
├── build\_web  
├── cmake  
├── CMakeLists.txt  
├── CODE\_OF\_CONDUCT.md  
├── CONTRIBUTING.md  
├── docs  
├── include  
├── lib  
├── LICENSE  
├── README.md  
├── SECURITY.md  
├── shaders  
├── src  
├── tests  
├── tools  
└── web  
web  
├── demo.html  
├── demo.js  
└── demo.wasm  
MakefileIn order to understand the ***main.c*** you can refer to the official explanation in the MLX42 web directory: [https://github.com/codam-coding-college/MLX42/tree/master/web](https://github.com/codam-coding-college/MLX42/tree/master/web?ref=evil-penguin.com)  
  
![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_17-56-08.png)

Then you can access index.html with a webserver, I use python's easy solution:  
  
python3 -m http.server 8080  
Go to [http://127.0.0.1:8080/web/demo.html](http://127.0.0.1:8080/web/demo.html?ref=evil-penguin.com)

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_17-48-29.png)

Finally let's publish it in a website.  
  
Upload your code to a GitHub repository (it can be private)  
  
echo "# mlx\_web\_example" >> README.md  
git init  
git add README.md  
git commit -m "first commit"  
git branch -M main  
git remote add origin https://github.com/pulgamecanica/mlx\_web\_example.git  
git push -u origin main  
make fclean  
git add .  
git commit -m "second commit"  
git push***Go To Settings***

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-00-30.png)

***Go to Pages***

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-00-39.png)

***Open the "Branch" dropdown***

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-00-46.png)

***Select the "main" branch***  
  
This will automatically publish your files in the main branch to a web server. Your files will be indexed, this mean that the **demo.html** which was generated by *emscripten* will also be available. I'll explain how you can get the link... 

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-00-51.png)

***You might see this warning if you repo is private (this is normal)***

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-01-06.png)

***Go to the repo's settings***

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-01-14.png)

***Toggle the box to use the GitHub Pages website URL***

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-01-21.png)

***Toggle it again to deactivate it, we just wanted it to pre-fill the URL***

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-01-42.png)

Add the suffix ***/web/demo.html*** at the end of the link so it goes directly to the page!

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-02-14.png)

You can visit my example website here:  
  
[https://pulgamecanica.github.io/mlx\_web\_example/web/demo.html](https://pulgamecanica.github.io/mlx%5Fweb%5Fexample/web/demo.html?ref=evil-penguin.com)  
  
Now we will explore how you can change the website aspect because the default one is not very nice.  
  
First let's understand the html which was generated automatically:  
  
Starting with the ***"head"***

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-41-17.png)

In the head there is nothing too interesting, you can see some css and the tile, not that the page is not using viewport scaling, you might want to add this for phone compatibility, depending on the project specifications.  
  
Let's start customizing the page, change the title and move the css to a ***style.css*** file.  
  
The new head looks like this:  
  
<head>  
 <meta charset=utf-8>  
 <meta content="text/html; charset=utf-8"http-equiv=Content-Type>  
 <title>Pulga Page</title>  
 <link rel="stylesheet" href="style.css">   
</head>  
You will need to create the style.css and put the css there, this helps dealing with the css and making changes easier.  
  
We'll get back to the ***style.css*** later and I will give you some cool ideas.  
  
For now let's move to the body and understand what is happening.

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-47-36.png)

We have the following elements:  
  
\- image base64 encoded link <a>  
\- spinner <div>  
\- status <div>  
\- controls <span>  
\- progress <div>  
\- webgl canvas <div>  
\- terminal <textarea>  
  
Start by removing the image link ***<a>*** and the controls ***<span>***

![](https://evil-penguin.com/content/images/2026/11/Screenshot_from_2025-06-30_18-52-51.png)

Now your body should look like this:  
  
<div id="background"></div>  
  
<div id="main-container">  
 <div class="status-panel">  
 <div class="spinner" id="spinner"></div>  
 <div class="emscripten" id="status">Downloading...</div>  
 <progress hidden id="progress" max="100" value="0"></progress>  
 </div>  
  
 <div class="canvas-container">  
 <canvas  
 class="emscripten"  
 id="canvas"  
 oncontextmenu="*event*.*preventDefault*()"  
 tabindex="-1"  
 \></canvas>  
 </div>  
  
 <button id="toggle-terminal">Toggle Terminal</button>  
 <textarea id="output" rows="8" hidden></textarea>  
</div>  
Also add the following to the js script:  
  
*const* toggleBtn = *document*.*getElementById*("toggle-terminal");  
  
toggleBtn.*addEventListener*("click", () *\=>* {  
 outputElement.hidden = !outputElement.hidden;  
});  
And replace the ***style.css*** with the following  
  
/\* Background styling \*/  
body, html {  
 margin: 0;  
 padding: 0;  
 height: 100%;  
 font-family: 'Press Start 2P', monospace;  
 background: linear-gradient(to bottom right, #1b1c2b, #0f0f0f);  
 color: #33ff33;  
 overflow: hidden;  
}  
  
#background {  
 position: absolute;  
 top: 0;  
 left: 0;  
 width: 100%;  
 height: 100%;  
 z-index: -1;  
 background-image: radial-gradient(#3f3f3f 1px, transparent 1px);  
 background-size: 16px 16px;  
 opacity: 0.1;  
}  
  
/\* Layout container \*/  
#main-container {  
 display: flex;  
 flex-direction: column;  
 align-items: center;  
 padding: 1rem;  
 height: 100%;  
 box-sizing: border-box;  
}  
  
/\* Canvas focus \*/  
.canvas-container {  
 flex-grow: 1;  
 display: flex;  
 justify-content: center;  
 align-items: center;  
 padding: 1rem;  
}  
  
canvas {  
 border: 4px solid #33ff33;  
 box-shadow: 0 0 12px #33ff33;  
 width: 90vw;  
 height: 60vh;  
 max-width: 1024px;  
 background: black;  
}  
  
/\* Status elements \*/  
.status-panel {  
 text-align: center;  
 margin-bottom: 1rem;  
}  
  
.spinner {  
 width: 24px;  
 height: 24px;  
 border: 4px solid #33ff33;  
 border-top: 4px solid transparent;  
 border-radius: 50%;  
 animation: spin 1s linear infinite;  
 margin: auto;  
}  
  
@keyframes spin {  
 to {  
 transform: rotate(360deg);  
 }  
}  
  
progress {  
 width: 60%;  
 height: 10px;  
 margin-top: 0.5rem;  
}  
  
/\* Terminal output \*/  
#output {  
 width: 90%;  
 max-width: 1000px;  
 background: black;  
 color: #33ff33;  
 border: 2px solid #33ff33;  
 padding: 0.5rem;  
 resize: none;  
 font-size: 12px;  
 font-family: 'Courier New', monospace;  
 margin-top: 1rem;  
}  
  
/\* Toggle button \*/  
#toggle-terminal {  
 background-color: black;  
 border: 2px solid #33ff33;  
 color: #33ff33;  
 font-family: 'Press Start 2P', monospace;  
 font-size: 10px;  
 padding: 0.5rem 1rem;  
 margin-top: 1rem;  
 cursor: pointer;  
}  
  
#toggle-terminal:hover {  
 background-color: #111;  
 box-shadow: 0 0 6px #33ff33;  
}  
  
/\* Responsive tweaks \*/  
@media (max-width: 768px) {  
 canvas {  
 width: 95vw;  
 height: 50vh;  
 }  
  
 #output {  
 font-size: 10px;  
 }  
}  
Finally this is how it should look like:

![](https://evil-penguin.com/content/images/2026/11/Screencastfrom2025-06-3019-00-58-ezgif.com-video-to-gif-converter.gif)

Finally, in order to avoid emcc from **overwriting** your modified **html** file, you should definately change in the makefile the following line:  
  
WEB = web/demo.html  
**TO** 
  
**WEB = web/demo.js**   
This way emcc will know to only generate the html  
  
Also if your project loads anything, you will need to preload it like so:  
  
emcc -DWEB -O3  
 ... ... ...  
 \-s ALLOW\_MEMORY\_GROWTH \\  
 \-s ALLOW\_MEMORY\_GROWTH \\  
 **\--preload-file <your\_assets\_directory>**  
Hope you liked it! :D