css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              .container
  .center-body.hidden
    div
      h1 Random quotes from romantic<sup>*</sup> films.
      .hearts
        - for (i = 1; i <= 4; i++)
          .heart(id='heart-'+i)
            svg.heart-bg(viewbox='0 0 1121.8766 952.871')
              use(xlink:href="#heart")
            .text-front.js-text-front
              .heart-shape.heart-left
              .heart-shape.heart-right
              p.heart-text.js-heart-text
      p#attr From <a target="_blank"></a>.
      p
        strong 
          a.js-reload Try another movie.
  p.disclaimer * : According to <a href="https://en.wikiquote.org/wiki/Category:Romance_films" target="_blank">WikiQuote</a>.

#splash.visible
  h2 loading...


<svg style="display:none;"><symbol id="heart" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1121.8766" height="952.871" viewBox="0 0 1121.8766 952.871"><defs><clipPath id="a" transform="translate(0 0)"><path d="M513.0783,52.8614l65.9066,42.4105q10.933,7.0353,21.1819,14.93L534.26,67.7918Q524.023,59.9013,513.0783,52.8614Z" style="fill:none"/></clipPath><clipPath id="b" transform="translate(0 0)"><path d="M385.5956,848.4261,319.689,806.0156c59.8759,38.53,126.2749,74.0005,194.9349,104.4449L580.53,952.871C511.8705,922.4266,445.4715,886.9559,385.5956,848.4261Z" style="fill:none"/></clipPath><clipPath id="c" transform="translate(0 0)"><path d="M916.4346,48.7705,982.3411,91.181a278.19,278.19,0,0,1,37.2458,28.651c58.718,53.794,94.5924,131.6087,101.1817,219.1512,6.1275,81.9863-13.3124,165.8575-54.5224,236.222-46.9286,79.9049-119.0775,155.54-214.3523,225.0113C773.49,857.3871,679.0077,909.9931,580.53,952.871l-65.9066-42.4105c98.4772-42.878,192.9593-95.4839,271.3634-152.6546C881.262,688.3346,953.4109,612.7,1000.34,532.7947c41.21-70.3645,60.65-154.2357,54.5224-236.222C1048.2727,209.03,1012.3984,131.2155,953.68,77.4215A278.1915,278.1915,0,0,0,916.4346,48.7705Z" style="fill:none"/></clipPath></defs><title>heart-2-inline</title><g style="isolation:isolate"><g style="isolation:isolate"><g style="clip-path:url(#a)"><g style="isolation:isolate"><path d="M513.0783,52.8614l65.9066,42.4105q10.933,7.0353,21.1819,14.93L534.26,67.7918q-10.2372-7.8905-21.1819-14.93" transform="translate(0 0)" /></g></g></g><g style="isolation:isolate"><g style="clip-path:url(#b)"><g style="isolation:isolate"><path d="M514.6239,910.4605,580.53,952.871c-68.66-30.4444-135.059-65.9151-194.9349-104.4449L319.689,806.0156c59.8759,38.53,126.2749,74.0005,194.9349,104.4449" transform="translate(0 0)" /></g></g></g><path d="M743.3655,0C824.45,0,899.0945,27.4954,953.68,77.4215c58.718,53.794,94.5924,131.6087,101.1817,219.1512C1060.99,378.559,1041.55,462.43,1000.34,532.7947,953.4109,612.7,881.262,688.3346,785.9873,757.806c-78.4041,57.1706-172.8862,109.7766-271.3634,152.6546-96.4792-42.78-188.4941-95.4839-264.2193-152.6546C158.385,688.3346,89.7825,612.6014,46.5956,532.7947,8.4882,462.43-7.0218,378.559,2.9474,296.5727,13.6387,209.03,53.1592,131.2155,114.389,77.4215,171.323,27.4954,247.256,0,328.34,0c76.4638,0,149.5132,24.3153,205.92,67.7918C592.6045,24.4136,666.9016,0,743.3655,0Z" transform="translate(0 0)" fill="currentcolor"/><g style="isolation:isolate"><g style="clip-path:url(#c)"><g style="isolation:isolate"><path d="M916.4346,48.7705,982.3411,91.181a278.1914,278.1914,0,0,1,37.2458,28.651c58.718,53.794,94.5924,131.6087,101.1817,219.1512,6.1275,81.9863-13.3124,165.8575-54.5224,236.222-46.9286,79.9049-119.0775,155.54-214.3523,225.0113C773.49,857.3871,679.0077,909.9931,580.53,952.871l-65.9066-42.4105c98.4772-42.878,192.9593-95.4839,271.3634-152.6546C881.262,688.3346,953.4109,612.7,1000.34,532.7947c41.21-70.3645,60.65-154.2357,54.5224-236.222C1048.2727,209.03,1012.3984,131.2155,953.68,77.4215a278.1914,278.1914,0,0,0-37.2458-28.651" transform="translate(0 0)" /></g></g></g></g></symbol>
</svg>

            
          
!
            
              body{
  margin:0;
  font-family: 'Aleo', serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

*, *:before, *:after {
  box-sizing: border-box;
}

.container{
  display: flex;
  flex-direction: column;
  width:100%;
  padding:20px;
  min-height:100vh;
  align-items: center;
  justify-content: center;
  
}

.center-body{
  width:100%;
  display: flex;
  flex:1;
  max-width:1200px;
  align-items: center;
  justify-content: center;
  transition:0.125s opacity;
  &.hidden{
    opacity:0;
  }
  > div{
    width:100%;
  }
}

h1{
  padding:0;
  margin:0;
  text-align:center;
  font-weight:400;
  font-size:32px;
  line-height:1.33;
  color:#353535;
  font-family: 'Pinyon Script', cursive;
}

h2{
  padding:0;
  margin:0;
  text-align:center;
  font-weight:400;
  font-size:24px;
  line-height:1.33;
  color:#353535;
  font-family: 'Palatino', serif;
}

p{
  padding:0 0 10px; 
  margin:0;
  font-weight:300;
  text-align:center;

  a{
    color:darken(#91d1da, 20%);
    transition:0.125s all;
    text-decoration:underline;
    cursor:pointer;
    &:hover{
      color:darken(#91d1da, 26%);
    }
  }
  &.disclaimer{
    width:100%;
    text-align:right;
    font-size:12px;
    padding:0;
  }
}

.hearts{
  display: grid;
  padding:20px 0;
  @media(min-width:480px){
    grid-template-columns:1fr 1fr;
  }
  @media(min-width:768px){
    grid-template-columns:1fr 1fr 1fr 1fr;
  }
  gap:20px;
}

.heart{
  position:relative;
}

.text-front{
  position:absolute;
  top:9%;
  width:96%;
  left:-1%;
  height:79%;
  text-transform: uppercase;
  transform:rotateY(9deg) rotateX(-11deg);
}

$hearts_colors: (
  green: #b2d9a4,
  orange: #fecaa2,
  yellow: #f8e888,
  purple: #d6cade,
  pink: #f0c5d9,
  blue: #91d1da
);

@each $name, $hex in $hearts_colors {
  .heart-#{$name}{
    color:$hex; // main color
    fill:darken($hex, 20%); // shadow
  }
}

.heart-shape{
  width:50%;
  height:100%;
  font-size:0;
  padding:0;
  margin:0;
  &.heart-left{
    shape-outside:polygon(
      0% 0%, 
      22% 0%, 
      8.5% 19.30%, 
      8.5% 46.1%, 
      33% 73.46%, 
      58% 87.02%, 
      100% 100%, 
      0% 100%
    );
    float:left;
  }	
  &.heart-right{
    shape-outside:polygon(
      100% 0%, 
      78% 0%, 
      91.5% 19.30%, 
      91.5% 46.1%, 
      67% 73.46%, 
      32% 87.02%, 
      0% 100%, 
      100% 100%
    );
    float:right;
  }	
}

.heart-text{
  background-image:url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/67732/text-bg.png');
  -webkit-text-fill-color: transparent;
  -webkit-background-clip: text;
  padding:0;
  margin:0;
  text-align:center;
  color:#d57777;
  font-weight:800;
  font-family:'Nunito', serif;
  line-height:1.33;
}

#splash{
  position:absolute;
  top:0;
  left:0;
  bottom:0;
  right:0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color:#fff;
  opacity:0;
  transition:0.1s all;
  &.visible{
    opacity:1;
  }
}
            
          
!
            
              const api_endpoint = 'https://en.wikiquote.org/w/api.php';
const xhr = new XMLHttpRequest();
const colors = ['green', 'orange', 'yellow', 'purple', 'pink', 'blue'];
let film_attr = {},all_quotes, container_height, displayed_quotes, parsed_quotes = [], hash;

const random_movie = () => { 
  const categories = ['Romance_films', 'Romantic_comedy_films', 'Romantic_drama_films'];
  xhr.open('GET', api_endpoint+'?action=query&list=categorymembers&cmtitle=Category:'+categories[Math.floor(Math.random()*categories.length)]+'&cmlimit=max&format=json&origin=*');
  xhr.onload = function() {
    if (xhr.status >= 200 && xhr.status < 400) {
      let data = JSON.parse(xhr.responseText);
      get_page(data.query.categorymembers[Math.floor(Math.random()*data.query.categorymembers.length)].pageid);
    }
  };
  xhr.send();
};

const get_page = (pageid) => {
  displayed_quotes = [];
  parsed_quotes = [];

  xhr.open('GET', api_endpoint+'?action=parse&pageid='+pageid+'&format=json&origin=*');
  xhr.onload = function() {
    if (xhr.status >= 200 && xhr.status < 400) {
      const data = JSON.parse(xhr.responseText);
      film_attr.title = data.parse.title;
      film_attr.href = data.parse.iwlinks[0].url;
      const body_text = data.parse.text[Object.keys(data.parse.text)[0]];

      // filter all the non-quotes from the page
      const ignored_section_titles = ['taglines', 'external links', 'external link', 'supporting characters', 'cameos', 'tagline', 'see also', 'props'];
      const sections = data.parse.sections;
      all_quotes = [];
      for(let i=0; i<sections.length; i++){
        if(sections[i].toclevel == 1){
          if(!ignored_section_titles.includes(sections[i].line.toLowerCase()) && sections[i].line.toLowerCase().indexOf('about') < 0 && sections[i].line.toLowerCase().indexOf('cast') < 0){
                       anchor_unique_start_text = '<span class="mw-headline" id="'+sections[i].anchor+'">';
            section_start_pos = body_text.indexOf('</h2', body_text.indexOf(anchor_unique_start_text)+3);
            section_end_pos = body_text.indexOf('<h2', section_start_pos)+3;
            // console.log(body_text.substring(section_start_pos, section_end_pos))
            all_quotes = all_quotes+body_text.substring(section_start_pos, section_end_pos);
          }
        }
      }
      
      const parser = new DOMParser();
      const doc = parser.parseFromString(all_quotes, "text/html");

      lis = doc.querySelectorAll('ul > li > ul');
      lis.forEach(function(li){
        li.remove();
      });

      lis = doc.querySelectorAll('li');
      lis.forEach(function(li){
        if(!li.classList.contains('toclevel-1') && li.parentNode.parentNode.tagName != 'DD'){
          txt = check_quote(li.textContent);
          if(txt){
            parsed_quotes.push(txt);
          }
        }
      });

      dds = doc.querySelectorAll('dd');
      dds.forEach(function(dd){
        strong = dd.querySelectorAll('b,strong,big');
        if(strong.length > 1){
          for(let i=1; i<strong.length; i++){
            txt = check_quote(strong[i].textContent);
            if(txt){
              parsed_quotes.push(txt);
            }
          }
        }
      });
      
      for(let i=0; i<4; i++){
        if(parsed_quotes.length){
          index = Math.floor(Math.random()*parsed_quotes.length);
          displayed_quotes.push(parsed_quotes[index]);
          parsed_quotes.splice(index, 1);
        }
      }

      // if there aren't enough standalone quotes, use out-of-context dialogue ones
      if(parsed_quotes.length<4){
        dds = doc.querySelectorAll('dd');
        dds.forEach(function(dd){
          strong = dd.querySelectorAll('b,strong,big');
          if(strong.length <= 1){
            txt = check_quote(dd.textContent);
            if(txt){
              parsed_quotes.push(txt);
            }
          }
        });
        
        l = 4-(displayed_quotes.length);
        for(let i=0; i<l; i++){
          if(parsed_quotes.length){
            index = Math.floor(Math.random()*parsed_quotes.length);
            displayed_quotes.push(parsed_quotes[index]);
            parsed_quotes.splice(index, 1);
          }
        }
      }
      
      if(displayed_quotes.length < 4){
        random_movie();
      }else{
        populate_page();
      }
    }
  };
  xhr.send();
}

const check_quote = (txt) => {
  // clean up the quotes and only use the ones that meet a certain character/word limit
  
  // remove brackets that are typically stage direction
  txt = txt.replace(/ *\[[^\]]*]/, ''); 
  txt = txt.replace(/\([^\)\(]*\)/, "");

  if(txt.indexOf(': ')>-1 || txt.indexOf(': ')>-1){
    txt = txt.substring(txt.indexOf(": ")+2);
  }
  
  txt = txt.trim();

  if(txt.indexOf(' ')>-1){
    if(txt.match(/([\s]+)/g).length < 1){
      return false;
    }
  }else{
    return false;
  }

  if(txt.length < 150){
    return txt;
  }

  return false;
}

const populate_page = () => {
  document.querySelector('#attr a').innerHTML = film_attr.title;
  document.querySelector('#attr a').setAttribute('href', film_attr.href);
  document.querySelector('.center-body').classList.remove('hidden');
  for(let i=1; i<5; i++){
    heart = document.getElementById('heart-'+i);
    heart.querySelector('.js-heart-text').innerHTML = displayed_quotes[i-1];
  }
  document.querySelectorAll('.heart').forEach(function(e){
    e.className='heart heart-'+rand_array_item(colors);
  });
  
  if(document.querySelector('#splash.visible')){
    document.querySelector('#splash.visible').classList.remove('visible');
    splash = document.getElementById('splash');
    window.setTimeout(function(){
      splash.parentNode.removeChild(splash);
    }, 2000);
  }

  scale_text();
}

const scale_text = () => {
  // scale the text up until it no longer fits in its container
  // at this point, it's just guess and check
  container_height = document.querySelector('.js-text-front').clientHeight;
  document.querySelectorAll('.js-heart-text').forEach(function(e){
    e.style.paddingTop = e.style.paddingBottom = 0;
    e.style.fontSize = '50px';
  });
  decrease_font_size(50);
}

const decrease_font_size = (size) => {
  // decrease the font size until it all fits in the container
  max_height = 0;
  document.querySelectorAll('.js-heart-text').forEach(function(e){
    e.style.fontSize = (size-0.1)+'px';
    if(e.offsetHeight > max_height){
      max_height = e.clientHeight;
    }
  });
  if(max_height > container_height){
    decrease_font_size(size-0.1);
  }else{
    // we've hit the max height
    document.querySelectorAll('.js-heart-text').forEach(function(e){
      decrease_text_padding(e, container_height);
    });
  }
}

const decrease_text_padding = (e, padding) =>{
  e.style.paddingTop = e.style.paddingBottom = padding-0.1+'px';
  if(e.clientHeight > container_height){
    decrease_text_padding(e,padding-0.1);
  }
}

const rand_array_item = (array) =>{
  return array[Math.floor(Math.random()*array.length)];
}

(function() {
  var throttle = function(type, name, obj) {
      obj = obj || window;
      var running = false;
      var func = function() {
          if (running) { return; }
          running = true;
           requestAnimationFrame(function() {
              obj.dispatchEvent(new CustomEvent(name));
              running = false;
          });
      };
      obj.addEventListener(type, func);
  };

  /* init - you can init any event */
  throttle("resize", "optimizedResize");
})();

// handle event

window.addEventListener("optimizedResize", function() {
  scale_text();
});

document.querySelector('.js-reload').addEventListener("click", function() {
  random_movie();
});


WebFont.load({
  google: {
    families: ['Aleo:300,400', 'Pinyon Script', 'Nunito:800']
  },
  classes: false,
  active: function(){
    random_movie();
  }
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console