|
| 1 | +#!/usr/bin/env jruby -v -W2 |
| 2 | +# frozen_string_literal: true |
| 3 | +require 'propane' |
| 4 | + |
| 5 | +# The Colorist class |
| 6 | +class OscillatorSketch < Propane::App |
| 7 | + load_library :hype |
| 8 | + HYPE = %w[H HCanvas HDrawablePool HRect].freeze |
| 9 | + EXTENDED = %w[ |
| 10 | + behavior.HTimer behavior.HOscillator colorist.HPixelColorist |
| 11 | + ].freeze |
| 12 | + hype_format = 'hype.%s' |
| 13 | + hype_extended_format = 'hype.extended.%s' |
| 14 | + HYPE.each { |klass| java_import format(hype_format, klass) } |
| 15 | + EXTENDED.each { |klass| java_import format(hype_extended_format, klass) } |
| 16 | + |
| 17 | + attr_reader :colors, :pool, :xo, :ao, :wo, :ro, :zo |
| 18 | + |
| 19 | + def setup |
| 20 | + sketch_title 'Oscillator' |
| 21 | + H.init(self) |
| 22 | + H.background(color(0xff000000)) |
| 23 | + H.use3D(true) |
| 24 | + @colors = HPixelColorist.new(data_path('gradient.jpg')) |
| 25 | + canvas = HCanvas.new(P3D).autoClear(true) |
| 26 | + H.add(canvas) |
| 27 | + @pool = HDrawablePool.new(1_000) |
| 28 | + pool.autoParent(canvas) |
| 29 | + .add(HRect.new.rounding(10)) |
| 30 | + .on_create do |obj| |
| 31 | + i = pool.current_index |
| 32 | + obj.no_stroke |
| 33 | + .size(rand(40..80), rand(60..80)) |
| 34 | + .loc(rand(width), rand(height)) |
| 35 | + .anchorAt(H::CENTER) |
| 36 | + @xo = HOscillator.new |
| 37 | + .target(obj) |
| 38 | + .property(H::X) |
| 39 | + .relative_val(obj.x) |
| 40 | + .range(rand(-10..-5), rand(5..10)) |
| 41 | + .speed(rand(0.005..0.2)) |
| 42 | + .freq(10) |
| 43 | + .current_step(i) |
| 44 | + @ao = HOscillator.new |
| 45 | + .target(obj) |
| 46 | + .property(H::ALPHA) |
| 47 | + .range(50, 255) |
| 48 | + .speed(rand(0.3..0.9)) |
| 49 | + .freq(5) |
| 50 | + .current_step(i) |
| 51 | + @wo = HOscillator.new |
| 52 | + .target(obj) |
| 53 | + .property(H::WIDTH) |
| 54 | + .range(-obj.width, obj.width) |
| 55 | + .speed(rand(0.05..0.2)) |
| 56 | + .freq(10) |
| 57 | + .current_step(i) |
| 58 | + @ro = HOscillator.new |
| 59 | + .target(obj) |
| 60 | + .property(H::ROTATION) |
| 61 | + .range(-180, 180) |
| 62 | + .speed(rand(0.005..0.05)) |
| 63 | + .freq(10) |
| 64 | + .current_step(i) |
| 65 | + @zo = HOscillator.new |
| 66 | + .target(obj) |
| 67 | + .property(H::Z) |
| 68 | + .range(-400, 400) |
| 69 | + .speed(rand(0.005..0.01)) |
| 70 | + .freq(15) |
| 71 | + .current_step(i * 5) |
| 72 | + end |
| 73 | + .onRequest do |obj| |
| 74 | + obj.scale(1).alpha(0).loc(rand(width), rand(height), -rand(200)) |
| 75 | + xo.register |
| 76 | + ao.register |
| 77 | + wo.register |
| 78 | + ro.register |
| 79 | + zo.register |
| 80 | + end.onRelease do |
| 81 | + xo.unregister |
| 82 | + ao.unregister |
| 83 | + wo.unregister |
| 84 | + ro.unregister |
| 85 | + zo.unregister |
| 86 | + end |
| 87 | + HTimer.new(50) |
| 88 | + .callback do |
| 89 | + pool.request |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def draw |
| 94 | + pool.each do |d| |
| 95 | + d.loc(d.x, d.y - rand(0.25..1), d.z) |
| 96 | + d.no_stroke |
| 97 | + d.fill(colors.get_color(d.x, d.y)) |
| 98 | + # if the z axis hits this range, change fill to light yellow |
| 99 | + d.fill(color(0xffFFFFCC)) if d.z > -10 && d.z < 10 |
| 100 | + pool.release(d) if d.y < -40 |
| 101 | + end |
| 102 | + H.draw_stage |
| 103 | + end |
| 104 | + |
| 105 | + def settings |
| 106 | + size 640, 640, P3D |
| 107 | + end |
| 108 | +end |
| 109 | + |
| 110 | +OscillatorSketch.new |
0 commit comments