Friday, October 30, 2015

Starting a New Game

I've been kicking around the idea for a while now, but tonight I finally started a new game for Amphibian.com. Actually starting a new project is almost as difficult as finishing one, but if I never start there's no chance of ever finishing.

I don't have any code or pictures to share yet, but this one is going to star Science Frog. I'm trying to make it in the style of a NES Megaman game, and I happened to find a great resource for information about old video game physics.

TASVideos is a site which, among other things, catalogs lots of data about how the physics worked in games to assist speedrunners. This page on the NES Rockman game (also known as Megaman) has a great breakdown of the horizontal and vertical speed of Megaman as well as the gravity constants and lots of other good information. With this, I should be able to make Science Frog's jumps mimic Megaman's.

So far though, these numbers have not resulted in any great achievements. The jumps are still all out-of-whack probably due to my inability to get the code right using Phaser. I'll keep trying and hopefully by Monday I'll have something to show (along with some pictures of my kids in their Halloween costumes).

And speaking of Halloween costumes, since tomorrow is Halloween I have a Jack-o-lantern theme today in the comic (and the color-change animation effect I talked about on Monday!)

Amphibian.com comic for 30 October 2015

Wednesday, October 28, 2015

Bit Rot. Is it real?

Have you heard of bit rot before? It's the perception that software (the bits) will degrade (or rot) after sitting around for a long time. It will begin to malfunction despite no apparent changes in the environment. I've been writing software long enough to know that it doesn't change on its own, but I've experienced bit rot before. It sure seems real, despite the logical portion of my brain telling me that it can't be.


It sounds crazy, but a system that hasn't been used in a long time just stops working. We have a system at work which we use every few months for demonstrations or training and in between those times it sits idle. Every time we go to use the thing, we find it to be completely crashed in some weird way. Did it get lonely?

And what about the speed of a computer? Have you noticed that they just seem to slow down over time, even if nothing else changes? I had a computer that was new in 2008 and had no real upgrades performed on it for about 5 years. No new software, no upgrades to the operating system. At the end, it was so slow as to be practically unusable. Was it always that slow and I just didn't notice because every other computer I used was similar in speed? Or did it actually slow down over time? I'm fairly certain my cell phone is slowing down as well. Does that count as bit rot too? Or is that something else?

I have no explanations, but this phenomenon can't be ignored. It can be talked about in frog comics though.

Amphibian.com comic for 28 October 2015

Monday, October 26, 2015

Animate Colors with jQuery

While working on an upcoming comic, I wanted to add a color-change effect to an element. I soon learned that jQuery's animate() function doesn't support animation of non-numeric CSS properties. So while I can animate a width between 100px and 200px, for example, I can't animate a color between #000000 and #FF00FF.

But all is not lost! There is a plugin, appropriately named jQuery Color, which adds the color animation feature. It is trivial to use and extremely small to download.

To start using it, just download the .js file from their GitHub page. I am using the minified version. Include it in your page after jQuery. Then, just animate colors the same way you would animate other CSS properties and it will magically work!

In the following example, the page starts out containing a DIV with a black background. As soon as jQuery is ready, an animation changes the background color to purple over a span of 10 seconds.

<!doctype html>

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

<body>

  <div id="test-div" style="width: 500px; height: 300px; background-color: #000000;"></div>

</body>

<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/color/jquery.color-2.1.2.min.js"></script>
<script>

$(function() {

    $('#test-div').animate({backgroundColor: '#FF00FF'}, 10000);

});

</script>

</html>

It doesn't get much easier than that! But what comic prompted me to use this? It's not for today's. You'll just have to wait to find out (or look in the repository on GitHub).

Amphibian.com comic for 26 October 2015

Friday, October 23, 2015

Froggy Pictures from Texas

You may not know this, but whenever I travel away from home I always have a plush frog with me. This started way back, probably 8 years ago, when my daughter Alex told me to take her Froggy with me on a business trip. I took pictures of him doing various things with me, and continued the tradition with every subsequent trip. When our second daughter was born, Alex gave Froggy to her but he still comes with me on trips.

