changeset 44:9c7c3cb5f776

simplified cropping logic
author Atul Varma <varmaa@toolness.com>
date Mon, 14 Dec 2009 11:33:35 -0800
parents 1909896414d8
children 193b70fd268e
files mbp.js
diffstat 1 files changed, 20 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/mbp.js	Mon Dec 14 11:20:11 2009 -0800
+++ b/mbp.js	Mon Dec 14 11:33:35 2009 -0800
@@ -34,26 +34,34 @@
               best = this;
           });
 
+        var cropAttr = $(best).attr("data-crop");
+        var scaling = 1;
+
         // If our best picture doesn't have a cropping rect associated
         // with it, we'll use the scaled cropping rect of another
         // resolution.
-        if (!$(best).attr("data-crop") && lastCrop) {
-          var scaling = dWidth(best) / dWidth(lastCrop);
-          var parts = $(lastCrop).attr("data-crop").match(rectRegexp);
-          if (!parts)
-            throw new Error("Invalid 'data-crop' for " + lastCrop.href);
+        if (!cropAttr && lastCrop) {
+          cropAttr = $(lastCrop).attr("data-crop");
+          scaling = dWidth(best) / dWidth(lastCrop);
+        }
+
+        if (!cropAttr)
+          throw new Error("No 'data-crop' for " + best.href);
 
-          var newParts = [];
-          for (var i = 1; i <= 4; i++)
-            newParts.push(Math.floor(parts[i] * scaling));
-          $(best).attr("data-crop", "rect(" + newParts.join(", ") + ")");
-        }
+        var parts = cropAttr.match(rectRegexp);
+        if (!parts)
+          throw new Error("Invalid 'data-crop' for " + best.href);
+
+        var crop = {top: Math.floor(parts[1] * scaling),
+                    right: Math.floor(parts[2] * scaling),
+                    bottom: Math.floor(parts[3] * scaling),
+                    left: Math.floor(parts[4] * scaling)};
 
         var image = $("<img/>");
         image.attr("src", best.href);
         image.attr("width", $(best).attr("data-width"));
         image.attr("height", $(best).attr("data-height"));
-        image.attr("data-crop", $(best).attr("data-crop"));
+        image.data("crop-parts", crop);
         $(this).find(".link").empty().append(image);
         pictures.remove();
       });
@@ -67,15 +75,7 @@
         function onLoaded() {
           $("#countdown").text(--loadsLeft);
 
-          var crop = $(this).attr("data-crop");
-          var parts = crop.match(rectRegexp);
-          if (!parts)
-            throw new Error("Invalid 'data-crop' for " + this.src);
-
-          var crop = {top: parts[1],
-                      right: parts[2],
-                      bottom: parts[3],
-                      left: parts[4]};
+          var crop = $(this).data("crop-parts");
           crop.width = crop.right - crop.left;
           crop.height = crop.bottom - crop.top;
           var canvas = document.createElement("canvas");