rubysketch 0.5.23 → 0.5.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db5c56e16498e5154331831f3bf4998b3b100f2ad3d3eb1f788ae47737565f30
4
- data.tar.gz: f727f500188243a395e6fb650b3abbfd490c2e9f1c57086e18f004dacc38e684
3
+ metadata.gz: 5538cde508d958c69978b0cdf51727303c3c509f96fae21a30b41cfabc99a9a8
4
+ data.tar.gz: 9b7fd39a1684f7fe49fbc98250c0315038419566443209fddce8701ff9683228
5
5
  SHA512:
6
- metadata.gz: cf517cd8f127348737c9bcdae738e450bf8115752e9e5d6cc02318c2d2735333ff8023c413a1e940639f025fa85630c3481b67d70169868e9cc1b9c301515ed8
7
- data.tar.gz: da8c7932841db2db82eede5d6bfbe4f0040e745b3d284ca58c041089b9cceff582b58800fd00d468a8efba62f62f6046623c900bb78dea8610d0aedbaca8aaa4
6
+ metadata.gz: f71cd9de84e98317c61ad25c013cf0d6ec2257cc884138984908ee6ecd505c7a87068798539b8491fcccad90e7a829dea8d62fb907483c46be215cea010cb074
7
+ data.tar.gz: 4758d91420fb1069ce87438ec0df5cfea7af382847f9375bc7869f48fce5ed3d21936d0af79c0c3b2a166121991f922a82944c5a89515e1b09f872fb8723b7cd
data/ChangeLog.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # rubysketch ChangeLog
2
2
 
3
3
 
4
+ ## [v0.5.24] - 2023-07-21
5
+
6
+ - Add animate(), animateValue(), and EASINGS
7
+ - Add setTimeout(), setInterval(), and clearTimer() (also aliased as clearTimeout, clearInterval)
8
+
9
+
4
10
  ## [v0.5.23] - 2023-07-11
5
11
 
6
12
  - Update dependencies
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.23
1
+ 0.5.24
@@ -2,11 +2,11 @@ require 'beeps'
2
2
  require 'processing/all'
3
3
 
4
4
  require 'rubysketch/extension'
5
- require 'rubysketch/window'
6
5
  require 'rubysketch/helper'
7
6
 
8
7
  require 'rubysketch/sprite'
9
8
  require 'rubysketch/sound'
9
+ require 'rubysketch/easings'
10
10
  require 'rubysketch/context'
11
11
 
12
12
 
@@ -9,7 +9,134 @@ module RubySketch
9
9
  # @private
10
10
  def initialize(window)
11
11
  super
12
+ @timers__, @nextTimerID__ = {}, 0
13
+
12
14
  @layer__ = window.add_overlay SpriteLayer.new
15
+
16
+ window.update_window = proc do
17
+ fireTimers__
18
+ Beeps.process_streams!
19
+ end
20
+ end
21
+
22
+ # Calls block after specified seconds
23
+ #
24
+ # @param [Numeric] seconds Time at which the block is called
25
+ # @param [Array] args Arguments passed to block
26
+ # @param [Object] id Timer object identifier
27
+ #
28
+ # @return [Object] Timer object identifier
29
+ #
30
+ def setTimeout(seconds = 0, *args, id: nextTimerID__, &block)
31
+ return unless block
32
+ setTimeout__ id, Time.now.to_f + seconds, args, &block
33
+ end
34
+
35
+ # Repeats block call at each interval
36
+ #
37
+ # @param [Numeric] seconds Each interval duration
38
+ # @param [Array] args Arguments passed to block
39
+ # @param [Object] id Timer object identifier
40
+ # @param [Boolean] now Wheather or not to call the block right now
41
+ #
42
+ # @return [Object] Timer object identifier
43
+ #
44
+ def setInterval(seconds = 0, *args, id: nextTimerID__, now: false, &block)
45
+ return unless block
46
+ time = Time.now.to_f
47
+ block.call *args if now
48
+ setInterval__ id, time, seconds, args, &block
49
+ end
50
+
51
+ # @private
52
+ def setTimeout__(id, time, args = [], &block)
53
+ @timers__[id] = [time, args, block]
54
+ id
55
+ end
56
+
57
+ # @private
58
+ def setInterval__(id, startTime, seconds, args = [], &block)
59
+ now, nextTime = Time.now.to_f, startTime + seconds
60
+ nextTime = now if nextTime < now
61
+ setTimeout__ id, nextTime do
62
+ block.call *args
63
+ setInterval__ id, nextTime, seconds, args, &block
64
+ end
65
+ end
66
+
67
+ # Stops timeout or interval timer by id
68
+ #
69
+ # @param [Object] id The identifier of timeout or interval timer to stop
70
+ #
71
+ # @return [nil] nil
72
+ #
73
+ def clearTimer(id)
74
+ @timers__.delete id
75
+ nil
76
+ end
77
+
78
+ alias clearTimeout clearTimer
79
+ alias clearInterval clearTimer
80
+
81
+ # @private
82
+ def nextTimerID__()
83
+ @nextTimerID__ += 1
84
+ @nextTimerID__
85
+ end
86
+
87
+ # @private
88
+ def fireTimers__()
89
+ now = Time.now.to_f
90
+ blocks = []
91
+ @timers__.delete_if do |_, (time, args, block)|
92
+ (now >= time).tap {|fire| blocks.push [block, args] if fire}
93
+ end
94
+ blocks.each {|block, args| block.call *args}
95
+ end
96
+
97
+ # Animate with easing functions
98
+ #
99
+ # @param [Numeric] seconds Animation duration
100
+ # @param [Object] id Timer object identifier
101
+ # @param [Symbol] easing Easing function name
102
+ #
103
+ # @return [Object] Timer object identifier
104
+ #
105
+ def animate(seconds, id: nextTimerID__, easing: :expoOut, &block)
106
+ fun = EASINGS[easing] or raise "'#{easing}' easing function not found"
107
+ start = Time.now.to_f
108
+ eachDrawBlock = lambda do
109
+ t = (Time.now.to_f - start) / seconds
110
+ if t >= 1.0
111
+ block.call fun.call(1.0), true
112
+ else
113
+ block.call fun.call(t), false
114
+ setTimeout 0, id: id, &eachDrawBlock
115
+ end
116
+ end
117
+ setTimeout 0, id: id, &eachDrawBlock
118
+ end
119
+
120
+ # Animate value with easing functions
121
+ #
122
+ # @param [Numeric] seconds Animation duration
123
+ # @param [Numeric, Vector] from Value at the beggining of the animation
124
+ # @param [Numeric, Vector] to Value at the end of the animation
125
+ # @param [Object] id Timer object identifier
126
+ # @param [Symbol] easing Easing function name
127
+ #
128
+ # @return [Object] Timer object identifier
129
+ #
130
+ def animateValue(seconds, from:, to:, id: nextTimerID__, easing: :expoOut, &block)
131
+ if from.is_a? Vector
132
+ animate seconds, id: id, easing: easing do |t, finished|
133
+ block.call Vector.lerp(from, to, t), finished
134
+ end
135
+ else
136
+ animate seconds, id: id, easing: easing do |t, finished|
137
+ block.call lerp(from, to, t), finished
138
+ end
139
+ end
13
140
  end
14
141
 
15
142
  # Creates a new sprite and add it to physics engine.
@@ -0,0 +1,105 @@
1
+ module RubySketch
2
+
3
+ pi = Math::PI
4
+ pi_div2 = pi / 2.0
5
+
6
+ c1 = 1.70158
7
+ c2 = c1 * 1.525
8
+ c3 = c1 + 1.0
9
+ c4 = pi * 2 / 3.0
10
+ c5 = pi * 2 / 4.5
11
+
12
+ n1 = 7.5625
13
+ d1 = 2.75
14
+
15
+ expoInOut = -> x {
16
+ x == 0 ? 0.0 :
17
+ x == 1 ? 1.0 :
18
+ x < 0.5 ?
19
+ 2.0 ** ( 20.0 * x - 10.0) / 2.0 :
20
+ (2.0 - 2.0 ** (-20.0 * x + 10.0)) / 2.0
21
+ }
22
+
23
+ circInOut = -> x {
24
+ x < 0.5 ?
25
+ (1.0 - Math.sqrt(1.0 - ( 2.0 * x) ** 2.0)) / 2.0 :
26
+ (1.0 + Math.sqrt(1.0 - (-2.0 * x + 2.0) ** 2.0)) / 2.0
27
+ }
28
+
29
+ backInOut = -> x {
30
+ x < 0.5 ?
31
+ ((2.0 * x) ** 2.0 * ((c2 + 1.0) * (x * 2.0) - c2)) / 2.0 :
32
+ ((2.0 * x - 2.0) ** 2.0 * ((c2 + 1.0) * (x * 2.0 - 2.0) + c2) + 2.0) / 2.0
33
+ }
34
+
35
+ elasticIn = -> x {
36
+ x == 0 ? 0.0 :
37
+ x == 1 ? 1.0 :
38
+ -(2 ** (10.0 * x - 10.0)) * Math.sin((x * 10.0 - 10.75) * c4)
39
+ }
40
+
41
+ elasticOut = -> x {
42
+ x == 0 ? 0.0 :
43
+ x == 1 ? 1.0 :
44
+ 2 ** (-10.0 * x) * Math.sin((x * 10.0 - 0.75) * c4) + 1.0
45
+ }
46
+
47
+ elasticInOut = -> x {
48
+ x == 0 ? 0.0 :
49
+ x == 1 ? 1.0 :
50
+ x < 0.5 ?
51
+ -(2 ** ( 20.0 * x - 10.0) * Math.sin((20.0 * x - 11.125) * c5)) / 2.0 :
52
+ (2 ** (-20.0 * x + 10.0) * Math.sin((20.0 * x - 11.125) * c5)) / 2.0 + 1.0
53
+ }
54
+
55
+ bounceOut = -> x {
56
+ x < 1.0 / d1 ? n1 * x ** 2 :
57
+ x < 2.0 / d1 ? n1 * (x -= 1.5 / d1) * x + 0.75 :
58
+ x < 2.5 / d1 ? n1 * (x -= 2.25 / d1) * x + 0.9375 :
59
+ n1 * (x -= 2.625 / d1) * x + 0.984375
60
+ }
61
+
62
+ bounceInOut = -> x {
63
+ x < 0.5 ?
64
+ (1.0 - bounceOut[1.0 - 2.0 * x]) / 2.0 :
65
+ (1.0 + bounceOut[2.0 * x - 1.0]) / 2.0
66
+ }
67
+
68
+ EASINGS = {
69
+ linear: -> x {x},
70
+
71
+ sineIn: -> x {1.0 - Math.cos(x * pi_div2)},
72
+ quadIn: -> x {x ** 2.0},
73
+ cubicIn: -> x {x ** 3.0},
74
+ quartIn: -> x {x ** 4.0},
75
+ quintIn: -> x {x ** 5.0},
76
+ expoIn: -> x {x == 0 ? 0.0 : 2.0 ** (10.0 * x - 10.0)},
77
+ circIn: -> x {1.0 - Math.sqrt(1.0 - x ** 2.0)},
78
+ backIn: -> x {c3 * x ** 3.0 - c1 * x ** 2.0},
79
+ elasticIn: elasticIn,
80
+ bounceIn: -> x {1.0 - bounceOut[1.0 - x]},
81
+
82
+ sineOut: -> x {Math.sin(x * pi_div2)},
83
+ quadOut: -> x {1.0 - (1.0 - x) ** 2.0},
84
+ cubicOut: -> x {1.0 - (1.0 - x) ** 3.0},
85
+ quartOut: -> x {1.0 - (1.0 - x) ** 4.0},
86
+ quintOut: -> x {1.0 - (1.0 - x) ** 5.0},
87
+ expoOut: -> x {x == 1 ? 1.0 : 1.0 - 2.0 ** (-10.0 * x)},
88
+ circOut: -> x {Math.sqrt(1.0 - (x - 1.0) ** 2.0)},
89
+ backOut: -> x {1.0 + c3 * (x - 1.0) ** 3.0 + c1 * (x - 1.0) ** 2.0},
90
+ elasticOut: elasticOut,
91
+ bounceOut: bounceOut,
92
+
93
+ sineInOut: -> x {-(Math.cos(pi * x) - 1.0) / 2.0},
94
+ quadInOut: -> x {x < 0.5 ? 2.0 * x ** 2 : 1.0 - (-2.0 * x + 2.0) ** 2.0 / 2.0},
95
+ cubicInOut: -> x {x < 0.5 ? 4.0 * x ** 3 : 1.0 - (-2.0 * x + 2.0) ** 3.0 / 2.0},
96
+ quartInOut: -> x {x < 0.5 ? 8.0 * x ** 4 : 1.0 - (-2.0 * x + 2.0) ** 4.0 / 2.0},
97
+ quintInOut: -> x {x < 0.5 ? 16.0 * x ** 5 : 1.0 - (-2.0 * x + 2.0) ** 5.0 / 2.0},
98
+ expoInOut: expoInOut,
99
+ circInOut: circInOut,
100
+ backInOut: backInOut,
101
+ elasticInOut: elasticInOut,
102
+ bounceInOut: bounceInOut
103
+ }
104
+
105
+ end# RubySketch
data/lib/rubysketch.rb CHANGED
@@ -4,7 +4,7 @@ require 'rubysketch/all'
4
4
  module RubySketch
5
5
  w = (ENV['WIDTH'] || 500).to_i
6
6
  h = (ENV['HEIGHT'] || 500).to_i
7
- WINDOW = RubySketch::Window.new(w, h) {start}
7
+ WINDOW = Processing::Window.new(w, h) {start}
8
8
  CONTEXT = RubySketch::Context.new WINDOW
9
9
 
10
10
  refine Object do
data/rubysketch.gemspec CHANGED
@@ -30,7 +30,7 @@ Gem::Specification.new do |s|
30
30
  s.add_runtime_dependency 'beeps', '~> 0.1.41'
31
31
  s.add_runtime_dependency 'rays', '~> 0.1.43'
32
32
  s.add_runtime_dependency 'reflexion', '~> 0.1.49'
33
- s.add_runtime_dependency 'processing', '~> 0.5.24'
33
+ s.add_runtime_dependency 'processing', '~> 0.5.25'
34
34
 
35
35
  s.add_development_dependency 'rake'
36
36
  s.add_development_dependency 'test-unit'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysketch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.23
4
+ version: 0.5.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-11 00:00:00.000000000 Z
11
+ date: 2023-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xot
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 0.5.24
89
+ version: 0.5.25
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 0.5.24
96
+ version: 0.5.25
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rake
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -161,11 +161,11 @@ files:
161
161
  - lib/rubysketch.rb
162
162
  - lib/rubysketch/all.rb
163
163
  - lib/rubysketch/context.rb
164
+ - lib/rubysketch/easings.rb
164
165
  - lib/rubysketch/extension.rb
165
166
  - lib/rubysketch/helper.rb
166
167
  - lib/rubysketch/sound.rb
167
168
  - lib/rubysketch/sprite.rb
168
- - lib/rubysketch/window.rb
169
169
  - pod.rake
170
170
  - rubysketch.gemspec
171
171
  - src/RubySketch.h
@@ -1,14 +0,0 @@
1
- module RubySketch
2
-
3
-
4
- # @private
5
- class Window < Processing::Window
6
-
7
- def on_update(e)
8
- Beeps.process_streams!
9
- end
10
-
11
- end# Window
12
-
13
-
14
- end# RubySketch