Monday, August 11, 2014

Gopher, Anyone?

Does anyone remember Gopher?

Anyone?

I guess probably not. There used to be this document access protocol called Gopher that people used instead of the Hypertext Transfer Protocol (a.k.a. HTTP) that we know and love today. Well, maybe "instead of" isn't the right phrase. We used it alongside HTTP. It was like this other kind of thing you could do with the Internet. It's mostly forgotten now. We almost exclusively use HTTP to access documents over the Internet these days.

It's worth talking about because we often think of the Web (the entity created by HTTP) and the Internet as the same thing. The proliferation of ReST Web Services today as a means of opening up almost any kind of service imaginable over HTTP is increasing the confusion. Maybe because I used to do networking over a 2400 bps modem on a Tandy 1000 computer I don't see the Internet that way.

Tandy1000HX tweaked.jpg
"Tandy 1000HX tweaked"
The Internet is a wide area network of computers and computer-like devices using TCP/IP. HTTP is a protocol on top of that, just like Gopher, FTP, SSH, and a thousand others. Maybe someday HTTP won't be so dominant and some other protocol will find more widespread appeal.

And here's another thought. No one probably remembers using Lynx either. I used that to browse the web in college. Good times. Good times...

I sure could gopher a giant chocolate chip cookie right now.

Amphibian.com comic for August 11, 2014


Image Credits:
"Tandy1000HX tweaked" by Jesster79 (Non-creditable tweaks by Ubcule) - This file was derived from: Tandy1000HX.jpg . Licensed under CC BY-SA 3.0 via Wikimedia Commons.

Friday, August 8, 2014

JavaScript Colors: RGB to HEX

In my web comic editor, I needed to be able to both read and set the background color values on HTML elements. Since I've been working with colors as hex codes since, umm, forever, I thought it would be good to work with the colors that way. But to my dismay, I found that you can set the color of an element using a hex string (e.g. #ff0000), an RGB string (e.g. rgb(255,0,0)), or an HSL string (e.g. hsl(360,100%,50%)) but when you read the value it is always in RGB format. Here's an example:

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Color Test</title>
</head>

<body>

<div id="hextest" style="background-color: #FF0000; width: 100px; height: 100px;"></div>

<div id="rgbtest" style="background-color: rgb(0,255,0); width: 100px; height: 100px;"></div>

<div id="hsltest" style="background-color: hsl(240,100%,50%); width: 100px; height: 100px;"></div>

</body>

<script>

var a = document.getElementById('hextest').style.backgroundColor;
var b = document.getElementById('rgbtest').style.backgroundColor;
var c = document.getElementById('hsltest').style.backgroundColor;

console.log(a);
console.log(b);
console.log(c);

</script>

</html>


Maybe you've had this problem yourself. Fortunately, I found a handy JavaScript function that will convert RGB strings back into hex. It is a very compact and interesting function. Let's take a look at it.

function rgb2hex(rgb) {
 
 function hex(x) {
  return ("0" + parseInt(x).toString(16)).slice(-2);
 }
 var r = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
 var h = '#' + hex(r[1]) + hex(r[2]) + hex(r[3]);
 return h.toUpperCase();

}

There's a lot of good JavaScript to learn about here. First, you may notice that the function has another function declared inside of it. The inner function, hex, is essentially private to the rgb2hex function the same as a variable definition would be. It's like an anonymous inner function that isn't so anonymous. So what's the hex function doing?

It assumes, first of all, that you give it a String representation of an integer. Like "35" or something. The parseInt() function turns that into a JavaScript Number so that you can call toString(16) on it. What does that do, you ask? The parameter "16" is the radix. Without going into too much detail about what a radix is, just know that using radix 2 will give you the number's value in binary, radix 8 will give you the number's value in octal, and radix 16 gives you the number's value in hexadecimal. So calling toString(16) on a Number object with value 28 will give you the String "1c". But what's up with the fact that "0" is prepended to that value and then the whole thing is sliced with a slice parameter of -2? That's just a simple way of making sure that the return value of the function is always 2 characters and is left-padded with zeros. If the input to the function is less than 16, then the toString(16) call will return a String that is only 1 character long. But if the input is greater than 15, the result will be at least 2 characters. So if we always put a "0" in front and then just return the last two characters of the String, it will ensure that the hex value is 0-padded. Using slice() with a negative value as the parameter is a trick that returns the characters from the end of the String instead of the beginning. For example, "whatnot".slice(-3) returns "not".

Wow, that's quite a lot going on in that one line of JavaScript. The rest of the rgb2hex function isn't quite so tricky. Line 6 just uses a regular expression to find the 3 numbers in the RGB string. For example, with an input String of "rgb(100,200,150)" the value for r will be an array containing the values "100", "200", and "150". Line 7 just uses those three values as parameters to the hex function and sticks the "#" on the front. The last line returns the complete value in all upper case, since I like #FFFFFF better than #ffffff.

Anyway, I hope this function is as helpful to you as it was to me.

Amphibian.com comic for August 8, 2014



Wednesday, August 6, 2014

Your Uncoolness is the Coolest

When I was in fourth grade, my vision had deteriorated to the point that I could not adequately function in the classroom. I suppose it was inevitable, since heredity had given me a disadvantage in the vision department. But when I was 10 years old, having to wear glasses was terrible. They weren't anything close to the stylish frames I own today. They were thick, dark, plastic frames that looked terrible by today's standards. Or did they...

Now that we've delved into the pain of my childhood, let's talk about my oldest daughter. At 14, she still had perfect vision but chose to start wearing fake glasses just for the look. And by look, of course I mean style. I wear glasses for the look too, but in my case that means so that I can actually look at things (I can't wear contacts or have eye surgery, but that's a story for another day). Back to my daughter...the glasses she wants to wear are the dark plastic type frames that I once despised. What happened?

I won't embarrass her by posting her picture here, but she actually looks really good in the glasses. She even had her school picture taken wearing them last year. When I was her age, all I dreamed about was being able to get my picture taken without my glasses.

Here's my theory on what has happened. Somehow, over time, kids bought the notion that being yourself and being different were good things. It's cool to be different. So now, everyone wants to be different. And they become more different by assuming the qualities that have historically caused other children to be labeled as different. It's some kind of coolness paradox. If you do the things that make you uncool you achieve coolness.

Personally, I think they're missing the point. You should be the best you that you can be. If you wear glasses, great. Pick some that work with your face and be confident and happy. If you love to write software and be alone in your room, great. Write some really fun apps for phones. If you love to play soccer, great. Go run and kick things and whatnot. Maybe you don't know what makes you different and special yet. That's fine too, just try different activities and you'll discover something. Don't rule anything out, unless of course it's illegal and/or dangerous. I found that same year I got my glasses that I loved programming. I did it every day from then on and didn't really care if I was cool or not.

Remember, you are different enough just by the fact that you are yourself. Unless, of course, you are an identical twin.

Amphibian.com comic for August 6, 2014

Monday, August 4, 2014

Presentation Matters

How much does presentation matter to you? I'm not talking about PowerPoint. Do you favor style over substance? I think about this sometimes. I think about it when I consider if I should buy new wheels (style) or a new throttle body (substance) for my Mustang. I think about it when I consider if I should spend my time building a better user interface or adding more features.

I also think about this: years ago, and I can't find the source anymore, I read an interesting article about the qualities different socioeconomic classes in America look for in food. There are apparently big differences.

According to my long-forgotten source, the primary concern among the lower class is the quantity of food. They want to make sure that there is enough. Enough for themselves and enough to feed their family. If you go to their house for dinner, they'll ask you if you got enough to eat. I suppose this makes sense.

The middle class will be concerned with how the food tastes. They probably feel secure that they'll have enough to eat, so they want it to taste good. If they make dinner for you, they'll probably ask you how you like the taste. Flavor is important. Sure, I get that. I like my food to taste good. Who doesn't?

Bica do Sapato, Lisbon
Now, the upper class. As this theory goes, they are most concerned with how the food is presented. If you are upper class, maybe you understand this. If you are a simple country boy from the hills of Appalachia like I am, you probably don't get it. I've been to some fancy restaurants (see below), where I think upper class people must like to eat because the food is very nicely displayed on the plate. I almost feel bad eating it sometimes because I think somebody in the kitchen must have put a lot of work into making the dish look so nice. The portion size is small (only lower class people care about portion size) and the flavor is just alright (only middle class people care about taste) but the presentation is excellent. And it costs a lot.

Now you'll think about this sometimes, too. Like the next time you order a pizza.



Some Fancy Restaurants at which I have Dined

  • I proposed to my wife at the Valley Green Inn in Philadelphia. Excellent presentation. Try the duck.
  • Morton's The Steakhouse in San Francisco. It's a chain, I guess. But a very classy chain. I was under-dressed.
  • The Monet Room at the William Penn Inn in Gwynedd was very nice. I was not under-dressed.
  • We ate at the California Grill at Disney World once. We did it just to get the good view for the Magic Kingdom fireworks, but they have a Warm Octopus Salad. How fancy is that?


Amphibian.com comic for August 4, 2014



Food Image Credits:

"Bica do Sapato, Lisbon (5834390296)" by Ewan Munro from London, UK - Bica do Sapato, Lisbon. Uploaded by tm. Licensed under CC BY-SA 2.0 via Wikimedia Commons.

Friday, August 1, 2014

Web Comic Launch

