Advertisement
Guest User

OBS Bug Report

a guest
May 14th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.54 KB | None | 0 0
  1. <html>
  2. <head>
  3.     <title>Orbiting Planets</title>
  4.     <style>
  5.         body {
  6.             margin: 0px;
  7.             overflow: hidden;
  8.         }
  9.     </style>
  10. </head>
  11.  
  12. <body>
  13.     <canvas id="sky"></canvas>
  14.     <script>
  15.         // Get the canvas and set up some parameters.
  16.         var canvas = document.getElementById("sky");
  17.         var context = canvas.getContext("2d");
  18.         var starCount = 200;
  19.         var canvasWidth = window.innerWidth;
  20.         var canvasHeight = window.innerHeight;
  21.  
  22.         // Set the canvas size.
  23.         canvas.width = canvasWidth;
  24.         canvas.height = canvasHeight;
  25.        
  26.         // Store star data
  27.         storedStars = new Array();
  28.         for (var i = 0; i < starCount; i++) {
  29.             storedStars[i] = {radius: 0, x: 0, y:0, bluramount: 0};
  30.             storedStars[i].radius = 1 + Math.random() * 2;
  31.             storedStars[i].x = canvasWidth * Math.random();
  32.             storedStars[i].y = canvasHeight * Math.random();
  33.             storedStars[i].bluramount = 1 + Math.random() * 4 * storedStars[i].radius;
  34.         }
  35.  
  36.         function drawBackground() {
  37.             // Background.
  38.             // We create a radial gradient from the bottom-left.
  39.             var gradient = context.createRadialGradient(0, canvasHeight, 0, 0, canvasHeight, 1000);
  40.             gradient.addColorStop(0, "#151530");
  41.             gradient.addColorStop(1, "#252540");
  42.  
  43.             context.fillStyle = gradient;
  44.             context.beginPath();
  45.             context.arc(0, canvasHeight, canvasWidth*1.42, 0, Math.PI * 2, true);
  46.             context.fill();
  47.  
  48.  
  49.             // Stars.
  50.             // We randomly place a number of stars using shadows as a highlight.
  51.             context.fillStyle = "#dadffa";
  52.             context.shadowOffsetX = 0;
  53.             context.shadowOffsetY = 0;
  54.             context.shadowColor = "#a3ade6";
  55.  
  56.             for (var i = 0; i < starCount; i++) {
  57.                 var radius = storedStars[i].radius;
  58.                
  59.                 context.beginPath();
  60.                 context.arc(storedStars[i].x, storedStars[i].y, radius, 0, Math.PI * 2, true);
  61.                 context.shadowBlur = storedStars[i].bluramount;
  62.                 context.fill();
  63.             }
  64.             context.shadowBlur = 0;
  65.         }
  66.  
  67.  
  68.         function getFontHeight(name) {
  69.             return (context.measureText(name).actualBoundingBoxAscent - context.measureText(name).actualBoundingBoxDescent);
  70.         }
  71.  
  72.         function getFontWidth(name) {
  73.             return context.measureText(name).width;
  74.         }
  75.  
  76.         function drawCircle(x,y,radius,color,name) {
  77.             context.fillStyle = "#000000";
  78.             context.beginPath();
  79.             context.arc(x,y,radius,0,Math.PI*2);
  80.             context.fill();
  81.            
  82.             context.fillStyle = color;
  83.             context.beginPath();
  84.             context.arc(x,y,radius-8,0,Math.PI*2);
  85.             context.fill();
  86.            
  87.             context.fillStyle = "#000000";
  88.             var fontsize = 100;
  89.             context.font = fontsize + "px sans-serif";
  90.             while (getFontWidth(name) > 1.2*radius) {
  91.                 fontsize = fontsize - 1;
  92.                 context.font = fontsize + "px sans-serif";
  93.             }
  94.             context.fillText(name,x - getFontWidth(name)/2,y + getFontHeight(name)/2);
  95.         }
  96.  
  97.         function orbitStar(distanceFromStar,radius,color,name,time) {
  98.             if (name in savedPlanets) {
  99.                 context.putImageData(savedPlanets[name].imagedata,savedPlanets[name].x,savedPlanets[name].y);
  100.             }
  101.             planetx = canvasWidth/2 + distanceFromStar*Math.cos(time);
  102.             planety = canvasHeight/2 + distanceFromStar*Math.sin(time);
  103.             savedPlanets[name] = {imagedata: context.getImageData(planetx-radius-2,planety-radius-2,radius*2+4,radius*2+4), x: planetx-radius-2, y: planety-radius-2};
  104.             drawCircle(planetx,planety,radius,color,name);
  105.         }
  106.  
  107.         savedPlanets = new Array();
  108.  
  109.         drawBackground();
  110.         drawCircle(canvasWidth/2,canvasHeight/2,100,"#FFFF00","Star");
  111.  
  112.         function step(time) {
  113.             orbitStar(230,70,"#CC0000","Planet",time/1000);
  114.             window.requestAnimationFrame(step);
  115.         }
  116.         window.requestAnimationFrame(step);
  117.     </script>
  118. </body>
  119. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement