Git Product home page Git Product logo

rayjs's Introduction

rayjs logo

rayjs - Javascript + Raylib

QuickJS based Javascript bindings for raylib in a single ~3mb executable

What is this?

rayjs is small ES2020 compliant Javascript interpreter based on QuickJS with bindings for Raylib. You can use it to develop desktop games with Javascript.

What this is not

rayjs is not a binding for NodeJS nor is it running in the browser (yet). It's comes with its own Javascript engine (QuickJS) similar to how NodeJS comes with the V8 engine. That makes it much easier to run and distribute rayjs programs as all you need to run a program / game is the small rayjs executable. No installation, no dlls or additional files are needed.

Features

  • Compiles into a single, small executable without any dependencies for easy distribution
  • Use modern Javascript features like classes or async/await
  • In-depth auto-complete with definitions for the whole API

Getting started

  1. Download the binary for your platform from the release section.
  2. Unzip the executable to a folder and create a new text file in the same folder. Rename the file to main.js
  3. Open the file with a text-editor (e.g. Notepad) and add the following code
    const screenWidth = 800;
    const screenHeight = 450;
    
    initWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
    
    setTargetFPS(60);   
    while (!windowShouldClose()) {
        beginDrawing();
    
            clearBackground(RAYWHITE);
    
            drawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
    
        endDrawing();
    }
    closeWindow();
  4. Run the rayjs executable
  5. Congratulations, you have created your first rayjs app.

Running code

rayjs will run code in three different modes

  1. If no parameter is given it will look for a file called main.js in the executable directory
  2. It will run a given Javascript file given as a command line argument like this rayjs <filename>
  3. It will look for a file called main.js in a folder given as a command line argument like this rayjs <foldername>

The directory of the main Javascript module will also be the working directory of the app. Modules and resources will be loaded relative to it.

API support

The following raylib APIs are supported so far (with a few exceptions):

  • core (no VR support yet)
  • shapes
  • textures
  • text (no support for GlyphInfo yet)
  • models (no animation support)
  • shaders
  • audio
  • raymath
  • rcamera
  • rlights
  • raygui
  • reasings

Similar to including a header in C and for your convenience, all types/functions are provided globally. They are additionally available in a module called 'raylib'

To check which API functions are not available (yet) check /bindings/src/index.ts for ignore statements.

Additional APIs

Rayjs comes with some additional functionality on top of raylib to make writing raylib code with Javascript easier

/** Replace material in slot materialIndex (Material is NOT unloaded) */
declare function setModelMaterial(model: Model, materialIndex: number, material: Material): void;
/** Get material in slot materialIndex */
declare function getModelMaterial(model: Model, materialIndex: number): Material;
/** Get a single mesh from a model */
declare function getModelMesh(model: Model, meshIndex: number): Mesh;
/** Set shader constant in shader locations array */
declare function setShaderLocation(shader: Shader, constant: number, location: number): void;
/** Read a single pixel from an image */
declare function imageReadPixel(image: Image, x: number, y: number): Color;
/** Make a deep-copy of an existing mesh */
declare function meshCopy(mesh: Mesh): Mesh;
/** Create a new mesh that contains combined attributes of two meshes */
declare function meshMerge(a: Mesh, b: Mesh): Mesh;

Additionally it also comes with bindings to lightmapper.h. See below for more information.

Auto-Complete / Intellisense

rayjs comes with full auto-complete support in the form of the definitions file lib.raylib.d.ts. These will work with Typescript and Javascript. In order to use them with Javascript you should create a Typescript configuration file in the project root (even if you are not using Typescript) called tsconfig.json with the following configuration

{
  "compilerOptions": {
    "allowJs": true,
    "target": "es2020",
    "lib": [
      "ES2020"
    ]
  }
}

After that put the lib.raylib.d.ts file in the same folder and optionally restart your IDE. Auto-complete should be working:

Examples

Some official raylib examples were already ported to Javascript and can be found in the examples folder.

Additional examples are described here.

./rayjs examples/js_example_project

Barebones example project on how to structure a project that uses Javascript

./rayjs examples/js_mesh_generation.js

Shows how to create a mesh from Javascript ArrayBuffers

./rayjs examples/shaders/js_shaders_gradient_lighting.js

Creates a gradient and uses it as lighting for a 3d scene

./rayjs examples/ts_dungeon

Small example game that uses Typescript with Webpack for transpilation and bundling

./rayjs examples/ts_game

Example how to integrate existing JS libraries. This example integrates the Inkjs library to compile and play a game written in the Ink interactive fiction language.

Lightmapper usage

Rayjs integrates the lightmapper.h library to render baked lighting. The example demonstrates it's usage.

./rayjs examples/js_lightmapper.js

Meshes must have unwrapped lightmap uvs in the second UV channel.

The example uses an environment that is uniform white which will lead to baking pure ambient occlusion. To bake other light sources, lower the amount of ambient lighting and everything that is rendered with a color other than black will become an emissive lightsource during baking. Rendering will just work as usual and custom shaders are supported. E.g. while the raylib default shader does not support color intensities greater than 1.0, the lightmapper does support them for higher intensity lighting.

The example will try to bake lighting alongside the render loop which is still buggy and leads to artifacts. Baking before rendering works better.

Performance

QuickJS is one of the faster JS interpreters. I'm getting about 13000 bunnys in the unmodified bunnmark before dropping any frames on my 2020 Macbook Air M1 which seems pretty good. Bunnymark

Building

Here are some basic steps if you want to compile rayjs yourself. You should use CMake for building. Please note that QuickJS needs Mingw in order to compile correctly on Windows

Check out required files

git clone https://github.com/mode777/rayjs.git
git submodule update --init --recursive

Build with cmake

Make sure you have cmake installed and in your path.

cd rayjs
mkdir build
cd build
cmake ..
make

rayjs's People

Contributors

mode777 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rayjs's Issues

Feature request: Allow combination of zip files with rayjs executable

Feature request:

Love2d has a method for creating distributable files without exposing the underlying code. Their method involves combining a .zip or .love file into the project using the built in copy methods available on mac/windows/linux. I very much think this is a possibility for rayjs and would allow users to create exes without have js files adjacent to the rayjs executable. I attempted this method myself with rayjs and windows, however it was unable to find the js files.

The command I used to test
cmd /c copy /b rayjs.exe+main.js TestGame.exe

Love2d's distribution documentation

The code execution cannot proceed because libwinpthread-1.dll was not found (on Windows 11)

Hello! I tried rayjs on Linux and it works nicely but I just downloaded the Windows version directly from the releases tab and I cannot run rayjs.exe unless I download libwinpthread-1.dll from external sources. Is this normal? Would it be possible to link it so users don't have to download it externally every time? I'm on a pretty new Windows 11 installation so I might be missing dependencies...

Thank you very much!

native module

I love what you are doing here!

I think it would also be cool to load a normal native quickjs module, so games can run in plain quickjs runtime. I am not sure, but it seems like it might just take a little work with the bindings you have made. I saw this article. If there is interest, I can look into it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.