GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — HumiditySensor/HumiditySensor/module.html at main · Wilkware/HumiditySensor Expand file tree/ Copy pathmodule.htmlMore file actions 1026 lines (936 loc) · 39.6 KB / Copy pathmodule.htmlFile metadata and controls1026 lines (936 loc) · 39.6 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 <!doctype html> <html lang="en"> <head> <title>HumiditySensor</title> <meta charset="utf-8" /> <style> :root { --chart-color: var(--accent-color, #11A0F3); --chart-temp-color: color-mix(in srgb, var(--content-color, #FFFFFF) 55%, transparent); --band-color: #58A906; --hint-color: #58A906; } *, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; } html, body { margin: 0px; height: 100%; width: 100%; overflow: hidden; } #card { position: relative; width: 100%; height: 100%; padding: var(--margin-top) var(--margin-side) calc(var(--margin-bottom, 0px) * 0.5); container: card / size; touch-action: pan-y; } /* ── Chart: humidity history in the background, starts below the indoor values */ .hs-chart-layer { position: absolute; left: 0; right: 0; top: var(--chart-top, 30%); bottom: 0; pointer-events: none; } #chart { display: block; width: 100%; height: 100%; } /* Value at the pointer position (mouse hover, touch) */ #chart-tip { z-index: 2; } .hs-cursor { position: absolute; top: 0; bottom: 0; width: 1px; margin-left: -0.5px; background: color-mix(in srgb, var(--content-color) 35%, transparent); } .hs-cursor-dot { position: absolute; width: 8px; height: 8px; margin: -4px 0 0 -4px; border-radius: 50%; background: var(--chart-color); box-shadow: 0 0 0 3px var(--card-color, #2B2C30); } .hs-tip { position: absolute; font-size: clamp(9px, 4.6cqh, 12px); line-height: 1; white-space: nowrap; color: var(--content-color); background: color-mix(in srgb, var(--content-color) 12%, var(--card-color, #2B2C30)); padding: 3px 7px; border-radius: 999px; } .hs-band { fill: color-mix(in srgb, var(--band-color) 10%, transparent); stroke: none; } .hs-line { fill: none; stroke: var(--chart-color); stroke-width: 1.5; stroke-opacity: 0.55; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; } .hs-line-temp { fill: none; stroke: var(--chart-temp-color); stroke-width: 1.5; stroke-opacity: 0.55; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; } .hs-area { fill: url(#hs-gradient); stroke: none; } .hs-area-temp { fill: url(#hs-gradient-temp); stroke: none; } /* ── Content ─────────────────────────────────────────────────────── */ #content { position: relative; z-index: 1; display: grid; grid-template-columns: minmax(0, 1fr) auto; grid-template-rows: auto auto auto minmax(0, 1fr); grid-template-areas: "room hint" "main main" "sub sub" "extra extra"; column-gap: 8px; row-gap: clamp(4px, 3cqh, 10px); width: 100%; height: 100%; container-type: size; } /* ── Wide enough: without room name the recommendation moves up ── */ @container card (min-width: 260px) { #content.no-room { grid-template-rows: auto auto minmax(0, 1fr); grid-template-areas: "main hint" "sub sub" "extra extra"; } #content.no-room .hs-hint { align-self: start; } } /* ── Wide tiles: secondary values next to the indoor values ─────── */ @container card (min-aspect-ratio: 1.8) and (min-width: 360px) { #content { grid-template-columns: auto minmax(0, 1fr) auto; grid-template-rows: auto auto minmax(0, 1fr); grid-template-areas: "room room hint" "main sub sub" "extra extra extra"; column-gap: clamp(12px, 4cqw, 24px); } #content.no-room { grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "main sub hint" "extra extra extra"; } .hs-sub { align-self: end; justify-content: flex-end; padding-bottom: 0.2em; } } /* ── Head: room & recommendation ─────────────────────────────────── */ .hs-head { display: contents; } .hs-room { grid-area: room; align-self: center; font-size: clamp(8px, 4.5cqh, 11px); letter-spacing: 0.09em; text-transform: uppercase; opacity: 0.5; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; } .hs-hint { display: flex; align-items: center; gap: clamp(3px, 1.5cqw, 6px); grid-area: hint; justify-self: end; align-self: center; font-size: clamp(10px, 5.5cqh, 14px); font-weight: 600; line-height: 1; color: #FFFFFF; background: var(--hint-color); padding: clamp(5px, 2.6cqh, 8px) clamp(10px, 3.5cqw, 14px); border-radius: 999px; white-space: nowrap; transition: background 0.18s; } /* Optical centering: with line-height 1 the capitals sit above the middle */ #hint-text { display: block; line-height: 1; position: relative; top: 0.07em; } @supports (text-box: trim-both cap alphabetic) { #hint-text { top: 0; text-box: trim-both cap alphabetic; padding-block: 0.15em; } } /* Pulsing dot: the notification conditions are met */ .hs-dot { display: none; flex-shrink: 0; width: 0.5em; height: 0.5em; border-radius: 50%; background: #FFFFFF; animation: hs-pulse 1.6s ease-out infinite; } .hs-hint.hs-alert .hs-dot { display: block; } @keyframes hs-pulse { 0% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.7); } 70% { box-shadow: 0 0 0 0.45em rgba(255, 255, 255, 0); } 100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0); } } @media (prefers-reduced-motion: reduce) { .hs-dot { animation: none; } } /* ── Main: indoor values ─────────────────────────────────────────── */ .hs-main { grid-area: main; display: flex; align-items: baseline; flex-wrap: wrap; column-gap: clamp(8px, 4cqw, 16px); row-gap: 2px; } .hs-humidity { font-size: clamp(22px, 20cqh, 52px); line-height: 1; white-space: nowrap; } .hs-unit { font-size: 0.45em; opacity: 0.5; vertical-align: super; margin-left: 2px; } .hs-temperature { font-size: clamp(12px, 8cqh, 20px); line-height: 1; opacity: 0.75; white-space: nowrap; } .hs-temperature svg, .hs-temperature i { margin-right: 0.2em; vertical-align: -0.1em; } .hs-sub { grid-area: sub; font-size: clamp(9px, 5cqh, 12px); color: color-mix(in srgb, var(--content-color) 60%, transparent); white-space: nowrap; overflow: hidden; display: flex; align-items: center; justify-content: space-between; gap: clamp(8px, 4cqw, 16px); } .hs-sub-item { display: flex; align-items: center; gap: clamp(2px, 1cqw, 5px); min-width: 0; } /* Difference always on the right, also without outdoor values */ #difference-item { margin-left: auto; } /* ── Extra: configurable elements ────────────────────────────────── */ .hs-extra { grid-area: extra; min-height: 0; display: flex; flex-direction: column; justify-content: flex-end; gap: clamp(4px, 3cqh, 9px); overflow: hidden; } .hs-text { font-size: clamp(9px, 5cqh, 12px); color: color-mix(in srgb, var(--content-color) 70%, transparent); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; } .hs-scale { display: flex; flex-direction: column; gap: 3px; } .hs-scale-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; min-width: 0; } .hs-scale-label { font-size: clamp(7px, 3.8cqh, 9px); letter-spacing: 0.09em; text-transform: uppercase; opacity: 0.5; white-space: nowrap; } .hs-scale-value { font-size: clamp(9px, 4.6cqh, 12px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; } .hs-bar { position: relative; height: 4px; border-radius: 2px; margin: 3px 0; background: color-mix(in srgb, var(--content-color) 10%, transparent); } /* Limit mark: gap in the bar with small ticks above and below, visible on every color */ .hs-mark { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: var(--card-color, #2B2C30); } .hs-mark::before, .hs-mark::after { content: ''; position: absolute; left: 0; width: 2px; height: 3px; border-radius: 1px; background: color-mix(in srgb, var(--content-color) 60%, transparent); } .hs-mark::before { bottom: calc(100% + 1px); } .hs-mark::after { top: calc(100% + 1px); } .hs-knob { position: absolute; top: 50%; width: 10px; height: 10px; margin: -5px 0 0 -5px; border-radius: 50%; background: var(--knob-color, var(--content-color)); box-shadow: 0 0 0 4px color-mix(in srgb, var(--knob-color, var(--content-color)) 18%, transparent); transition: left 0.6s ease, background 0.3s; } .hs-chips { display: flex; flex-wrap: wrap; gap: clamp(3px, 1.5cqw, 6px); } .hs-chip { flex: 1 1 0; justify-content: center; min-width: max-content; font-size: clamp(9px, 4.6cqh, 12px); color: color-mix(in srgb, var(--content-color) 65%, transparent); background: color-mix(in srgb, var(--content-color) 9%, var(--card-color, #2B2C30)); display: flex; align-items: center; gap: clamp(2px, 1cqw, 5px); padding: clamp(1px, 0.8cqh, 3px) clamp(6px, 2.2cqw, 10px); border-radius: 999px; white-space: nowrap; } .hs-hidden { display: none !important; } /* ── Very small tiles: only recommendation and indoor values, otherwise fitItems() decides */ @container (max-height: 80px) { .hs-sub, .hs-extra { display: none; } } @container (max-width: 200px) { .hs-room { display: none; } } </style> <script src="/icons.js" crossorigin="anonymous"></script> </head> <body> <div id="card"> <div class="hs-chart-layer" id="chart-layer"> <svg id="chart" viewBox="0 0 100 100" preserveAspectRatio="none"> <defs> <linearGradient id="hs-gradient" x1="0" y1="0" x2="0" y2="1"> <stop offset="0%" stop-color="var(--chart-color)" stop-opacity="0.26" /> <stop offset="100%" stop-color="var(--chart-color)" stop-opacity="0.06" /> </linearGradient> <linearGradient id="hs-gradient-temp" x1="0" y1="0" x2="0" y2="1"> <stop offset="0%" stop-color="var(--chart-temp-color)" stop-opacity="0.12" /> <stop offset="100%" stop-color="var(--chart-temp-color)" stop-opacity="0" /> </linearGradient> </defs> <rect id="chart-band" class="hs-band" x="0" width="100" y="0" height="0" /> <path id="chart-area" class="hs-area" d="" /> <path id="chart-temp-area" class="hs-area-temp" d="" /> <path id="chart-temp" class="hs-line-temp" d="" /> <path id="chart-line" class="hs-line" d="" /> </svg> </div> <div class="hs-chart-layer hs-hidden" id="chart-tip"> <div class="hs-cursor" id="cursor"></div> <div class="hs-cursor-dot" id="cursor-dot"></div> <div class="hs-tip" id="tip"></div> </div> <div id="content"> <div class="hs-head"> <div class="hs-room" id="room"></div> <div class="hs-hint" id="hint"> <span class="hs-dot"></span> <span id="hint-text">--</span> </div> </div> <div class="hs-main" id="main"> <div class="hs-humidity" id="humidity">--</div> <div class="hs-temperature" id="temperature-box"><i class="fa-light fa-thermometer-half"></i><span id="temperature">--</span></div> </div> <div class="hs-sub" id="sub-row"> <div class="hs-sub-item" id="outdoor-item"> <i class="fa-light fa-house-tree"></i> <span id="outdoor"></span> </div> <div class="hs-sub-item hs-hidden" id="difference-item"> <i class="fa-light fa-plus-minus"></i> <span id="difference"></span> </div> </div> <div class="hs-extra" id="extra"></div> </div> </div> <script> // ── INIT: set root/style variables ─────────────────────────────────── const p = new URLSearchParams(location.search); const root = document.documentElement; const m = { margintop: ['--margin-top'], marginside: ['--margin-side'], marginbottom: ['--margin-bottom'] }; for (const k in m) { const v = p.get(k); if (!v) continue; m[k].forEach(c => root.style.setProperty(c, /^\d+(\.\d+)?$/.test(v) ? `${v}px` : v)); } // ── ELEMENTS: ──────────────────────────────────────────────────────── const elContent = document.getElementById('content'); const elRoom = document.getElementById('room'); const elHint = document.getElementById('hint'); const elHintText = document.getElementById('hint-text'); const elHumidity = document.getElementById('humidity'); const elTemperature = document.getElementById('temperature'); const elTemperatureBox = document.getElementById('temperature-box'); const elSubRow = document.getElementById('sub-row'); const elOutdoorItem = document.getElementById('outdoor-item'); const elOutdoor = document.getElementById('outdoor'); const elDifferenceItem = document.getElementById('difference-item'); const elDifference = document.getElementById('difference'); const elExtra = document.getElementById('extra'); const elCard = document.getElementById('card'); const elMain = document.getElementById('main'); const elChartLayer = document.getElementById('chart-layer'); const elChartTip = document.getElementById('chart-tip'); const elCursor = document.getElementById('cursor'); const elCursorDot = document.getElementById('cursor-dot'); const elTip = document.getElementById('tip'); // ── STATE: ─────────────────────────────────────────────────────────── let items = []; let chart = null; // Rendered history: humidity points with their values and times, temperature series let plot = null; let tipTimer = null; // ── ICON: FontAwesome icon mapping ────────────────────────────────── function getIconType(iconName) { if (!iconName || iconName === 'Transparent') { return { type: 'none', classes: '' }; } const raw = String(iconName).trim(); const iconMapping = window.iconMapping || {}; const mapped = iconMapping[raw] ?? raw; const iconClass = String(mapped).trim(); const tokens = iconClass.split(/\s+/).filter(Boolean); if (tokens.some(t => t === 'fak' || t === 'fa-kit')) { return { type: 'kit', classes: (tokens.includes('fa-fw') ? '' : 'fa-fw ') + iconClass }; } if (tokens.some(t => t === 'fa-light' || t === 'fal')) { return { type: 'fa-light', classes: (tokens.includes('fa-fw') ? '' : 'fa-fw ') + iconClass }; } const baseName = iconClass.startsWith('fa-') ? iconClass.substring(3) : iconClass; try { if (window.FontAwesome && typeof window.FontAwesome.findIconDefinition === 'function') { if (window.FontAwesome.findIconDefinition({ prefix: 'fal', iconName: baseName })) { return { type: 'fa-light', classes: `fa-fw fa-light fa-${baseName}` }; } } } catch (e) {} return { type: 'kit', classes: `fa-fw fa-kit fak fa-${baseName}` }; } function createIcon(name) { const icon = document.createElement('i'); icon.className = getIconType(name).classes; return icon; } // ── FUNCTION: value with a smaller unit (e.g. "64 %") ──────────────── function setValueWithUnit(el, text) { el.textContent = ''; const match = String(text ?? '--').match(/^\s*([-+]?[\d.,]+)\s*(.*)$/); if (!match) { el.textContent = text ?? '--'; return; } el.append(document.createTextNode(match[1])); if (match[2]) { const unit = document.createElement('span'); unit.className = 'hs-unit'; unit.textContent = match[2]; el.append(unit); } } // ── FUNCTION: background of a scale (segments or gradient) ─────────── const neutralColor = 'color-mix(in srgb, var(--content-color) 15%, transparent)'; function scaleBackground(colors, mode) { const list = colors.map(c => c || neutralColor); if (list.length === 0) return ''; const step = 100 / list.length; if (mode === 'gradient') { const stops = list.map((c, i) => `${c} ${((i + 0.5) * step).toFixed(2)}%`); return `linear-gradient(to right, ${stops.join(', ')})`; } const stops = list.map((c, i) => `${c} ${(i * step).toFixed(2)}% ${((i + 1) * step).toFixed(2)}%`); return `linear-gradient(to right, ${stops.join(', ')})`; } function knobColor(colors, mode, pos) { if (colors.length === 0) return ''; const index = Math.min(colors.length - 1, Math.floor(pos * colors.length)); return colors[index] || ''; } // ── BUILD: configurable elements ───────────────────────────────────── function buildItems() { elExtra.innerHTML = ''; let chips = null; for (const item of items) { if (item.type === 'text') { const text = document.createElement('div'); text.className = 'hs-text'; text.textContent = item.text ?? ''; elExtra.append(text); chips = null; } else if (item.type === 'scale') { const scale = document.createElement('div'); scale.className = 'hs-scale'; const head = document.createElement('div'); head.className = 'hs-scale-head'; const label = document.createElement('span'); label.className = 'hs-scale-label'; label.textContent = item.label ?? ''; const value = document.createElement('span'); value.className = 'hs-scale-value'; value.textContent = item.text ?? ''; head.append(label, value); const bar = document.createElement('div'); bar.className = 'hs-bar'; const colors = Array.isArray(item.colors) ? item.colors : []; bar.style.background = scaleBackground(colors, item.mode); for (const mark of Array.isArray(item.marks) ? item.marks : []) { const line = document.createElement('div'); line.className = 'hs-mark'; line.style.left = `calc(5px + (100% - 10px) * ${Math.max(0, Math.min(1, Number(mark))).toFixed(3)})`; bar.append(line); } if (item.pos !== null && item.pos !== undefined && !isNaN(item.pos)) { const pos = Math.max(0, Math.min(1, Number(item.pos))); const knob = document.createElement('div'); knob.className = 'hs-knob'; knob.style.left = `calc(5px + (100% - 10px) * ${pos.toFixed(3)})`; const color = knobColor(colors, item.mode, pos); if (color) knob.style.setProperty('--knob-color', color); bar.append(knob); } scale.title = `${item.label ?? ''}: ${item.text ?? ''}`; scale.append(head, bar); elExtra.append(scale); chips = null; } else if (item.type === 'chip') { // Consecutive chips share one row if (chips === null) { chips = document.createElement('div'); chips.className = 'hs-chips'; elExtra.append(chips); } const chip = document.createElement('div'); chip.className = 'hs-chip'; if (item.label) chip.title = item.label; const text = document.createElement('span'); text.textContent = item.text ?? ''; chip.append(createIcon(item.icon), text); chips.append(chip); } } fitItems(); } // ── FUNCTION: hide the last elements (lowest priority) which do not fit function fitItems() { // Chips are hidden one by one, all other elements as a whole const units = []; for (const el of elExtra.children) { el.classList.remove('hs-hidden'); if (el.classList.contains('hs-chips')) { units.push(...el.children); } else { units.push(el); } } units.forEach(el => el.classList.remove('hs-hidden')); for (const el of units.reverse()) { if (elExtra.scrollHeight <= elExtra.clientHeight + 1) break; el.classList.add('hs-hidden'); const row = el.parentElement; if (row.classList.contains('hs-chips') && !row.querySelector('.hs-chip:not(.hs-hidden)')) { row.classList.add('hs-hidden'); } } } // ── FUNCTION: smooth path through the points (Catmull-Rom) ─────────── function smoothPath(pts) { if (pts.length < 2) return ''; let d = `M ${pts[0].x.toFixed(2)},${pts[0].y.toFixed(2)}`; for (let i = 0; i < pts.length - 1; i++) { const p0 = pts[i - 1] || pts[i]; const p1 = pts[i]; const p2 = pts[i + 1]; const p3 = pts[i + 2] || p2; const c1x = p1.x + (p2.x - p0.x) / 6, c1y = p1.y + (p2.y - p0.y) / 6; const c2x = p2.x - (p3.x - p1.x) / 6, c2y = p2.y - (p3.y - p1.y) / 6; d += ` C ${c1x.toFixed(2)},${c1y.toFixed(2)} ${c2x.toFixed(2)},${c2y.toFixed(2)} ${p2.x.toFixed(2)},${p2.y.toFixed(2)}`; } return d; } function toPoints(values, min, max) { const range = (max - min) || 1; const step = 100 / Math.max(1, values.length - 1); return values.map((v, i) => ({ x: i * step, y: 100 - ((v - min) / range) * 100 })); } // ── FUNCTION: own color or the default of the stylesheet ──────────── function setColor(property, color) { if (color) { root.style.setProperty(property, color); } else { root.style.removeProperty(property); } } // ── RENDER: history chart ──────────────────────────────────────────── function renderChart() { const band = document.getElementById('chart-band'); const line = document.getElementById('chart-line'); const area = document.getElementById('chart-area'); const temp = document.getElementById('chart-temp'); const tempArea = document.getElementById('chart-temp-area'); hideTip(); const series = toSeries(chart?.values, chart?.times); const values = series.map(s => s.v); if (values.length < 2) { [line, area, temp, tempArea].forEach(el => el.setAttribute('d', '')); band.setAttribute('height', '0'); plot = null; return; } setColor('--chart-color', chart.color); setColor('--chart-temp-color', chart.tempColor); // Scale the humidity together with the comfort band, so the band stays meaningful let min = Math.min(...values), max = Math.max(...values); const hasBand = Array.isArray(chart.band) && chart.band.length === 2; if (hasBand) { min = Math.min(min, chart.band[0]); max = Math.max(max, chart.band[1]); } const pad = Math.max(2, (max - min) * 0.1); min -= pad; max += pad; const pts = toPoints(values, min, max); const path = smoothPath(pts); line.setAttribute('d', path); area.setAttribute('d', path + ` L 100,100 L 0,100 Z`); if (hasBand) { const y1 = 100 - ((chart.band[1] - min) / (max - min)) * 100; const y2 = 100 - ((chart.band[0] - min) / (max - min)) * 100; band.setAttribute('y', y1.toFixed(2)); band.setAttribute('height', Math.max(0, y2 - y1).toFixed(2)); } else { band.setAttribute('height', '0'); } plot = { pts, series, temps: toSeries(chart.temps, chart.tempTimes) }; // Temperature with its own scale, only the trend matters const temps = plot.temps.map(s => s.v); if (temps.length >= 2) { const tMin = Math.min(...temps), tMax = Math.max(...temps); const tPad = Math.max(0.5, (tMax - tMin) * 0.15); const tempPath = smoothPath(toPoints(temps, tMin - tPad, tMax + tPad)); temp.setAttribute('d', tempPath); tempArea.setAttribute('d', tempPath + ' L 100,100 L 0,100 Z'); } else { temp.setAttribute('d', ''); tempArea.setAttribute('d', ''); } } // ── FUNCTION: values with their times, invalid values are dropped ──── function toSeries(values, times) { const list = []; if (!Array.isArray(values)) return list; values.forEach((v, i) => { if (v !== null && !isNaN(v)) { list.push({ v: Number(v), t: Array.isArray(times) ? Number(times[i]) : NaN }); } }); return list; } // ── FUNCTION: number and time formats of the browser language ─────── function formatNumber(value, digits) { return Number(value).toLocaleString(undefined, { minimumFractionDigits: digits, maximumFractionDigits: digits }); } function formatHumidity(value) { return `${formatNumber(value, 0)} %`; } // Unit of the indoor temperature as delivered by its presentation (e.g. "°C") function temperatureUnit() { const match = elTemperature.textContent.match(/[^\d.,\s+-][^\d]*$/); return match ? match[0].trim() : '°C'; } // Periods longer than a day also need the weekday function formatTime(time, span) { if (isNaN(time)) return ''; const options = { hour: '2-digit', minute: '2-digit' }; if (span > 26 * 3600) options.weekday = 'short'; return new Date(time * 1000).toLocaleString(undefined, options); } function nearest(list, time) { if (list.length === 0 || isNaN(time)) return null; let best = null; for (const entry of list) { if (best === null || Math.abs(entry.t - time) < Math.abs(best.t - time)) best = entry; } return Math.abs(best.t - time) <= 5400 ? best : null; } // ── FUNCTION: tip above (or below, if there is no room) a chart point function placeLabel(el, pt, prefer, gap) { const height = elChartLayer.clientHeight; const space = el.offsetHeight + gap; const y = pt.y / 100 * height; let above = prefer === 'above'; if (above && y < space) { above = false; } else if (!above && y > height - space) { above = true; } el.style.left = `${pt.x}%`; el.style.top = `${pt.y}%`; // Shift proportionally to the position, so the label stays inside at the edges el.style.transform = `translate(${-pt.x}%, ${above ? `calc(-100% - ${gap}px)` : `${gap}px`})`; } // ── RENDER: values at the pointer position ─────────────────────────── function showTip(clientX) { if (!plot) return; clearTimeout(tipTimer); const rect = elChartLayer.getBoundingClientRect(); const count = plot.pts.length; const frac = Math.max(0, Math.min(1, (clientX - rect.left) / rect.width)); const index = Math.round(frac * (count - 1)); const pt = plot.pts[index]; const entry = plot.series[index]; const parts = []; const time = formatTime(entry.t, plot.series[count - 1].t - plot.series[0].t); if (time) parts.push(time); parts.push(formatHumidity(entry.v)); const temp = nearest(plot.temps, entry.t); if (temp) parts.push(`${formatNumber(temp.v, 1)} ${temperatureUnit()}`); elTip.textContent = parts.join(' · '); elChartTip.classList.remove('hs-hidden'); elCursor.style.left = `${pt.x}%`; elCursorDot.style.left = `${pt.x}%`; elCursorDot.style.top = `${pt.y}%`; placeLabel(elTip, pt, 'above', 9); } function hideTip() { clearTimeout(tipTimer); elChartTip.classList.add('hs-hidden'); } function inChart(e) { const rect = elChartLayer.getBoundingClientRect(); return e.clientY >= rect.top && e.clientY <= rect.bottom; } // ── LAYOUT: chart starts below the indoor values (at the middle of the line below), but keeps at least half the height function layoutChart() { const card = elCard.getBoundingClientRect(); if (card.height === 0) return; let start = elMain.getBoundingClientRect().bottom + 6; const sub = elSubRow.getBoundingClientRect(); if (sub.height > 0) start = Math.max(start, (sub.top + sub.bottom) / 2); const top = Math.max(0, Math.min(start - card.top, card.height * 0.5)); root.style.setProperty('--chart-top', `${top.toFixed(1)}px`); } // ── SDK: handleMessage (data transfer) ─────────────────────────────── function handleMessage(encodedData) { const decodedData = JSON.parse(encodedData); let needsItems = false; let needsChart = false; let needsSub = false; for (const parameter in decodedData) { const value = decodedData[parameter]; switch (parameter) { case 'room': elRoom.textContent = value ?? ''; elRoom.classList.toggle('hs-hidden', !value); elContent.classList.toggle('no-room', !value); break; case 'hintText': elHintText.textContent = value ?? '--'; break; case 'hintColor': if (value) root.style.setProperty('--hint-color', value); break; case 'humidity': setValueWithUnit(elHumidity, value); break; case 'temperature': elTemperature.textContent = value ?? '--'; break; case 'outdoor': elOutdoor.textContent = value ?? ''; elOutdoorItem.classList.toggle('hs-hidden', !value); needsSub = true; break; case 'difference': elDifference.textContent = value ?? ''; elDifferenceItem.classList.toggle('hs-hidden', !value); needsSub = true; break; case 'alert': elHint.classList.toggle('hs-alert', !!value); break; case 'titles': elHint.title = value?.hint ?? ''; elHumidity.title = value?.humidity ?? ''; elTemperatureBox.title = value?.temperature ?? ''; elOutdoorItem.title = value?.outdoor ?? ''; elDifferenceItem.title = value?.difference ?? ''; break; case 'items': items = Array.isArray(value) ? value : []; needsItems = true; break; case 'chart': chart = value; needsChart = true; break; default: console.warn('Unknown parameter:', parameter); } } if (needsSub) { elSubRow.classList.toggle('hs-hidden', !elOutdoor.textContent && !elDifference.textContent); } if (needsItems) buildItems(); if (needsChart) renderChart(); } // ── INIT: ──────────────────────────────────────────────────────────── new ResizeObserver(() => fitItems()).observe(elExtra); const chartObserver = new ResizeObserver(() => layoutChart()); chartObserver.observe(elCard); chartObserver.observe(elMain); chartObserver.observe(elSubRow); // Mouse: values while hovering; touch: values while the finger moves sideways, briefly kept afterwards let tracking = false; elCard.addEventListener('pointermove', e => { if (e.pointerType === 'mouse') { if (plot && inChart(e)) { showTip(e.clientX); |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |