Data2viz
.play
Explore
Sketches
Create
Sketch
Documentation
Let's start
Core mechanisms
Colors and gradients
Scales
Chromatic Scales
Forces
Random numbers
Fork
View
Editor
Details
Sign up
Log in
Starfield
import io.data2viz.color.* import io.data2viz.geom.* import io.data2viz.math.* import io.data2viz.viz.* import io.data2viz.random.* import kotlin.math.sqrt private val vizWidth = 600.0 private val vizHeight = 600.0 private var center = Point(vizWidth / 2, vizHeight / 2) private val randomAngle = RandomDistribution.uniform(.02, 2 * PI) private val randomDistance = RandomDistribution.uniform(1.1, 4.0) private val randomDepth = RandomDistribution.uniform(0.4, 1.0) private val randomRadius = RandomDistribution.exponential() private var frame = .0 data class Star(val visual: CircleNode, var radius: Double, var angle: Angle, var distance: Double, val depth: Double) private var totalStars = 2000 private lateinit var galaxy: GroupNode private lateinit var stars: List<Star> fun main() { viz { size = size(vizWidth, vizHeight) rect { width = vizWidth height = vizHeight fill = Colors.Web.black } galaxy = group { stars = (0 until totalStars).map { val angle = randomAngle().rad val starDis = randomDistance() * randomDistance() * randomDistance() val starDepth = randomDepth() val starPos = Point(starDis*angle.cos, starDis*angle.sin) var starRadius = 0.1 + (randomRadius() * sqrt(starDepth) / 1.3) Star( circle { x = starPos.x y = starPos.y radius = starRadius fill = Colors.Web.white.withAlpha((starDepth*120).pct) }, starRadius, angle, starDis, starDepth ) } } animation { frame += 0.05 galaxy.apply { transform { translate(center.x + (15 * frame.rad.sin), center.y + (10 * (frame / 1.3).rad.cos)) } } stars.forEach { star -> star.distance *= 1.0 + (sqrt(star.distance) * star.depth / 200.0) star.angle += (star.depth / 1.5).deg val starX = star.distance * star.angle.cos val starY = star.distance * star.angle.sin val alpha = star.depth * star.distance star.visual.x = starX star.visual.y = starY if (alpha <= 102) star.visual.fill = Colors.Web.white.withAlpha(alpha.pct) star.visual.radius = star.radius * (sqrt(star.distance) / 10.0) if (starX < -center.x || starX > center.x || starY < -center.y || starY > center.y) { star.distance = randomDistance() * randomDistance() star.radius = 0.1 + (randomRadius() * sqrt(star.depth) / 1.3) star.angle = randomAngle().rad } } } }.bindRendererOnNewCanvas() }
Sketch created by
pierre
1694
0
0
Animation
Fun
Description
Just a simple star field.
comments
Submit
Lovers
comments