Advertisement
renix1

generate wallpaper

Mar 5th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.         <title>Wallpaper</title>
  4.         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  5.         <style>
  6.             * {
  7.                 margin: 0;
  8.                 padding: 0;
  9.             }
  10.  
  11.             body {
  12.                 background-color: #fff;
  13.                 text-align: center;
  14.             }
  15.  
  16.             .container {
  17.                 transition: .4s;
  18.                 margin: 0 auto;
  19.                 background-color: #F5F5F5;
  20.                 width: 800px;
  21.                 height: 600px;
  22.                 position: relative;
  23.                 top: 47.5%;
  24.                 transform: translateY(-50%);
  25.                 text-align: center;
  26.                 border-radius: 8px;
  27.                 box-shadow: 2px 2px 18px 2px rgba(0,0,0, .75);
  28.             }
  29.  
  30.             .container:hover {
  31.                 transition: .4s;
  32.                 box-shadow: 2px 2px 40px 2px rgba(0,0,0, .75);
  33.             }
  34.  
  35.             .container img {
  36.                 width: 800px;
  37.                 height: 600px;
  38.                 position: absolute;
  39.                 left: 0px;
  40.                 top: 0px;
  41.                 display: block;
  42.                 border-radius: 8px;
  43.             }
  44.  
  45.             .container img:empty {
  46.                 display: none;
  47.             }
  48.  
  49.             .btn{
  50.                 transition: .4s ease-out;
  51.                 margin-top: 5px;
  52.                 padding: 5px;
  53.                 font-size: 1.3em;
  54.                 outline: none;
  55.                 color: #000;
  56.                 border: none;
  57.                 box-decoration-break: unset;
  58.                 background-color: transparent;
  59.             }
  60.  
  61.             .btn:hover {
  62.                 transition: .6s ease-in-out;
  63.                 color: dodgerblue;
  64.             }
  65.  
  66.             .fa-spinner {
  67.                 animation-name: loading;
  68.                 animation-duration: .5s;
  69.                 font-size: 36px;
  70.                 position: relative;
  71.                 top: 50%;
  72.                 animation-iteration-count: infinite;
  73.                 z-index: 999;
  74.                 display: none;
  75.                 color: #fff;
  76.                 text-shadow: 1px 2px #000;
  77.             }
  78.  
  79.             @keyframes loading {
  80.                 to {
  81.                     transform: rotate(360deg);
  82.                 }
  83.             }
  84.         </style>
  85.     </head>
  86.     <body>
  87.         <button id="myButton" class="btn"><i class="fa fa-image"></i></button>
  88.         <a id="downloadButton" href="#" download>
  89.             <button class="btn">
  90.                 <i class="fa fa-download"></i>             
  91.             </button>
  92.         </a>
  93.         <div class="container">
  94.             <i id="loadingProgress" class="fa fa-spinner"></i>
  95.             <img id="myWallpaper" src="">
  96.         </div>
  97.         <script>
  98.             function generateImage () {
  99.                 var loading = document.getElementById('loadingProgress');
  100.                 const xhr = new XMLHttpRequest();
  101.                 const url = 'https://api.desktoppr.co/1/wallpapers';
  102.  
  103.                 // Handling request
  104.                 xhr.responseType = 'json';
  105.                 xhr.onreadystatechange = function () {
  106.                     if (xhr.readyState === XMLHttpRequest.DONE) {
  107.                         var images = xhr.response.response;
  108.                         var randomNumber = Math.floor(Math.random() * 19);
  109.                         var wallpaper = document.getElementById('myWallpaper');
  110.                         var image = images[randomNumber].image.url;
  111.                         wallpaper.src = image;
  112.                         loading.style.display = 'none';
  113.                         wallpaper.style.display = 'block';
  114.                     } else {
  115.                         loading.style.display = 'block';
  116.                     }
  117.                 }
  118.  
  119.                 // Sending request
  120.                 xhr.open('GET', url);
  121.                 xhr.send();
  122.             }
  123.  
  124.             function downloadImage (element) {
  125.                 var image = document.getElementById('myWallpaper');
  126.                 element.setAttribute('href', image.src);
  127.                 console.log(element);
  128.             }
  129.  
  130.             var generateBtn = document.getElementById('myButton');
  131.             generateBtn.addEventListener('click', generateImage);
  132.  
  133.             var downloadBtn = document.getElementById('downloadButton');
  134.             downloadBtn.addEventListener('click', function () {
  135.                 downloadImage(this);
  136.             });
  137.         </script>
  138.     </body>
  139. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement