Class: RubySketch::Processing::Context

Inherits:
Object
  • Object
show all
Includes:
GraphicsContext
Defined in:
lib/rubysketch/processing.rb

Overview

Processing context

Constant Summary collapse

Vector =
Processing::Vector
Capture =
Processing::Capture
Graphics =
Processing::Graphics

Constants included from GraphicsContext

GraphicsContext::ALT, GraphicsContext::BACKSPACE, GraphicsContext::BASELINE, GraphicsContext::BOTTOM, GraphicsContext::BUTT, GraphicsContext::CAPSLOCK, GraphicsContext::CENTER, GraphicsContext::CLEAR, GraphicsContext::COMMAND, GraphicsContext::CONTROL, GraphicsContext::CORNER, GraphicsContext::CORNERS, GraphicsContext::DEGREES, GraphicsContext::DELETE, GraphicsContext::DOWN, GraphicsContext::ENTER, GraphicsContext::ESC, GraphicsContext::F1, GraphicsContext::F10, GraphicsContext::F11, GraphicsContext::F12, GraphicsContext::F13, GraphicsContext::F14, GraphicsContext::F15, GraphicsContext::F16, GraphicsContext::F17, GraphicsContext::F18, GraphicsContext::F19, GraphicsContext::F2, GraphicsContext::F20, GraphicsContext::F21, GraphicsContext::F22, GraphicsContext::F23, GraphicsContext::F24, GraphicsContext::F3, GraphicsContext::F4, GraphicsContext::F5, GraphicsContext::F6, GraphicsContext::F7, GraphicsContext::F8, GraphicsContext::F9, GraphicsContext::FUNCTION, GraphicsContext::HALF_PI, GraphicsContext::HELP, GraphicsContext::HOME, GraphicsContext::HSB, GraphicsContext::LEFT, GraphicsContext::MITER, GraphicsContext::OPTION, GraphicsContext::PAGEDOWN, GraphicsContext::PAGEUP, GraphicsContext::PI, GraphicsContext::QUARTER_PI, GraphicsContext::RADIANS, GraphicsContext::RADIUS, GraphicsContext::RGB, GraphicsContext::RIGHT, GraphicsContext::ROUND, GraphicsContext::SECTION, GraphicsContext::SHIFT, GraphicsContext::SPACE, GraphicsContext::SQUARE, GraphicsContext::TAB, GraphicsContext::TAU, GraphicsContext::TOP, GraphicsContext::TWO_PI, GraphicsContext::UP, GraphicsContext::WIN

Instance Method Summary collapse

Methods included from GraphicsContext

#angleMode, #arc, #background, #bezier, #circle, #colorMode, #copy, #curve, #ellipse, #ellipseMode, #fill, #height, #image, #imageMode, #init__, #line, #noFill, #noStroke, #point, #pop, #popMatrix, #popStyle, #push, #pushMatrix, #pushStyle, #quad, #rect, #rectMode, #resetMatrix, #rotate, #scale, #square, #stroke, #strokeCap, #strokeJoin, #strokeWeight, #text, #textAlign, #textAscent, #textDescent, #textFont, #textSize, #textWidth, #translate, #triangle, #updateCanvas__, #width

Instance Method Details

#abs(value) ⇒ Numeric

Returns the absolute number of the value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    absolute number



2212
2213
2214
# File 'lib/rubysketch/processing.rb', line 2212

def abs(value)
  value.abs
end

#acos(value) ⇒ Numeric

Returns the inverse of cos().

Parameters:

  • value (Numeric)

    value for calculation

Returns:

  • (Numeric)

    the arc cosine



2495
2496
2497
# File 'lib/rubysketch/processing.rb', line 2495

def acos(value)
  Math.acos value
end

#asin(value) ⇒ Numeric

Returns the inverse of sin().

Parameters:

  • value (Numeric)

    value for calculation

Returns:

  • (Numeric)

    the arc sine



2485
2486
2487
# File 'lib/rubysketch/processing.rb', line 2485

def asin(value)
  Math.asin value
end

#atan(value) ⇒ Numeric

Returns the inverse of tan().

Parameters:

  • value (Numeric)

    value for valculation

Returns:

  • (Numeric)

    the arc tangent



2505
2506
2507
# File 'lib/rubysketch/processing.rb', line 2505

def atan(value)
  Math.atan value
end

#atan2(y, x) ⇒ Numeric

Returns the angle from a specified point.

Parameters:

  • y (Numeric)

    y of the point

  • x (Numeric)

    x of the point

Returns:

  • (Numeric)

    the angle in radians



2516
2517
2518
# File 'lib/rubysketch/processing.rb', line 2516

def atan2(y, x)
  Math.atan2 y, x
end

#ceil(value) ⇒ Numeric

Returns the closest integer number greater than or equal to the value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    rounded up number



2222
2223
2224
# File 'lib/rubysketch/processing.rb', line 2222

def ceil(value)
  value.ceil
end

#constrain(value, min, max) ⇒ Numeric

Constrains the number between min..max.

Parameters:

  • value (Numeric)

    number to be constrained

  • min (Numeric)

    lower bound of the range

  • max (Numeric)

    upper bound of the range

Returns:

  • (Numeric)

    constrained number



2425
2426
2427
# File 'lib/rubysketch/processing.rb', line 2425

def constrain(value, min, max)
  value < min ? min : (value > max ? max : value)
end

#cos(angle) ⇒ Numeric

Returns the cosine of an angle.

Parameters:

  • angle (Numeric)

    angle in radians

Returns:

  • (Numeric)

    the cosine



2465
2466
2467
# File 'lib/rubysketch/processing.rb', line 2465

def cos(angle)
  Math.cos angle
end

#createCanvas(width, height, pixelDensity: self.pixelDensity) ⇒ nil

Changes canvas size.

Parameters:

  • width (Integer)

    new width

  • height (Integer)

    new height

  • pixelDensity (Numeric) (defaults to: self.pixelDensity)

    new pixel density

Returns:

  • (nil)

    nil



2038
2039
2040
2041
# File 'lib/rubysketch/processing.rb', line 2038

def createCanvas(width, height, pixelDensity: self.pixelDensity)
  resizeCanvas__ :createCanvas, width, height, pixelDensity
  nil
end

#createCapture(*args) ⇒ Capture

Creates a camera object as a video input device.

Returns:



2575
2576
2577
# File 'lib/rubysketch/processing.rb', line 2575

def createCapture(*args)
  Capture.new(*args)
end

#createGraphics(width, height) ⇒ Graphics

Creates a new off-screen graphics context object.

Parameters:

  • width (Numeric)

    width of graphics image

  • height (Numeric)

    height of graphics image

Returns:



2586
2587
2588
# File 'lib/rubysketch/processing.rb', line 2586

def createGraphics(width, height)
  Graphics.new width, height
end

#createVectorVector #createVector(x, y) ⇒ Vector #createVector(x, y, z) ⇒ Vector

Creates a new vector.

Parameters:

  • x (Numeric)

    x of new vector

  • y (Numeric)

    y of new vector

  • z (Numeric)

    z of new vector

Returns:



2567
2568
2569
# File 'lib/rubysketch/processing.rb', line 2567

def createVector(*args)
  Vector.new(*args, context: self)
end

#degrees(radian) ⇒ Numeric

Converts radian to degree.

Parameters:

  • radian (Numeric)

    radian to convert

Returns:

  • (Numeric)

    degree



2445
2446
2447
# File 'lib/rubysketch/processing.rb', line 2445

def degrees(radian)
  radian * RAD2DEG__
end

#displayDensityNumeric

Returns pixel density of display.

Returns:

  • (Numeric)

    pixel density



2070
2071
2072
# File 'lib/rubysketch/processing.rb', line 2070

def displayDensity()
  @window__.painter.pixel_density
end

#dist(x1, y1, x2, y2) ⇒ Numeric #dist(x1, y1, z1, x2, y2, z2) ⇒ Numeric

Returns distance between 2 points.

Parameters:

  • x1 (Numeric)

    x of first point

  • y1 (Numeric)

    y of first point

  • z1 (Numeric)

    z of first point

  • x2 (Numeric)

    x of second point

  • y2 (Numeric)

    y of second point

  • z2 (Numeric)

    z of second point

Returns:

  • (Numeric)

    distance between 2 points



2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
# File 'lib/rubysketch/processing.rb', line 2331

def dist(*args)
  case args.size
  when 4
    x1, y1, x2, y2 = *args
    xx, yy = x2 - x1, y2 - y1
    Math.sqrt xx * xx + yy * yy
  when 3
    x1, y1, z1, x2, y2, z2 = *args
    xx, yy, zz = x2 - x1, y2 - y1, z2 - z1
    Math.sqrt xx * xx + yy * yy + zz * zz
  else raise ArgumentError
  end
end

#draw(&block) ⇒ Object

Defines draw block.



1931
1932
1933
1934
# File 'lib/rubysketch/processing.rb', line 1931

def draw(&block)
  @drawBlock__ = block if block
  nil
end

#exp(n) ⇒ Numeric

Returns Euler's number e raised to the power of value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    result number



2262
2263
2264
# File 'lib/rubysketch/processing.rb', line 2262

def exp(n)
  Math.exp n
end

#floor(value) ⇒ Numeric

Returns the closest integer number less than or equal to the value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    rounded down number



2232
2233
2234
# File 'lib/rubysketch/processing.rb', line 2232

def floor(value)
  value.floor
end

#frameCountInteger

Returns number of frames since program started.

Returns:

  • (Integer)

    total number of frames



2094
2095
2096
# File 'lib/rubysketch/processing.rb', line 2094

def frameCount()
  @frameCount__
end

#frameRateFloat

Returns number of frames per second.

Returns:

  • (Float)

    frames per second



2102
2103
2104
# File 'lib/rubysketch/processing.rb', line 2102

def frameRate()
  @window__.event.fps
end

#keyString

Returns the last key that was pressed or released.

Returns:

  • (String)

    last key



2110
2111
2112
# File 'lib/rubysketch/processing.rb', line 2110

def key()
  @key__
end

#keyCodeSymbol

Returns the last key code that was pressed or released.

Returns:

  • (Symbol)

    last key code



2118
2119
2120
# File 'lib/rubysketch/processing.rb', line 2118

def keyCode()
  @keyCode__
end

#keyPressed(&block) ⇒ Boolean

Defines keyPressed block.

Returns:

  • (Boolean)

    is any key pressed or not



1940
1941
1942
1943
# File 'lib/rubysketch/processing.rb', line 1940

def keyPressed(&block)
  @keyPressedBlock__ = block if block
  not @keysPressed__.empty?
end

#keyReleased(&block) ⇒ Object

Defines keyReleased block.



1947
1948
1949
1950
# File 'lib/rubysketch/processing.rb', line 1947

def keyReleased(&block)
  @keyReleasedBlock__ = block if block
  nil
end

#keyTyped(&block) ⇒ Object

Defines keyTyped block.



1954
1955
1956
1957
# File 'lib/rubysketch/processing.rb', line 1954

def keyTyped(&block)
  @keyTypedBlock__ = block if block
  nil
end

#lerp(start, stop, amount) ⇒ Numeric

Returns the interpolated number between range start..stop.

Parameters:

  • start (Numeric)

    lower bound of the range

  • stop (Numeric)

    upper bound of the range

  • amount (Numeric)

    amount to interpolate

Returns:

  • (Numeric)

    interporated number



2365
2366
2367
# File 'lib/rubysketch/processing.rb', line 2365

def lerp(start, stop, amount)
  start + (stop - start) * amount
end

#loadImage(filename, extension = nil) ⇒ Image

Loads image.

Parameters:

  • filename (String)

    file name to load image

  • extension (String) (defaults to: nil)

    type of image to load (ex. 'png')

Returns:

  • (Image)

    loaded image object



2597
2598
2599
2600
# File 'lib/rubysketch/processing.rb', line 2597

def loadImage(filename, extension = nil)
  filename = getImage__ filename, extension if filename =~ %r|^https?://|
  Image.new Rays::Image.load filename
end

#log(n) ⇒ Numeric

Returns the natural logarithm (the base-e logarithm) of a number.

Parameters:

  • value (Numeric)

    number (> 0.0)

Returns:

  • (Numeric)

    result number



2252
2253
2254
# File 'lib/rubysketch/processing.rb', line 2252

def log(n)
  Math.log n
end

#loopnil

Enables calling draw block on every frame.

Returns:

  • (nil)

    nil



2182
2183
2184
# File 'lib/rubysketch/processing.rb', line 2182

def loop()
  @loop__ = true
end

#mag(x, y) ⇒ Numeric #mag(x, y, z) ⇒ Numeric

Returns the magnitude (or length) of a vector.

Parameters:

  • x (Numeric)

    x of point

  • y (Numeric)

    y of point

  • z (Numeric)

    z of point

Returns:

  • (Numeric)

    magnitude



2308
2309
2310
2311
2312
2313
2314
2315
# File 'lib/rubysketch/processing.rb', line 2308

def mag(*args)
  x, y, z = *args
  case args.size
  when 2 then Math.sqrt x * x + y * y
  when 3 then Math.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.

Parameters:

  • value (Numeric)

    number to be mapped

  • start1 (Numeric)

    lower bound of the range1

  • stop1 (Numeric)

    upper bound of the range1

  • start2 (Numeric)

    lower bound of the range2

  • stop2 (Numeric)

    upper bound of the range2

Returns:

  • (Numeric)

    mapped number



2379
2380
2381
# File 'lib/rubysketch/processing.rb', line 2379

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.

Parameters:

  • a (Numeric)

    value to compare

  • b (Numeric)

    value to compare

  • c (Numeric)

    value to compare

  • array (Numeric)

    values to compare

Returns:

  • (Numeric)

    maximum value



2413
2414
2415
# File 'lib/rubysketch/processing.rb', line 2413

def max(*args)
  args.flatten.max
end

#min(a, b) ⇒ Numeric #min(a, b, c) ⇒ Numeric #min(array) ⇒ Numeric

Returns minimum value.

Parameters:

  • a (Numeric)

    value to compare

  • b (Numeric)

    value to compare

  • c (Numeric)

    value to compare

  • array (Numeric)

    values to compare

Returns:

  • (Numeric)

    minimum value



2396
2397
2398
# File 'lib/rubysketch/processing.rb', line 2396

def min(*args)
  args.flatten.min
end

#motion(&block) ⇒ Object

Defines motion block.



2012
2013
2014
2015
# File 'lib/rubysketch/processing.rb', line 2012

def motion(&block)
  @motionBlock__ = block if block
  nil
end

#motionGravityVector

Returns vector for real world gravity

Returns:



2174
2175
2176
# File 'lib/rubysketch/processing.rb', line 2174

def motionGravity()
  @motionGravity__
end

#mouseButtonNumeric

Returns which mouse button was pressed

Returns:

  • (Numeric)

    LEFT, RIGHT, CENTER or 0



2158
2159
2160
# File 'lib/rubysketch/processing.rb', line 2158

def mouseButton()
  (@pointersPressed__ & [LEFT, RIGHT, CENTER]).last || 0
end

#mouseDragged(&block) ⇒ Object

Defines mouseDragged block.



1984
1985
1986
1987
# File 'lib/rubysketch/processing.rb', line 1984

def mouseDragged(&block)
  @mouseDraggedBlock__ = block if block
  nil
end

#mouseMoved(&block) ⇒ Object

Defines mouseMoved block.



1977
1978
1979
1980
# File 'lib/rubysketch/processing.rb', line 1977

def mouseMoved(&block)
  @mouseMovedBlock__ = block if block
  nil
end

#mousePressed(&block) ⇒ Boolean

Defines mousePressed block.

Returns:

  • (Boolean)

    is any mouse button pressed or not



1963
1964
1965
1966
# File 'lib/rubysketch/processing.rb', line 1963

