OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
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. | |
12 * | |
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. | |
24 */ | |
25 | |
26 // pixelzoomer is shared with LayoutTests/fast/harness/results.html | |
27 // Unfortuantely, there's no good way to share code between these two uses. | |
28 | |
29 var pixelzoomer = pixelzoomer || {}; | |
30 | |
31 (function() { | |
32 | |
33 var kZoomFactor = 6; | |
34 var kDelayTimeoutMS = 400; | |
35 | |
36 var kResultWidth = 800; | |
37 var kResultHeight = 600; | |
38 | |
39 var kZoomedResultWidth = kResultWidth * kZoomFactor; | |
40 var kZoomedResultHeight = kResultHeight * kZoomFactor; | |
41 | |
42 function matchesSelector(node, selector) | |
43 { | |
44 if (node.webkitMatchesSelector) | |
45 return node.webkitMatchesSelector(selector); | |
46 | |
47 if (node.mozMatchesSelector) | |
48 return node.mozMatchesSelector(selector); | |
49 } | |
50 | |
51 function parentOfType(node, selector) | |
52 { | |
53 while (node = node.parentNode) { | |
54 if (matchesSelector(node, selector)) | |
55 return node; | |
56 } | |
57 return null; | |
58 } | |
59 | |
60 function zoomImageContainer(url) | |
61 { | |
62 var container = document.createElement('div'); | |
63 container.className = 'zoom-image-container'; | |
64 | |
65 var title = url.match(/\-([^\-]*)\.png/)[1]; | |
66 | |
67 var label = document.createElement('div'); | |
68 label.className = 'label'; | |
69 label.appendChild(document.createTextNode(title)); | |
70 container.appendChild(label); | |
71 | |
72 var imageContainer = document.createElement('div'); | |
73 imageContainer.className = 'scaled-image-container'; | |
74 | |
75 var image = new Image(); | |
76 image.src = url; | |
77 image.style.display = 'none'; | |
78 | |
79 var canvas = document.createElement('canvas'); | |
80 | |
81 imageContainer.appendChild(image); | |
82 imageContainer.appendChild(canvas); | |
83 container.appendChild(imageContainer); | |
84 | |
85 return container; | |
86 } | |
87 | |
88 function createContainer(e) | |
89 { | |
90 var tbody = parentOfType(e.target, 'tbody'); | |
91 var row = tbody.querySelector('tr'); | |
92 var images = row.querySelectorAll('img[src$=".png"]'); | |
93 | |
94 var container = document.createElement('div'); | |
95 container.className = 'pixel-zoom-container'; | |
96 | |
97 for (var i = 0; i < images.length; i++) | |
98 container.appendChild(zoomImageContainer(images[i].src)); | |
99 | |
100 document.body.appendChild(container); | |
101 drawAll(); | |
102 } | |
103 | |
104 function draw(imageContainer) | |
105 { | |
106 var image = imageContainer.querySelector('img'); | |
107 var canvas = imageContainer.querySelector('canvas'); | |
108 | |
109 if (!image.complete) { | |
110 image.onload = function() { | |
111 draw(imageContainer); | |
112 }; | |
113 return; | |
114 } | |
115 | |
116 canvas.width = imageContainer.clientWidth; | |
117 canvas.height = imageContainer.clientHeight; | |
118 | |
119 var ctx = canvas.getContext('2d'); | |
120 ctx.mozImageSmoothingEnabled = false; | |
121 ctx.imageSmoothingEnabled = false; | |
122 ctx.translate(imageContainer.clientWidth / 2, imageContainer.clientHeight /
2); | |
123 ctx.translate(-pixelzoomer._percentX * kZoomedResultWidth, -pixelzoomer._per
centY * kZoomedResultHeight); | |
124 ctx.strokeRect(-1.5, -1.5, kZoomedResultWidth + 2, kZoomedResultHeight + 2); | |
125 ctx.scale(kZoomFactor, kZoomFactor); | |
126 ctx.drawImage(image, 0, 0); | |
127 } | |
128 | |
129 function drawAll() | |
130 { | |
131 Array.prototype.forEach.call(document.querySelectorAll('.pixel-zoom-containe
r .scaled-image-container'), draw); | |
132 } | |
133 | |
134 function handleMouseOut(e) | |
135 { | |
136 if (e.relatedTarget && e.relatedTarget.tagName != 'IFRAME') | |
137 return; | |
138 | |
139 // If e.relatedTarget is null, we've moused out of the document. | |
140 var container = document.querySelector('.pixel-zoom-container'); | |
141 if (container) | |
142 container.remove(); | |
143 } | |
144 | |
145 function handleMouseMove(e) | |
146 { | |
147 if (pixelzoomer._mouseMoveTimeout) | |
148 clearTimeout(pixelzoomer._mouseMoveTimeout); | |
149 | |
150 if (parentOfType(e.target, '.pixel-zoom-container')) | |
151 return; | |
152 | |
153 var container = document.querySelector('.pixel-zoom-container'); | |
154 | |
155 var resultContainer = (e.target.className == 'result-container') ? | |
156 e.target : parentOfType(e.target, '.result-container'); | |
157 if (!resultContainer || !resultContainer.querySelector('img')) { | |
158 if (container) | |
159 container.remove(); | |
160 return; | |
161 } | |
162 | |
163 var targetLocation = e.target.getBoundingClientRect(); | |
164 pixelzoomer._percentX = (e.clientX - targetLocation.left) / targetLocation.w
idth; | |
165 pixelzoomer._percentY = (e.clientY - targetLocation.top) / targetLocation.he
ight; | |
166 | |
167 if (!container) { | |
168 if (pixelzoomer.showOnDelay) { | |
169 pixelzoomer._mouseMoveTimeout = setTimeout(function() { | |
170 createContainer(e); | |
171 }, kDelayTimeoutMS); | |
172 return; | |
173 } | |
174 | |
175 createContainer(e); | |
176 return; | |
177 } | |
178 | |
179 drawAll(); | |
180 } | |
181 | |
182 pixelzoomer.showOnDelay = true; | |
183 | |
184 pixelzoomer.installEventListeners = function() | |
185 { | |
186 document.addEventListener('mousemove', handleMouseMove, false); | |
187 document.addEventListener('mouseout', handleMouseOut, false); | |
188 }; | |
189 | |
190 })(); | |
OLD | NEW |