Canvas tag: Collision detection & pixel decay

UPDATED: March 14, 2011 here

Continuing my tests/experiments with the Canvas tag has led to the Ping prototype. I had 3 minimal things I wanted to accomplish: Detecting the intersection of a 360 degree arc of ray/line segments to previously defined in map shapes; a visual decay/fade out of intersection points on the canvas, and lastly a test of performance. In addition I decided to experiment with another approach to Javascript object construction in the hopes of getting a performance gain.

performance
None of the code involved is optimized or really geared towards performance [ I am using JQuery.each(array|object, function() ) ] and a fairly inefficient pattern for determining intersection ( leveraging CPU with little or no caching ) but was mostly happy with the results when using Google Chrome on Ubuntu 9.10 and utterly surprised with Firefox 3.5 & 3.6 on the same system. It was literally a night and day difference with Chrome purring along and Firefox struggling to keep up.

JS Objects

Besides the canvas tag related stuff, I was experimenting with another approach to class construction that for the moment seems to goofy to be viable.

appLib.Point = function (x, y) {
        this.x = x;
        this.y = y;
    };

app.w(appLib.Point, function () {

    this.dist = function (o) {
        var tX = Math.pow(this.x - o.x, 2);
        var tY = Math.pow(this.y - o.y, 2);
        return Math.sqrt((tX) + (tY));
    };

    /**
     *Bad design choice
     *@deprecated */
    this.slope = function (o) {
        return (this.y - o.y) / (this.x - o.x);
    };
});

app.w( scope, anonymous) basically makes this in context of the anonymous function == scope.prototype. Therefore making it easier to add prototypical methods to the final product. My thought was to avoid closure based creation and rely on Function.prototype.method = function to make the objects leaner/faster. I figured that Javascript engines have already been optimized to a degree to build an object from it’s prototype. From my perspective, there didn’t seem to be a noticeable difference.

Final thoughts
Ping functions as expected on Chrome but otherwise if not for my quad-core environment, I imagine it would be a rather abysmal user experience. According to top and Chrome’s built in task manager, its devouring near 100% cpu time and has a semi-stable memory leak ( drifting up and down around 1,500 KB ever 30 seconds ) when in a resting state.