Wednesday, January 20, 2010

DPS911 Project – Processing.js – v0.4

A bit of touching up
So here we are: the first release for DPS911 this semester! For this release I've added missing functionality to Processing.js's (Pjs) implementation of text(). Unlike before, the function may now handle the printing of variable of integers, floats, doubles, and bytes, in addition to strings.

Now I wait for a (hopefully) favourable peer review.

Pretty pictures
Here we see a live demo of how it works in Pjs now as shown on this page:



And here we see the same demo as displayed in the Processing Development Environment:


What are we looking at?
The Pjs implementation had to take a few things into account. Processing displays floats and doubles to the thousandths digit, which is easy enough to replicate in JavaScript using toFixed(). But notice how Processing rounds: for 2.7185 it rounds down to 2.718 instead of up to 2.719.

toFixed(), by itself, rounds 2.7185 to 2.719, so a little bit of extra code was needed to alter this, like so:

if ( (val*1000) - Math.floor( val * 1000 ) == 0.5 ) {
val = val - 0.0001;
}
val = val.toFixed(3);

So for 2.7185 we craftily modify it to 2.7184 before sending it to toFixed().

Pertinent links

1 comment:

Thomas B. Higgins said...

Nice work, and I appreciate the attention to detail.

This feature may seem like a small thing, but it's a real convenience. This sort of improvement is helping make Processing.js rock solid and the full equal of the original!

Post a Comment