Since I've been in the Dallas, Texas area this week, I thought I might as well share some of the Froggy pictures. This frog has been many places. This is his third time in Texas.

Plush frogs like to take selfies, just like people.

He likes to eat green foods. We eat dinner together every night.

He helps me write my blog. His typing is poor.

I often find him sitting by the pool.

We sometimes share a pizza. This one was pretty good.

Big storm in the Dallas area tonight. Froggy watches the rain.
Froggy was not the inspiration for Amphibian.com. I was drawing frogs and putting them on websites years before Alex was born. In fact, I started drawing frogs when I was 11 years old, just a year older than Alex is now. Which is probably why someone thought a plush frog would make a nice gift for my newborn daughter (Froggy is technically a Mary Meyer Sweet Rascals Freda Frog if you want one of your own. They're getting hard to find nowadays).

Amphibian.com comic for 23 October 2015

Wednesday, October 21, 2015

Bleeding Edge JavaScript Math

In today's comic, Science Frog finds himself on the bleeding edge by accident - literally. Just so I have a nice tie-in with that, I'm going to share some bleeding edge JavaScript math functions. Not literally. You won't actually bleed. Unless you cut yourself on your keyboard, in which case you really should get a better keyboard.

Why do I say this is bleeding edge? I guess I could say cutting edge or leading edge. I could also just drop the idioms and say that these are new language features. The first is only available in the Firefox nightly builds at the moment. How's that for the edge of some kind?!

What is that new feature? An exponentiation operator! That's right, no more need for Math.pow()! Just use double asterisks. This is a feature currently planned for ECMAScript 7 next year, but you can try it in a Firefox nightly build today (it may be included in next month's standard release):

var e = 2 ** 8;
var p = Math.pow(2, 8);

console.log(e === p);  // prints true

Not exciting enough for you? Come on, Java doesn't even have an exponentiation operator and it's been a legitimate language for at least 10 years! I guess Python does, but now all you Python devs don't have an excuse for not switching to JavaScript.

Now for something slightly less edgy. Since Math.pow() will become obsolete, I guess it's only fair that it get a new function. Added as part of ECMAScript 6 this year, Math.fround() is that function. Given a number, it returns the nearest single-precision floating point number. What? Try this (it works in standard Firefox releases since 26 and Chrome since 38):

var x = Math.fround(2.567);
console.log(x); // prints 2.566999912261963

var y = Math.fround(3);
console.log(y); // prints 3

var z = Math.fround(3.3);
console.log(z); // prints 3.299999952316284

I'm sure it makes sense now - Math.fround() gives you the most precise floating point equivalent of a number.

That's all I have to share today. I'm actually away from home this week - in the Dallas area for some software architecture training. But I did all the comics for this week before I left, so no problems there! Missing the comics is fround upon. Sorry, couldn't resist that one.

Amphibian.com comic for 21 October 2015

Monday, October 19, 2015

JavaScript Collections - Set

In addition to Map, JavaScript also got another popular collection type this year - the Set. As part of ECMAScript 2015, which is now mostly supported by Node.js, this new build-in object type was added to the language. It's also available on the client-side in Firefox, but it's probably best to keep it on the server side for right now.

So how does it work? Pretty much like you'd expect a Set to work. You can add values to it, but it can only hold unique values (contrast this with Array, which can hold multiple of the same object).

var s = new Set();

s.add("string value");
s.add(4);
s.add(false);

console.log(s.size); // prints 3

console.log(s.has(4));               // prints true
console.log(s.has("not in there"));  // prints false
console.log(s.has(3+1));             // prints true
console.log(s.has(false));           // prints true

s.add(4);            // no effect
console.log(s.size); // still prints 3

s.delete(4);
console.log(s.size); // prints 2

As you can see from the example, it supports the standard functions for adding, removing, and checking. Note that, just like Map, the type of objects that can be put in a Set are wide open. You can keep Strings, Numbers, Booleans, and even Functions in the same set. The Booleans are interesting - look at line 5 above. You can add false to the Set. Then s.has(false) returns true. But the fun doesn't end there. You can even put Infinity (NaN) in the Set if you want. Why would you want to? I have no idea, but it works.

