require 'ruby2d' set width: 800, height: 600, title: "My Ruby Sketch", background: '#111' on :key_down do |e| clear if e.key == 'r' end
(0...800).each do |x| (0...600).each do |y| # Mandelbrot or pattern color = ((x ^ y) % 256) png[x, y] = ChunkyPNG::Color.rgb(color, color, color) end end
show require 'ruby2d' set width: 800, height: 600 ruby sketch
Run: ruby test.rb a) Drawing primitives (Ruby2D) require 'ruby2d' set width: 800, height: 600, background: 'black' Shapes Circle.new(x: 200, y: 200, radius: 80, sectors: 32, color: 'red') Rectangle.new(x: 400, y: 100, width: 150, height: 150, color: 'blue') Line.new(x: 0, y: 500, x2: 800, y2: 400, width: 5, color: 'lime') Triangle.new(x1: 600, y1: 400, x2: 700, y2: 500, x3: 500, y3: 500, color: 'yellow')
update do particles.each do |p| angle = noise[p.x * 0.005, p.y * 0.005, Time.now.to_f * 0.1] * Math::PI * 2 p.x += Math.cos(angle) * 2 p.y += Math.sin(angle) * 2 p.x = 0 if p.x > 800 p.x = 800 if p.x < 0 p.y = 0 if p.y > 600 p.y = 600 if p.y < 0 Rectangle.new(x: p.x, y: p.y, width: 2, height: 2, color: 'white') end end require 'ruby2d' set width: 800, height: 600, title:
noise = RubyNoise::Perlin.new particles = []
on :key_down do |event| clear if event.key == 'c' end require 'ruby2d' set width: 800
def draw_circle(x, y, radius, depth) return if depth > 5 || radius < 2 Circle.new(x: x, y: y, radius: radius, color: "hsl(#depth * 60, 100%, 50%)") draw_circle(x + radius/2, y, radius/2, depth + 1) draw_circle(x - radius/2, y, radius/2, depth + 1) draw_circle(x, y + radius/2, radius/2, depth + 1) end