Skip to content

Instantly share code, notes, and snippets.

@djmetzle
Created June 19, 2019 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djmetzle/5cb1bf185239f5a0d5f9ee03d384d092 to your computer and use it in GitHub Desktop.
Save djmetzle/5cb1bf185239f5a0d5f9ee03d384d092 to your computer and use it in GitHub Desktop.
Procedurally generated background
require 'victor'
require 'rgb'
WIDTH=3840
HEIGHT=2160
prng = Random.new
RING_RAD = prng.rand(30..80)
ANGLE_SPREAD=Random.new.rand(3..10)
HUE_STEP = 0.5 / ( WIDTH / RING_RAD)
svg = Victor::SVG.new width: WIDTH, height: HEIGHT, style: { background: '#111' }
Seed = Struct.new :angle, :x, :y
start_hue = prng.rand(0.0..1.0)
color = RGB::Color.from_fractions(start_hue,1.0,0.7)
svg.build do
rect x: 0, y: 0, width: WIDTH, height: HEIGHT, fill: '#111'
css['.rings'] = {
stroke: color.to_rgb_hex,
stroke_width: 1,
"fill-opacity": 0.0
}
circle cx: WIDTH/2, cy: HEIGHT/2, r: 2, fill: '#FFF'
seeds = (1..3).map do |i|
prng.rand(0...360)
end.map do |angle|
rad = 2.0 * Math::PI * angle / 360.0
x = WIDTH/2 + RING_RAD * Math.cos(rad)
y = HEIGHT/2 + RING_RAD * Math.sin(rad)
Seed.new angle, x, y
end
seeds.each do |seed|
path d: ['M', WIDTH/2, HEIGHT/2, 'L', seed.x, seed.y ], stroke: color.to_rgb_hex, stroke_width: 1
end
hue = start_hue
RING_RAD.step([WIDTH,HEIGHT].max, RING_RAD) do |r|
hue = ( hue + HUE_STEP ) % 1.0
color = RGB::Color.from_fractions(hue,1.0,0.7)
circle cx: WIDTH/2, cy: HEIGHT/2, r: r, class: 'rings', stroke: color.to_rgb_hex
last_seeds = seeds
seeds = Array.new
last_seeds.each do |seed|
(1..(prng.rand(1..3))).map do |i|
prng.rand((seed.angle-ANGLE_SPREAD)..(seed.angle+ANGLE_SPREAD))
end.map do |angle|
rad = 2.0 * Math::PI * angle / 360.0
x = WIDTH/2 + r * Math.cos(rad)
y = HEIGHT/2 + r * Math.sin(rad)
Seed.new angle, x, y
end.each do |next_seed|
path d: ['M', seed.x, seed.y, 'L', next_seed.x, next_seed.y ], stroke: color.to_rgb_hex, stroke_width: 1
seeds << next_seed
end
end
seeds = seeds.uniq
end
end
svg.save 'tree'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment