I wondered about writing a starry (animated) sky on canvas, in theory everything worked out ... but when I added it as a background, the site started working with brakes, most likely I’ve got somewhere with the drawing and animation code.
Checked on OC - windows 7 (64) in Firefox 61.0 - hangs, Chrome 67.0.3396.99 - also hangs, but not so noticeably.
I ask for help from those who understand. Tell me what to rewrite / add, where nakosyachil, or your answer. Thank.
Link to the site (LP) to which stuck this animation > The site is here < . And just in case, the link separately to the animation > Codepen <
var c = document.getElementById('star-sky'); var ctx = c.getContext('2d'); var x = c.width = window.innerWidth; var y = c.height = window.innerHeight; var nStar = Math.round((x + y) / 5); var randomSize = Math.floor((Math.random() * 2) + 1); var stars = []; function createStars() { 'use strict'; stars = []; for (var i = 0; i <= nStar; i++) { stars.push({ x: Math.random() * x, y: Math.random() * y, o: Math.random(), r: Math.random(), s: 0.02, }) if (randomSize > .1) { ctx.shadowBlur = Math.floor((Math.random() * 15)); ctx.shadowColor = "white"; } } } function drawing() { 'use strict'; requestAnimationFrame(drawing); ctx.clearRect(0, 0, innerWidth, innerHeight); for (var i = 0; i <= nStar; i++) { var e = stars[i]; if (eo > 1 || eo < 0) { es = -es; } eo += es; ctx.beginPath(); ctx.arc(ex, ey, er, 0, Math.PI * 2, false); ctx.strokeStyle = 'rgba(255, 255, 255, ' + eo + ')'; ctx.stroke(); } } window.onload = function() { createStars(); drawing(); }; * { margin: 0; padding: 0; } #star-sky { background: linear-gradient(#00111e 30%, #033d5e); vertical-align: bottom; height: 100vh; width: 100%; } <canvas id="star-sky"></canvas>