Today I launch my web comic, which I have so creatively titled Amphibian.com. I've wanted to do a new comic for years, but I was waiting for the right inspiration. Unless you live in central Pennsylvania (inside this area) you probably don't know that 20 years ago I published a comic about a frog in a local newspaper. But newspapers are dead. Print comics are dead. My comic was barely alive in the first place. What's changed?

Web Comics Made with 100% Real Web


If you've ever read a comic on your computer or mobile device, you may have noticed that most of them are pretty much just print comics converted to a JPG or PNG and stuck on a web page. Most are probably made by talented artists, working with some artisty tools, maybe on a computer but maybe still on paper. Anyone who's ever seen my work knows that I am not a talented artist. But I am pretty good with the technologies that make web sites. And I have been drawing a frog so for so long that it looks like an almost legitimate frog.

So I decided to make a web comic that was made with real web technologies. I designed it to be optimized for mobile devices, and easy to share with your friends. Here's a breakdown of what it's made of.

  • HTML + CSS: The comics are just HTML markup and CSS styling. I didn't draw a box and then draw some stuff in it, I style a <div> to have a border and then position individual <img> objects in it to arrange the scene. Even the speech balloons are just <p> tags with appropriate styles applied.
  • SVG: I take responsive design to a whole other level. When you look at the comics on your phone, not only is the page header and navigation being restyled to fit the smaller screen, the comic images themselves are scaled down as well. Have a retina display? Make the page as big as you want, my frogs won't get all pixelated on you. And with Gzip compression, most of the images are actually smaller downloads than a large PNG would be.
  • JavaScript: In both the client and server, I'm using JavaScript to make things better. In your browser, jQuery lets me animate parts of the comic scenes easily. I can make things change when you click (or touch) the characters in the cells. These can be part of the jokes or just for silly fun. The back-end is made with Node and the Express framework. The part that you don't see is an editor that combines client-side goodness with REST web services to enable me to take the ideas from my head and put them in the comics with ease.
Comic Editor

Why Would Someone Do This?


Are there advantages to this approach to a web comic? I think there are many.

First of all, using web technologies to make web comics really uses the medium to its fullest. Web sites don't just look like newspaper pages (at least not anymore) so why should comics? We have all these great features in our web browsers and we are barely using them. I want to use them just a little bit more.

Second, comics made this way are mobile-friendly. They read top-to-bottom and don't require you to pinch or swipe or poke or jab or jump or anything. If you've ever tried to read a "normal" comic on a mobile phone in it's typical configuration - a.k.a. portrait mode - you know what I'm taking about. My comics scale to your device without giving up image quality and can be enjoyed with just your thumb on the phone.

Also, the HTML content can be read by the crawlers and even translators. Like I mentioned, the words my frogs speak are just normal HTML paragraph tags. The contents of my comics will be picked up by the search engine spiders and make it easier to find my stuff. And a tool like Google Translate can show you the comics in lots of other languages. I'm not sure if the jokes make sense in Swedish, but the words might!

Take My Code, Please


As always, my code is open source and on GitHub. My frogs and jokes aren't, but you probably don't want them anyway.


Amphibian.com comic for August 1, 2014


Sunday, May 4, 2014

Late for the (Multi)party

Last month I started making a legitimate web application with Node and Express. It's been a generally positive experience, but because I'm using the newest version of Express (4.1) I'm finding a lot of outdated information on the web about how things are accomplished. In version 4.x, Express no longer depends on Connect and most of the previously included middleware is now in separate repos. Overall, I think this is a positive change that adds more flexibility to the system.

Today I wanted to add the ability to perform file uploads to my application. I needed a way to handle the "multipart/form-data" POSTs, and most of the common examples point to the bodyParser middleware. This middleware is no longer recommended for use nor is it included by default (since 4.x) and searching for it led me to a different solution that was more appropriate for my particular needs.

There is a little confusion out there about body parsing middleware. Prior versions of Express included a middleware called "bodyParser" which could handle JSON, urlencoded, and raw request bodies. It is deprecated now in favor of the similarly-named "body-parser" middleware. So what's different about them, beside a hyphen and capitalization?

The new one (hyphenated) does not do multipart posts, which is a good thing. The old one (non hyphenated) did, but did so by writing all the uploaded files to a temp directory and making you process them from there manually. It was easy to forget to clean up the files, and then you might fill up your disk.

Bottom line: don't use bodyParser. Use body-parser. Even if you aren't using Express 4.x, get the new one and use it instead.

But now back to my actual problem. I can use body-parser from now until the cows come home and I still can't process multipart file uploads. There are other middlewares that I could use to process the uploads, so I had to be sure to pick the most appropriate one. In particular, I was looking for one which would allow me to process the stream of the POST body to get Buffer objects which I could write to my database.
I like to party!

