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
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() and imageMode().

:corner
CORNERS =

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

:corners
CENTER =

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

:center
RADIUS =

Mode for rectMode() and ellipseMode().

:radius
PROJECT =

Mode for strokeCap().

:square
MITER =

Mode for strokeJoin().

:miter
ROUND =

Mode for strokeCap() and strokeJoin().

:round
SQUARE =

Mode for strokeCap() and strokeJoin().

:butt
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
THRESHOLD =

Filter type for filter()

:threshold
GRAY =

Filter type for filter()

:gray
INVERT =

Filter type for filter()

:invert
BLUR =

Filter type for filter()

:blur
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

Instance Method Summary collapse

Instance Method Details

#abs(value) ⇒ Numeric

Returns the absolute number of the value.



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

def abs(value)
  value.abs
end

#acos(value) ⇒ Numeric

Returns the inverse of cos().



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

def acos(value)
  Math.acos value
end

#alpha(color) ⇒ Numeric

Returns the red value of the color.



376
377
378
# File 'lib/processing/graphics_context.rb', line 376

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

#angleMode(mode = nil) ⇒ RADIANS, DEGREES

Sets angle mode.



415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/processing/graphics_context.rb', line 415

def angleMode(mode = nil)
  if mode != nil
    @angleMode__  = mode
    @angleScale__ =
      case mode.downcase.to_sym
      when RADIANS then RAD2DEG__
      when DEGREES then 1.0
      else raise ArgumentError, "invalid angle mode: #{mode}"
      end
  end
  @angleMode__
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().



910
911
912
913
914
915
916
917
# File 'lib/processing/graphics_context.rb', line 910

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

#asin(value) ⇒ Numeric

Returns the inverse of sin().



1615
1616
1617
# File 'lib/processing/graphics_context.rb', line 1615

def asin(value)
  Math.asin value
end

#atan(value) ⇒ Numeric

Returns the inverse of tan().



1635
1636
1637
# File 'lib/processing/graphics_context.rb', line 1635

def atan(value)
  Math.atan value
end

#atan2(y, x) ⇒ Numeric

Returns the angle from a specified point.



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

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.



784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/processing/graphics_context.rb', line 784

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

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

Draws a Bezier spline curve.



1009
1010
1011
1012
1013
# File 'lib/processing/graphics_context.rb', line 1009

def bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2)
  assertDrawing__
  @painter__.bezier x1, y1, cx1, cy1, cx2, cy2, x2, y2
  nil
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.



1122
1123
1124
1125
1126
1127
1128
1129
# File 'lib/processing/graphics_context.rb', line 1122

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

#blendMode(mode = nil) ⇒ nil

Sets blend mode. Default is BLEND.



512
513
514
515
516
517
518
# File 'lib/processing/graphics_context.rb', line 512

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.



368
369
370
# File 'lib/processing/graphics_context.rb', line 368

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

#ceil(value) ⇒ Numeric

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



1336
1337
1338
# File 'lib/processing/graphics_context.rb', line 1336

def ceil(value)
  value.ceil
end

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

Draws a circle.



891
892
893
# File 'lib/processing/graphics_context.rb', line 891

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

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

Limits the drawable rectangle.

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



659
660
661
662
663
# File 'lib/processing/graphics_context.rb', line 659

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.



342
343
344
345
346
# File 'lib/processing/graphics_context.rb', line 342

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.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/processing/graphics_context.rb', line 311

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.



1555
1556
1557
# File 'lib/processing/graphics_context.rb', line 1555

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.



1100
1101
1102
# File 'lib/processing/graphics_context.rb', line 1100

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.



1595
1596
1597
# File 'lib/processing/graphics_context.rb', line 1595

def cos(angle)
  Math.cos angle
end

#createCapture(*args) ⇒ Capture

Creates a camera object as a video input device.



1772
1773
1774
# File 'lib/processing/graphics_context.rb', line 1772

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

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

Creates a new off-screen graphics context object.



1726
1727
1728
# File 'lib/processing/graphics_context.rb', line 1726

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.

Raises:

  • (ArgumentError)


1712
1713
1714
1715
1716
# File 'lib/processing/graphics_context.rb', line 1712

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__;

“‘



1762
1763
1764
1765
1766
# File 'lib/processing/graphics_context.rb', line 1762

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

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

Creates a new vector.



1697
1698
1699
# File 'lib/processing/graphics_context.rb', line 1697

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.



988
989
990
991
992
# File 'lib/processing/graphics_context.rb', line 988

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

#degrees(radian) ⇒ Numeric

Converts radian to degree.



1575
1576
1577
# File 'lib/processing/graphics_context.rb', line 1575

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.



1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
# File 'lib/processing/graphics_context.rb', line 1445

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, d) ⇒ nil Also known as: drawEllipse

Draws an ellipse.

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



874
875
876
877
878
879
# File 'lib/processing/graphics_context.rb', line 874

def ellipse(a, b, c, d)
  assertDrawing__
  x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
  @painter__.ellipse x, y, w, h
  nil
end

#ellipseMode(mode) ⇒ nil

Sets ellipse mode. Default is CENTER.

CORNER -> ellipse(left, top, width, height) CORNERS -> ellipse(left, top, right, bottom) CENTER -> ellipse(center_x, center_y, width, height) RADIUS -> ellipse(center_x, center_y, radius_h, radius_v)



477
478
479
# File 'lib/processing/graphics_context.rb', line 477

def ellipseMode(mode)
  @ellipseMode__ = mode
end

#exp(n) ⇒ Numeric

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



1376
1377
1378
# File 'lib/processing/graphics_context.rb', line 1376

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.



538
539
540
541
# File 'lib/processing/graphics_context.rb', line 538

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

#filter(*args) ⇒ Object

Applies an image filter to screen.

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



762
763
764
# File 'lib/processing/graphics_context.rb', line 762

def filter(*args)
  @filter__ = Shader.createFilter__(*args)
end

#floor(value) ⇒ Numeric

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



1346
1347
1348
# File 'lib/processing/graphics_context.rb', line 1346

def floor(value)
  value.floor
end

#green(color) ⇒ Numeric

Returns the green value of the color.



360
361
362
# File 'lib/processing/graphics_context.rb', line 360

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

#heightNumeric

Returns the height of the graphics object.



267
268
269
# File 'lib/processing/graphics_context.rb', line 267

def height()
  @image__.height
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().



1073
1074
1075
1076
1077
1078
1079
# File 'lib/processing/graphics_context.rb', line 1073

def image(img, a, b, c = nil, d = nil)
  assertDrawing__
  x, y, w, h = toXYWH__ @imageMode__, a, b, c || img.width, d || img.height
  tint       = @tint__ ? toRGBA__(*@tint__) : 1
  img.drawImage__ @painter__, x, y, w, h, fill: tint, 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, center_x, center_y, width, height)



491
492
493
# File 'lib/processing/graphics_context.rb', line 491

def imageMode(mode)
  @imageMode__ = mode
end

#lerp(start, stop, amount) ⇒ Numeric

Returns the interpolated number between range start..stop.



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

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

#lerpColor(color1, color2, amount) ⇒ Integer

Returns the interpolated color between color1 and color2.



1491
1492
1493
1494
1495
1496
1497
# File 'lib/processing/graphics_context.rb', line 1491

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.



821
822
823
824
825
# File 'lib/processing/graphics_context.rb', line 821

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

#loadImage(filename, extension = nil) ⇒ Image

Loads image.



1783
1784
1785
1786
# File 'lib/processing/graphics_context.rb', line 1783

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

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

Loads shader file.



1798
1799
1800
# File 'lib/processing/graphics_context.rb', line 1798

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

#log(n) ⇒ Numeric

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



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

def log(n)
  Math.log n
end

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

Returns the magnitude (or length) of a vector.



1422
1423
1424
1425
1426
1427
1428
1429
# File 'lib/processing/graphics_context.rb', line 1422

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.



1509
1510
1511
# File 'lib/processing/graphics_context.rb', line 1509

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.