def mousePressed(&block)
  @mousePressedBlock__ = block if block
  not @pointersPressed__.empty?
end

#mouseReleased(&block) ⇒ Object

Defines mouseReleased block.



1970
1971
1972
1973
# File 'lib/rubysketch/processing.rb', line 1970

def mouseReleased(&block)
  @mouseReleasedBlock__ = block if block
  nil
end

#mouseXNumeric

Returns mouse x position

Returns:

  • (Numeric)

    horizontal position of mouse



2126
2127
2128
# File 'lib/rubysketch/processing.rb', line 2126

def mouseX()
  @pointerPos__[0]
end

#mouseYNumeric

Returns mouse y position

Returns:

  • (Numeric)

    vertical position of mouse



2134
2135
2136
# File 'lib/rubysketch/processing.rb', line 2134

def mouseY()
  @pointerPos__[1]
end

#noise(x) ⇒ Numeric #noise(x, y) ⇒ Numeric #noise(x, y, z) ⇒ Numeric

Returns the perlin noise value.

Parameters:

  • x (Numeric)

    horizontal point in noise space

  • y (Numeric) (defaults to: 0)

    vertical point in noise space

  • z (Numeric) (defaults to: 0)

    depth point in noise space

Returns:

  • (Numeric)

    noise value (0.0..1.0)



2532
2533
2534
# File 'lib/rubysketch/processing.rb', line 2532

def noise(x, y = 0, z = 0)
  Rays.perlin(x, y, z) / 2.0 + 0.5
end

#noLoopnil

Disables calling draw block on every frame.

Returns:

  • (nil)

    nil



2190
2191
2192
# File 'lib/rubysketch/processing.rb', line 2190

def noLoop()
  @loop__ = false
end

#norm(value, start, stop) ⇒ Numeric

Normalize the value from range start..stop into 0..1.

Parameters:

  • value (Numeric)

    number to be normalized

  • start (Numeric)

    lower bound of the range

  • stop (Numeric)

    upper bound of the range

Returns:

  • (Numeric)

    normalized value between 0..1



2353
2354
2355
# File 'lib/rubysketch/processing.rb', line 2353

def norm(value, start, stop)
  (value.to_f - start.to_f) / (stop.to_f - start.to_f)
end

#pixelDensity(density = nil) ⇒ Numeric

Changes and returns canvas pixel density.

Parameters:

  • density (Numeric) (defaults to: nil)

    new pixel density

Returns:

  • (Numeric)

    current pixel density



2049
2050
2051
2052
# File 'lib/rubysketch/processing.rb', line 2049

def pixelDensity(density = nil)
  resizeCanvas__ :pixelDensity, width, height, density if density
  @painter__.pixel_density
end

#pmouseXNumeric

Returns mouse x position in previous frame

Returns:

  • (Numeric)

    horizontal position of mouse



2142
2143
2144
# File 'lib/rubysketch/processing.rb', line 2142

def pmouseX()
  @pointerPrevPos__[0]
end

#pmouseYNumeric

Returns mouse y position in previous frame

Returns:

  • (Numeric)

    vertical position of mouse



2150
2151
2152
# File 'lib/rubysketch/processing.rb', line 2150

def pmouseY()
  @pointerPrevPos__[1]
end

#pow(value, exponent) ⇒ Numeric

Returns value raised to the power of exponent.

Parameters:

  • value (Numeric)

    base number

  • exponent (Numeric)

    exponent number

Returns:

  • (Numeric)

    value ** exponent



2273
2274
2275
# File 'lib/rubysketch/processing.rb', line 2273

def pow(value, exponent)
  value ** exponent
end

#radians(degree) ⇒ Numeric

Converts degree to radian.

Parameters:

  • degree (Numeric)

    degree to convert

Returns:

  • (Numeric)

    radian



2435
2436
2437
# File 'lib/rubysketch/processing.rb', line 2435

def radians(degree)
  degree * DEG2RAD__
end

#randomFloat #random(high) ⇒ Float #random(low, high) ⇒ Float #random(choices) ⇒ Float

Returns a random number in range low…high

Parameters:

  • low (Numeric)

    lower limit

  • high (Numeric)

    upper limit

  • choices (Array)

    array to choose from

Returns:

  • (Float)

    random number



2549
2550
2551
2552
2553
# File 'lib/rubysketch/processing.rb', line 2549

def random(*args)
  return args.first.sample if args.first.kind_of? Array
  high, low = args.reverse
  rand (low || 0).to_f...(high || 1).to_f
end

#redrawnil

Calls draw block to redraw frame.

Returns:

  • (nil)

    nil



2198
2199
2200
# File 'lib/rubysketch/processing.rb', line 2198

def redraw()
  @redraw__ = true
end

#round(value) ⇒ Numeric

Returns the closest integer number.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    rounded number



2242
2243
2244
# File 'lib/rubysketch/processing.rb', line 2242

def round(value)
  value.round
end

#setup(&block) ⇒ Object

Defines setup block.



1924
1925
1926
1927
# File 'lib/rubysketch/processing.rb', line 1924

def setup(&block)
  @window__.setup = block
  nil
end

#sin(angle) ⇒ Numeric

Returns the sine of an angle.

Parameters:

  • angle (Numeric)

    angle in radians

Returns:

  • (Numeric)

    the sine



2455
2456
2457
# File 'lib/rubysketch/processing.rb', line 2455

def sin(angle)
  Math.sin angle
end

#size(width, height, pixelDensity: self.pixelDensity) ⇒ nil

Changes canvas size.

Parameters:

  • width (Integer)

    new width

  • height (Integer)

    new height

  • pixelDensity (Numeric) (defaults to: self.pixelDensity)

    new pixel density

Returns:

  • (nil)

    nil



2025
2026
2027
2028
# File 'lib/rubysketch/processing.rb', line 2025

def size(width, height, pixelDensity: self.pixelDensity)
  resizeCanvas__ :size, width, height, pixelDensity
  nil
end

#sq(value) ⇒ Numeric

Returns squared value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    squared value



2283
2284
2285
# File 'lib/rubysketch/processing.rb', line 2283

def sq(value)
  value * value
end

#sqrt(value) ⇒ Numeric

Returns squared value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    squared value



2293
2294
2295
# File 'lib/rubysketch/processing.rb', line 2293

def sqrt(value)
  Math.sqrt value
end

#tan(angle) ⇒ Numeric

Returns the ratio of the sine and cosine of an angle.

Parameters:

  • angle (Numeric)

    angle in radians

Returns:

  • (Numeric)

    the tangent



2475
2476
2477
# File 'lib/rubysketch/processing.rb', line 2475

def tan(angle)
  Math.tan angle
end

#touchEnded(&block) ⇒ Object

Defines touchEnded block.



1998
1999
2000
2001
# File 'lib/rubysketch/processing.rb', line 1998

def touchEnded(&block)
  @touchEndedBlock__ = block if block
  nil
end

#touchesArray

Returns array of touches

Returns:

  • (Array)

    Touch objects



2166
2167
2168
# File 'lib/rubysketch/processing.rb', line 2166

def touches()
  @touches__
end

#touchMoved(&block) ⇒ Object

Defines touchMoved block.



2005
2006
2007
2008
# File 'lib/rubysketch/processing.rb', line 2005

def touchMoved(&block)
  @touchMovedBlock__ = block if block
  nil
end

#touchStarted(&block) ⇒ Object

Defines touchStarted block.



1991
1992
1993
1994
# File 'lib/rubysketch/processing.rb', line 1991

def touchStarted(&block)
  @touchStartedBlock__ = block if block
  nil
end

#windowHeightNumeric

Returns window height.

Returns:

  • (Numeric)

    window height



2086
2087
2088
# File 'lib/rubysketch/processing.rb', line 2086

def windowHeight()
  @window__.height
end

#windowWidthNumeric

Returns window width.

Returns:

  • (Numeric)

    window width



2078
2079
2080
# File 'lib/rubysketch/processing.rb', line 2078

def windowWidth()
  @window__.width
end