Module: Processing::GraphicsContext

Included in:
Context, Graphics
Defined in:
lib/processing/graphics_context.rb

Overview

Drawing context

Constant Summary collapse

PI =

PI

Math::PI
HALF_PI =

PI / 2

PI / 2
QUARTER_PI =

PI / 4

PI / 4
TWO_PI =

PI * 2

PI * 2
TAU =

PI * 2

PI * 2
PROCESSING =

Processing mode for renderMode().

:processing
P5JS =

p5.js mode for renderMode().

:p5js
RGBA =

RGBA format for createImage().

:rgba
RGB =

RGB format for createImage, or RGB mode for colorMode().

:rgb
HSB =

HSB mode for colorMode().

:hsb
RADIANS =

Radian mode for angleMode().

:radians
DEGREES =

Degree mode for angleMode().

:degrees
CORNER =

Mode for rectMode(), ellipseMode(), imageMode(), and shapeMode().

:corner
CORNERS =

Mode for rectMode(), ellipseMode(), imageMode(), and shapeMode().

:corners
CENTER =

Mode for rectMode(), ellipseMode(), imageMode(), shapeMode(), and textAlign().

:center
RADIUS =

Mode for rectMode() and ellipseMode().

:radius
ROUND =

Mode for strokeCap() and strokeJoin().

:round
SQUARE =

Mode for strokeCap().

:butt
PROJECT =

Mode for strokeCap().

:square
MITER =

Mode for strokeJoin().

:miter
BEVEL =

Mode for strokeCap() and strokeJoin().

:square
BLEND =

Mode for blendMode().

:normal
ADD =

Mode for blendMode().

:add
SUBTRACT =

Mode for blendMode().

:subtract
LIGHTEST =

Mode for blendMode().

:lightest
DARKEST =

Mode for blendMode().

:darkest
EXCLUSION =

Mode for blendMode().

:exclusion
MULTIPLY =

Mode for blendMode().

:multiply
SCREEN =

Mode for blendMode().

:screen
REPLACE =

Mode for blendMode().

:replace
LEFT =

Key code or Mode for textAlign().

:left
RIGHT =

Key code or Mode for textAlign().

:right
TOP =

Mode for textAlign().

:top
BOTTOM =

Mode for textAlign().

:bottom
BASELINE =

Mode for textAlign().

:baseline
IMAGE =

Mode for textureMode().

:image
NORMAL =

Mode for textureMode().

:normal
CLAMP =

Mode for textureWrap().

:clamp
REPEAT =

Mode for textureWrap().

:repeat
THRESHOLD =

Filter type for filter()

:threshold
GRAY =

Filter type for filter()

:gray
INVERT =

Filter type for filter()

:invert
BLUR =

Filter type for filter()

:blur
LINE =

Shape mode for createShape()

:line
RECT =

Shape mode for createShape()

:rect
ELLIPSE =

Shape mode for createShape()

:ellipse
ARC =

Shape mode for createShape()

:arc
TRIANGLE =

Shape mode for createShape()

:triangle
QUAD =

Shape mode for createShape()

:quad
GROUP =

Shape mode for createShape()

:group
POINTS =

Shape mode for beginShape()

:points
LINES =

Shape mode for beginShape()

:lines
TRIANGLES =

Shape mode for beginShape()

:triangles
TRIANGLE_FAN =

Shape mode for beginShape()

:triangle_fan
TRIANGLE_STRIP =

Shape mode for beginShape()

:triangle_strip
QUADS =

Shape mode for beginShape()

:quads
QUAD_STRIP =

Shape mode for beginShape()

:quad_strip
TESS =

Shape mode for beginShape()

:tess
OPEN =

OPEN flag for endShape()

:open
CLOSE =

CLOSE flag for endShape()

:close
ENTER =

Key codes.

:enter
SPACE =
:space
TAB =
:tab
DELETE =
:delete
BACKSPACE =
:backspace
ESC =
:escape
HOME =
:home
PAGEUP =

END = :end

:pageup
PAGEDOWN =
:pagedown
CLEAR =
:clear
SHIFT =
:shift
CONTROL =
:control
ALT =
:alt
WIN =
:win
COMMAND =
:command
OPTION =
:option
FUNCTION =
:function
CAPSLOCK =
:capslock
SECTION =
:section
HELP =
:help
F1 =
:f1
F2 =
:f2
F3 =
:f3
F4 =
:f4
F5 =
:f5
F6 =
:f6
F7 =
:f7
F8 =
:f8
F9 =
:f9
F10 =
:f10
F11 =
:f11
F12 =
:f12
F13 =
:f13
F14 =
:f14
F15 =
:f15
F16 =
:f16
F17 =
:f17
F18 =
:f18
F19 =
:f19
F20 =
:f20
F21 =
:f21
F22 =
:f22
F23 =
:f23
F24 =
:f24
UP =
:up
DOWN =
:down
COLOR_CODES =
{
  aliceblue:            '#f0f8ff',
  antiquewhite:         '#faebd7',
  aqua:                 '#00ffff',
  aquamarine:           '#7fffd4',
  azure:                '#f0ffff',
  beige:                '#f5f5dc',
  bisque:               '#ffe4c4',
  black:                '#000000',
  blanchedalmond:       '#ffebcd',
  blue:                 '#0000ff',
  blueviolet:           '#8a2be2',
  brown:                '#a52a2a',
  burlywood:            '#deb887',
  cadetblue:            '#5f9ea0',
  chartreuse:           '#7fff00',
  chocolate:            '#d2691e',
  coral:                '#ff7f50',
  cornflowerblue:       '#6495ed',
  cornsilk:             '#fff8dc',
  crimson:              '#dc143c',
  cyan:                 '#00ffff',
  darkblue:             '#00008b',
  darkcyan:             '#008b8b',
  darkgoldenrod:        '#b8860b',
  darkgray:             '#a9a9a9',
  darkgreen:            '#006400',
  darkgrey:             '#a9a9a9',
  darkkhaki:            '#bdb76b',
  darkmagenta:          '#8b008b',
  darkolivegreen:       '#556b2f',
  darkorange:           '#ff8c00',
  darkorchid:           '#9932cc',
  darkred:              '#8b0000',
  darksalmon:           '#e9967a',
  darkseagreen:         '#8fbc8f',
  darkslateblue:        '#483d8b',
  darkslategray:        '#2f4f4f',
  darkslategrey:        '#2f4f4f',
  darkturquoise:        '#00ced1',
  darkviolet:           '#9400d3',
  deeppink:             '#ff1493',
  deepskyblue:          '#00bfff',
  dimgray:              '#696969',
  dimgrey:              '#696969',
  dodgerblue:           '#1e90ff',
  firebrick:            '#b22222',
  floralwhite:          '#fffaf0',
  forestgreen:          '#228b22',
  fuchsia:              '#ff00ff',
  gainsboro:            '#dcdcdc',
  ghostwhite:           '#f8f8ff',
  goldenrod:            '#daa520',
  gold:                 '#ffd700',
  gray:                 '#808080',
  green:                '#008000',
  greenyellow:          '#adff2f',
  grey:                 '#808080',
  honeydew:             '#f0fff0',
  hotpink:              '#ff69b4',
  indianred:            '#cd5c5c',
  indigo:               '#4b0082',
  ivory:                '#fffff0',
  khaki:                '#f0e68c',
  lavenderblush:        '#fff0f5',
  lavender:             '#e6e6fa',
  lawngreen:            '#7cfc00',
  lemonchiffon:         '#fffacd',
  lightblue:            '#add8e6',
  lightcoral:           '#f08080',
  lightcyan:            '#e0ffff',
  lightgoldenrodyellow: '#fafad2',
  lightgray:            '#d3d3d3',
  lightgreen:           '#90ee90',
  lightgrey:            '#d3d3d3',
  lightpink:            '#ffb6c1',
  lightsalmon:          '#ffa07a',
  lightseagreen:        '#20b2aa',
  lightskyblue:         '#87cefa',
  lightslategray:       '#778899',
  lightslategrey:       '#778899',
  lightsteelblue:       '#b0c4de',
  lightyellow:          '#ffffe0',
  lime:                 '#00ff00',
  limegreen:            '#32cd32',
  linen:                '#faf0e6',
  magenta:              '#ff00ff',
  maroon:               '#800000',
  mediumaquamarine:     '#66cdaa',
  mediumblue:           '#0000cd',
  mediumorchid:         '#ba55d3',
  mediumpurple:         '#9370db',
  mediumseagreen:       '#3cb371',
  mediumslateblue:      '#7b68ee',
  mediumspringgreen:    '#00fa9a',
  mediumturquoise:      '#48d1cc',
  mediumvioletred:      '#c71585',
  midnightblue:         '#191970',
  mintcream:            '#f5fffa',
  mistyrose:            '#ffe4e1',
  moccasin:             '#ffe4b5',
  navajowhite:          '#ffdead',
  navy:                 '#000080',
  oldlace:              '#fdf5e6',
  olive:                '#808000',
  olivedrab:            '#6b8e23',
  orange:               '#ffa500',
  orangered:            '#ff4500',
  orchid:               '#da70d6',
  palegoldenrod:        '#eee8aa',
  palegreen:            '#98fb98',
  paleturquoise:        '#afeeee',
  palevioletred:        '#db7093',
  papayawhip:           '#ffefd5',
  peachpuff:            '#ffdab9',
  peru:                 '#cd853f',
  pink:                 '#ffc0cb',
  plum:                 '#dda0dd',
  powderblue:           '#b0e0e6',
  purple:               '#800080',
  rebeccapurple:        '#663399',
  red:                  '#ff0000',
  rosybrown:            '#bc8f8f',
  royalblue:            '#4169e1',
  saddlebrown:          '#8b4513',
  salmon:               '#fa8072',
  sandybrown:           '#f4a460',
  seagreen:             '#2e8b57',
  seashell:             '#fff5ee',
  sienna:               '#a0522d',
  silver:               '#c0c0c0',
  skyblue:              '#87ceeb',
  slateblue:            '#6a5acd',
  slategray:            '#708090',
  slategrey:            '#708090',
  snow:                 '#fffafa',
  springgreen:          '#00ff7f',
  steelblue:            '#4682b4',
  tan:                  '#d2b48c',
  teal:                 '#008080',
  thistle:              '#d8bfd8',
  tomato:               '#ff6347',
  turquoise:            '#40e0d0',
  violet:               '#ee82ee',
  wheat:                '#f5deb3',
  white:                '#ffffff',
  whitesmoke:           '#f5f5f5',
  yellow:               '#ffff00',
  yellowgreen:          '#9acd32',
  none:                 '#00000000',
}

Instance Method Summary collapse

Instance Method Details

#abs(value) ⇒ Numeric

Returns the absolute number of the value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    absolute number

See Also:



2494
2495
2496
# File 'lib/processing/graphics_context.rb', line 2494

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

See Also:



2865
2866
2867
# File 'lib/processing/graphics_context.rb', line 2865

def acos(value)
  Math.acos value
end

#alpha(color) ⇒ Numeric

Returns the red value of the color.

Parameters:

  • color (Numeric)

    color value

Returns:

  • (Numeric)

    the red value

See Also:



756
757
758
# File 'lib/processing/graphics_context.rb', line 756

def alpha(color)
  ((color >> 24) & 0xff) / 255.0 * @colorMaxes__[3]
end

#angleMode(mode = nil) ⇒ RADIANS, DEGREES

Sets angle mode.

Parameters:

Returns:

See Also:



849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/processing/graphics_context.rb', line 849

def angleMode(mode = nil)
  if mode != nil
    @angleMode__  = mode
    @toRad__, @toDeg__, @fromRad__, @fromDeg__ =
      case mode.downcase.to_sym
      when RADIANS then [1.0,      RAD2DEG__, 1.0,       DEG2RAD__]
      when DEGREES then [DEG2RAD__, 1.0,      RAD2DEG__, 1.0]
      else raise ArgumentError, "invalid angle mode: #{mode}"
      end
  end
  @angleMode__
end

#applyMatrix(array) ⇒ nil #applyMatrix(a, b, c, d, e, f) ⇒ nil #applyMatrix(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) ⇒ nil

Reset current transformation matrix with 2x3, or 4x4 matrix.

Parameters:

  • array (Array)

    6 or 16 numbers which define the matrix

  • a (Numeric)

    number which defines the matrix

  • b (Numeric)

    number which defines the matrix

  • c (Numeric)

    number which defines the matrix

  • d (Numeric)

    number which defines the matrix

  • e (Numeric)

    number which defines the matrix

  • f (Numeric)

    number which defines the matrix

  • g (Numeric)

    number which defines the matrix

  • h (Numeric)

    number which defines the matrix

  • i (Numeric)

    number which defines the matrix

  • j (Numeric)

    number which defines the matrix

  • k (Numeric)

    number which defines the matrix

  • l (Numeric)

    number which defines the matrix

  • m (Numeric)

    number which defines the matrix

  • n (Numeric)

    number which defines the matrix

  • o (Numeric)

    number which defines the matrix

  • p (Numeric)

    number which defines the matrix

Returns:

  • (nil)

    nil

Raises:

  • (ArgumentError)

See Also:



2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
# File 'lib/processing/graphics_context.rb', line 2290

def applyMatrix(*args)
  assertDrawing__
  args = args.first if args.first.kind_of?(Array)
  if args.size == 6
    a, b, c, d, e, f = args
    args = [
      a, b, 0, 0,
      c, d, 0, 0,
      0, 0, 1, 0,
      e, f, 0, 1
    ]
  end
  raise ArgumentError unless args.size == 16
  m = Rays::Matrix.new(*args)
  m.transpose! if @p5jsMode__
  @painter__.matrix *= m
  nil
end

#arc(a, b, c, d, start, stop) ⇒ nil Also known as: drawArc

Draws an arc.

The parameters a, b, c, and d are determined by ellipseMode().

Parameters:

  • a (Numeric)

    horizontal position of the shape, by default

  • b (Numeric)

    vertical position of the shape, by default

  • c (Numeric)

    width of the shape, by default

  • d (Numeric)

    height of the shape, by default

  • start (Numeric)

    angle to start the arc

  • stop (Numeric)

    angle to stop the arc

Returns:

  • (nil)

    nil

See Also:



1603
1604
1605
1606
1607
1608
1609
# File 'lib/processing/graphics_context.rb', line 1603

def arc(a, b, c, d, start, stop)
  assertDrawing__
  x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
  from, to   = toDegrees__(-start), toDegrees__(-stop)
  @painter__.ellipse x, y, w, h, from: from, to: to
  nil
end

#asin(value) ⇒ Numeric

Returns the inverse of sin().

Parameters:

  • value (Numeric)

    value for calculation

Returns:

  • (Numeric)

    the arc sine

See Also:



2852
2853
2854
# File 'lib/processing/graphics_context.rb', line 2852

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

See Also:



2878
2879
2880
# File 'lib/processing/graphics_context.rb', line 2878

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

See Also:



2892
2893
2894
# File 'lib/processing/graphics_context.rb', line 2892

def atan2(y, x)
  Math.atan2 y, x
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.

Parameters:

  • str (String)

    color code like ‘#00AAFF’

  • gray (Integer)

    gray value (0..255)

  • r (Integer)

    red value (0..255)

  • g (Integer)

    green value (0..255)

  • b (Integer)

    blue value (0..255)

  • alpha (Integer)

    alpha value (0..255)

Returns:

  • (nil)

    nil

See Also:



1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
# File 'lib/processing/graphics_context.rb', line 1450

def background(*args)
  assertDrawing__
  rgba = toRGBA__(*args)
  if rgba[3] == 1
    @painter__.background(*rgba)
  else
    @painter__.push fill: rgba, stroke: :none, blend_mode: :replace do |_|
      @painter__.rect 0, 0, width, height
    end
  end
  nil
end

#beginContournil

Begins drawing a hole inside shape.

Examples:

beginShape
vertex 10, 10
vertex 10, 50
vertex 50, 50
vertex 90, 10
beginContour
vertex 20, 20
vertex 30, 20
vertex 30, 30
vertex 20, 30
endContour
endShape CLOSE

Returns:

  • (nil)

    nil

See Also:



1903
1904
1905
1906
# File 'lib/processing/graphics_context.rb', line 1903

def beginContour()
  (@drawingShape__ or raise "beginContour() must be called after beginShape()")
    .beginContour
end

#beginShape(type = nil) ⇒ nil

Begins drawing complex shapes.

Examples:

# Draws polygon
beginShape
vertex 10, 10
vertex 10, 50
vertex 50, 50
vertex 90, 10
endShape CLOSE

# Draws triangles
beginShape TRIANGLES
vertex 10, 10
vertex 10, 50
vertex 50, 50
endShape

Parameters:

Returns:

  • (nil)

    nil

See Also:



1857
1858
1859
1860
1861
# File 'lib/processing/graphics_context.rb', line 1857

def beginShape(type = nil)
  raise "beginShape() cannot be called twice" if @drawingShape__
  @drawingShape__ = createShape
  @drawingShape__.beginShape type
end

#bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2) ⇒ nil Also known as: drawBezier

Draws a Bezier spline curve.

Parameters:

  • x1 (Numeric)

    horizontal position of first point

  • y1 (Numeric)

    vertical position of first point

  • cx1 (Numeric)

    horizontal position of first control point

  • cy1 (Numeric)

    vertical position of first control point

  • cx2 (Numeric)

    horizontal position of second control point

  • cy2 (Numeric)

    vertical position of second control point

  • x2 (Numeric)

    horizontal position of second point

  • y2 (Numeric)

    vertical position of second point

Returns:

  • (nil)

    nil

See Also:



1718
1719
1720
1721
1722
1723
1724
# File 'lib/processing/graphics_context.rb', line 1718

def bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2)
  assertDrawing__
  @painter__.nsegment = @bezierDetail__
  @painter__.bezier x1, y1, cx1, cy1, cx2, cy2, x2, y2
  @painter__.nsegment = 0
  nil
end

#bezierDetail(detail) ⇒ nil

Sets the resolution at which Bezier’s curve is displayed. The default value is 20.

Parameters:

  • detail (Numeric)

    resolution of the curves

Returns:

  • (nil)

    nil

See Also:



1139
1140
1141
1142
1143
# File 'lib/processing/graphics_context.rb', line 1139

def bezierDetail(detail)
  detail = 1 if detail < 1
  @bezierDetail__ = detail
  nil
end

#bezierPoint(a, b, c, d, t) ⇒ Numeric

Evaluates the Bezier at point t for points a, b, c, d.

Parameters:

  • a (Numeric)

    coordinate of first point on the curve

  • b (Numeric)

    coordinate of first control point

  • c (Numeric)

    coordinate of second control point

  • d (Numeric)

    coordinate of second point on the curve

  • t (Numeric)

    value between 0.0 and 1.0

Returns:

  • (Numeric)

    interpolated value

See Also:



2957
2958
2959
2960
2961
2962
2963
# File 'lib/processing/graphics_context.rb', line 2957

def bezierPoint(a, b, c, d, t)
  tt = 1.0 - t
  tt ** 3.0 * a +
  tt ** 2.0 * b * 3.0 * t +
  t  ** 2.0 * c * 3.0 * tt +
  t  ** 3.0 * d
end

#bezierTangent(a, b, c, d, t) ⇒ Numeric

Calculates the tangent of a point on a Bezier curve.

Parameters:

  • a (Numeric)

    coordinate of first point on the curve

  • b (Numeric)

    coordinate of first control point

  • c (Numeric)

    coordinate of second control point

  • d (Numeric)

    coordinate of second point on the curve

  • t (Numeric)

    value between 0.0 and 1.0

Returns:

  • (Numeric)

    tangent value

See Also:



2978
2979
2980
2981
2982
2983
2984
2985
2986
# File 'lib/processing/graphics_context.rb', line 2978

def bezierTangent(a, b, c, d, t)
  tt = 1.0 - t
  3.0 * d * t  ** 2.0 -
  3.0 * c * t  ** 2.0 +
  6.0 * c * tt *  t   -
  6.0 * b * tt *  t   +
  3.0 * b * tt ** 2.0 -
  3.0 * a * tt ** 2.0
end

#bezierVertex(x2, y2, x3, y3, x4, y4) ⇒ nil

Append bezier vertex for shape polygon.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

Returns:

  • (nil)

    nil

See Also:



1965
1966
1967
1968
# File 'lib/processing/graphics_context.rb', line 1965

def bezierVertex(x2, y2, x3, y3, x4, y4)
  (@drawingShape__ or raise "bezierVertex() must be called after beginShape()")
    .bezierVertex x2, y2, x3, y3, x4, y4
end

#blend(sx, sy, sw, sh, dx, dy, dw, dh, mode) ⇒ nil #blend(img, sx, sy, sw, sh, dx, dy, dw, dh, mode) ⇒ nil

Blends image.

Parameters:

  • img (Image) (defaults to: nil)

    image for blend source

  • sx (Numrtic)

    x position of source region

  • sy (Numrtic)

    y position of source region

  • sw (Numrtic)

    width of source region

  • sh (Numrtic)

    height of source region

  • dx (Numrtic)

    x position of destination region

  • dy (Numrtic)

    y position of destination region

  • dw (Numrtic)

    width of destination region

  • dh (Numrtic)

    height of destination region

  • mode (BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE)

    blend mode

Returns:

  • (nil)

    nil

See Also:



2030
2031
2032
2033
2034
2035
# File 'lib/processing/graphics_context.rb', line 2030

def blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode)
  assertDrawing__
  (img || self).drawImage__(
    @painter__, sx, sy, sw, sh, dx, dy, dw, dh,
    fill: getTint__, stroke: :none, blend_mode: mode)
end

#blendMode(mode = nil) ⇒ nil

Sets blend mode. Default is BLEND.



971
972
973
974
975
976
977
# File 'lib/processing/graphics_context.rb', line 971

def blendMode(mode = nil)
  if mode != nil
    @blendMode__          = mode
    @painter__.blend_mode = mode
  end
  @blendMode__
end

#blue(color) ⇒ Numeric

Returns the blue value of the color.

Parameters:

  • color (Numeric)

    color value

Returns:

  • (Numeric)

    the blue value

See Also:



743
744
745
# File 'lib/processing/graphics_context.rb', line 743

def blue(color)
  (color & 0xff) / 255.0 * @colorMaxes__[2]
end

#brightness(color) ⇒ Numeric

Returns the brightness value of the color.

Parameters:

  • color (Numeric)

    color value

Returns:

  • (Numeric)

    the brightness value

See Also:



797
798
799
800
# File 'lib/processing/graphics_context.rb', line 797

def brightness(color)
  _, _, b = color2raw__(color).to_hsv
  b * (@hsbColor__ ? @colorMaxes__[2] : 1)
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

See Also:



2507
2508
2509
# File 'lib/processing/graphics_context.rb', line 2507

def ceil(value)
  value.ceil
end

#circle(x, y, extent) ⇒ nil Also known as: drawCircle

Draws a circle.

Parameters:

  • x (Numeric)

    horizontal position of the shape

  • y (Numeric)

    vertical position of the shape

  • extent (Numeric)

    width and height of the shape

Returns:

  • (nil)

    nil

See Also:



1581
1582
1583
# File 'lib/processing/graphics_context.rb', line 1581

def circle(x, y, extent)
  ellipse x, y, extent, extent
end

#clearObject



1463
1464
1465
1466
1467
# File 'lib/processing/graphics_context.rb', line 1463

def clear()
  assertDrawing__
  @painter__.background 0, 0
  nil
end

#clip(a, b, c, d) ⇒ nil

Limits the drawable rectangle.

The parameters a, b, c, and d are determined by rectMode().

Parameters:

  • a (Numeric)

    horizontal position of the drawable area, by default

  • b (Numeric)

    vertical position of the drawable area, by default

  • c (Numeric)

    width of the drawable area, by default

  • d (Numeric)

    height of the drawable area, by default

Returns:

  • (nil)

    nil

See Also:



1201
1202
1203
1204
1205
# File 'lib/processing/graphics_context.rb', line 1201

def clip(a, b, c, d)
  x, y, w, h = toXYWH__ @imageMode__, a, b, c, d
  @painter__.clip x, y, w, h
  nil
end

#color(gray) ⇒ Integer #color(gray, alpha) ⇒ Integer #color(v1, v2, v3) ⇒ Integer #color(v1, v2, v3, alpha) ⇒ Integer

Creates color value.

Parameters:

  • gray (Numeric)

    the value for gray

  • alpha (Numeric)

    the value for alpha

  • v1 (Numeric)

    the value for red or hue

  • v2 (Numeric)

    the value for green or saturation

  • v3 (Numeric)

    the value for blue or brightness

Returns:

  • (Integer)

    the rgba color value

See Also:



693
694
695
696
697
# File 'lib/processing/graphics_context.rb', line 693

def color(*args)
  toRGBA__(*args)
    .map {|n| (n * 255).to_i.clamp 0, 255}
    .then {|r, g, b, a| Image.toColor__ r, g, b, a}
end

#colorMode(mode) ⇒ RGB, HSB #colorMode(mode, max) ⇒ RGB, HSB #colorMode(mode, max1, max2, max3) ⇒ RGB, HSB #colorMode(mode, max1, max2, max3, maxA) ⇒ RGB, HSB

Sets color mode and max color values.

Parameters:

  • mode (RGB, HSB) (defaults to: nil)

    RGB or HSB

  • max (Numeric)

    max values for all color values

  • max1 (Numeric)

    max value for red or hue

  • max2 (Numeric)

    max value for green or saturation

  • max3 (Numeric)

    max value for blue or brightness

  • maxA (Numeric)

    max value for alpha

Returns:

See Also:



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/processing/graphics_context.rb', line 659

def colorMode(mode = nil, *maxes)
  if mode != nil
    mode = mode.downcase.to_sym
    raise ArgumentError, "invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
    raise ArgumentError unless [0, 1, 3, 4].include?(maxes.size)

    @colorMode__ = mode
    @hsbColor__  = mode == 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
  end
  @colorMode__
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

See Also:



2774
2775
2776
# File 'lib/processing/graphics_context.rb', line 2774

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

#copy(sx, sy, sw, sh, dx, dy, dw, dh) ⇒ nil #copy(img, sx, sy, sw, sh, dx, dy, dw, dh) ⇒ nil

Copies image.

Parameters:

  • img (Image) (defaults to: nil)

    image for copy source

  • sx (Numrtic)

    x position of source region

  • sy (Numrtic)

    y position of source region

  • sw (Numrtic)

    width of source region

  • sh (Numrtic)

    height of source region

  • dx (Numrtic)

    x position of destination region

  • dy (Numrtic)

    y position of destination region

  • dw (Numrtic)

    width of destination region

  • dh (Numrtic)

    height of destination region

Returns:

  • (nil)

    nil

See Also:



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

def copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
  blend img, sx, sy, sw, sh, dx, dy, dw, dh, BLEND
end

#cos(angle) ⇒ Numeric

Returns the cosine of an angle.

Parameters:

  • angle (Numeric)

    angle in radians

Returns:

  • (Numeric)

    the cosine

See Also:



2826
2827
2828
# File 'lib/processing/graphics_context.rb', line 2826

def cos(angle)
  Math.cos angle
end

#createCapture(*args) ⇒ Capture

Creates a camera object as a video input device.

Returns:



3288
3289
3290
# File 'lib/processing/graphics_context.rb', line 3288

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

#createFont(name, size) ⇒ Font

Creates a new font object.

Parameters:

  • name (String)

    font name

  • size (Numeric)

    font size (max 256)

Returns:

  • (Font)

    new font

See Also:



3139
3140
3141
3142
# File 'lib/processing/graphics_context.rb', line 3139

def createFont(name, size)
  size = FONT_SIZE_MAX__ if size && size > FONT_SIZE_MAX__
  Font.new Rays::Font.new(name, size || FONT_SIZE_DEFAULT__)
end

#createGraphics(width, height, pixelDensity = 1) ⇒ Graphics

Creates a new off-screen graphics context object.

Parameters:

  • width (Numeric)

    width of graphics image

  • height (Numeric)

    height of graphics image

  • pixelDensity (Numeric) (defaults to: 1)

    pixel density of graphics image

Returns:

See Also:



3240
3241
3242
# File 'lib/processing/graphics_context.rb', line 3240

def createGraphics(width, height, pixelDensity = 1)
  Graphics.new width, height, pixelDensity
end

#createImage(w, h) ⇒ Image #createImage(w, h, format) ⇒ Image

Creates a new image object.

Parameters:

  • w (Numeric)

    width of new image

  • h (Numeric)

    height of new image

  • format (RGB, RGBA) (defaults to: RGBA)

    image format

Returns:

Raises:

  • (ArgumentError)

See Also:



3158
3159
3160
3161
3162
# File 'lib/processing/graphics_context.rb', line 3158

def createImage(w, h, format = RGBA)
  colorspace = {RGB => Rays::RGB, RGBA => Rays::RGBA}[format]
  raise ArgumentError, "Unknown image format" unless colorspace
  Image.new Rays::Image.new(w, h, colorspace).paint {background 0, 0}
end

#createShader(vertPath, fragPath) ⇒ Shader #createShader(vertSource, fragSource) ⇒ Shader

Creates a shader object.

Passing nil for a vertex shader parameter causes the following default vertex shader to be used. “‘ attribute vec3 position; attribute vec3 texCoord; attribute vec4 color; varying vec4 vertPosition; varying vec4 vertTexCoord; varying vec4 vertColor; uniform mat4 transform; uniform mat4 texMatrix; void main ()

vec4 pos__   = vec4(position, 1.0);
vertPosition = pos__;
vertTexCoord = texMatrix * vec4(texCoord, 1.0);
vertColor    = color;
gl_Position  = transform * pos__;

“‘

Parameters:

  • vertPath (String)

    vertex shader file path

  • fragPath (String)

    fragment shader file path

  • vertSource (String)

    vertex shader source

  • fragSource (String)

    fragment shader source

Returns:

See Also:



3278
3279
3280
3281
3282
# File 'lib/processing/graphics_context.rb', line 3278

def createShader(vert, frag)
  vert = File.read if vert && File.exist?(vert)
  frag = File.read if frag && File.exist?(frag)
  Shader.new vert, frag
end

#createShapeShape #createShape(LINE, x1, y1, x2, y2) ⇒ Shape #createShape(RECT, a, b, c, d) ⇒ Shape #createShape(ELLIPSE, a, b, c, d) ⇒ Shape #createShape(ARC, a, b, c, d, start, stop) ⇒ Shape #createShape(TRIANGLE, x1, y1, x2, y2, x3, y3) ⇒ Shape #createShape(QUAD, x1, y1, x2, y2, x3, y3, x4, y4) ⇒ Shape #createShape(GROUP) ⇒ Shape

Creates a new shape object.

Parameters:

Returns:

See Also:



3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
# File 'lib/processing/graphics_context.rb', line 3181

def createShape(kind = nil, *args)
  case kind
  when LINE     then createLineShape__(    *args)
  when RECT     then createRectShape__(    *args)
  when ELLIPSE  then createEllipseShape__( *args)
  when ARC      then createArcShape__(     *args)
  when TRIANGLE then createTriangleShape__(*args)
  when QUAD     then createQuadShape__(    *args)
  when GROUP    then Shape.new nil, [], context: self
  when nil      then Shape.new context: self
  else raise ArgumentError, "Unknown shape kind '#{kind}'"
  end
end

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

Creates a new vector object.

Parameters:

  • x (Numeric)

    x of new vector

  • y (Numeric)

    y of new vector

  • z (Numeric)

    z of new vector

Returns:

See Also:



3126
3127
3128
# File 'lib/processing/graphics_context.rb', line 3126

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

#curve(cx1, cy1, x1, y1, x2, y2, cx2, cy2) ⇒ nil Also known as: drawCurve

Draws a Catmull-Rom spline curve.

Parameters:

  • cx1 (Numeric)

    horizontal position of beginning control point

  • cy1 (Numeric)

    vertical position of beginning control point

  • x1 (Numeric)

    horizontal position of first point

  • y1 (Numeric)

    vertical position of first point

  • x2 (Numeric)

    horizontal position of second point

  • y2 (Numeric)

    vertical position of second point

  • cx2 (Numeric)

    horizontal position of ending control point

  • cy2 (Numeric)

    vertical position of ending control point

Returns:

  • (nil)

    nil

See Also:



1692
1693
1694
1695
1696
1697
1698
# File 'lib/processing/graphics_context.rb', line 1692

def curve(cx1, cy1, x1, y1, x2, y2, cx2, cy2)
  assertDrawing__
  @painter__.nsegment = @curveDetail__
  @painter__.curve cx1, cy1, x1, y1, x2, y2, cx2, cy2
  @painter__.nsegment = 0
  nil
end

#curveDetail(detail) ⇒ nil

Sets the resolution at which curves display. The default value is 20 while the minimum value is 3.

Parameters:

  • detail (Numeric)

    resolution of the curves

Returns:

  • (nil)

    nil

See Also:



1109
1110
1111
1112
1113
# File 'lib/processing/graphics_context.rb', line 1109

def curveDetail(detail)
  detail = 3 if detail < 3
  @curveDetail__ = detail
  nil
end

#curvePoint(a, b, c, d, t) ⇒ Numeric

Evaluates the curve at point t for points a, b, c, d.

Parameters:

  • a (Numeric)

    coordinate of first control point

  • b (Numeric)

    coordinate of first point on the curve

  • c (Numeric)

    coordinate of second point on the curve

  • d (Numeric)

    coordinate of second control point

  • t (Numeric)

    value between 0.0 and 1.0

Returns:

  • (Numeric)

    interpolated value

See Also:



2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
# File 'lib/processing/graphics_context.rb', line 2909

def curvePoint(a, b, c, d, t)
  s  = @curveTightness__
  t3 = t * t * t
  t2 = t * t
  f1 = ( s - 1.0) / 2.0 * t3 + ( 1.0 - s)       * t2 + (s - 1.0) / 2.0 * t
  f2 = ( s + 3.0) / 2.0 * t3 + (-5.0 - s) / 2.0 * t2 +  1.0
  f3 = (-3.0 - s) / 2.0 * t3 + ( s + 2.0)       * t2 + (1.0 - s) / 2.0 * t
  f4 = ( 1.0 - s) / 2.0 * t3 + ( s - 1.0) / 2.0 * t2
  a * f1 + b * f2 + c * f3 + d * f4
end

#curveTangent(a, b, c, d, t) ⇒ Numeric

Calculates the tangent of a point on a curve.

Parameters:

  • a (Numeric)

    coordinate of first control point

  • b (Numeric)

    coordinate of first point on the curve

  • c (Numeric)

    coordinate of second point on the curve

  • d (Numeric)

    coordinate of second control point

  • t (Numeric)

    value between 0.0 and 1.0

Returns:

  • (Numeric)

    tangent value

See Also:



2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
# File 'lib/processing/graphics_context.rb', line 2933

def curveTangent(a, b, c, d, t)
  s = @curveTightness__
  tt3 = t * t * 3.0
  t2  = t * 2.0
  f1  = ( s - 1.0) / 2.0 * tt3 + ( 1.0 - s)       * t2 + (s - 1.0) / 2.0
  f2  = ( s + 3.0) / 2.0 * tt3 + (-5.0 - s) / 2.0 * t2
  f3  = (-3.0 - s) / 2.0 * tt3 + ( s + 2.0)       * t2 + (1.0 - s) / 2.0
  f4  = ( 1.0 - s) / 2.0 * tt3 + ( s - 1.0) / 2.0 * t2
  a * f1 + b * f2 + c * f3 + d * f4
end

#curveTightness(tightness) ⇒ nil

Sets the quality of curve forms.

Parameters:

  • tightness (Numeric)

    determines how the curve fits to the vertex points

Returns:

  • (nil)

    nil

See Also:



1124
1125
1126
1127
# File 'lib/processing/graphics_context.rb', line 1124

def curveTightness(tightness)
  @curveTightness__ = tightness
  nil
end

#curveVertex(x, y) ⇒ nil

Append curve vertex for shape polygon.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

Returns:

  • (nil)

    nil

See Also:



1950
1951
1952
1953
# File 'lib/processing/graphics_context.rb', line 1950

def curveVertex(x, y)
  (@drawingShape__ or raise "curveVertex() must be called after beginShape()")
    .curveVertex x, y
end

#degrees(radian) ⇒ Numeric

Converts radian to degree.

Parameters:

  • radian (Numeric)

    radian to convert

Returns:

  • (Numeric)

    degree

See Also:



2800
2801
2802
# File 'lib/processing/graphics_context.rb', line 2800

def degrees(radian)
  radian * RAD2DEG__
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

See Also:



2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
# File 'lib/processing/graphics_context.rb', line 2643

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

#ellipse(a, b, c) ⇒ nil #ellipse(a, b, c, d) ⇒ nil Also known as: drawEllipse

Draws an ellipse.

The parameters a, b, c, and d are determined by ellipseMode().

Parameters:

  • a (Numeric)

    horizontal position of the shape, by default

  • b (Numeric)

    vertical position of the shape, by default

  • c (Numeric)

    width of the shape, by default

  • d (Numeric) (defaults to: nil)

    height of the shape, by default

Returns:

  • (nil)

    nil

See Also:



1561
1562
1563
1564
1565
1566
# File 'lib/processing/graphics_context.rb', line 1561

def ellipse(a, b, c, d = nil)
  assertDrawing__
  x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, (d || c)
  @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(centerX, centerY, width, height) RADIUS -> ellipse(centerX, centerY, radiusH, radiusV)



914
915
916
# File 'lib/processing/graphics_context.rb', line 914

def ellipseMode(mode)
  @ellipseMode__ = mode
end

#endContournil

Ends drawing a hole.



1915
1916
1917
1918
# File 'lib/processing/graphics_context.rb', line 1915

def endContour()
  (@drawingShape__ or raise "endContour() must be called after beginShape()")
    .endContour
end

#endShapenil #endShape(CLOSE) ⇒ nil

Ends drawing complex shapes.

Parameters:

  • mode (CLOSE) (defaults to: nil)

    Use CLOSE to create looped polygon

Returns:

  • (nil)

    nil

See Also:



1874
1875
1876
1877
1878
1879
1880
# File 'lib/processing/graphics_context.rb', line 1874

def endShape(mode = nil)
  s = @drawingShape__ or raise "endShape() must be called after beginShape()"
  s.endShape mode
  shape s
  @drawingShape__ = nil
  nil
end

#exp(n) ⇒ Numeric

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

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    result number

See Also:



2559
2560
2561
# File 'lib/processing/graphics_context.rb', line 2559

def exp(n)
  Math.exp n
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.

Parameters:

  • rgb (String)

    color code like ‘#00AAFF’

  • gray (Integer)

    gray value (0..255)

  • r (Integer)

    red value (0..255)

  • g (Integer)

    green value (0..255)

  • b (Integer)

    blue value (0..255)

  • alpha (Integer)

    alpha value (0..255)

Returns:

  • (nil)

    nil

See Also:



1000
1001
1002
1003
# File 'lib/processing/graphics_context.rb', line 1000

def fill(*args)
  @painter__.fill(*toRGBA__(*args))
  nil
end

#filter(*args) ⇒ nil

Applies an image filter to screen.

overload filter(shader) overload filter(type) overload filter(type, param)

Parameters:

Returns:

  • (nil)

    nil

See Also:



1424
1425
1426
1427
# File 'lib/processing/graphics_context.rb', line 1424

def filter(*args)
  @filter__ = Shader.createFilter__(*args)
  nil
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

See Also:



2520
2521
2522
# File 'lib/processing/graphics_context.rb', line 2520

def floor(value)
  value.floor
end

#green(color) ⇒ Numeric

Returns the green value of the color.

Parameters:

  • color (Numeric)

    color value

Returns:

  • (Numeric)

    the green value

See Also:



730
731
732
# File 'lib/processing/graphics_context.rb', line 730

def green(color)
  ((color >> 8) & 0xff) / 255.0 * @colorMaxes__[1]
end

#heightNumeric

Returns the height of the graphics object.



591
592
593
# File 'lib/processing/graphics_context.rb', line 591

def height()
  getInternal__.height
end

#hue(color) ⇒ Numeric

Returns the hue value of the color.

Parameters:

  • color (Numeric)

    color value

Returns:

  • (Numeric)

    the hue value

See Also:



769
770
771
772
# File 'lib/processing/graphics_context.rb', line 769

def hue(color)
  h, = color2raw__(color).to_hsv
  h * (@hsbColor__ ? @colorMaxes__[0] : 1)
end

#image(img, a, b) ⇒ nil #image(img, a, b, c, d) ⇒ nil Also known as: drawImage

Draws an image.

The parameters a, b, c, and d are determined by imageMode().

Parameters:

  • img (Image)

    image to draw

  • a (Numeric)

    horizontal position of the image, by default

  • b (Numeric)

    vertical position of the image, by default

  • c (Numeric) (defaults to: nil)

    width of the image, by default

  • d (Numeric) (defaults to: nil)

    height of the image, by default

Returns:

  • (nil)

    nil

See Also:



1790
1791
1792
1793
1794
1795
# File 'lib/processing/graphics_context.rb', line 1790

def image(img, a, b, c = nil, d = nil)
  assertDrawing__
  x, y, w, h = toXYWH__ @imageMode__, a, b, c || img.width, d || img.height
  img.drawImage__ @painter__, x, y, w, h, fill: getTint__, stroke: :none
  nil
end

#imageMode(mode) ⇒ nil

Sets image mode. Default is CORNER.

CORNER -> image(img, left, top, width, height) CORNERS -> image(img, left, top, right, bottom) CENTER -> image(img, centerX, centerY, width, height)



931
932
933
# File 'lib/processing/graphics_context.rb', line 931

def imageMode(mode)
  @imageMode__ = mode
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

See Also:



2683
2684
2685
# File 'lib/processing/graphics_context.rb', line 2683

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

#lerpColor(color1, color2, amount) ⇒ Integer

Returns the interpolated color between color1 and color2.

Parameters:

  • color1 (Integer)

    the 1st color for interpolation

  • color2 (Integer)

    the 2nd color for interpolation

  • amount (Numeric)

    amount to interpolate

Returns:

  • (Integer)

    interporated color

See Also:



2698
2699
2700
2701
2702
2703
2704
# File 'lib/processing/graphics_context.rb', line 2698

def lerpColor(color1, color2, amount)
  color(
    lerp(red(  color1), red(  color2), amount),
    lerp(green(color1), green(color2), amount),
    lerp(blue( color1), blue( color2), amount),
    lerp(alpha(color1), alpha(color2), amount))
end

#line(x1, y1, x2, y2) ⇒ nil Also known as: drawLine

Draws a line.

Parameters:

  • x1 (Numeric)

    horizontal position of first point

  • y1 (Numeric)

    vertical position of first point

  • x2 (Numeric)

    horizontal position of second point

  • y2 (Numeric)

    vertical position of second point

Returns:

  • (nil)

    nil

See Also:



1499
1500
1501
1502
1503
# File 'lib/processing/graphics_context.rb', line 1499

def line(x1, y1, x2, y2)
  assertDrawing__
  @painter__.line x1, y1, x2, y2
  nil
end

#loadFont(filename) ⇒ Font

Loads font from file.

Parameters:

  • filename (String)

    file name to load font file

Returns:

  • (Font)

    loaded font object

See Also:



3301
3302
3303
3304
3305
3306
3307
# File 'lib/processing/graphics_context.rb', line 3301

def loadFont(filename)
  ext = File.extname filename
  raise "unsupported font type -- '#{ext}'" unless ext =~ /^\.?(ttf|otf)$/i

  filename = httpGet__ filename, ext if filename =~ %r|^https?://|
  Font.new Rays::Font.load filename
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

See Also:



3319
3320
3321
3322
3323
3324
3325
# File 'lib/processing/graphics_context.rb', line 3319

def loadImage(filename, extension = nil)
  ext = extension || File.extname(filename)
  raise "unsupported image type -- '#{ext}'" unless ext =~ /^\.?(png|jpg|gif)$/i

  filename = httpGet__ filename, ext if filename =~ %r|^https?://|
  Image.new Rays::Image.load filename
end

#loadPixelsnil

Loads all pixels to the ‘pixels’ array.



2044
2045
2046
# File 'lib/processing/graphics_context.rb', line 2044

def loadPixels()
  @pixels__ = getInternal__.pixels
end

#loadShader(fragPath) ⇒ Shader #loadShader(fragPath, vertPath) ⇒ Shader

Loads shader file.

Parameters:

  • fragPath (String)

    fragment shader file path

  • vertPath (String) (defaults to: nil)

    vertex shader file path

Returns:

  • (Shader)

    loaded shader object

See Also:



3366
3367
3368
# File 'lib/processing/graphics_context.rb', line 3366

def loadShader(fragPath, vertPath = nil)
  createShader vertPath, fragPath
end

#loadShape(filename) ⇒ Object



3349
3350
3351
# File 'lib/processing/graphics_context.rb', line 3349

def loadShape(filename)
  Processing::SVGLoader.new(self).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

See Also:



2546
2547
2548
# File 'lib/processing/graphics_context.rb', line 2546

def log(n)
  Math.log n
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

See Also:



2617
2618
2619
2620
2621
2622
2623
2624
# File 'lib/processing/graphics_context.rb', line 2617

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

See Also:



2719
2720
2721
# File 'lib/processing/graphics_context.rb', line 2719

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

See Also:



2759
2760
2761
# File 'lib/processing/graphics_context.rb', line 2759

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

See Also:



2739
2740
2741
# File 'lib/processing/graphics_context.rb', line 2739

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

#noClipnil

Disables clipping.

Returns:

  • (nil)

    nil

See Also:



1213
1214
1215
1216
# File 'lib/processing/graphics_context.rb', line 1213

def noClip()
  @painter__.no_clip
  nil
end

#noFillnil

Disables filling.



1012
1013
1014
1015
# File 'lib/processing/graphics_context.rb', line 1012

def noFill()
  @painter__.fill nil
  nil
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)

See Also:



3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
# File 'lib/processing/graphics_context.rb', line 3003

def noise(x, y = 0, z = 0)
  seed, falloff = @noiseSeed__, @noiseFallOff__
  amp           = 0.5
  @noiseOctaves__.times.reduce(0) do |sum|
    value = (Rays.perlin(x, y, z, seed) / 2.0 + 0.5) * amp
    x    *= 2
    y    *= 2
    z    *= 2
    amp  *= falloff
    sum + value
  end
end

#noiseDetail(lod, falloff = nil) ⇒ nil

Adjusts the character and level of detail produced by the Perlin noise function.

Parameters:

  • lod (Numeric)

    number of octaves to be used by the noise

  • falloff (Numeric) (defaults to: nil)

    falloff factor for each octave

Returns:

  • (nil)

    nil

See Also:



3039
3040
3041
3042
# File 'lib/processing/graphics_context.rb', line 3039

def noiseDetail(lod, falloff = nil)
  @noiseOctaves__ = lod     if lod     && lod > 0
  @noiseFallOff__ = falloff if falloff && falloff > 0
end

#noiseSeed(seed) ⇒ nil

Sets the seed value for noise()

Parameters:

  • seed (Numeric)

    seed value

Returns:

  • (nil)

    nil

See Also:



3025
3026
3027
# File 'lib/processing/graphics_context.rb', line 3025

def noiseSeed(seed)
  @noiseSeed__ = Random.new(seed).rand 0.0..1.0
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

See Also:



2668
2669
2670
# File 'lib/processing/graphics_context.rb', line 2668

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

#noStrokenil

Disables drawing stroke.



1050
1051
1052
1053
# File 'lib/processing/graphics_context.rb', line 1050

def noStroke()
  @painter__.stroke nil
  nil
end

#noTintnil

Resets tint color.



1178
1179
1180
# File 'lib/processing/graphics_context.rb', line 1178

def noTint()
  @tint__ = nil
end

#pixelDensityNumeric

Returns the pixel density of the graphics object.



622
623
624
# File 'lib/processing/graphics_context.rb', line 622

def pixelDensity()
  @painter__.pixel_density
end

#pixelHeightNumeric

Returns the height of the graphics object in pixels.

Returns:

  • (Numeric)

    height

See Also:



611
612
613
# File 'lib/processing/graphics_context.rb', line 611

def pixelHeight()
  height * pixelDensity
end

#pixelsArray

An array of all pixels. Call loadPixels() before accessing the array.



2076
2077
2078
# File 'lib/processing/graphics_context.rb', line 2076

def pixels()
  @pixels__
end

#pixelWidthNumeric

Returns the width of the graphics object in pixels.

Returns:

  • (Numeric)

    width

See Also:



601
602
603
# File 'lib/processing/graphics_context.rb', line 601

def pixelWidth()
  width * pixelDensity
end

#point(x, y) ⇒ nil Also known as: drawPoint

Draws a point.

Parameters:

  • x (Numeric)

    horizontal position

  • y (Numeric)

    vertical position

Returns:

  • (nil)

    nil

See Also:



1479
1480
1481
1482
1483
# File 'lib/processing/graphics_context.rb', line 1479

def point(x, y)
  assertDrawing__
  @painter__.point x, y
  nil
end

#popnil

Restore styles and transformations from stack.



2453
2454
2455
2456
# File 'lib/processing/graphics_context.rb', line 2453

def pop()
  popMatrix
  popStyle
end

#popMatrixnil

Pops the current transformation matrix from stack.

Returns:

  • (nil)

    nil

See Also:



2254
2255
2256
2257
2258
2259
# File 'lib/processing/graphics_context.rb', line 2254

def popMatrix()
  assertDrawing__
  raise "matrix stack underflow" if @matrixStack__.empty?
  @painter__.matrix = @matrixStack__.pop
  nil
end

#popStylenil

Restore style values from the style stack.

Returns:

  • (nil)

    nil

See Also:



2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
# File 'lib/processing/graphics_context.rb', line 2390

def popStyle()
  assertDrawing__
  raise "style stack underflow" if @styleStack__.empty?
  @painter__.fill,
  @painter__.stroke,
  @painter__.stroke_width,
  @painter__.stroke_cap,
  @painter__.stroke_join,
  @painter__.miter_limit,
  @painter__.line_height,
  @painter__.clip,
  @painter__.blend_mode,
  @painter__.font,
  @painter__.texture,
  @painter__.texcoord_mode,
  @painter__.texcoord_wrap,
  @painter__.shader,
  @colorMode__,
  @hsbColor__,
  @colorMaxes__,
  @angleMode__,
  @toRad__,
  @toDeg__,
  @fromRad__,
  @fromDeg__,
  @rectMode__,
  @ellipseMode__,
  @imageMode__,
  @shapeMode__,
  @blendMode__,
  @curveDetail__,
  @curveTightness__,
  @bezierDetail__,
  @textAlignH__,
  @textAlignV__,
  @textFont__,
  @tint__ = @styleStack__.pop
  @textFont__.setSize__ @painter__.font.size
  nil
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

See Also:



2573
2574
2575
# File 'lib/processing/graphics_context.rb', line 2573

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

#printMatrixnil

Prints matrix elements to console.

Returns:

  • (nil)

    nil

See Also:



2328
2329
2330
2331
2332
2333
# File 'lib/processing/graphics_context.rb', line 2328

def printMatrix()
  m = @painter__.matrix
  m.transpose! if @p5jsMode__
  print "%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n%f %f %f %f\n" % m.to_a
  nil
end

#push(&block) ⇒ Object

Save current styles and transformations to stack.

Returns:

  • (Object)

    result of the expression at the end of the block

See Also:



2438
2439
2440
2441
2442
2443
2444
# File 'lib/processing/graphics_context.rb', line 2438

def push(&block)
  pushMatrix
  pushStyle
  block.call if block
ensure
  pop if block
end

#pushMatrix(&block) ⇒ Object

Pushes the current transformation matrix to stack.

Returns:

  • (Object)

    result of the expression at the end of the block

See Also:



2240
2241
2242
2243
2244
2245
2246
# File 'lib/processing/graphics_context.rb', line 2240

def pushMatrix(&block)
  assertDrawing__
  @matrixStack__.push @painter__.matrix
  block.call if block
ensure
  popMatrix if block
end

#pushStyle(&block) ⇒ Object

Save current style values to the style stack.

Returns:

  • (Object)

    result of the expression at the end of the block

See Also:



2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
# File 'lib/processing/graphics_context.rb', line 2341

def pushStyle(&block)
  assertDrawing__
  @styleStack__.push [
    @painter__.fill,
    @painter__.stroke,
    @painter__.stroke_width,
    @painter__.stroke_cap,
    @painter__.stroke_join,
    @painter__.miter_limit,
    @painter__.line_height!,
    @painter__.clip,
    @painter__.blend_mode,
    @painter__.font,
    @painter__.texture,
    @painter__.texcoord_mode,
    @painter__.texcoord_wrap,
    @painter__.shader,
    @colorMode__,
    @hsbColor__,
    @colorMaxes__,
    @angleMode__,
    @toRad__,
    @toDeg__,
    @fromRad__,
    @fromDeg__,
    @rectMode__,
    @ellipseMode__,
    @imageMode__,
    @shapeMode__,
    @blendMode__,
    @curveDetail__,
    @curveTightness__,
    @bezierDetail__,
    @textAlignH__,
    @textAlignV__,
    @textFont__,
    @tint__,
  ]
  block.call if block
ensure
  popStyle if block
end

#quad(x1, y1, x2, y2, x3, y3, x4, y4) ⇒ nil Also known as: drawQuad

Draws a quad.

Parameters:

  • x1 (Numeric)

    horizontal position of first point

  • y1 (Numeric)

    vertical position of first point

  • x2 (Numeric)

    horizontal position of second point

  • y2 (Numeric)

    vertical position of second point

  • x3 (Numeric)

    horizontal position of third point

  • y3 (Numeric)

    vertical position of third point

  • x4 (Numeric)

    horizontal position of fourth point

  • y4 (Numeric)

    vertical position of fourth point

Returns:

  • (nil)

    nil

See Also:



1668
1669
1670
1671
1672
# File 'lib/processing/graphics_context.rb', line 1668

def quad(x1, y1, x2, y2, x3, y3, x4, y4)
  assertDrawing__
  @painter__.line x1, y1, x2, y2, x3, y3, x4, y4, loop: true
  nil
end

#quadraticVertex(cx, cy, x3, y3) ⇒ nil

Append quadratic vertex for shape polygon.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

Returns:

  • (nil)

    nil

See Also:



1980
1981
1982
1983
# File 'lib/processing/graphics_context.rb', line 1980

def quadraticVertex(cx, cy, x3, y3)
  (@drawingShape__ or raise "quadraticVertex() must be called after beginShape()")
    .quadraticVertex cx, cy, x3, y3
end

#radians(degree) ⇒ Numeric

Converts degree to radian.

Parameters:

  • degree (Numeric)

    degree to convert

Returns:

  • (Numeric)

    radian

See Also:



2787
2788
2789
# File 'lib/processing/graphics_context.rb', line 2787

def radians(degree)
  degree * DEG2RAD__
end

#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

See Also:



3059
3060
3061
3062
3063
3064
3065
3066
3067
# File 'lib/processing/graphics_context.rb', line 3059

def random(*args)
  if args.first.kind_of? Array
    a = args.first
    a.empty? ? nil : a[@random__.rand a.size]
  else
    high, low = args.reverse
    @random__.rand (low || 0).to_f...(high || 1).to_f
  end
end

#randomGaussian(mean = 0, sd = 1) ⇒ Float

Returns a random number fitting a Gaussian, or normal, distribution.

Parameters:

  • mean (Numeric) (defaults to: 0)

    mean

  • sd (Numeric) (defaults to: 1)

    standard deviation

Returns:

  • (Float)

    random number

See Also:



3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
# File 'lib/processing/graphics_context.rb', line 3093

def randomGaussian(mean = 0, sd = 1)
  value =
    if @nextGaussian__
      x, @nextGaussian__ = @nextGaussian__, nil
      x
    else
      a, b, w = 0, 0, 1
      until w < 1
        a = random(2) - 1
        b = random(2) - 1
        w = a ** 2 + b ** 2
      end
      w = Math.sqrt(-2 * Math.log(w) / w)
      @randomGaussian__ = a * w
      b * w
    end
  value * sd + mean
end

#randomSeed(seed) ⇒ nil

Sets the seed value for random()

Parameters:

  • seed (Numeric)

    seed value

Returns:

  • (nil)

    nil

See Also:



3078
3079
3080
3081
# File 'lib/processing/graphics_context.rb', line 3078

def randomSeed(seed)
  @random__       = Random.new seed
  @nextGaussian__ = nil
end

#rect(a, b, c, d) ⇒ nil #rect(a, b, c, d, r) ⇒ nil #rect(a, b, c, d, tl, tr, br, bl) ⇒ nil Also known as: drawRect

Draws a rectangle.

The parameters a, b, c, and d are determined by rectMode().

Parameters:

  • a (Numeric)

    horizontal position of the shape, by default

  • b (Numeric)

    vertical position of the shape, by default

  • c (Numeric)

    width of the shape, by default

  • d (Numeric)

    height of the shape, by default

  • r (Numeric)

    radius for all corners

  • tl (Numeric)

    radius for top-left corner

  • tr (Numeric)

    radius for top-right corner

  • br (Numeric)

    radius for bottom-right corner

  • bl (Numeric)

    radius for bottom-left corner