The solution that I liked best turned out to not be a middleware at all. After much research, I decided to use Multiparty directly. Multiparty is a module for parsing multipart-form data requests which supports streams2, and doesn't require the data to be written to disk (it can, though, if you really want it to).

Here is a stripped-down version of the code I used, which keeps the uploaded files completely in memory so they can be written to a database or put on your file system somewhere. Whatever you want to do.


var express = require('express');
var multiparty = require('multiparty');

var app = express();

app.post('/images', function(req, res, next) {
 
 var uploadName = '';
 var uploadType = '';
 var chunks = [];
 var totalLength = 0;

 var form = new multiparty.Form();
 
 form.on('error', function(err) {
  // maybe handle errors here, or whatever
  next(err);
 });
 
 form.on('close', function() {
  
  var b = Buffer.concat(chunks, totalLength);
  console.log('storing file %s (%d bytes)', uploadName, b.length);
  // store the image in your database or whatever
  res.send(200);

 });

 form.on('part', function(part) {
  
  part.on('data', function(chunk) {
     chunks.push(chunk);
     totalLength += chunk.length;
  });
  part.on('end', function() {
     uploadName = part.filename;
     uploadType = part.headers['content-type'];
  });
  
 });
 
 form.parse(req);
    
});

var server = app.listen(3000, function() {
 console.log('listening on port %d', server.address().port);
});

It is fairly simple to use in this case. You just create a Multiparty Form object and parse the request with it. It will emit events as it goes.

The "part" event will be emitted for each part of the post. My posts only contain a single form parameter, which is the file, which makes this example really simple. The parameter passed to the handler for the "part" events is a Readable Stream, so it also will emit events as you process the data. I listen for the "data" events and append the chunks to an array of Buffers. When I see the "end" event, that part is over and I set the uploaded file's name and mime-type.

Typically (assuming no errors) the next event emitted by the form is the "close" event. This is where I concatenate all those Buffer chunks together to get the Buffer for the whole upload. I then store the file and return the response code. You have to make sure the listen for the "error" event just in case...if you don't and an error is encountered, your clients will hang forever waiting on a response.

If you want to handle multiple files in a single post, you'd want to move the chunks and totalLength variables into the "part" handler for the form and the processing of the Buffer into the handler for the "end" event for the part instead of the "close" event for the form like I did above. You still have to respond to the client in the "close" handler - don't forget that! It would look something like this:

 form.on('close', function() {
   res.send(200);
 });

 form.on('part', function(part) {

   var chunks = [];
   var totalLength = 0;

   part.on('data', function(chunk) {
      chunks.push(chunk);
      totalLength += chunk.length;
   });
   part.on('end', function() {
      var uploadName = part.filename;
      var uploadType = part.headers['content-type'];
      var b = Buffer.concat(chunks, totalLength);
      console.log('storing file %s (%d bytes)', uploadName, b.length);
      // store the image in your database or whatever
   });
  
 });
 
If you don't like my example, you can also use Multiparty much like the old bodyParser middleware to write the uploaded files to a temp directory if you want. See the readme for the complete API.

I would definitely recommend taking a look at this module if you desire to have a similar file-upload functionality.

Sunday, April 27, 2014

Conditional Attributes with Jade

I've been working with Node lately to create a complete web application (not just a Websockets back-end to an HTML5 game) and I am using the Jade template engine with Express.

I really like Express and Jade but the other day I ran into an issue and I just couldn't figure out how to get the HTML output I wanted. The problem was that I wanted to output attributes conditionally. Not just the values of the attributes conditionally, but the whole attribute. Sometimes I wanted to output this:

<img something="whatnot" foo="bar" src="/images/frog.svg" />

and sometimes I wanted to output this

<img something="whatnot" src="/images/frog.svg" />

based on a condition.

It's easy to output attribute values based on conditions. The value for an attribute can actually be any JavaScript expression. So these are all valid in Jade templates:

img(foo='bar')

img(foo=(x == 12 ? 'bar' : 'ribbit'))

img(foo='var-in-the-' + b + '-middle')

I just couldn't figure out how to prevent an attribute from being output at all based on a condition. I finally settled on this:

img(foo=(x == 12 ? 'bar' : ''))

which output this HTML when x is anything but 12:

<img foo="" />

Not really ideal, but I could make it work. I did make it work, but it bothered me. I lost sleep over it. I couldn't eat. I was withdrawn from my family. My children thought that I didn't love them anymore. Okay, maybe I'm exaggerating a little. But seriously, I didn't like it. I didn't want that attribute to come out at all. Today, I found the answer:

img(foo=(x == 12 ? 'bar' : undefined))

Undefined! Of course! Like I said, any valid JavaScript expression can be used. Now when x is anything but 12, the foo attribute is undefined and does not output to the HTML. In case you are curious, null also works in place of undefined. Using undefined is probably better though.

I am sleeping much better now.