1543
1544
1545
# File 'lib/processing/graphics_context.rb', line 1543

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

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

Returns minimum value.



1526
1527
1528
# File 'lib/processing/graphics_context.rb', line 1526

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

#noClipnil

Disables clipping.



669
670
671
672
# File 'lib/processing/graphics_context.rb', line 669

def noClip()
  @painter__.no_clip
  nil
end

#noFillnil

Disables filling.



547
548
549
550
# File 'lib/processing/graphics_context.rb', line 547

def noFill()
  @painter__.fill nil
  nil
end

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

Returns the perlin noise value.



1662
1663
1664
# File 'lib/processing/graphics_context.rb', line 1662

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

#norm(value, start, stop) ⇒ Numeric

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



1467
1468
1469
# File 'lib/processing/graphics_context.rb', line 1467

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

#noStrokenil

Disables drawing stroke.



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

def noStroke()
  @painter__.stroke nil
  nil
end

#noTintnil

Resets tint color.



644
645
646
# File 'lib/processing/graphics_context.rb', line 644

def noTint()
  @tint__ = nil
end

#pixelDensityNumeric

Returns the pixel density of the graphics object.



291
292
293
# File 'lib/processing/graphics_context.rb', line 291

def pixelDensity()
  @painter__.pixel_density
end

#pixelHeightNumeric

Returns the height of the graphics object in pixels.



283
284
285
# File 'lib/processing/graphics_context.rb', line 283

def pixelHeight()
  height * pixelDensity
end

#pixelWidthNumeric

Returns the width of the graphics object in pixels.



275
276
277
# File 'lib/processing/graphics_context.rb', line 275

def pixelWidth()
  width * pixelDensity
end

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

Draws a point.



804
805
806
807
808
# File 'lib/processing/graphics_context.rb', line 804

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

#popnil

Restore styles and transformations from stack.



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

def pop()
  popMatrix
  popStyle
end

#popMatrixnil

Pops the current transformation matrix from stack.



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

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

#popStylenil

Restore style values from the style stack.



1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
# File 'lib/processing/graphics_context.rb', line 1253

def popStyle()
  assertDrawing__
  raise "style stack underflow" if @styleStack__.empty?
  @painter__.fill,
  @painter__.stroke,
  @painter__.stroke_width,
  @painter__.stroke_cap,
  @painter__.stroke_join,
  @painter__.clip,
  @painter__.blend_mode,
  @painter__.font,
  @painter__.shader,
  @hsbColor__,
  @colorMaxes__,
  @angleScale__,
  @rectMode__,
  @ellipseMode__,
  @imageMode__,
  @textAlignH__,
  @textAlignV__,
  @tint__ = @styleStack__.pop
  nil
end

#pow(value, exponent) ⇒ Numeric

Returns value raised to the power of exponent.



1387
1388
1389
# File 'lib/processing/graphics_context.rb', line 1387

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

#push(&block) ⇒ Object

Save current styles and transformations to stack.



1281
1282
1283
1284
1285
1286
1287
# File 'lib/processing/graphics_context.rb', line 1281

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

#pushMatrix(&block) ⇒ Object

Pushes the current transformation matrix to stack.



1189
1190
1191
1192
1193
1194
1195
# File 'lib/processing/graphics_context.rb', line 1189

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.



1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
# File 'lib/processing/graphics_context.rb', line 1222

