2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 // pixelzoomer is shared with LayoutTests/fast/harness/results.html
27 // Unfortuantely, there's no good way to share code between these two uses.
29 var pixelzoomer = pixelzoomer || {};
34 var kDelayTimeoutMS = 400;
36 var kResultWidth = 800;
37 var kResultHeight = 600;
39 var kZoomedResultWidth = kResultWidth * kZoomFactor;
40 var kZoomedResultHeight = kResultHeight * kZoomFactor;
42 function matchesSelector(node, selector)
44 if (node.webkitMatchesSelector)
45 return node.webkitMatchesSelector(selector);
47 if (node.mozMatchesSelector)
48 return node.mozMatchesSelector(selector);
51 function parentOfType(node, selector)
53 while (node = node.parentNode) {
54 if (matchesSelector(node, selector))
60 function zoomImageContainer(url)
62 var container = document.createElement('div');
63 container.className = 'zoom-image-container';
65 var title = url.match(/\-([^\-]*)\.png/)[1];
67 var label = document.createElement('div');
68 label.className = 'label';
69 label.appendChild(document.createTextNode(title));
70 container.appendChild(label);
72 var imageContainer = document.createElement('div');
73 imageContainer.className = 'scaled-image-container';
75 var image = new Image();
77 image.style.display = 'none';
79 var canvas = document.createElement('canvas');
81 imageContainer.appendChild(image);
82 imageContainer.appendChild(canvas);
83 container.appendChild(imageContainer);
88 function createContainer(e)
90 var tbody = parentOfType(e.target, 'tbody');
91 var row = tbody.querySelector('tr');
92 var images = row.querySelectorAll('img[src$=".png"]');
94 var container = document.createElement('div');
95 container.className = 'pixel-zoom-container';
97 for (var i = 0; i < images.length; i++)
98 container.appendChild(zoomImageContainer(images[i].src));
100 document.body.appendChild(container);
104 function draw(imageContainer)
106 var image = imageContainer.querySelector('img');
107 var canvas = imageContainer.querySelector('canvas');
109 if (!image.complete) {
110 image.onload = function() {
111 draw(imageContainer);
116 canvas.width = imageContainer.clientWidth;
117 canvas.height = imageContainer.clientHeight;
119 var ctx = canvas.getContext('2d');
120 ctx.webkitImageSmoothingEnabled = false;
121 ctx.mozImageSmoothingEnabled = false;
122 ctx.imageSmoothingEnabled = false;
123 ctx.translate(imageContainer.clientWidth / 2, imageContainer.clientHeight / 2);
124 ctx.translate(-pixelzoomer._percentX * kZoomedResultWidth, -pixelzoomer._percentY * kZoomedResultHeight);
125 ctx.strokeRect(-1.5, -1.5, kZoomedResultWidth + 2, kZoomedResultHeight + 2);
126 ctx.scale(kZoomFactor, kZoomFactor);
127 ctx.drawImage(image, 0, 0);
132 Array.prototype.forEach.call(document.querySelectorAll('.pixel-zoom-container .scaled-image-container'), draw);
135 function handleMouseOut(e)
137 if (e.relatedTarget && e.relatedTarget.tagName != 'IFRAME')
140 // If e.relatedTarget is null, we've moused out of the document.
141 $('pixel-zoom-container').detach();
144 function handleMouseMove(e)
146 if (pixelzoomer._mouseMoveTimeout)
147 clearTimeout(pixelzoomer._mouseMoveTimeout);
149 if (parentOfType(e.target, '.pixel-zoom-container'))
152 var container = document.querySelector('.pixel-zoom-container');
154 var resultContainer = (e.target.className == 'result-container') ?
155 e.target : parentOfType(e.target, '.result-container');
156 if (!resultContainer || !resultContainer.querySelector('img')) {
157 $(container).detach();
161 var targetLocation = e.target.getBoundingClientRect();
162 pixelzoomer._percentX = (e.clientX - targetLocation.left) / targetLocation.width;
163 pixelzoomer._percentY = (e.clientY - targetLocation.top) / targetLocation.height;
166 if (pixelzoomer.showOnDelay) {
167 pixelzoomer._mouseMoveTimeout = setTimeout(function() {
180 pixelzoomer.showOnDelay = true;
182 pixelzoomer.installEventListeners = function()
184 document.addEventListener('mousemove', handleMouseMove, false);
185 document.addEventListener('mouseout', handleMouseOut, false);