Browse files

a bit of optimization on the mousemove handler. bit too slow and this…

… is a little better. alsodo not add heat for non-existent pixels off the far edges of the image.
  • Loading branch information...
1 parent 64c04f5 commit 79dda7095669c863d2874bb87e04064f6de28fd2 mpd committed May 12, 2010
Showing with 28 additions and 24 deletions.
  1. +28 −24 heatmap.js
View
52 heatmap.js
@@ -22,31 +22,37 @@ $(function () {
height: $('#result').height()
});
- $('#trackme').mousemove(function (event) {
- var key, eventX, eventY, i, j, initialI, initialJ;
-
- eventX = event.pageX - this.offsetLeft;
- eventY = event.pageY - this.offsetTop;
-
- initialI = eventX - 1;
- initialJ = eventY - 1;
-
- for (i = initialI; i <= eventX + 1; i += 1) {
- if (i < 0) {
- continue;
- }
-
- for (j = initialJ; j <= eventY + 1; j += 1) {
- if (j < 0) {
+ (function () {
+ var key, eventX, eventY, i, j, initialI, initialJ,
+ canvasWidth = canvas.attr('width'),
+ canvasHeight = canvas.attr('height');
+
+ $('#trackme').mousemove(function (event) {
+ eventX = event.pageX - this.offsetLeft;
+ eventY = event.pageY - this.offsetTop;
+
+ initialI = eventX - 1;
+ initialJ = eventY - 1;
+
+ for (i = initialI; i <= eventX + 1; i += 1) {
+ if (i < 0 || i >= canvasWidth) {
continue;
}
-
- key = i + "," + j;
- heat[key] = heat[key] || 0;
- heat[key] += heatMask[i - initialI][j - initialJ];
+
+ for (j = initialJ; j <= eventY + 1; j += 1) {
+ if (j < 0 || j >= canvasHeight) {
+ continue;
+ }
+
+ if (heatMask[i - initialI][j - initialJ] > 0) {
+ key = i + "," + j;
+ heat[key] = heat[key] || 0;
+ heat[key] += heatMask[i - initialI][j - initialJ];
+ }
+ }
}
- }
- });
+ });
+ }());
setInterval(function () {
// renderHeat(normalizeHeat(smoothHeat(heat)));
@@ -208,6 +214,4 @@ $(function () {
cache.hueToRGB[cachekey] = [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
return cache.hueToRGB[cachekey];
};
-
-
});

0 comments on commit 79dda70

Please sign in to comment.