Returns:

  • (nil)

    nil

See Also:



1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
# File 'lib/processing/graphics_context.rb', line 1530

def rect(a, b, c, d, *args)
  assertDrawing__
  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(centerX, centerY, width, height) RADIUS -> rect(centerX, centerY, radiusH, radiusV)



896
897
898
# File 'lib/processing/graphics_context.rb', line 896

def rectMode(mode)
  @rectMode__ = mode
end

#red(color) ⇒ Numeric

Returns the red value of the color.

Parameters:

  • color (Numeric)

    color value

Returns:

  • (Numeric)

    the red value

See Also:



717
718
719
# File 'lib/processing/graphics_context.rb', line 717

def red(color)
  ((color >> 16) & 0xff) / 255.0 * @colorMaxes__[0]
end

#renderMode(mode = nil) ⇒ PROCESSING, P5JS

Sets render mode.

Parameters:

  • mode (PROCESSING, P5JS) (defaults to: nil)

    compatible to Processing or p5.js

Returns:



632
633
634
635
636
637
638
# File 'lib/processing/graphics_context.rb', line 632

def renderMode(mode = nil)
  if mode
    @renderMode__ = mode
    @p5jsMode__   = mode == P5JS
  end
  @renderMode__
end

#requestImage(filename, extension = nil) ⇒ Image

Loads image on a new thread. When the image is loading, its width and height will be 0. If an error occurs while loading the image, its width and height wil be -1.

Parameters:

  • filename (String)

    file name to load image

  • extension (String) (defaults to: nil)

    type of image to load (ex. ‘png’)

Returns:

  • (Image)

    loading image object

See Also:



3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
# File 'lib/processing/graphics_context.rb', line 3338

def requestImage(filename, extension = nil)
  img = Image.new nil
  Thread.new filename, extension do |fn, ext|
    loaded = loadImage(fn, ext) or raise
    img.setInternal__ loaded.getInternal__
  rescue
    img.setInternal__ nil, true
  end
  img
end

#resetMatrixnil

Reset current transformation matrix with identity matrix.



2316
2317
2318
2319
2320
# File 'lib/processing/graphics_context.rb', line 2316

def resetMatrix()
  assertDrawing__
  @painter__.matrix = 1
  nil
end

#resetShadernil

Resets shader.



1404
1405
1406
1407
# File 'lib/processing/graphics_context.rb', line 1404

def resetShader()
  @painter__.no_shader
  nil
end

#rotate(angle) ⇒ nil

Applies rotation matrix to current transformation matrix.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



2145
2146
2147
2148
2149
# File 'lib/processing/graphics_context.rb', line 2145

def rotate(angle)
  assertDrawing__
  @painter__.rotate toDegrees__ angle
  nil
end

#rotateX(angle) ⇒ nil

Applies rotation around the x-axis.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



2160
2161
2162
2163
2164
# File 'lib/processing/graphics_context.rb', line 2160

def rotateX(angle)
  assertDrawing__
  @painter__.rotate toDegrees__(angle), 1, 0, 0
  nil
end

#rotateY(angle) ⇒ nil

Applies rotation around the y-axis.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



2175
2176
2177
2178
2179
# File 'lib/processing/graphics_context.rb', line 2175

def rotateY(angle)
  assertDrawing__
  @painter__.rotate toDegrees__(angle), 0, 1, 0
  nil
end

#rotateZ(angle) ⇒ nil

Applies rotation around the z-axis.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



2190
2191
2192
2193
2194
# File 'lib/processing/graphics_context.rb', line 2190

def rotateZ(angle)
  assertDrawing__
  @painter__.rotate toDegrees__(angle), 0, 0, 1
  nil
end

#round(value) ⇒ Numeric

Returns the closest integer number.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    rounded number

See Also:



2533
2534
2535
# File 'lib/processing/graphics_context.rb', line 2533

def round(value)
  value.round
end

#saturation(color) ⇒ Numeric

Returns the saturation value of the color.

Parameters:

  • color (Numeric)

    color value

Returns:

  • (Numeric)

    the saturation value

See Also:



783
784
785
786
# File 'lib/processing/graphics_context.rb', line 783

def saturation(color)
  _, s, = color2raw__(color).to_hsv
  s * (@hsbColor__ ? @colorMaxes__[1] : 1)
end

#save(filename) ⇒ nil

Saves screen image to file.

Parameters:

  • filename (String)

    file name to save image

Returns:

  • (nil)

    nil

See Also:



2089
2090
2091
2092
# File 'lib/processing/graphics_context.rb', line 2089

def save(filename)
  getInternal__.save filename
  nil
end

#scale(s) ⇒ nil #scale(x, y) ⇒ nil #scale(x, y, z) ⇒ nil

Applies scale matrix to current transformation matrix.

Parameters:

  • s (Numeric)

    horizontal and vertical scale

  • x (Numeric)

    horizontal scale

  • y (Numeric) (defaults to: nil)

    vertical scale

  • z (Numeric) (defaults to: 1)

    depth scale

Returns:

  • (nil)

    nil

See Also:



2130
2131
2132
2133
2134
# File 'lib/processing/graphics_context.rb', line 2130

def scale(x, y = nil, z = 1)
  assertDrawing__
  @painter__.scale x, (y || x), z
  nil
end

#shader(shader) ⇒ nil

Sets shader.

Parameters:

  • shader (Shader)

    a shader to apply

Returns:

  • (nil)

    nil

See Also:



1392
1393
1394
1395
# File 'lib/processing/graphics_context.rb', line 1392

def shader(shader)
  @painter__.shader shader&.getInternal__
  nil
end

#shape(img, a, b) ⇒ nil #shape(img, a, b, c, d) ⇒ nil Also known as: drawShape

Draws a shape.

The parameters a, b, c, and d are determined by shapeMode().

Parameters:

  • shp (Shape)

    shape to draw

  • a (Numeric) (defaults to: 0)

    horizontal position of the shape, by default

  • b (Numeric) (defaults to: 0)

    vertical position of the shape, by default

  • c (Numeric) (defaults to: nil)

    width of the shape, by default

  • d (Numeric) (defaults to: nil)

    height of the shape, by default

Returns:

  • (nil)

    nil

See Also:



1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
# File 'lib/processing/graphics_context.rb', line 1816

def shape(shp, a = 0, b = 0, c = nil, d = nil)
  assertDrawing__
  return nil unless shp.isVisible

  drawWithTexture__ do |_|
    if c || d || @shapeMode__ != CORNER
      x, y, w, h = toXYWH__ @shapeMode__, a, b, c || shp.width, d || shp.height
      shp.draw__ @painter__, x, y, w, h
    else
      shp.draw__ @painter__, a, b
    end
  end
  nil
end

#shapeMode(mode) ⇒ nil

Sets shape mode. Default is CORNER.

CORNER -> shape(shp, left, top, width, height) CORNERS -> shape(shp, left, top, right, bottom) CENTER -> shape(shp, centerX, centerY, width, height)

Parameters:

Returns:

  • (nil)

    nil

See Also:



947
948
949
# File 'lib/processing/graphics_context.rb', line 947

def shapeMode(mode)
  @shapeMode__ = mode
end

#shearX(angle) ⇒ nil

Applies shear around the x-axis.

Parameters:

  • angle (Numeric)

    angle for shearing

Returns:

  • (nil)

    nil

See Also:



2205
2206
2207
2208
2209
2210
2211
2212
2213
# File 'lib/processing/graphics_context.rb', line 2205

def shearX(angle)
  t = Math.tan toRadians__(angle)
  @painter__.matrix *= Rays::Matrix.new(
    1, t, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1)
  nil
end

#shearY(angle) ⇒ nil

Applies shear around the y-axis.

Parameters:

  • angle (Numeric)

    angle for shearing

Returns:

  • (nil)

    nil

See Also:



2224
2225
2226
2227
2228
2229
2230
2231
2232
# File 'lib/processing/graphics_context.rb', line 2224

def shearY(angle)
  t = Math.tan toRadians__(angle)
  @painter__.matrix *= Rays::Matrix.new(
    1, 0, 0, 0,
    t, 1, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1)
  nil
end

#sin(angle) ⇒ Numeric

Returns the sine of an angle.

Parameters:

  • angle (Numeric)

    angle in radians

Returns:

  • (Numeric)

    the sine

See Also:



2813
2814
2815
# File 'lib/processing/graphics_context.rb', line 2813

def sin(angle)
  Math.sin angle
end

#sq(value) ⇒ Numeric

Returns squared value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    squared value

See Also:



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

def sq(value)
  value * value
end

#sqrt(value) ⇒ Numeric

Returns squared value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    squared value

See Also:



2599
2600
2601
# File 'lib/processing/graphics_context.rb', line 2599

def sqrt(value)
  Math.sqrt value
end

#square(x, y, extent) ⇒ nil Also known as: drawSquare

Draws a square.

Parameters:

  • x (Numeric)

    horizontal position of the shape

  • y (Numeric)

    vertical position of the shape

  • extent (Numeric)

    width and height of the shape

Returns:

  • (nil)

    nil

See Also:



1624
1625
1626
# File 'lib/processing/graphics_context.rb', line 1624

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.

Parameters:

  • rgb (String)

    color code like ‘#00AAFF’

  • gray (Integer)

    gray value (0..255)

  • r (Integer)

    red value (0..255)

  • g (Integer)

    green value (0..255)

  • b (Integer)

    blue value (0..255)

  • alpha (Integer)

    alpha value (0..255)

Returns:

  • (nil)

    nil

See Also:



