Mercurial > mozilla-the-big-picture
comparison mbp.js @ 44:9c7c3cb5f776
simplified cropping logic
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 14 Dec 2009 11:33:35 -0800 |
parents | 1909896414d8 |
children |
comparison
equal
deleted
inserted
replaced
43:1909896414d8 | 44:9c7c3cb5f776 |
---|---|
32 } | 32 } |
33 } else | 33 } else |
34 best = this; | 34 best = this; |
35 }); | 35 }); |
36 | 36 |
37 var cropAttr = $(best).attr("data-crop"); | |
38 var scaling = 1; | |
39 | |
37 // If our best picture doesn't have a cropping rect associated | 40 // If our best picture doesn't have a cropping rect associated |
38 // with it, we'll use the scaled cropping rect of another | 41 // with it, we'll use the scaled cropping rect of another |
39 // resolution. | 42 // resolution. |
40 if (!$(best).attr("data-crop") && lastCrop) { | 43 if (!cropAttr && lastCrop) { |
41 var scaling = dWidth(best) / dWidth(lastCrop); | 44 cropAttr = $(lastCrop).attr("data-crop"); |
42 var parts = $(lastCrop).attr("data-crop").match(rectRegexp); | 45 scaling = dWidth(best) / dWidth(lastCrop); |
43 if (!parts) | 46 } |
44 throw new Error("Invalid 'data-crop' for " + lastCrop.href); | |
45 | 47 |
46 var newParts = []; | 48 if (!cropAttr) |
47 for (var i = 1; i <= 4; i++) | 49 throw new Error("No 'data-crop' for " + best.href); |
48 newParts.push(Math.floor(parts[i] * scaling)); | 50 |
49 $(best).attr("data-crop", "rect(" + newParts.join(", ") + ")"); | 51 var parts = cropAttr.match(rectRegexp); |
50 } | 52 if (!parts) |
53 throw new Error("Invalid 'data-crop' for " + best.href); | |
54 | |
55 var crop = {top: Math.floor(parts[1] * scaling), | |
56 right: Math.floor(parts[2] * scaling), | |
57 bottom: Math.floor(parts[3] * scaling), | |
58 left: Math.floor(parts[4] * scaling)}; | |
51 | 59 |
52 var image = $("<img/>"); | 60 var image = $("<img/>"); |
53 image.attr("src", best.href); | 61 image.attr("src", best.href); |
54 image.attr("width", $(best).attr("data-width")); | 62 image.attr("width", $(best).attr("data-width")); |
55 image.attr("height", $(best).attr("data-height")); | 63 image.attr("height", $(best).attr("data-height")); |
56 image.attr("data-crop", $(best).attr("data-crop")); | 64 image.data("crop-parts", crop); |
57 $(this).find(".link").empty().append(image); | 65 $(this).find(".link").empty().append(image); |
58 pictures.remove(); | 66 pictures.remove(); |
59 }); | 67 }); |
60 | 68 |
61 var images = $(".mbp-entry img"); | 69 var images = $(".mbp-entry img"); |
65 images.each( | 73 images.each( |
66 function() { | 74 function() { |
67 function onLoaded() { | 75 function onLoaded() { |
68 $("#countdown").text(--loadsLeft); | 76 $("#countdown").text(--loadsLeft); |
69 | 77 |
70 var crop = $(this).attr("data-crop"); | 78 var crop = $(this).data("crop-parts"); |
71 var parts = crop.match(rectRegexp); | |
72 if (!parts) | |
73 throw new Error("Invalid 'data-crop' for " + this.src); | |
74 | |
75 var crop = {top: parts[1], | |
76 right: parts[2], | |
77 bottom: parts[3], | |
78 left: parts[4]}; | |
79 crop.width = crop.right - crop.left; | 79 crop.width = crop.right - crop.left; |
80 crop.height = crop.bottom - crop.top; | 80 crop.height = crop.bottom - crop.top; |
81 var canvas = document.createElement("canvas"); | 81 var canvas = document.createElement("canvas"); |
82 var self = $(this.parentNode).clone(); | 82 var self = $(this.parentNode).clone(); |
83 var maxWidth = Math.max($(document.body).width(), containerWidth); | 83 var maxWidth = Math.max($(document.body).width(), containerWidth); |