def pushStyle(&block)
  assertDrawing__
  @styleStack__.push [
    @painter__.fill,
    @painter__.stroke,
    @painter__.stroke_width,
    @painter__.stroke_cap,
    @painter__.stroke_join,
    @painter__.clip,
    @painter__.blend_mode,
    @painter__.font,
    @painter__.shader,
    @hsbColor__,
    @colorMaxes__,
    @angleScale__,
    @rectMode__,
    @ellipseMode__,
    @imageMode__,
    @textAlignH__,
    @textAlignV__,
    @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.



967
968
969
970
971
# File 'lib/processing/graphics_context.rb', line 967

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

#radians(degree) ⇒ Numeric

Converts degree to radian.



1565
1566
1567
# File 'lib/processing/graphics_context.rb', line 1565

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



1679
1680
1681
1682
1683
# File 'lib/processing/graphics_context.rb', line 1679

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

#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().



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

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(center_x, center_y, width, height) RADIUS -> rect(center_x, center_y, radius_h, radius_v)



462
463
464
# File 'lib/processing/graphics_context.rb', line 462

def rectMode(mode)
  @rectMode__ = mode
end

#red(color) ⇒ Numeric

Returns the red value of the color.



352
353
354
# File 'lib/processing/graphics_context.rb', line 352

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

#resetMatrixnil

Reset current transformation matrix with identity matrix.



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

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

#resetShadernil

Resets shader.



747
748
749
750
# File 'lib/processing/graphics_context.rb', line 747

def resetShader()
  @painter__.no_shader
  nil
end

#rotate(angle) ⇒ nil

Applies rotation matrix to current transformation matrix.



1179
1180
1181
1182
1183
# File 'lib/processing/graphics_context.rb', line 1179

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

#round(value) ⇒ Numeric

Returns the closest integer number.



1356
1357
1358
# File 'lib/processing/graphics_context.rb', line 1356

def round(value)
  value.round
end

#save(filename) ⇒ Object

Saves screen image to file.



1135
1136
1137
# File 'lib/processing/graphics_context.rb', line 1135

def save(filename)
  @window__.canvas_image.save filename
end

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

Applies scale matrix to current transformation matrix.



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

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

#shader(shader) ⇒ nil

Sets shader.



738
739
740
741
# File 'lib/processing/graphics_context.rb', line 738

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

#sin(angle) ⇒ Numeric

Returns the sine of an angle.



1585
1586
1587
# File 'lib/processing/graphics_context.rb', line 1585

def sin(angle)
  Math.sin angle
end

#sq(value) ⇒ Numeric

Returns squared value.



1397
1398
1399
# File 'lib/processing/graphics_context.rb', line 1397

def sq(value)
  value * value
end

#sqrt(value) ⇒ Numeric

Returns squared value.



1407
1408
1409
# File 'lib/processing/graphics_context.rb', line 1407

def sqrt(value)
  Math.sqrt value
end

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

Draws a square.



929
930
931
# File 'lib/processing/graphics_context.rb', line 929

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.



570
571
572
573
# File 'lib/processing/graphics_context.rb', line 570

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

#strokeCap(cap) ⇒ nil

Sets stroke cap mode.



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

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

#strokeJoin(join) ⇒ nil

Sets stroke join mode.



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

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

#strokeWeight(weight) ⇒ nil

Sets stroke weight.



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

def strokeWeight(weight)
  @painter__.stroke_width weight
  nil
end

#tan(angle) ⇒ Numeric

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



1605
1606
1607
# File 'lib/processing/graphics_context.rb', line 1605

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().



1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'lib/processing/graphics_context.rb', line 1035

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) ⇒ Object



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

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

#textAscentObject



707
708
709
# File 'lib/processing/graphics_context.rb', line 707

def textAscent()
  @painter__.font.ascent
end

#textDescentObject



711
712
713
# File 'lib/processing/graphics_context.rb', line 711

def textDescent()
  @painter__.font.descent
end

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

Sets font.



687
688
689
690
# File 'lib/processing/graphics_context.rb', line 687

def textFont(font = nil, size = nil)
  setFont__ font, size if font || size
  Font.new @painter__.font
end

#textSize(size) ⇒ nil

Sets text size.



698
699
700
701
# File 'lib/processing/graphics_context.rb', line 698

def textSize(size)
  setFont__ nil, size
  nil
end

#textWidth(str) ⇒ Object



703
704
705
# File 'lib/processing/graphics_context.rb', line 703

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.



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

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

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

Applies translation matrix to current transformation matrix.



1150
1151
1152
1153
1154
# File 'lib/processing/graphics_context.rb', line 1150

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.



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

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

#widthNumeric

Returns the width of the graphics object.



259
260
261
# File 'lib/processing/graphics_context.rb', line 259

def width()
  @image__.width
end