SmallPT in Javascript

I recently found a very interesting project, called SmallPT. It is photorealistic 3D renderer, written in 99 lines. So I decided to rewrite it into JavaScript.


SmallPT is a photorealistic renderer. It uses Monte Carlo path tracing to render the scene. It contains 3 materials: diffuse material, mirror and glass, and one primitive: sphere. For more info, read the official Kevin Beason's webpage.

Rewriting C++ to JS was pretty simple. I just had to remove type identifiers ("var" instead of "int", "double", "Vec", ...). I also had to rewrite classes into JS constructors. In Javascript, you can not overload operators, so I had to rewrite all expressions with vector operations into function calls.

Try it yourself

Below is the official 99-line JS version of SmallPT. You can pass it 3 comma-separated parameters: width, height and number of iterations. E.g. smallpt.html#800,600,4 will produce a 800x600 image with 4 iterations of algorithm (more iterations = better image).

I also have written another version with explicit light sampling, also from Kevin Beason. I added several scenes and a simple UI with continuous updating of bitmap.

Old comments (closed because of spam)


  1. Andy said:

    Thanks a lot – was gonna port to js but I will use yours instead.

    Using Web Workers can provide some speedup without resorting to porting the code to WebGL.

    December 30th, 2013
  2. Andy said:

    You get at least a 2x speed up with Web workers – at least on my machine. You can get the web worker version at:

    December 31st, 2013