Debian Codegraph @

test/common/pixeldiff.html

raw
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Cockpit Integration Tests - Pixel diffs</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6 <style>
7 /*
8 SPDX-License-Identifier: MIT
9
10 Copyright (c) 2018 Diamant Haxhimusa
11 */
12 .pixelcompare-wrapper {
13 background: red;
14 }
15 .pixelcompare-wrapper.pixelcompare-horizontal {
16 height: 100% !important;
17 }
18 .pixelcompare-horizontal .pixelcompare-handle:before, .pixelcompare-horizontal .pixelcompare-handle:after, .pixelcompare-vertical .pixelcompare-handle:before, .pixelcompare-vertical .pixelcompare-handle:after {
19 content: "";
20 display: block;
21 background: #fff;
22 position: absolute;
23 z-index: 30;
24 }
25 img.pixelcompare-before, img.pixelcompare-after {
26 object-fit: cover;
27 -webkit-user-select: none;
28 -khtml-user-select: none;
29 -moz-user-select: none;
30 -o-user-select: none;
31 user-select: none;
32 }
33
34 .pixelcompare-before-label, .pixelcompare-after-label, .pixelcompare-overlay {
35 position: absolute;
36 top: 0;
37 width: 100%;
38 height: 100%;
39 }
40
41 .pixelcompare-before-label, .pixelcompare-after-label, .pixelcompare-overlay {
42 transition-duration: 0.5s;
43 }
44
45 .pixelcompare-before-label, .pixelcompare-after-label {
46 transition-property: opacity;
47 }
48
49 .pixelcompare-horizontal .pixelcompare-before-label:before, .pixelcompare-horizontal .pixelcompare-after-label:before {
50 top: 50%;
51 margin-top: -19px;
52 }
53
54 .pixelcompare-vertical .pixelcompare-before-label:before, .pixelcompare-vertical .pixelcompare-after-label:before {
55 left: 50%;
56 margin-left: -45px;
57 text-align: center;
58 width: 90px;
59 }
60
61 .pixelcompare-left-arrow, .pixelcompare-right-arrow, .pixelcompare-up-arrow, .pixelcompare-down-arrow {
62 width: 0;
63 height: 0;
64 border: 6px inset transparent;
65 position: absolute;
66 }
67
68 .pixelcompare-left-arrow, .pixelcompare-right-arrow {
69 top: 50%;
70 margin-top: -6px;
71 }
72
73 .pixelcompare-up-arrow, .pixelcompare-down-arrow {
74 left: 50%;
75 margin-left: -6px;
76 }
77
78 .pixelcompare-container {
79 box-sizing: content-box;
80 z-index: 0;
81 height: 100%;
82 overflow: hidden;
83 position: relative;
84 -webkit-user-select: none;
85 -khtml-user-select: none;
86 -moz-user-select: none;
87 -o-user-select: none;
88 user-select: none;
89 }
90 .pixelcompare-container img {
91 max-width: 100%;
92 position: absolute;
93 top: 0;
94 display: block;
95 }
96 /* .pixelcompare-container.active .pixelcompare-overlay,
97 .pixelcompare-container.active :hover.pixelcompare-overlay {
98 background: transparent;
99 } */
100 .pixelcompare-container.active .pixelcompare-overlay .pixelcompare-before-label,
101 .pixelcompare-container.active .pixelcompare-overlay .pixelcompare-after-label,
102 .pixelcompare-container.active :hover.pixelcompare-overlay .pixelcompare-before-label,
103 .pixelcompare-container.active :hover.pixelcompare-overlay .pixelcompare-after-label {
104 opacity: 0;
105 }
106 .pixelcompare-container * {
107 box-sizing: content-box;
108 }
109
110 .pixelcompare-before-label {
111 opacity: 0;
112 }
113 .pixelcompare-before-label:before {
114 content: attr(data-content);
115 }
116
117 .pixelcompare-after-label {
118 opacity: 0;
119 }
120 .pixelcompare-after-label:before {
121 content: attr(data-content);
122 }
123
124 .pixelcompare-horizontal .pixelcompare-before-label:before {
125 left: 10px;
126 }
127
128 .pixelcompare-horizontal .pixelcompare-after-label:before {
129 right: 10px;
130 }
131
132 .pixelcompare-vertical .pixelcompare-before-label:before {
133 top: 10px;
134 }
135
136 .pixelcompare-vertical .pixelcompare-after-label:before {
137 bottom: 10px;
138 }
139
140 .pixelcompare-overlay {
141 transition-property: background;
142 background: transparent;
143 z-index: 25;
144 }
145 .pixelcompare-overlay:hover,
146 .pixelcompare-container.active .pixelcompare-overlay,
147 .pixelcompare-handle:hover + .pixelcompare-overlay {
148 background: rgba(0, 0, 0, 0.5);
149 }
150 .pixelcompare-overlay:hover .pixelcompare-after-label {
151 opacity: 1;
152 }
153 .pixelcompare-overlay:hover .pixelcompare-before-label {
154 opacity: 1;
155 }
156
157 .pixelcompare-before {
158 z-index: 20;
159 }
160
161 .pixelcompare-after {
162 z-index: 10;
163 }
164
165 .pixelcompare-vertical .pixelcompare-handle {
166 background-color: rgba(247, 237, 237, 0.71);
167 height: 20px;
168 width: 65px;
169 position: absolute;
170 left: 50%;
171 top: 50%;
172 margin-left: -19px;
173 margin-top: -10px;
174 border-radius: 1000px;
175 box-shadow: 0px 0px 12px rgba(51, 51, 51, 0.5);
176 z-index: 40;
177 cursor: pointer;
178 }
179
180 .pixelcompare-horizontal .pixelcompare-handle {
181 background-color: rgba(247, 237, 237, 0.71);
182 height: 65px;
183 width: 20px;
184 position: absolute;
185 left: 50%;
186 top: 50%;
187 margin-top: -19px;
188 margin-left: -10px;
189 border-radius: 1000px;
190 box-shadow: 0px 0px 12px rgba(51, 51, 51, 0.5);
191 z-index: 40;
192 cursor: pointer;
193 }
194
195 .pixelcompare-horizontal .pixelcompare-handle:before {
196 bottom: 50%;
197 height: 799px;
198 margin-bottom: 33px;
199 margin-left: 10px;
200 background: none;
201 position: absolute;
202 z-index: 999;
203 border-right: 1px dashed rgba(0, 0, 0, 0.5);
204 }
205
206 .pixelcompare-horizontal .pixelcompare-handle:after {
207 top: 50%;
208 height: 257px;
209 margin-top: 33px;
210 margin-left: 10px;
211 background: none;
212 position: absolute;
213 z-index: 999;
214 border-right: 1px dashed rgba(0, 0, 0, 0.5);
215 }
216
217
218 .pixelcompare-vertical .pixelcompare-handle:before {
219 margin-top: 10px;
220 margin-left: 11px;
221 width: 100vw;
222 background: none;
223 position: absolute;
224 z-index: 999;
225 border-top: 1px dashed rgba(0, 0, 0, 0.5);
226 right: 50%;
227 margin-right: 33px;
228 }
229
230 .pixelcompare-vertical .pixelcompare-handle:after {
231 margin-top: 10px;
232 width: 100vw;
233 background: none;
234 position: absolute;
235 z-index: 999;
236 border-top: 1px dashed rgba(0, 0, 0, 0.5);
237 left: 50%;
238 margin-left: 33px;
239 }
240
241 .pixelcompare-left-arrow {
242 border-right: 6px solid rgba(247, 237, 237, 0.71);
243 left: 50%;
244 margin-left: -13px;
245 }
246
247 .pixelcompare-right-arrow {
248 border-left: 6px solid rgba(247, 237, 237, 0.71);
249 right: 50%;
250 margin-right: -13px;
251 }
252
253 .pixelcompare-up-arrow {
254 border-bottom: 6px solid rgba(247, 237, 237, 0.71);
255 top: 50%;
256 margin-top: -13px;
257 }
258
259 .pixelcompare-down-arrow {
260 border-top: 6px solid rgba(247, 237, 237, 0.71);
261 bottom: 50%;
262 margin-bottom: -13px;
263 }
264 </style>
265 <script>
266 "use strict";
267 /**
268 * PIXELCOMPARE
269 * Javascript image comparison
270 * @author [email protected]
271
272 SPDX-License-Identifier: MIT
273
274 Copyright (c) 2018 Diamant Haxhimusa
275 */
276 window.setup_pixelcompare = function() {
277 var _extend = function(defaults, options) {
278 var extended = {};
279 var prop;
280 for (prop in defaults) {
281 if(Object.prototype.hasOwnProperty.call(defaults, prop)) {
282 extended[prop] = defaults[prop];
283 }
284 }
285 for (prop in options) {
286 if(Object.prototype.hasOwnProperty.call(options, prop)) {
287 extended[prop] = options[prop];
288 }
289 }
290 return extended;
291 };
292
293 var _addClass = function(element, classname) {
294 var arr;
295 arr = element.className.split(" ");
296 if(arr.indexOf(classname) == -1) {
297 element.className += " " + classname;
298 }
299 };
300
301 var _hasClass = function(element, className) {
302 return new RegExp("(\\s|^)" + className + "(\\s|$)").test(
303 element.className
304 );
305 };
306
307 var _removeClass = function(element, className) {
308 element.classList.remove(className);
309 };
310
311 var _wrap = function(element, tag, sliderOrientation) {
312 var div = document.createElement(tag);
313 _addClass(div, "pixelcompare-wrapper pixelcompare-" + sliderOrientation);
314 element.parentElement.insertBefore(div, element);
315 div.appendChild(element);
316 return div;
317 };
318
319 var _createSlider = function(beforeDirection, afterDirection) {
320 var pxcHandleNode = document.createElement("div");
321 _addClass(pxcHandleNode, "pixelcompare-handle");
322
323 var sliderChildNodeBeforeDirection = document.createElement("span");
324 _addClass(
325 sliderChildNodeBeforeDirection,
326 "pixelcompare-" + beforeDirection + "-arrow"
327 );
328 pxcHandleNode.appendChild(sliderChildNodeBeforeDirection);
329 var sliderChildNodeAfterDirection = document.createElement("span");
330 _addClass(
331 sliderChildNodeAfterDirection,
332 "pixelcompare-" + afterDirection + "-arrow"
333 );
334 pxcHandleNode.appendChild(sliderChildNodeAfterDirection);
335
336 pxcHandleNode.addEventListener("touchmove", function(e) {
337 e.preventDefault();
338 });
339 return pxcHandleNode;
340 };
341
342 var options = {
343 default_offset_pct: 0.5,
344 orientation: "horizontal",
345 overlay: false,
346 hover: false,
347 move_with_handle_only: true,
348 click_to_move: false,
349 showSlider: true,
350 };
351
352 var pxcContainers = document.querySelectorAll("[data-pixelcompare]");
353
354 pxcContainers.forEach(function(pcContainer) {
355 var sliderPct = options.default_offset_pct;
356 var imageContainer = pcContainer;
357 options.hover = pcContainer.hasAttribute("data-hover");
358 options.showSlider = options.hover ? pcContainer.hasAttribute("data-show-slider") : options.showSlider;
359 options.orientation = pcContainer.hasAttribute("data-vertical")
360 ? "vertical"
361 : "horizontal";
362 var orientations = ["vertical", "horizontal", "sides"];
363
364 var datasetOrientation = imageContainer.dataset.pixelcompareOrientation;
365 var sliderOrientation = orientations.includes(datasetOrientation)
366 ? datasetOrientation
367 : options.orientation;
368 var beforeDirection = sliderOrientation === "vertical" ? "down" : "left";
369 var afterDirection = sliderOrientation === "vertical" ? "up" : "right";
370
371 var container = _wrap(pcContainer, "div", sliderOrientation);
372
373 var beforeImg = container.querySelectorAll("img")[0];
374 beforeImg.draggable = false;
375 var afterImg = container.querySelectorAll("img")[1];
376 afterImg.draggable = false;
377
378 _addClass(container, "pixelcompare-container");
379 _addClass(beforeImg, "pixelcompare-before");
380 _addClass(afterImg, "pixelcompare-after");
381
382 var slider = null;
383 if(options.showSlider) {
384 slider = _createSlider(beforeDirection, afterDirection);
385 container.appendChild(slider);
386 }
387 if(options.overlay) {
388 var overlayNode = document.createElement("div");
389 _addClass(overlayNode, "pixelcompare-overlay");
390 container.appendChild(overlayNode);
391 }
392
393 var calcOffset = function(dimensionPct) {
394 var w = beforeImg.getBoundingClientRect().width;
395 var h = beforeImg.getBoundingClientRect().height;
396 return {
397 w: w + "px",
398 h: h + "px",
399 wp: dimensionPct * 100,
400 cw: dimensionPct * w + "px",
401 ch: dimensionPct * h + "px",
402 };
403 };
404
405 var adjustContainer = function(offset) {
406 if(sliderOrientation === "vertical") {
407 beforeImg.style.clip =
408 "rect(0, " + offset.w + ", " + offset.ch + ", 0)";
409 afterImg.style.clip =
410 "rect(" + offset.ch + ", " + offset.w + ", " + offset.h + ", 0)";
411 } else if(sliderOrientation === "sides") {
412 beforeImg.style.clipPath = `polygon(0% ${2 * (50 - offset.wp)}%, ${
413 2 * offset.wp
414 }% 100%, 0% 100%)`;
415 afterImg.style.clipPath = `polygon(100% ${2 * (100 - offset.wp)}%, ${
416 -2 * (50 - offset.wp)
417 }% 0%, 100% 0%)`;
418 } else {
419 beforeImg.style.clip =
420 "rect(0, " + offset.cw + ", " + offset.h + ", 0)";
421 afterImg.style.clip =
422 "rect(0, " + offset.w + "," + offset.h + "," + offset.cw + ")";
423 }
424 container.style.height = offset.h;
425 };
426
427 var adjustSlider = function(pct) {
428 var offset = calcOffset(pct);
429 if(slider) {
430 if(sliderOrientation === "vertical") {
431 slider.style.top = offset.ch;
432 } else {
433 slider.style.left = offset.cw;
434 }
435 }
436 adjustContainer(offset);
437 };
438
439 // Return the number specified or the min/max number if it outside the range given.
440 var minMaxNumber = function(num, min, max) {
441 return Math.max(min, Math.min(max, num));
442 };
443
444 // Calculate the slider percentage based on the position.
445 var getSliderPercentage = function(positionX, positionY) {
446 var sliderPercentage =
447 sliderOrientation === "vertical"
448 ? (positionY - offsetY) / imgHeight
449 : (positionX - offsetX) / imgWidth;
450 return minMaxNumber(sliderPercentage, 0, 1);
451 };
452
453 window.addEventListener("resize.pixelcompare", function(e) {
454 adjustSlider(sliderPct);
455 });
456
457 var offsetX = 0;
458 var offsetY = 0;
459 var imgWidth = 0;
460 var imgHeight = 0;
461 var onMoveStart = function(e) {
462 if(
463 ((e.distX > e.distY && e.distX < -e.distY) ||
464 (e.distX < e.distY && e.distX > -e.distY)) &&
465 sliderOrientation !== "vertical"
466 ) {
467 e.preventDefault();
468 } else if(
469 ((e.distX < e.distY && e.distX < -e.distY) ||
470 (e.distX > e.distY && e.distX > -e.distY)) &&
471 sliderOrientation === "vertical"
472 ) {
473 e.preventDefault();
474 }
475 _addClass(container, "active");
476 offsetX = container.offsetLeft;
477 offsetY = container.offsetTop;
478 imgWidth = beforeImg.getBoundingClientRect().width;
479 imgHeight = beforeImg.getBoundingClientRect().height;
480 };
481 var onMove = function(e) {
482 if(_hasClass(container, "active")) {
483 sliderPct = getSliderPercentage(
484 e.pageX || e.changedTouches[0].pageX,
485 e.pageY || e.changedTouches[0].pageY
486 );
487 adjustSlider(sliderPct);
488 }
489 };
490 var onMoveEnd = function() {
491 _removeClass(container, "active");
492 };
493
494 if(options.hover) {
495 container.addEventListener("mouseenter", onMoveStart);
496 container.addEventListener("mouseleave", onMoveEnd);
497 container.addEventListener("mousemove", onMove);
498 } else {
499 var moveTarget = options.move_with_handle_only ? slider : container;
500 window.addEventListener("mouseup", onMoveEnd);
501 container.addEventListener("mousemove", onMove);
502 moveTarget.addEventListener("mousedown", onMoveStart);
503 moveTarget.addEventListener("touchstart", onMoveStart);
504 container.addEventListener("touchmove", onMove);
505 window.addEventListener("touchend", onMoveEnd);
506 }
507
508 container
509 .querySelector("img")
510 .addEventListener("mousedown", function(event) {
511 event.preventDefault();
512 });
513
514 if(options.click_to_move) {
515 container.on("click", function(e) {
516 offsetX = container.offset().left;
517 offsetY = container.offset().top;
518 imgWidth = beforeImg.width();
519 imgHeight = beforeImg.height();
520 sliderPct = getSliderPercentage(e.pageX, e.pageY);
521 adjustSlider(sliderPct);
522 });
523 }
524 window.dispatchEvent(new Event("resize.pixelcompare"));
525 });
526 };
527 </script>
528 <script>
529 document.addEventListener("DOMContentLoaded", () => {
530 const base = window.location.hash.replace(/[^\w-]/g, "");
531 document.getElementById("key").textContent = base;
532
533 const e_new = document.getElementById("new");
534 e_new.textContent = base + "-pixels.png";
535 e_new.setAttribute("href", base + "-pixels.png");
536
537 const img_now = document.createElement("img");
538 img_now.setAttribute("src", base + "-pixels.png");
539
540 const img_ref = document.createElement("img");
541 img_ref.setAttribute("src", base + "-reference.png");
542
543 const img_delta = document.createElement("img");
544 img_delta.setAttribute("src", base + "-delta.png");
545
546 let loaded = { };
547
548 function load_event(tag) {
549 loaded[tag] = true;
550 if (loaded.now && loaded.ref && loaded.delta) {
551 const diff = document.getElementById("diff");
552 const w = Math.max(img_now.naturalWidth, img_ref.naturalWidth);
553 const h = Math.max(img_now.naturalHeight, img_ref.naturalHeight);
554 diff.style.width = w + "px";
555 diff.style.height = h + "px";
556 const ecompare = document.createElement("div");
557 ecompare.classList.add("pixelcompare");
558 ecompare.setAttribute("data-pixelcompare", "");
559 ecompare.appendChild(img_now);
560 ecompare.appendChild(img_ref);
561 diff.appendChild(ecompare);
562 window.setup_pixelcompare();
563
564 const delta = document.getElementById("delta");
565 delta.style.width = img_delta.naturalWidth + "px";
566 delta.style.height = img_delta.naturalHeight + "px";
567 delta.appendChild(img_delta);
568 }
569 }
570
571 img_now.addEventListener("load", () => load_event("now"));
572 img_ref.addEventListener("load", () => load_event("ref"));
573 img_delta.addEventListener("load", () => load_event("delta"));
574 });
575 </script>
576 </head
577 <body>
578 <h2>Pixel comparison for <span id="key"></span></h2>
579 <p>New <a id="new"></a> on the left, reference on the right.</p>
580 <div id="diff">
581 </div>
582 <h2>Changed pixels in red, ignored changes in green, ignored anti-aliasing changes is in yellow/orange</h2>
583 <div id="delta">
584 </div>
585 </body>
586 </html>