var b = new Set();

b.add(4/0);
b.add(3/0);

console.log(b.size); // 1, Infinity added twice

console.log(b.has(1/0)); // prints true

Okay, enough of that. What else should you know about Sets? You can iterate over them just like Arrays.

var c = new Set();

c.add("one");
c.add("too");
c.add("tree");

c.delete("too");
c.add("two");

for (i of c) {
    console.log(i); // one, tree, two
}

The iteration will produce the contents in insertion order, which is why the above example prints one, tree, and then two. There's not really a way to re-order the Set.

Just like I said about Maps, I haven't really been missing them from JavaScript. There's plenty of ways to get around their absence. But now that they're part of the language, I'm sure I'll end up using one somewhere. Personally, I think Sets add more to JavaScript than Maps do simply because of the unique item enforcement.

Don't miss today's Amphibian.com comic. Laughter keeps you healthy.

Amphibian.com comic for 19 October 2015

Friday, October 16, 2015

JavaScript Collections - Map

I still can't fold them back up!
Ready to be confused? Okay, so you know that JavaScript has a map function on Array objects. It creates a new Array by applying a given function to each element of the Array. But now it also has a Map collection object. Now when you talk about a Map in JavaScript, are you talking about a function on an Array or a key/value pair data structure?

Those of us who did a lot of programming in other languages before JavaScript are probably familiar with classic data structures like the Map. A Map is a (typically) indexed collection of keys and their associated values. If you know the key, you can grab the value. C++ has collections objects, as does Java. JavaScript has not had a native Map until recently, but I've never really missed it. Why? Keep reading.

In many of the newest browsers and in Node 4, we can now use Maps. Their functionality should come as no surprise to anyone familiar with basic data structures found in other languages.

var m = new Map();

m.set("key1", "value1");
m.set("key2", "value2");

console.log(m.size); // 2

console.log(m.get("key1")); // "value1"
console.log(m.get("key3")); // undefined
console.log(m.has("key2")); // true
console.log(m.has("key4")); // false

But this is JavaScript! We needn't limit ourselves to just one type of key or value! Check this out:

var p = new Map();

// function as a value...not too strange:

p.set("f1", function(x) { return x*42; });

console.log(p.get("f1")(2)); // 84

// function as a key...a bit odd:

var w = function(z) {
    return z % 3;
};

p.set(w, "weird");

console.log(p.has(w)); // true
console.log(p.get(w)); // weird

Sure, it's perfectly legal code...but why? I know I'm not the world's smartest software engineer, but I cannot think of an example where putting a function as the key in a Map would make any sense. If you can think of one, please comment below.

Now we're having fun with Maps and we're all excited about JavaScript getting this great new data structure. But this is not a whole lot different from what we could always do in JavaScript with objects, since objects behave pretty much like Maps.

var h = {};

h["key1"] = "value1";
h["key2"] = "value2";
h["func1"] = function(t) { return t / 8; };

console.log(h["key1"]);      // value1
console.log(h["key3"]);      // undefined
console.log(h["func1"](16)); // 2
console.log(h.func1(32));    // 4

See? Pretty much the same as my first Map example up at the top. Fine, plain Objects have no size field...so I suppose that's one thing that Maps offer. There are also some differences is how iteration is handled. Consider the following example:

var myMap = new Map();
myMap.set("k1", 33);
myMap.set("k3", 55);

var myObj = {};
myObj["k1"] = 33;
myObj["k3"] = 55;

for (e of myMap) {
    console.log(e);    // the key and value as an array
    console.log(e[0]); // just the key
    console.log(e[1]); // just the value
}

for (e in myObj) {
    console.log(e);        // just the key
    console.log(myObj[e]); // the value
}

Iterating over the Map with for (e of myMap) gives you values for e that are 2-element arrays containing both the key and the value for each Map entry. Iterating over the Object with for (e in myObj) gives you values of e that are just the key.

I'm not going to tell you that JavaScript Maps are the greatest thing since sliced bread, but maybe sliced bread was never that big of a deal anyway. It's not like people didn't have knives. And speaking of bread...

Amphibian.com comic for 16 October 2015