CSS and JS realize the beautiful starry sky trajectory movement effect
This article mainly shares with you a beautiful starry sky trajectory motion effect realized using CSS+JS. The effect is very realistic. The editor below brings you an example code. Friends who need it can refer to it. I hope it can help you.
Let me share with you the renderings first:
Let me share with you a beautiful starry sky trajectory motion effect achieved using CSS+JS. This effect is not inferior to Flash . The relevant codes are as follows:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>cloth</title> <style> body { background: #000; } img { display: block; float: left; margin: 0 1px 0 0; } canvas { background: #131c35 linear-gradient(#192853, #131c35); display: block; float: left; /* uncomment to test overlay */ /* position: absolute; left: 0; opacity: .5; top: 0; */ } </style> </head> <body> <p id="container"></p> <script type="text/javascript" src="http://cdn.gbtags.com/jquery/1.11.1/jquery.min.js"></script> <canvas id="c"></canvas> <img src="/static/imghwm/default1.png" data-src="http://dribbble.s3.amazonaws.com/users/36991/screenshots/674715/game.png" class="lazy" / alt="CSS and JS realize the beautiful starry sky trajectory movement effect" > <script> var c = document.getElementById('c'), ctx = c.getContext('2d'), cw = c.width = 400, ch = c.height = 300, rand = function(a,b){return ~~((Math.random()*(b-a+1))+a);}, dToR = function(degrees){ return degrees * (Math.PI / 180); }, circle = { x: (cw / 2) + 5, y: (ch / 2) + 22, radius: 90, speed: 2, rotation: 0, angleStart: 270, angleEnd: 90, hue: 220, thickness: 18, blur: 25 }, particles = [], particleMax = 100, updateCircle = function(){ if(circle.rotation < 360){ circle.rotation += circle.speed; } else { circle.rotation = 0; } }, renderCircle = function(){ ctx.save(); ctx.translate(circle.x, circle.y); ctx.rotate(dToR(circle.rotation)); ctx.beginPath(); ctx.arc(0, 0, circle.radius, dToR(circle.angleStart), dToR(circle.angleEnd), true); ctx.lineWidth = circle.thickness; ctx.strokeStyle = gradient1; ctx.stroke(); ctx.restore(); }, renderCircleBorder = function(){ ctx.save(); ctx.translate(circle.x, circle.y); ctx.rotate(dToR(circle.rotation)); ctx.beginPath(); ctx.arc(0, 0, circle.radius + (circle.thickness/2), dToR(circle.angleStart), dToR(circle.angleEnd), true); ctx.lineWidth = 2; ctx.strokeStyle = gradient2; ctx.stroke(); ctx.restore(); }, renderCircleFlare = function(){ ctx.save(); ctx.translate(circle.x, circle.y); ctx.rotate(dToR(circle.rotation+185)); ctx.scale(1,1); ctx.beginPath(); ctx.arc(0, circle.radius, 30, 0, Math.PI *2, false); ctx.closePath(); var gradient3 = ctx.createRadialGradient(0, circle.radius, 0, 0, circle.radius, 30); gradient3.addColorStop(0, 'hsla(330, 50%, 50%, .35)'); gradient3.addColorStop(1, 'hsla(330, 50%, 50%, 0)'); ctx.fillStyle = gradient3; ctx.fill(); ctx.restore(); }, renderCircleFlare2 = function(){ ctx.save(); ctx.translate(circle.x, circle.y); ctx.rotate(dToR(circle.rotation+165)); ctx.scale(1.5,1); ctx.beginPath(); ctx.arc(0, circle.radius, 25, 0, Math.PI *2, false); ctx.closePath(); var gradient4 = ctx.createRadialGradient(0, circle.radius, 0, 0, circle.radius, 25); gradient4.addColorStop(0, 'hsla(30, 100%, 50%, .2)'); gradient4.addColorStop(1, 'hsla(30, 100%, 50%, 0)'); ctx.fillStyle = gradient4; ctx.fill(); ctx.restore(); }, createParticles = function(){ if(particles.length < particleMax){ particles.push({ x: (circle.x + circle.radius * Math.cos(dToR(circle.rotation-85))) + (rand(0, circle.thickness*2) - circle.thickness), y: (circle.y + circle.radius * Math.sin(dToR(circle.rotation-85))) + (rand(0, circle.thickness*2) - circle.thickness), vx: (rand(0, 100)-50)/1000, vy: (rand(0, 100)-50)/1000, radius: rand(1, 6)/2, alpha: rand(10, 20)/100 }); } }, updateParticles = function(){ var i = particles.length; while(i--){ var p = particles[i]; p.vx += (rand(0, 100)-50)/750; p.vy += (rand(0, 100)-50)/750; p.x += p.vx; p.y += p.vy; p.alpha -= .01; if(p.alpha < .02){ particles.splice(i, 1) } } }, renderParticles = function(){ var i = particles.length; while(i--){ var p = particles[i]; ctx.beginPath(); ctx.fillRect(p.x, p.y, p.radius, p.radius); ctx.closePath(); ctx.fillStyle = 'hsla(0, 0%, 100%, '+p.alpha+')'; } }, clear = function(){ ctx.globalCompositeOperation = 'destination-out'; ctx.fillStyle = 'rgba(0, 0, 0, .1)'; ctx.fillRect(0, 0, cw, ch); ctx.globalCompositeOperation = 'lighter'; } loop = function(){ clear(); updateCircle(); renderCircle(); renderCircleBorder(); renderCircleFlare(); renderCircleFlare2(); createParticles(); updateParticles(); renderParticles(); } /* Append Canvas */ //document.body.appendChild(c); /* Set Constant Properties */ ctx.shadowBlur = circle.blur; ctx.shadowColor = 'hsla('+circle.hue+', 80%, 60%, 1)'; ctx.lineCap = 'round' var gradient1 = ctx.createLinearGradient(0, -circle.radius, 0, circle.radius); gradient1.addColorStop(0, 'hsla('+circle.hue+', 60%, 50%, .25)'); gradient1.addColorStop(1, 'hsla('+circle.hue+', 60%, 50%, 0)'); var gradient2 = ctx.createLinearGradient(0, -circle.radius, 0, circle.radius); gradient2.addColorStop(0, 'hsla('+circle.hue+', 100%, 50%, 0)'); gradient2.addColorStop(.1, 'hsla('+circle.hue+', 100%, 100%, .7)'); gradient2.addColorStop(1, 'hsla('+circle.hue+', 100%, 50%, 0)'); /* Loop It, Loop It Good */ setInterval(loop, 16); </script> </body> </html> -
Related recommendations:
Canvas Bessel formula derivation and objects following complex curve trajectories
Two JS methods to realize the parabolic trajectory motion of the ball
Realization of circular motion trajectory in css3 animation
The above is the detailed content of CSS and JS realize the beautiful starry sky trajectory movement effect. For more information, please follow other related articles on the PHP Chinese website!

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
