Making an interactive Mondrian Canvas Web App

(with the option to save and load your artwork)

In this tutorial I will show you how to make a basic web application running on a Sinatra(Ruby) server. We will be using Ruby with the "Sinatra Gem", Javascript, and of course, since it is a web application, HTML and CSS.

What we'll be making

If you don't already have Sinatra installed, you can find information on it here.

Or more simply, type this line into your Terminal to begin installation.

Don't have ruby?

Assuming you may already be a little familiar using Javascript, or at least what it can do as a client-side language (where its code is executed in the clients browser 100% locally), you may be wondering why we need to use a server-side language (our Ruby with Sinatra), to complete this little project at all. Simply, the answer is, we don't! However, once we get the canvas up and running using Javascript, we will be adding some extra functionality to save and load your artworks, this is where Ruby comes in. We'll be generating a "code" to represent the color scheme you've made, and sending that to our server to process and store. Similarly, we'll be asking the server to retrieve this information and send it back to the browser to load our paintings 'back to life', but this all comes at the end. Either way, lets get started.

Setup

Lets start by setting up our work-space. Since we WILL be using ruby before we are done, we're going to get everything running on our Sinatra server from the get-go. This way, we won't have to come back to change and rearrange our files when the time comes later.

Working on a Sinatra server does have some differences than working with only HTML, CSS, and JavaScript. For instance, we won't even be using .HTML files. Sinatra uses something called a .ERB, or Embedded Ruby document. This works just like a normal .HTML file, but with some added functionality, allowing us to embed bits of Ruby code to run within the page. More on that later

I've created a GitHub repository with all the stuff you need to get started in it HERE. Go ahead and clone that, or just follow the 5 steps below.

  1. Create a project folder. I'll call mine "Mondrian", but any name is fine.

  2. Inside your project folder, add some folders. "public" and "views". Make sure these are not uppercase. These folders are very important working with Sinatra. "views" will contain our .erb files, and public will hold some of our other resources, like your CSS and JavaScript files.
  3. Make a Ruby file - Create a blank text document, save it as "main.rb" - make sure to save this file in your root folder. Mine would be "Mondrian". It should look like this.. Right now, this file won't be doing much. However, we will have to run it to start up our local server. Its main responsibility, for now, is to display our index.erb. (the get "/" do) part, saying that when "/", or our index is requested in the browser, respond with our index.erb. Lets make that next.
  4. Create another blank text file. Call this one index.erb and save it in your "views" folder. We've got some HTML and CSS to get us started, so copy and paste this into your .erb
  5. Okay, great. Now your CSS. Copy and paste the this code in another blank file, then save it as mondrian.css. Its important that you save this into your "public" folder, otherwise it won't be read.

Great! Now start up your server. Navigate to your project folder in terminal (the one containing your main.rb and other folders). Once you're there, type "ruby main.rb" into the terminal window.. some code should run and you will see a message confirming your localhost has been started on some port. Mine was 4567. Go to http://localhost:4567, or your port number in your web-browser and you should now see your blank canvas awaiting you!

Alright! Lets start writing some code

Before we get started, take a second to look at some of the code you've been provided. You'll see a bunch of different classes in your HTML code, which will be an important part of what we'll be using to accomplish our goals. By adding event listeners, we will manipulate properties of the Document Object Model(DOM) in order to paint our soon to be beauitified canvas. Open your scripts.js, located in your public folder.

Javascript

Javascript will be responsible for the majority of our functionality. Lets start by adding a simple event listener. Go ahead and get this into your JS file (if you didn't clone my GitRep and its not already there.)

This is our first event listener. We'll be using these quite a bit within our JS. This bit of code is saying, okay, lets add an event listener to the "window". When this event happens, do some stuff in this function. This particular listener is for "load", meaning that, when the page, or "window", has loaded, this function will run. There are lots of different types of events we can listen for, things like keystrokes, clicks, etc, and we can apply them to just about anything within our page. Anyways, the rest of our JavaScript code will go inside this EventListener function.

Chosing a color

Lets make another "Event Listener". We need to be able to pick a color from the palate in order to paint with it. Our next goal is to create an Event Listener that says, whenever we click on one of our color options, it will somehow figure out what color it is, and set that to our current color so that we can paint with it. Remember, this code goes INSIDE of the window on load listener we have already created.

Here we have 3 variables. "curColor", which will store the current color we have selected to paint with. The other 2 look a little different - these variables store functions.