Guest User

font previewer thingy

a guest
Jun 8th, 2021
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.13 KB | None | 0 0
  1. <div>
  2.     <script>
  3.         window.fontTesterOptions = {
  4.             characters: 'aimepytgdobh',
  5.             initial: `
  6.                 imbody
  7.                 empathy
  8.             `,
  9.         };
  10.     </script>
  11.     <style>
  12.         .font-tester-container {
  13.             --colour-text: black;
  14.             --colour-background: rgba(0, 0, 0, .02);
  15.             --font-name: "wonky";
  16.         }
  17.  
  18.         @font-face {
  19.             font-family: "wonky";
  20.             /* Add other properties here, as needed. For example: */
  21.             /*
  22.             font-weight: 100 900;
  23.             font-style: normal italic;
  24.             */
  25.             src: url(data:application/font-woff;base64,/*truncated for ur convenience xdd*/);
  26.         }
  27.        
  28.         #font-tester {
  29.             line-height: 2em;
  30.             text-align: center;
  31.             font-family: var(--font-name);
  32.             font-size: 5rem;
  33.             border: 0;
  34.             background: var(--colour-background);
  35.             color: var(--colour-text);
  36.             box-sizing: border-box;
  37.             padding: 5rem;
  38.             transition: none !important;
  39.         }
  40.        
  41.         .font-tester-container {
  42.             width: 100%;
  43.             display: flex;
  44.             justify-content: center;
  45.             align-items: center;
  46.         }
  47.     </style>
  48.     <div class="font-tester-container">
  49.       <textarea id="font-tester"></textarea>
  50.     </div>
  51.     <script>
  52.         (() => {
  53.             const fontTesterElem = document.getElementById('font-tester');
  54.             const charactersRegex = new RegExp(
  55.                 // escaped characters + space + newline
  56.                 `[${window.fontTesterOptions.characters.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\n ]`
  57.            );
  58.            
  59.             fontTesterElem.textContent = window
  60.                 .fontTesterOptions
  61.                 .initial
  62.                     .trim()
  63.                     .replace(/ {4}|\t/g, '');
  64.  
  65.             fontTesterElem.addEventListener(
  66.                 'keypress',
  67.                 (event) => charactersRegex.test(event.key) || event.preventDefault(),
  68.             );
  69.            
  70.             new IntersectionObserver(
  71.                 (entry) => {
  72.                     if (!entry[0].isIntersecting) return;
  73.                     fontTesterElem.focus();
  74.                     // move cursor to end
  75.                     fontTesterElem.selectionEnd = fontTesterElem.selectionStart = fontTesterElem.textContent.length;
  76.                 },
  77.                 { threshold: 0.5 },
  78.             )
  79.                 .observe(fontTesterElem);
  80.         })();
  81.     </script>
  82. </div>
Add Comment
Please, Sign In to add comment