Class: RubySketch::Processing
- Inherits:
-
Object
- Object
- RubySketch::Processing
- Extended by:
- Starter
- Includes:
- Math
- Defined in:
- lib/rubysketch/processing.rb
Overview
Processing compatible API
Defined Under Namespace
Classes: Font, Image, TextBounds
Constant Summary collapse
- HALF_PI =
PI / 2
- QUARTER_PI =
PI / 4
- TWO_PI =
PI * 2
- TAU =
PI * 2
- RGB =
RGB mode for colorMode() function.
:RGB- HSB =
HSB mode for colorMode() function.
:HSB- RADIANS =
Radian mode for angleMode() function.
:RADIANS- DEGREES =
Degree mode for angleMode() function.
:DEGREES- CORNER =
:CORNER- CORNERS =
:CORNERS- CENTER =
:CENTER- RADIUS =
:RADIUS
Instance Method Summary collapse
-
#abs(value) ⇒ Numeric
Returns the absolute number of the value.
-
#angleMode(mode) ⇒ nil
Sets angle mode.
-
#arc(a, b, c, d, start, stop) ⇒ nil
Draws an arc.
-
#background(*args) ⇒ nil
Clears screen.
-
#ceil(value) ⇒ Numeric
Returns the closest integer number greater than or equal to the value.
-
#circle(x, y, extent) ⇒ nil
Draws a circle.
-
#colorMode(mode, *maxes) ⇒ nil
Sets color mode and max color values.
-
#constrain(value, min, max) ⇒ Numeric
Constrains the number between min..max.
-
#degrees(radian) ⇒ Numeric
Converts radian to degree.
-
#displayDensity ⇒ Numeric
Returns pixel density.
-
#dist(*args) ⇒ Numeric
Returns distance between 2 points.
-
#draw(&block) ⇒ Object
Define draw block.
-
#ellipse(a, b, c, d) ⇒ nil
Draws an ellipse.
-
#ellipseMode(mode) ⇒ nil
Sets ellipse mode.
-
#fill(*args) ⇒ nil
Sets fill color.
-
#floor(value) ⇒ Numeric
Returns the closest integer number less than or equal to the value.
-
#frameCount ⇒ Integer
Returns number of frames since program started.
-
#frameRate ⇒ Float
Returns number of frames per second.
- #height ⇒ Object
-
#image(img, x, y, w = nil, h = nil) ⇒ nil
Draws an image.
- #key(&block) ⇒ Object
-
#lerp(start, stop, amount) ⇒ Numeric
Returns the interpolated number between range start..stop.
-
#line(x1, y1, x2, y2) ⇒ nil
Draws a line.
-
#loadImage(filename, extension = nil) ⇒ Image
Loads image.
-
#mag(*args) ⇒ Numeric
Returns the magnitude (or length) of a vector.
-
#map(value, start1, stop1, start2, stop2) ⇒ Numeric
Maps a number from range start1..stop1 to range start2..stop2.
-
#max(*args) ⇒ Numeric
Returns maximum value.
-
#min(*args) ⇒ Numeric
Returns minimum value.
- #mouseDragged(&block) ⇒ Object
- #mouseMoved(&block) ⇒ Object
- #mousePressed(&block) ⇒ Object
- #mouseReleased(&block) ⇒ Object
-
#mouseX ⇒ Numeric
Returns mouse x position.
-
#mouseY ⇒ Numeric
Returns mouse y position.
-
#noFill ⇒ nil
Disables filling.
-
#noise(x, y = 0, z = 0) ⇒ Numeric
Returns the perlin noise value.
-
#norm(value, start, stop) ⇒ Numeric
Normalize the value from range start..stop into 0..1.
-
#noStroke ⇒ nil
Disables drawing stroke.
-
#pmouseX ⇒ Numeric
Returns mouse x position in previous frame.
-
#pmouseY ⇒ Numeric
Returns mouse y position in previous frame.
-
#point(x, y) ⇒ nil
Draws a point.
-
#pop ⇒ nil
Restore styles and transformations from stack.
-
#popMatrix ⇒ nil
Pops the current transformation matrix from stack.
-
#popStyle ⇒ nil
Restore style values from the style stack.
-
#pow(value, exponent) ⇒ Numeric
Returns value raised to the power of exponent.
-
#push ⇒ nil
Save current styles and transformations to stack.
-
#pushMatrix ⇒ nil
Pushes the current transformation matrix to stack.
-
#pushStyle ⇒ nil
Save current style values to the style stack.
-
#quad(x1, y1, x2, y2, x3, y3, x4, y4) ⇒ nil
Draws a quad.
-
#radians(degree) ⇒ Numeric
Converts degree to radian.
-
#rect(a, b, c, d, *args) ⇒ nil
Draws a rectangle.
-
#rectMode(mode) ⇒ nil
Sets rect mode.
-
#resetMatrix ⇒ nil
Reset current transformation matrix with identity matrix.
-
#rotate(angle) ⇒ nil
Applies rotation matrix to current transformation matrix.
-
#round(value) ⇒ Numeric
Returns the closest integer number.
-
#scale(x, y) ⇒ nil
Applies scale matrix to current transformation matrix.
-
#setup(&block) ⇒ Object
Define setup block.
-
#sq(value) ⇒ Numeric
Returns squared value.
-
#square(x, y, extent) ⇒ nil
Draws a square.
-
#stroke(*args) ⇒ nil
Sets stroke color.
-
#strokeWeight(weight) ⇒ nil
Sets stroke weight.
-
#text(str, x, y) ⇒ nil
Draws a text.
-
#textFont(name = nil, size = nil) ⇒ Font
Sets font.
-
#textSize(size) ⇒ nil
Sets text size.
-
#translate(x, y) ⇒ nil
Applies translation matrix to current transformation matrix.
-
#triangle(x1, y1, x2, y2, x3, y3) ⇒ nil
Draws a triangle.
- #width ⇒ Object
- #windowHeight ⇒ Object
- #windowWidth ⇒ Object
Methods included from Starter
Instance Method Details
#abs(value) ⇒ Numeric
Returns the absolute number of the value.
82 83 84 |
# File 'lib/rubysketch/processing.rb', line 82 def abs (value) value.abs end |
#angleMode(mode) ⇒ nil
Sets angle mode.
536 537 538 539 540 541 542 543 |
# File 'lib/rubysketch/processing.rb', line 536 def angleMode (mode) @angleScale__ = case mode when RADIANS then RAD2DEG__ when DEGREES then 1.0 else raise ArgumentError, "invalid angle mode: #{mode}" end nil end |
#arc(a, b, c, d, start, stop) ⇒ nil
Draws an arc.
814 815 816 817 818 819 820 |
# File 'lib/rubysketch/processing.rb', line 814 def arc (a, b, c, d, start, stop) x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d start = toAngle__ start stop = toAngle__ stop @painter__.ellipse x, y, w, h, from: start, to: stop nil end |
#background(str) ⇒ nil #background(str, alpha) ⇒ nil #background(gray) ⇒ nil #background(gray, alpha) ⇒ nil #background(r, g, b) ⇒ nil #background(r, g, b, alpha) ⇒ nil
Clears screen.
609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/rubysketch/processing.rb', line 609 def background (*args) rgba = toRGBA__ *args if rgba[3] == 1 @painter__.background *rgba else @painter__.push fill: rgba, stroke: nil do |_| @painter__.rect 0, 0, width, height end end nil end |
#ceil(value) ⇒ Numeric
Returns the closest integer number greater than or equal to the value.
92 93 94 |
# File 'lib/rubysketch/processing.rb', line 92 def ceil (value) value.ceil end |
#circle(x, y, extent) ⇒ nil
Draws a circle.
799 800 801 |
# File 'lib/rubysketch/processing.rb', line 799 def circle (x, y, extent) ellipse x, y, extent, extent end |
#colorMode(mode) ⇒ nil #colorMode(mode, max) ⇒ nil #colorMode(mode, max1, max2, max3) ⇒ nil #colorMode(mode, max1, max2, max3, maxA) ⇒ nil
Sets color mode and max color values.
489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/rubysketch/processing.rb', line 489 def colorMode (mode, *maxes) raise ArgumentError, "invalid color mode: #{mode}" unless [RGB, HSB].include?(mode) raise ArgumentError unless [0, 1, 3, 4].include?(maxes.size) @hsbColor__ = mode.upcase == HSB case maxes.size when 1 then @colorMaxes__ = [maxes.first.to_f] * 4 when 3, 4 then @colorMaxes__[0...maxes.size] = maxes.map &:to_f end nil end |
#constrain(value, min, max) ⇒ Numeric
Constrains the number between min..max.
265 266 267 |
# File 'lib/rubysketch/processing.rb', line 265 def constrain (value, min, max) value < min ? min : (value > max ? max : value) end |
#degrees(radian) ⇒ Numeric
Converts radian to degree.
285 286 287 |
# File 'lib/rubysketch/processing.rb', line 285 def degrees (radian) radian * RAD2DEG__ end |
#displayDensity ⇒ Numeric
Returns pixel density
437 438 439 |
# File 'lib/rubysketch/processing.rb', line 437 def displayDensity () @painter__.pixel_density end |
#dist(x1, y1, x2, y2) ⇒ Numeric #dist(x1, y1, z1, x2, y2, z2) ⇒ Numeric
Returns distance between 2 points.
171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/rubysketch/processing.rb', line 171 def dist (*args) case args.size when 4 x1, y1, x2, y2 = *args xx, yy = x2 - x1, y2 - y1 sqrt xx * xx + yy * yy when 3 x1, y1, z1, x2, y2, z2 = *args xx, yy, zz = x2 - x1, y2 - y1, z2 - z1 sqrt xx * xx + yy * yy + zz * zz else raise ArgumentError end end |
#draw(&block) ⇒ Object
Define draw block.
315 316 317 318 |
# File 'lib/rubysketch/processing.rb', line 315 def draw (&block) @drawBlock__ = block if block nil end |
#ellipse(a, b, c, d) ⇒ nil
Draws an ellipse.
785 786 787 788 789 |
# File 'lib/rubysketch/processing.rb', line 785 def ellipse (a, b, c, d) x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d @painter__.ellipse x, y, w, h nil end |
#ellipseMode(mode) ⇒ nil
Sets ellipse mode. Default is CENTER.
CORNER -> ellipse(left, top, width, height) CORNERS -> ellipse(left, top, right, bottom) CENTER -> ellipse(center_x, center_y, width, height) RADIUS -> ellipse(center_x, center_y, radius_h, radius_v)
576 577 578 |
# File 'lib/rubysketch/processing.rb', line 576 def ellipseMode (mode) @ellipseMode__ = mode end |
#fill(rgb) ⇒ nil #fill(rgb, alpha) ⇒ nil #fill(gray) ⇒ nil #fill(gray, alpha) ⇒ nil #fill(r, g, b) ⇒ nil #fill(r, g, b, alpha) ⇒ nil
Sets fill color.
639 640 641 642 |
# File 'lib/rubysketch/processing.rb', line 639 def fill (*args) @painter__.fill(*toRGBA__(*args)) nil end |
#floor(value) ⇒ Numeric
Returns the closest integer number less than or equal to the value.
102 103 104 |
# File 'lib/rubysketch/processing.rb', line 102 def floor (value) value.floor end |
#frameCount ⇒ Integer
Returns number of frames since program started.
421 422 423 |
# File 'lib/rubysketch/processing.rb', line 421 def frameCount () @frameCount__ end |
#frameRate ⇒ Float
Returns number of frames per second.
429 430 431 |
# File 'lib/rubysketch/processing.rb', line 429 def frameRate () @window__.event.fps end |
#height ⇒ Object
405 406 407 |
# File 'lib/rubysketch/processing.rb', line 405 def height () @window__.canvas.height end |
#image(img, x, y) ⇒ nil #image(img, x, y, w, h) ⇒ nil
Draws an image.
897 898 899 900 901 902 |
# File 'lib/rubysketch/processing.rb', line 897 def image (img, x, y, w = nil, h = nil) w ||= img.width h ||= img.height @painter__.image img.internal, x, y, w, h nil end |
#key(&block) ⇒ Object
320 321 322 323 |
# File 'lib/rubysketch/processing.rb', line 320 def key (&block) @window__.key = block nil end |
#lerp(start, stop, amount) ⇒ Numeric
Returns the interpolated number between range start..stop.
205 206 207 |
# File 'lib/rubysketch/processing.rb', line 205 def lerp (start, stop, amount) start + (stop - start) * amount end |
#line(x1, y1, x2, y2) ⇒ nil
Draws a line.
742 743 744 745 |
# File 'lib/rubysketch/processing.rb', line 742 def line (x1, y1, x2, y2) @painter__.line x1, y1, x2, y2 nil end |
#loadImage(filename, extension = nil) ⇒ Image
Loads image.
1049 1050 1051 1052 |
# File 'lib/rubysketch/processing.rb', line 1049 def loadImage (filename, extension = nil) filename = getImage__ filename, extension if filename =~ %r|^https?://| Image.new Rays::Image.load filename end |
#mag(x, y) ⇒ Numeric #mag(x, y, z) ⇒ Numeric
Returns the magnitude (or length) of a vector.
148 149 150 151 152 153 154 155 |
# File 'lib/rubysketch/processing.rb', line 148 def mag (*args) x, y, z = *args case args.size when 2 then sqrt x * x + y * y when 3 then sqrt x * x + y * y + z * z else raise ArgumentError end end |
#map(value, start1, stop1, start2, stop2) ⇒ Numeric
Maps a number from range start1..stop1 to range start2..stop2.
219 220 221 |
# File 'lib/rubysketch/processing.rb', line 219 def map (value, start1, stop1, start2, stop2) lerp start2, stop2, norm(value, start1, stop1) end |
#max(a, b) ⇒ Numeric #max(a, b, c) ⇒ Numeric #max(array) ⇒ Numeric
Returns maximum value.
253 254 255 |
# File 'lib/rubysketch/processing.rb', line 253 def max (*args) args.flatten.max end |
#min(a, b) ⇒ Numeric #min(a, b, c) ⇒ Numeric #min(array) ⇒ Numeric
Returns minimum value.
236 237 238 |
# File 'lib/rubysketch/processing.rb', line 236 def min (*args) args.flatten.min end |
#mouseDragged(&block) ⇒ Object
372 373 374 375 |
# File 'lib/rubysketch/processing.rb', line 372 def mouseDragged (&block) @mouseDraggedBlock__ = block if block nil end |
#mouseMoved(&block) ⇒ Object
359 360 361 362 |
# File 'lib/rubysketch/processing.rb', line 359 def mouseMoved (&block) @mouseMovedBlock__ = block if block nil end |
#mousePressed(&block) ⇒ Object
333 334 335 336 |
# File 'lib/rubysketch/processing.rb', line 333 def mousePressed (&block) @mousePressedBlock__ = block if block @mousePressed__ end |
#mouseReleased(&block) ⇒ Object
346 347 348 349 |
# File 'lib/rubysketch/processing.rb', line 346 def mouseReleased (&block) @mouseReleasedBlock__ = block if block nil end |
#mouseX ⇒ Numeric
Returns mouse x position
445 446 447 |
# File 'lib/rubysketch/processing.rb', line 445 def mouseX () @mouseX__ end |
#mouseY ⇒ Numeric
Returns mouse y position
453 454 455 |
# File 'lib/rubysketch/processing.rb', line 453 def mouseY () @mouseY__ end |
#noFill ⇒ nil
Disables filling.
682 683 684 685 |
# File 'lib/rubysketch/processing.rb', line 682 def noFill () @painter__.fill nil nil end |
#noise(x) ⇒ Numeric #noise(x, y) ⇒ Numeric #noise(x, y, z) ⇒ Numeric
Returns the perlin noise value.
1038 1039 1040 |
# File 'lib/rubysketch/processing.rb', line 1038 def noise (x, y = 0, z = 0) Rays.perlin(x, y, z) / 2.0 + 0.5 end |
#norm(value, start, stop) ⇒ Numeric
Normalize the value from range start..stop into 0..1.
193 194 195 |
# File 'lib/rubysketch/processing.rb', line 193 def norm (value, start, stop) (value.to_f - start.to_f) / (stop.to_f - start.to_f) end |
#noStroke ⇒ nil
Disables drawing stroke.
691 692 693 694 |
# File 'lib/rubysketch/processing.rb', line 691 def noStroke () @painter__.stroke nil nil end |
#pmouseX ⇒ Numeric
Returns mouse x position in previous frame
461 462 463 |
# File 'lib/rubysketch/processing.rb', line 461 def pmouseX () @mousePrevX__ end |
#pmouseY ⇒ Numeric
Returns mouse y position in previous frame
469 470 471 |
# File 'lib/rubysketch/processing.rb', line 469 def pmouseY () @mousePrevY__ end |
#point(x, y) ⇒ nil
Draws a point.
726 727 728 729 730 731 |
# File 'lib/rubysketch/processing.rb', line 726 def point (x, y) w = @painter__.stroke_width w = 1 if w == 0 @painter__.ellipse x - (w / 2.0), y - (w / 2.0), w, w nil end |
#pop ⇒ nil
Restore styles and transformations from stack.
1021 1022 1023 1024 |
# File 'lib/rubysketch/processing.rb', line 1021 def pop () popMatrix popStyle end |
#popMatrix ⇒ nil
Pops the current transformation matrix from stack.
956 957 958 959 960 |
# File 'lib/rubysketch/processing.rb', line 956 def popMatrix () raise "matrix stack underflow" if @matrixStack__.empty? @painter__.matrix = @matrixStack__.pop nil end |
#popStyle ⇒ nil
Restore style values from the style stack.
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 |
# File 'lib/rubysketch/processing.rb', line 994 def popStyle () raise "style stack underflow" if @styleStack__.empty? @painter__.fill, @painter__.stroke, @painter__.stroke_width, @painter__.font, @hsbColor__, @colorMaxes__, @angleScale__, @rectMode__, @ellipseMode__ = @styleStack__.pop nil end |
#pow(value, exponent) ⇒ Numeric
Returns value raised to the power of exponent.
123 124 125 |
# File 'lib/rubysketch/processing.rb', line 123 def pow (value, exponent) value ** exponent end |
#push ⇒ nil
Save current styles and transformations to stack.
1012 1013 1014 1015 |
# File 'lib/rubysketch/processing.rb', line 1012 def push () pushMatrix pushStyle end |
#pushMatrix ⇒ nil
Pushes the current transformation matrix to stack.
947 948 949 950 |
# File 'lib/rubysketch/processing.rb', line 947 def pushMatrix () @matrixStack__.push @painter__.matrix nil end |
#pushStyle ⇒ nil
Save current style values to the style stack.
975 976 977 978 979 980 981 982 983 984 985 986 987 988 |
# File 'lib/rubysketch/processing.rb', line 975 def pushStyle () @styleStack__.push [ @painter__.fill, @painter__.stroke, @painter__.stroke_width, @painter__.font, @hsbColor__, @colorMaxes__, @angleScale__, @rectMode__, @ellipseMode__ ] nil end |
#quad(x1, y1, x2, y2, x3, y3, x4, y4) ⇒ nil
Draws a quad.
863 864 865 866 |
# File 'lib/rubysketch/processing.rb', line 863 def quad (x1, y1, x2, y2, x3, y3, x4, y4) @painter__.line x1, y1, x2, y2, x3, y3, x4, y4, loop: true nil end |
#radians(degree) ⇒ Numeric
Converts degree to radian.
275 276 277 |
# File 'lib/rubysketch/processing.rb', line 275 def radians (degree) degree * DEG2RAD__ end |
#rect(a, b, c, d) ⇒ nil #rect(a, b, c, d, r) ⇒ nil #rect(a, b, c, d, tl, tr, br, bl) ⇒ nil
Draws a rectangle.
765 766 767 768 769 770 771 772 773 774 |
# File 'lib/rubysketch/processing.rb', line 765 def rect (a, b, c, d, *args) x, y, w, h = toXYWH__ @rectMode__, a, b, c, d case args.size when 0 then @painter__.rect x, y, w, h when 1 then @painter__.rect x, y, w, h, round: args[0] when 4 then @painter__.rect x, y, w, h, lt: args[0], rt: args[1], rb: args[2], lb: args[3] else raise ArgumentError # ToDo: refine error message end nil end |
#rectMode(mode) ⇒ nil
Sets rect mode. Default is CORNER.
CORNER -> rect(left, top, width, height) CORNERS -> rect(left, top, right, bottom) CENTER -> rect(center_x, center_y, width, height) RADIUS -> rect(center_x, center_y, radius_h, radius_v)
561 562 563 |
# File 'lib/rubysketch/processing.rb', line 561 def rectMode (mode) @rectMode__ = mode end |
#resetMatrix ⇒ nil
Reset current transformation matrix with identity matrix.
966 967 968 969 |
# File 'lib/rubysketch/processing.rb', line 966 def resetMatrix () @painter__.matrix = 1 nil end |
#rotate(angle) ⇒ nil
Applies rotation matrix to current transformation matrix.
938 939 940 941 |
# File 'lib/rubysketch/processing.rb', line 938 def rotate (angle) @painter__.rotate toAngle__ angle nil end |
#round(value) ⇒ Numeric
Returns the closest integer number.
112 113 114 |
# File 'lib/rubysketch/processing.rb', line 112 def round (value) value.round end |
#scale(s) ⇒ nil #scale(x, y) ⇒ nil
Applies scale matrix to current transformation matrix.
927 928 929 930 |
# File 'lib/rubysketch/processing.rb', line 927 def scale (x, y) @painter__.scale x, y nil end |
#setup(&block) ⇒ Object
Define setup block.
291 292 293 294 |
# File 'lib/rubysketch/processing.rb', line 291 def setup (&block) @window__.setup = block nil end |
#sq(value) ⇒ Numeric
Returns squared value.
133 134 135 |
# File 'lib/rubysketch/processing.rb', line 133 def sq (value) value * value end |
#square(x, y, extent) ⇒ nil
Draws a square.
830 831 832 |
# File 'lib/rubysketch/processing.rb', line 830 def square (x, y, extent) rect x, y, extent, extent end |
#stroke(rgb) ⇒ nil #stroke(rgb, alpha) ⇒ nil #stroke(gray) ⇒ nil #stroke(gray, alpha) ⇒ nil #stroke(r, g, b) ⇒ nil #stroke(r, g, b, alpha) ⇒ nil
Sets stroke color.
662 663 664 665 |
# File 'lib/rubysketch/processing.rb', line 662 def stroke (*args) @painter__.stroke(*toRGBA__(*args)) nil end |
#strokeWeight(weight) ⇒ nil
Sets stroke weight.
673 674 675 676 |
# File 'lib/rubysketch/processing.rb', line 673 def strokeWeight (weight) @painter__.stroke_width weight nil end |
#text(str) ⇒ nil #text(str, x, y) ⇒ nil
Draws a text.
879 880 881 882 |
# File 'lib/rubysketch/processing.rb', line 879 def text (str, x, y) @painter__.text str, x, y nil end |
#textFont(name = nil, size = nil) ⇒ Font
Sets font.
703 704 705 706 |
# File 'lib/rubysketch/processing.rb', line 703 def textFont (name = nil, size = nil) @painter__.font name, size if name || size Font.new @painter__.font end |
#textSize(size) ⇒ nil
Sets text size.
714 715 716 717 |
# File 'lib/rubysketch/processing.rb', line 714 def textSize (size) @painter__.font @painter__.font.name, size nil end |
#translate(x, y) ⇒ nil
Applies translation matrix to current transformation matrix.
911 912 913 914 |
# File 'lib/rubysketch/processing.rb', line 911 def translate (x, y) @painter__.translate x, y nil end |
#triangle(x1, y1, x2, y2, x3, y3) ⇒ nil
Draws a triangle.
845 846 847 848 |
# File 'lib/rubysketch/processing.rb', line 845 def triangle (x1, y1, x2, y2, x3, y3) @painter__.line x1, y1, x2, y2, x3, y3, loop: true nil end |
#width ⇒ Object
401 402 403 |
# File 'lib/rubysketch/processing.rb', line 401 def width () @window__.canvas.width end |
#windowHeight ⇒ Object
413 414 415 |
# File 'lib/rubysketch/processing.rb', line 413 def windowHeight () @window__.height end |
#windowWidth ⇒ Object
409 410 411 |
# File 'lib/rubysketch/processing.rb', line 409 def windowWidth () @window__.width end |