Module: Processing::GraphicsContext
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
-
#abs(value) ⇒ Numeric
Returns the absolute number of the value.
-
#acos(value) ⇒ Numeric
Returns the inverse of cos().
-
#angleMode(mode) ⇒ nil
Sets angle mode.
-
#arc(a, b, c, d, start, stop) ⇒ nil
Draws an arc.
-
#asin(value) ⇒ Numeric
Returns the inverse of sin().
-
#atan(value) ⇒ Numeric
Returns the inverse of tan().
-
#atan2(y, x) ⇒ Numeric
Returns the angle from a specified point.
-
#background(*args) ⇒ nil
Clears screen.
-
#bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2) ⇒ nil
Draws a Bezier spline curve.
-
#blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode) ⇒ nil
Blends image.
-
#blendMode(mode) ⇒ nil
Sets blend mode.
-
#ceil(value) ⇒ Numeric
Returns the closest integer number greater than or equal to the value.
-
#circle(x, y, extent) ⇒ nil
Draws a circle.
-
#clip(a, b, c, d) ⇒ nil
Limits the drawable rectangle.
-
#colorMode(mode, *maxes) ⇒ nil
Sets color mode and max color values.
-
#constrain(value, min, max) ⇒ Numeric
Constrains the number between min..max.
-
#copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh) ⇒ nil
Copies image.
-
#cos(angle) ⇒ Numeric
Returns the cosine of an angle.
-
#createCapture(*args) ⇒ Capture
Creates a camera object as a video input device.
-
#createGraphics(width, height) ⇒ Graphics
Creates a new off-screen graphics context object.
-
#createImage(w, h, format = RGBA) ⇒ Image
Creates a new image.
-
#createShader(vert, frag) ⇒ Shader
Creates a shader object.
-
#createVector(*args) ⇒ Vector
Creates a new vector.
-
#curve(cx1, cy1, x1, y1, x2, y2, cx2, cy2) ⇒ nil
Draws a Catmull-Rom spline curve.
-
#degrees(radian) ⇒ Numeric
Converts radian to degree.
-
#dist(*args) ⇒ Numeric
Returns distance between 2 points.
-
#ellipse(a, b, c, d) ⇒ nil
Draws an ellipse.
-
#ellipseMode(mode) ⇒ nil
Sets ellipse mode.
-
#exp(n) ⇒ Numeric
Returns Euler’s number e raised to the power of value.
-
#fill(*args) ⇒ nil
Sets fill color.
-
#filter(*args) ⇒ Object
Applies an image filter to screen.
-
#floor(value) ⇒ Numeric
Returns the closest integer number less than or equal to the value.
- #height ⇒ Object
-
#image(img, a, b, c = nil, d = nil) ⇒ nil
Draws an image.
-
#imageMode(mode) ⇒ nil
Sets image mode.
-
#lerp(start, stop, amount) ⇒ Numeric
Returns the interpolated number between range start..stop.
-
#line(x1, y1, x2, y2) ⇒ nil
Draws a line.
-
#loadImage(filename, extension = nil) ⇒ Image
Loads image.
-
#loadShader(fragPath, vertPath = nil) ⇒ Shader
Loads shader file.
-
#log(n) ⇒ Numeric
Returns the natural logarithm (the base-e logarithm) of a number.
-
#mag(*args) ⇒ Numeric
Returns the magnitude (or length) of a vector.
-
#map(value, start1, stop1, start2, stop2) ⇒ Numeric
Maps a number from range start1..stop1 to range start2..stop2.
-
#max(*args) ⇒ Numeric
Returns maximum value.
-
#min(*args) ⇒ Numeric
Returns minimum value.
-
#noClip ⇒ nil
Disables clipping.
-
#noFill ⇒ nil
Disables filling.
-
#noise(x, y = 0, z = 0) ⇒ Numeric
Returns the perlin noise value.
-
#norm(value, start, stop) ⇒ Numeric
Normalize the value from range start..stop into 0..1.
-
#noStroke ⇒ nil
Disables drawing stroke.
-
#noTint ⇒ nil
Resets tint color.
-
#point(x, y) ⇒ nil
Draws a point.
-
#pop ⇒ nil
Restore styles and transformations from stack.
-
#popMatrix ⇒ nil
Pops the current transformation matrix from stack.
-
#popStyle ⇒ nil
Restore style values from the style stack.
-
#pow(value, exponent) ⇒ Numeric
Returns value raised to the power of exponent.
-
#push(&block) ⇒ nil
Save current styles and transformations to stack.
-
#pushMatrix(&block) ⇒ nil
Pushes the current transformation matrix to stack.
-
#pushStyle(&block) ⇒ nil
Save current style values to the style stack.
-
#quad(x1, y1, x2, y2, x3, y3, x4, y4) ⇒ nil
Draws a quad.
-
#radians(degree) ⇒ Numeric
Converts degree to radian.
-
#random(*args) ⇒ Float
Returns a random number in range low…high.
-
#rect(a, b, c, d, *args) ⇒ nil
Draws a rectangle.
-
#rectMode(mode) ⇒ nil
Sets rect mode.
-
#resetMatrix ⇒ nil
Reset current transformation matrix with identity matrix.
-
#resetShader ⇒ nil
Resets shader.
-
#rotate(angle) ⇒ nil
Applies rotation matrix to current transformation matrix.
-
#round(value) ⇒ Numeric
Returns the closest integer number.
-
#save(filename) ⇒ Object
Saves screen image to file.
-
#scale(x, y) ⇒ nil
Applies scale matrix to current transformation matrix.
-
#shader(shader) ⇒ nil
Sets shader.
-
#sin(angle) ⇒ Numeric
Returns the sine of an angle.
-
#sq(value) ⇒ Numeric
Returns squared value.
-
#sqrt(value) ⇒ Numeric
Returns squared value.
-
#square(x, y, extent) ⇒ nil
Draws a square.
-
#stroke(*args) ⇒ nil
Sets stroke color.
-
#strokeCap(cap) ⇒ nil
Sets stroke cap mode.
-
#strokeJoin(join) ⇒ nil
Sets stroke join mode.
-
#strokeWeight(weight) ⇒ nil
Sets stroke weight.
-
#tan(angle) ⇒ Numeric
Returns the ratio of the sine and cosine of an angle.
-
#text(str, x, y, x2 = nil, y2 = nil) ⇒ nil
Draws a text.
- #textAlign(horizontal, vertical = BASELINE) ⇒ Object
- #textAscent ⇒ Object
- #textDescent ⇒ Object
-
#textFont(font = nil, size = nil) ⇒ Font
Sets font.
-
#textSize(size) ⇒ nil
Sets text size.
- #textWidth(str) ⇒ Object
-
#tint(*args) ⇒ nil
Sets fill color for drawing images.
-
#translate(x, y, z = 0) ⇒ nil
Applies translation matrix to current transformation matrix.
-
#triangle(x1, y1, x2, y2, x3, y3) ⇒ nil
Draws a triangle.
- #width ⇒ Object
Instance Method Details
#abs(value) ⇒ Numeric
Returns the absolute number of the value.
1185 1186 1187 |
# File 'lib/processing/graphics_context.rb', line 1185 def abs(value) value.abs end |
#acos(value) ⇒ Numeric
Returns the inverse of cos().
1468 1469 1470 |
# File 'lib/processing/graphics_context.rb', line 1468 def acos(value) Math.acos value end |
#angleMode(mode) ⇒ nil
Sets angle mode.
324 325 326 327 328 329 330 331 |
# File 'lib/processing/graphics_context.rb', line 324 def 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 nil end |
#arc(a, b, c, d, start, stop) ⇒ nil
Draws an arc.
The parameters a, b, c, and d are determined by ellipseMode().
784 785 786 787 788 789 790 791 |
# File 'lib/processing/graphics_context.rb', line 784 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().
1458 1459 1460 |
# File 'lib/processing/graphics_context.rb', line 1458 def asin(value) Math.asin value end |
#atan(value) ⇒ Numeric
Returns the inverse of tan().
1478 1479 1480 |
# File 'lib/processing/graphics_context.rb', line 1478 def atan(value) Math.atan value end |
#atan2(y, x) ⇒ Numeric
Returns the angle from a specified point.
1489 1490 1491 |
# File 'lib/processing/graphics_context.rb', line 1489 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.
668 669 670 671 672 673 674 675 676 677 678 679 |
# File 'lib/processing/graphics_context.rb', line 668 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
Draws a Bezier spline curve.
873 874 875 876 877 |
# File 'lib/processing/graphics_context.rb', line 873 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.
980 981 982 983 984 |
# File 'lib/processing/graphics_context.rb', line 980 def blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode) assertDrawing__ img ||= self img.drawImage__ @painter__, sx, sy, sw, sh, dx, dy, dw, dh, blend_mode: mode end |
#blendMode(mode) ⇒ nil
Sets blend mode. Default is BLEND.
399 400 401 402 |
# File 'lib/processing/graphics_context.rb', line 399 def blendMode(mode) @painter__.blend_mode = mode nil end |
#ceil(value) ⇒ Numeric
Returns the closest integer number greater than or equal to the value.
1195 1196 1197 |
# File 'lib/processing/graphics_context.rb', line 1195 def ceil(value) value.ceil end |
#circle(x, y, extent) ⇒ nil
Draws a circle.
767 768 769 |
# File 'lib/processing/graphics_context.rb', line 767 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().
543 544 545 546 547 |
# File 'lib/processing/graphics_context.rb', line 543 def clip(a, b, c, d) x, y, w, h = toXYWH__ @imageMode__, a, b, c, d @painter__.clip x, y, w, h nil end |
#colorMode(mode) ⇒ nil #colorMode(mode, max) ⇒ nil #colorMode(mode, max1, max2, max3) ⇒ nil #colorMode(mode, max1, max2, max3, maxA) ⇒ nil
Sets color mode and max color values.
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/processing/graphics_context.rb', line 276 def colorMode(mode, *maxes) 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) @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 nil end |
#constrain(value, min, max) ⇒ Numeric
Constrains the number between min..max.
1398 1399 1400 |
# File 'lib/processing/graphics_context.rb', line 1398 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.
958 959 960 |
# File 'lib/processing/graphics_context.rb', line 958 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.
1438 1439 1440 |
# File 'lib/processing/graphics_context.rb', line 1438 def cos(angle) Math.cos angle end |
#createCapture(*args) ⇒ Capture
Creates a camera object as a video input device.
1614 1615 1616 |
# File 'lib/processing/graphics_context.rb', line 1614 def createCapture(*args) Capture.new(*args) end |
#createGraphics(width, height) ⇒ Graphics
Creates a new off-screen graphics context object.
1568 1569 1570 |
# File 'lib/processing/graphics_context.rb', line 1568 def createGraphics(width, height) Graphics.new width, height end |
#createImage(w, h) ⇒ Image #createImage(w, h, format) ⇒ Image
Creates a new image.
1555 1556 1557 1558 1559 |
# File 'lib/processing/graphics_context.rb', line 1555 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__;
“‘
1604 1605 1606 1607 1608 |
# File 'lib/processing/graphics_context.rb', line 1604 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 |
#createVector ⇒ Vector #createVector(x, y) ⇒ Vector #createVector(x, y, z) ⇒ Vector
Creates a new vector.
1540 1541 1542 |
# File 'lib/processing/graphics_context.rb', line 1540 def createVector(*args) Vector.new(*args, context: self) end |
#curve(cx1, cy1, x1, y1, x2, y2, cx2, cy2) ⇒ nil
Draws a Catmull-Rom spline curve.
854 855 856 857 858 |
# File 'lib/processing/graphics_context.rb', line 854 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.
1418 1419 1420 |
# File 'lib/processing/graphics_context.rb', line 1418 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.
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 |
# File 'lib/processing/graphics_context.rb', line 1304 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
Draws an ellipse.
The parameters a, b, c, and d are determined by ellipseMode().
752 753 754 755 756 757 |
# File 'lib/processing/graphics_context.rb', line 752 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)
364 365 366 |
# File 'lib/processing/graphics_context.rb', line 364 def ellipseMode(mode) @ellipseMode__ = mode end |
#exp(n) ⇒ Numeric
Returns Euler’s number e raised to the power of value.
1235 1236 1237 |
# File 'lib/processing/graphics_context.rb', line 1235 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.
422 423 424 425 |
# File 'lib/processing/graphics_context.rb', line 422 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)
646 647 648 |
# File 'lib/processing/graphics_context.rb', line 646 def filter(*args) @filter__ = Shader.createFilter__(*args) end |
#floor(value) ⇒ Numeric
Returns the closest integer number less than or equal to the value.
1205 1206 1207 |
# File 'lib/processing/graphics_context.rb', line 1205 def floor(value) value.floor end |
#height ⇒ Object
256 257 258 |
# File 'lib/processing/graphics_context.rb', line 256 def height() @image__.height end |
#image(img, a, b) ⇒ nil #image(img, a, b, c, d) ⇒ nil
Draws an image.
The parameters a, b, c, and d are determined by imageMode().
933 934 935 936 937 938 939 |
# File 'lib/processing/graphics_context.rb', line 933 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)
378 379 380 |
# File 'lib/processing/graphics_context.rb', line 378 def imageMode(mode) @imageMode__ = mode end |
#lerp(start, stop, amount) ⇒ Numeric
Returns the interpolated number between range start..stop.
1338 1339 1340 |
# File 'lib/processing/graphics_context.rb', line 1338 def lerp(start, stop, amount) start + (stop - start) * amount end |
#line(x1, y1, x2, y2) ⇒ nil
Draws a line.
703 704 705 706 707 |
# File 'lib/processing/graphics_context.rb', line 703 def line(x1, y1, x2, y2) assertDrawing__ @painter__.line x1, y1, x2, y2 nil end |
#loadImage(filename, extension = nil) ⇒ Image
Loads image.
1625 1626 1627 1628 |
# File 'lib/processing/graphics_context.rb', line 1625 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.
1640 1641 1642 |
# File 'lib/processing/graphics_context.rb', line 1640 def loadShader(fragPath, vertPath = nil) createShader vertPath, fragPath end |
#log(n) ⇒ Numeric
Returns the natural logarithm (the base-e logarithm) of a number.
1225 1226 1227 |
# File 'lib/processing/graphics_context.rb', line 1225 def log(n) Math.log n end |
#mag(x, y) ⇒ Numeric #mag(x, y, z) ⇒ Numeric
Returns the magnitude (or length) of a vector.
1281 1282 1283 1284 1285 1286 1287 1288 |
# File 'lib/processing/graphics_context.rb', line 1281 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.
1352 1353 1354 |
# File 'lib/processing/graphics_context.rb', line 1352 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.
1386 1387 1388 |
# File 'lib/processing/graphics_context.rb', line 1386 def max(*args) args.flatten.max end |
#min(a, b) ⇒ Numeric #min(a, b, c) ⇒ Numeric #min(array) ⇒ Numeric
Returns minimum value.
1369 1370 1371 |
# File 'lib/processing/graphics_context.rb', line 1369 def min(*args) args.flatten.min end |
#noClip ⇒ nil
Disables clipping.
553 554 555 556 |
# File 'lib/processing/graphics_context.rb', line 553 def noClip() @painter__.no_clip nil end |
#noFill ⇒ nil
Disables filling.
431 432 433 434 |
# File 'lib/processing/graphics_context.rb', line 431 def noFill() @painter__.fill nil nil end |
#noise(x) ⇒ Numeric #noise(x, y) ⇒ Numeric #noise(x, y, z) ⇒ Numeric
Returns the perlin noise value.
1505 1506 1507 |
# File 'lib/processing/graphics_context.rb', line 1505 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.
1326 1327 1328 |
# File 'lib/processing/graphics_context.rb', line 1326 def norm(value, start, stop) (value.to_f - start.to_f) / (stop.to_f - start.to_f) end |
#noStroke ⇒ nil
Disables drawing stroke.
463 464 465 466 |
# File 'lib/processing/graphics_context.rb', line 463 def noStroke() @painter__.stroke nil nil end |
#noTint ⇒ nil
Resets tint color.
528 529 530 |
# File 'lib/processing/graphics_context.rb', line 528 def noTint() @tint__ = nil end |
#point(x, y) ⇒ nil
Draws a point.
688 689 690 691 692 |
# File 'lib/processing/graphics_context.rb', line 688 def point(x, y) assertDrawing__ @painter__.line x, y, x, y nil end |
#pop ⇒ nil
Restore styles and transformations from stack.
1152 1153 1154 1155 |
# File 'lib/processing/graphics_context.rb', line 1152 def pop() popMatrix popStyle end |
#popMatrix ⇒ nil
Pops the current transformation matrix from stack.
1057 1058 1059 1060 1061 1062 |
# File 'lib/processing/graphics_context.rb', line 1057 def popMatrix() assertDrawing__ raise "matrix stack underflow" if @matrixStack__.empty? @painter__.matrix = @matrixStack__.pop nil end |
#popStyle ⇒ nil
Restore style values from the style stack.
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 |
# File 'lib/processing/graphics_context.rb', line 1111 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__, @filter__ = @styleStack__.pop nil end |
#pow(value, exponent) ⇒ Numeric
Returns value raised to the power of exponent.
1246 1247 1248 |
# File 'lib/processing/graphics_context.rb', line 1246 def pow(value, exponent) value ** exponent end |
#push(&block) ⇒ nil
Save current styles and transformations to stack.
1140 1141 1142 1143 1144 1145 1146 |
# File 'lib/processing/graphics_context.rb', line 1140 def push(&block) pushMatrix pushStyle block.call if block ensure pop if block end |
#pushMatrix(&block) ⇒ nil
Pushes the current transformation matrix to stack.
1044 1045 1046 1047 1048 1049 1050 1051 |
# File 'lib/processing/graphics_context.rb', line 1044 def pushMatrix(&block) assertDrawing__ @matrixStack__.push @painter__.matrix block.call if block nil ensure popMatrix if block end |
#pushStyle(&block) ⇒ nil
Save current style values to the style stack.
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 |
# File 'lib/processing/graphics_context.rb', line 1078 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__, @filter__, ] block.call if block nil ensure popStyle if block end |
#quad(x1, y1, x2, y2, x3, y3, x4, y4) ⇒ nil
Draws a quad.
835 836 837 838 839 |
# File 'lib/processing/graphics_context.rb', line 835 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.
1408 1409 1410 |
# File 'lib/processing/graphics_context.rb', line 1408 def radians(degree) degree * DEG2RAD__ end |
#random ⇒ Float #random(high) ⇒ Float #random(low, high) ⇒ Float #random(choices) ⇒ Float
Returns a random number in range low…high
1522 1523 1524 1525 1526 |
# File 'lib/processing/graphics_context.rb', line 1522 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
Draws a rectangle.
The parameters a, b, c, and d are determined by rectMode().
729 730 731 732 733 734 735 736 737 738 739 |
# File 'lib/processing/graphics_context.rb', line 729 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)
349 350 351 |
# File 'lib/processing/graphics_context.rb', line 349 def rectMode(mode) @rectMode__ = mode end |
#resetMatrix ⇒ nil
Reset current transformation matrix with identity matrix.
1068 1069 1070 1071 1072 |
# File 'lib/processing/graphics_context.rb', line 1068 def resetMatrix() assertDrawing__ @painter__.matrix = 1 nil end |
#resetShader ⇒ nil
Resets shader.
631 632 633 634 |
# File 'lib/processing/graphics_context.rb', line 631 def resetShader() @painter__.no_shader nil end |
#rotate(angle) ⇒ nil
Applies rotation matrix to current transformation matrix.
1034 1035 1036 1037 1038 |
# File 'lib/processing/graphics_context.rb', line 1034 def rotate(angle) assertDrawing__ @painter__.rotate toAngle__ angle nil end |
#round(value) ⇒ Numeric
Returns the closest integer number.
1215 1216 1217 |
# File 'lib/processing/graphics_context.rb', line 1215 def round(value) value.round end |
#save(filename) ⇒ Object
Saves screen image to file.
990 991 992 |
# File 'lib/processing/graphics_context.rb', line 990 def save(filename) @window__.canvas_image.save filename end |
#scale(s) ⇒ nil #scale(x, y) ⇒ nil
Applies scale matrix to current transformation matrix.
1022 1023 1024 1025 1026 |
# File 'lib/processing/graphics_context.rb', line 1022 def scale(x, y) assertDrawing__ @painter__.scale x, y nil end |
#shader(shader) ⇒ nil
Sets shader.
622 623 624 625 |
# File 'lib/processing/graphics_context.rb', line 622 def shader(shader) @painter__.shader shader&.getInternal__ nil end |
#sin(angle) ⇒ Numeric
Returns the sine of an angle.
1428 1429 1430 |
# File 'lib/processing/graphics_context.rb', line 1428 def sin(angle) Math.sin angle end |
#sq(value) ⇒ Numeric
Returns squared value.
1256 1257 1258 |
# File 'lib/processing/graphics_context.rb', line 1256 def sq(value) value * value end |
#sqrt(value) ⇒ Numeric
Returns squared value.
1266 1267 1268 |
# File 'lib/processing/graphics_context.rb', line 1266 def sqrt(value) Math.sqrt value end |
#square(x, y, extent) ⇒ nil
Draws a square.
801 802 803 |
# File 'lib/processing/graphics_context.rb', line 801 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.
454 455 456 457 |
# File 'lib/processing/graphics_context.rb', line 454 def stroke(*args) @painter__.stroke(*toRGBA__(*args)) nil end |
#strokeCap(cap) ⇒ nil
Sets stroke cap mode.
485 486 487 488 |
# File 'lib/processing/graphics_context.rb', line 485 def strokeCap(cap) @painter__.stroke_cap cap nil end |
#strokeJoin(join) ⇒ nil
Sets stroke join mode.
496 497 498 499 |
# File 'lib/processing/graphics_context.rb', line 496 def strokeJoin(join) @painter__.stroke_join join nil end |
#strokeWeight(weight) ⇒ nil
Sets stroke weight.
474 475 476 477 |
# File 'lib/processing/graphics_context.rb', line 474 def strokeWeight(weight) @painter__.stroke_width weight nil end |
#tan(angle) ⇒ Numeric
Returns the ratio of the sine and cosine of an angle.
1448 1449 1450 |
# File 'lib/processing/graphics_context.rb', line 1448 def tan(angle) Math.tan angle end |
#text(str) ⇒ nil #text(str, x, y) ⇒ nil #text(str, a, b, c, d) ⇒ nil
Draws a text.
The parameters a, b, c, and d are determined by rectMode().
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 |
# File 'lib/processing/graphics_context.rb', line 897 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
599 600 601 602 |
# File 'lib/processing/graphics_context.rb', line 599 def textAlign(horizontal, vertical = BASELINE) @textAlignH__ = horizontal @textAlignV__ = vertical end |
#textAscent ⇒ Object
591 592 593 |
# File 'lib/processing/graphics_context.rb', line 591 def textAscent() @painter__.font.ascent end |
#textDescent ⇒ Object
595 596 597 |
# File 'lib/processing/graphics_context.rb', line 595 def textDescent() @painter__.font.descent end |
#textFont(font) ⇒ Font #textFont(name) ⇒ Font #textFont(font, size) ⇒ Font #textFont(name, size) ⇒ Font
Sets font.
571 572 573 574 |
# File 'lib/processing/graphics_context.rb', line 571 def textFont(font = nil, size = nil) setFont__ font, size if font || size Font.new @painter__.font end |
#textSize(size) ⇒ nil
Sets text size.
582 583 584 585 |
# File 'lib/processing/graphics_context.rb', line 582 def textSize(size) setFont__ nil, size nil end |
#textWidth(str) ⇒ Object
587 588 589 |
# File 'lib/processing/graphics_context.rb', line 587 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.
519 520 521 522 |
# File 'lib/processing/graphics_context.rb', line 519 def tint(*args) @tint__ = args nil end |
#translate(x, y) ⇒ nil #translate(x, y, z) ⇒ nil
Applies translation matrix to current transformation matrix.
1005 1006 1007 1008 1009 |
# File 'lib/processing/graphics_context.rb', line 1005 def translate(x, y, z = 0) assertDrawing__ @painter__.translate x, y, z nil end |
#triangle(x1, y1, x2, y2, x3, y3) ⇒ nil
Draws a triangle.
816 817 818 819 820 |
# File 'lib/processing/graphics_context.rb', line 816 def triangle(x1, y1, x2, y2, x3, y3) assertDrawing__ @painter__.line x1, y1, x2, y2, x3, y3, loop: true nil end |
#width ⇒ Object
252 253 254 |
# File 'lib/processing/graphics_context.rb', line 252 def width() @image__.width end |