Module: RubySketch::Processing::GraphicsContext
Overview
Drawing context
Constant Summary collapse
- Vector =
Processing::Vector
- 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
- RGB =
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- LEFT =
Mode for textAlign().
:LEFT- RIGHT =
Mode for textAlign().
:RIGHT- TOP =
Mode for textAlign().
:TOP- BOTTOM =
Mode for textAlign().
:BOTTOM- BASELINE =
Mode for textAlign().
:BASELINE- BUTT =
Mode for strokeCap().
:butt- MITER =
Mode for strokeJoin().
:miter- ROUND =
Mode for strokeCap() and strokeJoin().
:round- SQUARE =
Mode for strokeCap() and strokeJoin().
:square
Instance Method Summary collapse
-
#angleMode(mode) ⇒ nil
Sets angle mode.
-
#arc(a, b, c, d, start, stop) ⇒ nil
Draws an arc.
-
#background(*args) ⇒ nil
Clears screen.
- #beginDraw ⇒ Object
-
#bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2) ⇒ nil
Draws a Bezier spline curve.
-
#circle(x, y, extent) ⇒ nil
Draws a circle.
-
#colorMode(mode, *maxes) ⇒ nil
Sets color mode and max color values.
-
#copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh) ⇒ nil
Copies image.
-
#curve(cx1, cy1, x1, y1, x2, y2, cx2, cy2) ⇒ nil
Draws a Catmull-Rom spline curve.
-
#ellipse(a, b, c, d) ⇒ nil
Draws an ellipse.
-
#ellipseMode(mode) ⇒ nil
Sets ellipse mode.
- #endDraw ⇒ Object
-
#fill(*args) ⇒ nil
Sets fill color.
- #height ⇒ Object
-
#image(img, a, b, c = nil, d = nil) ⇒ nil
Draws an image.
-
#imageMode(mode) ⇒ nil
Sets image mode.
-
#line(x1, y1, x2, y2) ⇒ nil
Draws a line.
-
#noFill ⇒ nil
Disables filling.
-
#noStroke ⇒ nil
Disables drawing stroke.
-
#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.
-
#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.
-
#rect(a, b, c, d, *args) ⇒ nil
Draws a rectangle.
-
#rectMode(mode) ⇒ nil
Sets rect mode.
-
#resetMatrix ⇒ nil
Reset current transformation matrix with identity matrix.
-
#rotate(angle) ⇒ nil
Applies rotation matrix to current transformation matrix.
-
#scale(x, y) ⇒ nil
Applies scale matrix to current transformation matrix.
- #setup__(painter) ⇒ Object
-
#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.
-
#text(str, x, y, x2 = nil, y2 = nil) ⇒ nil
Draws a text.
- #textAlign(horizontal, vertical = BASELINE) ⇒ Object
- #textAscent ⇒ Object
- #textDescent ⇒ Object
-
#textFont(name = nil, size = nil) ⇒ Font
Sets font.
-
#textSize(size) ⇒ nil
Sets text size.
- #textWidth(str) ⇒ Object
-
#translate(x, y) ⇒ nil
Applies translation matrix to current transformation matrix.
-
#triangle(x1, y1, x2, y2, x3, y3) ⇒ nil
Draws a triangle.
- #width ⇒ Object
Instance Method Details
#angleMode(mode) ⇒ nil
Sets angle mode.
929 930 931 932 933 934 935 936 |
# File 'lib/rubysketch/processing.rb', line 929 def angleMode (mode) @angleScale__ = case mode.upcase.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.
1271 1272 1273 1274 1275 1276 1277 1278 |
# File 'lib/rubysketch/processing.rb', line 1271 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 |
#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.
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 |
# File 'lib/rubysketch/processing.rb', line 1159 def background (*args) assertDrawing__ rgba = toRGBA__ *args if rgba[3] == 1 @painter__.background *rgba else @painter__.push fill: rgba, stroke: nil do |_| @painter__.rect 0, 0, width, height end end nil end |
#beginDraw ⇒ Object
847 848 849 850 851 |
# File 'lib/rubysketch/processing.rb', line 847 def beginDraw () @matrixStack__.clear @styleStack__.clear @drawing__ = true end |
#bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2) ⇒ nil
Draws a Bezier spline curve.
1360 1361 1362 1363 1364 |
# File 'lib/rubysketch/processing.rb', line 1360 def bezier (x1, y1, cx1, cy1, cx2, cy2, x2, y2) assertDrawing__ @painter__.bezier x1, y1, cx1, cy1, cx2, cy2, x2, y2 nil end |
#circle(x, y, extent) ⇒ nil
Draws a circle.
1256 1257 1258 |
# File 'lib/rubysketch/processing.rb', line 1256 def circle (x, y, extent) ellipse x, y, extent, extent end |
#colorMode(mode) ⇒ nil #colorMode(mode, max) ⇒ nil #colorMode(mode, max1, max2, max3) ⇒ nil #colorMode(mode, max1, max2, max3, maxA) ⇒ nil
Sets color mode and max color values.
881 882 883 884 885 886 887 888 889 890 891 892 |
# File 'lib/rubysketch/processing.rb', line 881 def colorMode (mode, *maxes) mode = mode.upcase.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 |
#copy(sx, sy, sw, sh, dx, dy, dw, dh) ⇒ nil #copy(img, sx, sy, sw, sh, dx, dy, dw, dh) ⇒ nil
Copies image.
1440 1441 1442 1443 1444 |
# File 'lib/rubysketch/processing.rb', line 1440 def copy (img = nil, sx, sy, sw, sh, dx, dy, dw, dh) assertDrawing__ src = img&.getInternal__ || @window__.canvas @painter__.image src, sx, sy, sw, sh, dx, dy, dw, dh end |
#curve(cx1, cy1, x1, y1, x2, y2, cx2, cy2) ⇒ nil
Draws a Catmull-Rom spline curve.
1341 1342 1343 1344 1345 |
# File 'lib/rubysketch/processing.rb', line 1341 def curve (cx1, cy1, x1, y1, x2, y2, cx2, cy2) assertDrawing__ @painter__.curve cx1, cy1, x1, y1, x2, y2, cx2, cy2 nil end |
#ellipse(a, b, c, d) ⇒ nil
Draws an ellipse.
1241 1242 1243 1244 1245 1246 |
# File 'lib/rubysketch/processing.rb', line 1241 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)
969 970 971 |
# File 'lib/rubysketch/processing.rb', line 969 def ellipseMode (mode) @ellipseMode__ = mode end |
#endDraw ⇒ Object
853 854 855 |
# File 'lib/rubysketch/processing.rb', line 853 def endDraw () @drawing__ = false 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.
1016 1017 1018 1019 |
# File 'lib/rubysketch/processing.rb', line 1016 def fill (*args) @painter__.fill(*toRGBA__(*args)) nil end |
#height ⇒ Object
861 862 863 |
# File 'lib/rubysketch/processing.rb', line 861 def height () @image__.height end |
#image(img, a, b) ⇒ nil #image(img, a, b, c, d) ⇒ nil
Draws an image.
1416 1417 1418 1419 1420 1421 |
# File 'lib/rubysketch/processing.rb', line 1416 def image (img, a, b, c = nil, d = nil) assertDrawing__ x, y, w, h = toXYWH__ @imageMode__, a, b, c || img.width, d || img.height @painter__.image img.getInternal__, x, y, w, h 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)
983 984 985 |
# File 'lib/rubysketch/processing.rb', line 983 def imageMode (mode) @imageMode__ = mode end |
#line(x1, y1, x2, y2) ⇒ nil
Draws a line.
1196 1197 1198 1199 1200 |
# File 'lib/rubysketch/processing.rb', line 1196 def line (x1, y1, x2, y2) assertDrawing__ @painter__.line x1, y1, x2, y2 nil end |
#noFill ⇒ nil
Disables filling.
1081 1082 1083 1084 |
# File 'lib/rubysketch/processing.rb', line 1081 def noFill () @painter__.fill nil nil end |
#noStroke ⇒ nil
Disables drawing stroke.
1090 1091 1092 1093 |
# File 'lib/rubysketch/processing.rb', line 1090 def noStroke () @painter__.stroke nil nil end |
#point(x, y) ⇒ nil
Draws a point.
1179 1180 1181 1182 1183 1184 1185 |
# File 'lib/rubysketch/processing.rb', line 1179 def point (x, y) assertDrawing__ w = @painter__.stroke_width w = 1 if w == 0 @painter__.ellipse x - (w / 2.0), y - (w / 2.0), w, w nil end |
#pop ⇒ nil
Restore styles and transformations from stack.
1589 1590 1591 1592 |
# File 'lib/rubysketch/processing.rb', line 1589 def pop () popMatrix popStyle end |
#popMatrix ⇒ nil
Pops the current transformation matrix from stack.
1506 1507 1508 1509 1510 1511 |
# File 'lib/rubysketch/processing.rb', line 1506 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.
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 |
# File 'lib/rubysketch/processing.rb', line 1554 def popStyle () assertDrawing__ raise "style stack underflow" if @styleStack__.empty? @painter__.fill, @painter__.stroke, @painter__.stroke_width, @painter__.stroke_cap, @painter__.stroke_join, @painter__.font, @hsbColor__, @colorMaxes__, @angleScale__, @rectMode__, @ellipseMode__, @imageMode__ = @styleStack__.pop nil end |
#push(&block) ⇒ nil
Save current styles and transformations to stack.
1576 1577 1578 1579 1580 1581 1582 1583 |
# File 'lib/rubysketch/processing.rb', line 1576 def push (&block) pushMatrix pushStyle if block block.call pop end end |
#pushMatrix(&block) ⇒ nil
Pushes the current transformation matrix to stack.
1492 1493 1494 1495 1496 1497 1498 1499 1500 |
# File 'lib/rubysketch/processing.rb', line 1492 def pushMatrix (&block) assertDrawing__ @matrixStack__.push @painter__.matrix if block block.call popMatrix end nil end |
#pushStyle(&block) ⇒ nil
Save current style values to the style stack.
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 |
# File 'lib/rubysketch/processing.rb', line 1527 def pushStyle (&block) assertDrawing__ @styleStack__.push [ @painter__.fill, @painter__.stroke, @painter__.stroke_width, @painter__.stroke_cap, @painter__.stroke_join, @painter__.font, @hsbColor__, @colorMaxes__, @angleScale__, @rectMode__, @ellipseMode__, @imageMode__ ] if block block.call popStyle end nil end |
#quad(x1, y1, x2, y2, x3, y3, x4, y4) ⇒ nil
Draws a quad.
1322 1323 1324 1325 1326 |
# File 'lib/rubysketch/processing.rb', line 1322 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 |
#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.
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 |
# File 'lib/rubysketch/processing.rb', line 1220 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)
954 955 956 |
# File 'lib/rubysketch/processing.rb', line 954 def rectMode (mode) @rectMode__ = mode end |
#resetMatrix ⇒ nil
Reset current transformation matrix with identity matrix.
1517 1518 1519 1520 1521 |
# File 'lib/rubysketch/processing.rb', line 1517 def resetMatrix () assertDrawing__ @painter__.matrix = 1 nil end |
#rotate(angle) ⇒ nil
Applies rotation matrix to current transformation matrix.
1482 1483 1484 1485 1486 |
# File 'lib/rubysketch/processing.rb', line 1482 def rotate (angle) assertDrawing__ @painter__.rotate toAngle__ angle nil end |
#scale(s) ⇒ nil #scale(x, y) ⇒ nil
Applies scale matrix to current transformation matrix.
1470 1471 1472 1473 1474 |
# File 'lib/rubysketch/processing.rb', line 1470 def scale (x, y) assertDrawing__ @painter__.scale x, y nil end |
#setup__(painter) ⇒ Object
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 |
# File 'lib/rubysketch/processing.rb', line 820 def setup__ (painter) @painter__ = painter @painter__.miter_limit = 10 @drawing__ = false @hsbColor__ = false @colorMaxes__ = [1.0] * 4 @angleScale__ = 1.0 @rectMode__ = nil @ellipseMode__ = nil @imageMode__ = nil @textAlignH__ = nil @textAlignV__ = nil @matrixStack__ = [] @styleStack__ = [] colorMode RGB, 255 angleMode RADIANS rectMode CORNER ellipseMode CENTER imageMode CORNER textAlign LEFT fill 255 stroke 0 end |
#square(x, y, extent) ⇒ nil
Draws a square.
1288 1289 1290 |
# File 'lib/rubysketch/processing.rb', line 1288 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.
1039 1040 1041 1042 |
# File 'lib/rubysketch/processing.rb', line 1039 def stroke (*args) @painter__.stroke(*toRGBA__(*args)) nil end |
#strokeCap(cap) ⇒ nil
Sets stroke cap mode.
1061 1062 1063 1064 |
# File 'lib/rubysketch/processing.rb', line 1061 def strokeCap (cap) @painter__.stroke_cap cap nil end |
#strokeJoin(join) ⇒ nil
Sets stroke join mode.
1072 1073 1074 1075 |
# File 'lib/rubysketch/processing.rb', line 1072 def strokeJoin (join) @painter__.stroke_join join nil end |
#strokeWeight(weight) ⇒ nil
Sets stroke weight.
1050 1051 1052 1053 |
# File 'lib/rubysketch/processing.rb', line 1050 def strokeWeight (weight) @painter__.stroke_width weight nil end |
#text(str) ⇒ nil #text(str, x, y) ⇒ nil #text(str, a, b, c, d) ⇒ nil
Draws a text.
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 |
# File 'lib/rubysketch/processing.rb', line 1382 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
1130 1131 1132 1133 |
# File 'lib/rubysketch/processing.rb', line 1130 def textAlign (horizontal, vertical = BASELINE) @textAlignH__ = horizontal @textAlignV__ = vertical end |
#textAscent ⇒ Object
1122 1123 1124 |
# File 'lib/rubysketch/processing.rb', line 1122 def textAscent () @painter__.font.ascent end |
#textDescent ⇒ Object
1126 1127 1128 |
# File 'lib/rubysketch/processing.rb', line 1126 def textDescent () @painter__.font.descent end |
#textFont(name = nil, size = nil) ⇒ Font
Sets font.
1102 1103 1104 1105 |
# File 'lib/rubysketch/processing.rb', line 1102 def textFont (name = nil, size = nil) setFont__ name, size if name || size Font.new @painter__.font end |
#textSize(size) ⇒ nil
Sets text size.
1113 1114 1115 1116 |
# File 'lib/rubysketch/processing.rb', line 1113 def textSize (size) setFont__ @painter__.font.name, size nil end |
#textWidth(str) ⇒ Object
1118 1119 1120 |
# File 'lib/rubysketch/processing.rb', line 1118 def textWidth (str) @painter__.font.width str end |
#translate(x, y) ⇒ nil
Applies translation matrix to current transformation matrix.
1453 1454 1455 1456 1457 |
# File 'lib/rubysketch/processing.rb', line 1453 def translate (x, y) assertDrawing__ @painter__.translate x, y nil end |
#triangle(x1, y1, x2, y2, x3, y3) ⇒ nil
Draws a triangle.
1303 1304 1305 1306 1307 |
# File 'lib/rubysketch/processing.rb', line 1303 def triangle (x1, y1, x2, y2, x3, y3) assertDrawing__ @painter__.line x1, y1, x2, y2, x3, y3, loop: true nil end |
#width ⇒ Object
857 858 859 |
# File 'lib/rubysketch/processing.rb', line 857 def width () @image__.width end |