1038
1039
1040
1041
# File 'lib/processing/graphics_context.rb', line 1038

def stroke(*args)
  @painter__.stroke(*toRGBA__(*args))
  nil
end

#strokeCap(cap) ⇒ nil

Sets stroke cap mode. The default cap if ROUND.



1079
1080
1081
1082
# File 'lib/processing/graphics_context.rb', line 1079

def strokeCap(cap)
  @painter__.stroke_cap cap
  nil
end

#strokeJoin(join) ⇒ nil

Sets stroke join mode. The default join is MITER.



1094
1095
1096
1097
# File 'lib/processing/graphics_context.rb', line 1094

def strokeJoin(join)
  @painter__.stroke_join join
  nil
end

#strokeWeight(weight) ⇒ nil

Sets stroke weight.

Parameters:

  • weight (Numeric)

    width of stroke

Returns:

  • (nil)

    nil

See Also:



1064
1065
1066
1067
# File 'lib/processing/graphics_context.rb', line 1064

def strokeWeight(weight)
  @painter__.stroke_width weight
  nil
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

See Also:



2839
2840
2841
# File 'lib/processing/graphics_context.rb', line 2839

def tan(angle)
  Math.tan angle
end

#text(str) ⇒ nil #text(str, x, y) ⇒ nil #text(str, a, b, c, d) ⇒ nil Also known as: drawText

Draws a text.

The parameters a, b, c, and d are determined by rectMode().

Parameters:

  • str (String)

    text to draw

  • x (Numeric)

    horizontal position of the text

  • y (Numeric)

    vertical position of the text

  • a (Numeric)

    horizontal position of the text, by default

  • b (Numeric)

    vertical position of the text, by default

  • c (Numeric)

    width of the text, by default

  • d (Numeric)

    height of the text, by default

Returns:

  • (nil)

    nil

See Also:



1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
# File 'lib/processing/graphics_context.rb', line 1749

def text(str, x, y, x2 = nil, y2 = nil)
  assertDrawing__
  if x2
    raise ArgumentError, "missing y2 parameter" unless y2
    x, y, w, h = toXYWH__ @rectMode__, x, y, x2, y2
    case @textAlignH__
    when RIGHT  then x +=  w - @painter__.font.width(str)
    when CENTER then x += (w - @painter__.font.width(str)) / 2
    end
    case @textAlignV__
    when BOTTOM then y +=  h - @painter__.font.height
    when CENTER then y += (h - @painter__.font.height) / 2
    else
    end
  else
    y -= @painter__.font.ascent
  end
  @painter__.text str, x, y
  nil
end

#textAlign(horizontal, vertical = BASELINE) ⇒ nil

Sets the alignment for drawing text.

Parameters:

Returns:

  • (nil)

    nil

See Also:



1309
1310
1311
1312
1313
# File 'lib/processing/graphics_context.rb', line 1309

def textAlign(horizontal, vertical = BASELINE)
  @textAlignH__ = horizontal
  @textAlignV__ = vertical
  nil
end

#textAscentNumeric

Returns ascent of the current font at its current size.



1284
1285
1286
# File 'lib/processing/graphics_context.rb', line 1284

def textAscent()
  @painter__.font.ascent
end

#textDescentNumeric

Returns descent of the current font at its current size.



1295
1296
1297
# File 'lib/processing/graphics_context.rb', line 1295

def textDescent()
  @painter__.font.descent
end

#textFontFont #textFont(font) ⇒ Font #textFont(name) ⇒ Font #textFont(font, size) ⇒ Font #textFont(name, size) ⇒ Font

Sets text font. (Passing a font name as the first parameter is deprecated)

Parameters:

  • font (Font) (defaults to: nil)

    font

  • name (String)

    font name

  • size (Numeric) (defaults to: nil)

    font size (max 256)

Returns:

  • (Font)

    current font

See Also:



1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/processing/graphics_context.rb', line 1236

def textFont(font = nil, size = nil)
  if font != nil || size != nil
    size = FONT_SIZE_MAX__ if size && size > FONT_SIZE_MAX__
    if font.nil? || font.kind_of?(String)
      font = createFont font, size
    elsif size
      font.setSize__ size
    end
    @painter__.font = font.getInternal__
    @textFont__     = font
  end
  @textFont__
end

#textLeadingNumeric #textLeading(leading) ⇒ Numeric

Sets the spacing between lines of text in units of pixels.

Parameters:

  • leading (Numeric) (defaults to: nil)

    the size in pixels for spacing between lines

Returns:

  • (Numeric)

    current spacing

See Also:



1327
1328
1329
1330
# File 'lib/processing/graphics_context.rb', line 1327

def textLeading(leading = nil)
  @painter__.line_height = leading if leading
  @painter__.line_height
end

#textSize(size) ⇒ nil

Sets text size.

Parameters:

  • size (Numeric)

    font size (max 256)

Returns:

  • (nil)

    nil

See Also:



1259
1260
1261
1262
# File 'lib/processing/graphics_context.rb', line 1259

def textSize(size)
  textFont @textFont__, size
  nil
end

#texture(image) ⇒ nil

Sets texture.

Parameters:

  • image (Image)

    texture image

Returns:

  • (nil)

    nil

See Also:



1341
1342
1343
1344
# File 'lib/processing/graphics_context.rb', line 1341

def texture(image)
  @painter__.texture image&.getInternal__
  nil
end

#textureMode(mode) ⇒ nil

Sets the coordinate space for texture mapping.

Parameters:

  • mode (IMAGE, NORMAL)

    image coordinate, or normalized coordinate

Returns:

  • (nil)

    nil

See Also:



1364
1365
1366
1367
# File 'lib/processing/graphics_context.rb', line 1364

def textureMode(mode)
  @painter__.texcoord_mode = mode
  nil
end

#textureWrap(wrap) ⇒ nil

Sets the texture wrapping mode.

Parameters:

  • wrap (CLAMP, REPEAT)

    how texutres behave when go outside of the range

Returns:

  • (nil)

    nil

See Also:



1378
1379
1380
1381
# File 'lib/processing/graphics_context.rb', line 1378

def textureWrap(wrap)
  @painter__.texcoord_wrap = wrap
  nil
end

#textWidth(str) ⇒ Numeric

Returns the width of the text.

Parameters:

  • str (String)

    text string

Returns:

  • (Numeric)

    width of the text

See Also:



1273
1274
1275
# File 'lib/processing/graphics_context.rb', line 1273

def textWidth(str)
  @painter__.font.width str
end

#tint(rgb) ⇒ nil #tint(rgb, alpha) ⇒ nil #tint(gray) ⇒ nil #tint(gray, alpha) ⇒ nil #tint(r, g, b) ⇒ nil #tint(r, g, b, alpha) ⇒ nil

Sets fill color for drawing images.

Parameters:

  • rgb (String)

    color code like ‘#00AAFF’

  • gray (Integer)

    gray value (0..255)

  • r (Integer)

    red value (0..255)

  • g (Integer)

    green value (0..255)

  • b (Integer)

    blue value (0..255)

  • alpha (Integer)

    alpha value (0..255)

Returns:

  • (nil)

    nil

See Also:



1166
1167
1168
1169
# File 'lib/processing/graphics_context.rb', line 1166

def tint(*args)
  @tint__ = args
  nil
end

#translate(x, y) ⇒ nil #translate(x, y, z) ⇒ nil

Applies translation matrix to current transformation matrix.

Parameters:

  • x (Numeric)

    left/right translation

  • y (Numeric)

    up/down translation

  • z (Numeric) (defaults to: 0)

    forward/backward translation

Returns:

  • (nil)

    nil

See Also:



2108
2109
2110
2111
2112
# File 'lib/processing/graphics_context.rb', line 2108

def translate(x, y, z = 0)
  assertDrawing__
  @painter__.translate x, y, z
  nil
end

#triangle(x1, y1, x2, y2, x3, y3) ⇒ nil Also known as: drawTriangle

Draws a triangle.

Parameters:

  • x1 (Numeric)

    horizontal position of first point

  • y1 (Numeric)

    vertical position of first point

  • x2 (Numeric)

    horizontal position of second point

  • y2 (Numeric)

    vertical position of second point

  • x3 (Numeric)

    horizontal position of third point

  • y3 (Numeric)

    vertical position of third point

Returns:

  • (nil)

    nil

See Also:



1644
1645
1646
1647
1648
# File 'lib/processing/graphics_context.rb', line 1644

def triangle(x1, y1, x2, y2, x3, y3)
  assertDrawing__
  @painter__.line x1, y1, x2, y2, x3, y3, loop: true
  nil
end

#updatePixels(&block) ⇒ nil

Update the image pixels with the ‘pixels’ array.



2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
# File 'lib/processing/graphics_context.rb', line 2055

def updatePixels(&block)
  return if !block && !@pixels__
  if block
    loadPixels
    block.call pixels
  end
  getInternal__.tap do |img|
    img.pixels = @pixels__
    img.paint {} # update texture and set modifiied
  end
  @pixels__ = nil
end

#vertex(x, y) ⇒ nil #vertex(x, y, u, v) ⇒ nil

Append vertex for shape polygon.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

  • u (Numeric) (defaults to: nil)

    u texture coordinate of vertex

  • v (Numeric) (defaults to: nil)

    v texture coordinate of vertex

Returns:

  • (nil)

    nil

See Also:



1935
1936
1937
1938
# File 'lib/processing/graphics_context.rb', line 1935

def vertex(x, y, u = nil, v = nil)
  (@drawingShape__ or raise "vertex() must be called after beginShape()")
    .vertex x, y, u, v
end

#widthNumeric

Returns the width of the graphics object.



580
581
582
# File 'lib/processing/graphics_context.rb', line 580

def width()
  getInternal__.width
end