What is Pi.js?
Pi.js is a Javascript library that provides easy to use functions that can be used to make games and other graphical apps. There are also functions to help with sound and input including mouse, keyboard, and gamepads. Inspired by the language QBasic, Pi.js provides a simple and intuitive interface for creating interactive experiences.
Checkout the Pi.js reference for a detailed listing of all the functions including runnable examples for each function.
How to get started?
You can try it out online with one Pi.js' online editors.
- Playground - A simple online code editor that allows you to try out Pi.js online.
- Pi Editor - A more advanced online code editor that lets you upload image and sound files so you can build more advanced Pi.js projects and automatically saves your project to your local system. Note there is around a 5MB limit to the files you can upload.
Library Downloads
You can download the latest version from our downloads page.
Pi.js Essentials
Pi.js can be broken down two three main subjects.
- Graphics - Pi.js can create screens and provides a variety of tools for drawing graphics. This includes functions for drawing images and sprites, primative shapes, lines, and other graphical functions.
- Sound - You can dynamical generate music from a string of text, load and play music from files, or generate sounds and beeps by specifying the sound parameters.
- Input - Pi.js has a comprehensive set of functions to help make getting input from users fun and easy. This includes input for mouse, keyboard, and gamepads.
Running Locally
To run locally you will need to download the latest version of Pi.js and create an html file like below.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pi.js Demo</title>
</head>
<body>
<script src="pi-1.2.4.js"></script>
<script>
$.screen("320x200");
$.line(0, 0, 319, 199);
</script>
</body>
</html>
If you are running locally you might run into some problems when trying to render images without running under a server. This is due to a security feature that is not allowing the browser to get pixel data from an image on your local file system. To get around this you will need to run a local web server to run your app.
Here is a list of our recommended ways to setup a simple server for local development.
- Brackets.io - Brackets is a text editor but it also comes with a built-in web server that is super easy to use so this is highly recommended as a first choice if you don't really want to setup a server.
- Python Webserver - Python is a programing language but it also has a built-in server.
- PHP Webserver - PHP is another programming language that contains a built-in webserver.
- XAMPP - XAMP is a dedicated webserver that contains an easy-to-use setup.
Accessor Variables
Pi.js can be accessed via the global variables $ or pi, which provide a convenient way to access the Pi.js library from your code. These variables allow you to easily call the various functions and properties provided by Pi.js, making it simple to integrate Pi.js into your existing projects. The $ is what you will see in most examples on this site, but in case of a conflict with a library such as jQuery you can also use the pi variable to access Pi.js. If jQuery is already included before the Pi.js library then the $ will only be used by the jQuery library.
pi.screen("320x200");
pi.line(0, 0, 319, 199);
All functions are attached to the accessor variables and you can call the functions either through a list of parameters or a single object containing the named parameters. This comes in handy for functions such as screen that contain many optional parameters.
$.screen("320x200");
// The following two function calls are identical
$.line( 5, 5, 315, 195 );
$.line( { x1: 5, y1: 5, x2: 315, y2: 195 } );