') .css({ 'left': left + '%', 'top': top + '%', 'width': width + '%', 'height': height + '%', 'position': 'absolute', 'z-index': 10 }); } function position_overlays(page_id, page_width, page_height, coords_list, $image, $container, fudge) { if (!($container.length)) { // no actual search result for this page -- exit this function return true; } $container.find('div.image-highlight').remove(); if (!coords_list) { // coords data doesn't exist -- exit this function return true; } var container_width = $container.width(); var container_height = $container.height(); var image_marginLeft = $image[0].offsetLeft; var image_marginTop = $image[0].offsetTop; var image_width = $image.width(); var image_height = $image.height(); // The overlays are positioned relative to their parent, the // $container, but they need to show up in the right place relative // to the image. // The $container can be larger than the image, // so we take the ratio of the sizes and use it to scale the // positions, widths, and heights of the highlight overlays. // We also make the overlays a little larger so they're more visible // and we shift them a little using the "fudge" numbers. var marginLeft = image_marginLeft / container_width; var marginTop = image_marginTop / container_height; var scaleWidth = image_width / container_width; var scaleHeight = image_height / container_height; $.each(coords_list, function (index, coords) { // word coordinates: [left, top, width, height] var highlight_id = "highlight-" + page_id + '-' + index; var overlay_left = 100 * (marginLeft + scaleWidth * coords[0] / page_width) + fudge.left; var overlay_top = 100 * (marginTop + scaleHeight * coords[1] / page_height) + fudge.top; var overlay_width = 100 * coords[2] / page_width * scaleWidth + fudge.width; var overlay_height = 100 * coords[3] / page_height * scaleHeight + fudge.height; var $highlight_overlay = create_overlay(highlight_id, overlay_left, overlay_top, overlay_width, overlay_height); $container.append($highlight_overlay); }); } var word_coordinates_pages; word_coordinates_pages = {}; var get_$container; var get_$image; var fudge_numbers; get_$container = function(page_id) { return $("#highlights-container-" + page_id); }; get_$image = function(page_id) { return $("#iconic-segment-image-" + page_id); }; fudge_numbers = { left: -1.3, top: -0.5, width: 2.5, height: 1.3 }; if (!$.isEmptyObject(word_coordinates_pages)) { var $iconic = $(".iconic"); var loaded = 0; var loaded_total = $iconic.length; // make sure we wait til all the images are loaded before we try to position the highlights on them: $iconic.one("load", function() { loaded++; if (loaded === loaded_total) { $.each(word_coordinates_pages, function(page_id, page) { var $image = get_$image(page_id); var $container = get_$container(page_id); position_overlays(page_id, page.width, page.height, page.coords_list, $image, $container, fudge_numbers); }); } }).each(function() { // to make sure the "on load" event occurs even if the // images are already cached: if (this.complete) { $(this).load(); } }); } $(window).resize(function() { setTimeout(function() { $.each(word_coordinates_pages, function(page_id, page) { var $container = get_$container(page_id); var $image = get_$image(page_id, $container); position_overlays(page_id, page.width, page.height, page.coords_list, $image, $container, fudge_numbers); }); }, 300); });});