/usr/share/SuperCollider/HelpSource/Classes/Pen.schelp is in supercollider-common 1:3.8.0~repack-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 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 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 | class:: Pen
summary:: Draw custom graphics
categories:: GUI>Accessories
related:: Classes/QPenPrinter
description::
A class which allows you to draw custom graphics on a UserView or Window.
The following methods must be called within a link::Classes/Window#-drawFunc:: or a link::Classes/UserView#-drawFunc:: function, and will only be visible once the window or the view is refreshed. Each call to link::Classes/Window#-refresh:: or link::Classes/UserView#-refresh:: will 'overwrite' all previous drawing by executing the currently defined function, unless link::Classes/UserView#-clearOnRefresh:: is set to code::false::
classmethods::
private:: key
subsection:: Construct path
The following methods define paths. You will need to call link::#*stroke:: or link::#*fill:: to actually draw them.
method:: moveTo
Move the Pen to point.
argument:: point
An instance of link::Classes/Point::
method:: lineTo
Draw a line (define a path) from the current position to point.
argument:: point
An instance of link::Classes/Point::
method:: line
Draw a line (define a path) from p1 to p2. Current position will be p2.
argument:: p1
An instance of link::Classes/Point::
argument:: p2
An instance of link::Classes/Point::
method:: curveTo
Draws a cubic bezier curve from the current position to point.
strong::cpoint1:: and strong::cpoint2:: are control points determining the curve's curvature.
argument:: endPoint
An instance of link::Classes/Point::
argument:: cPoint1
An instance of link::Classes/Point::
argument:: cPoint2
An instance of link::Classes/Point::
method:: quadCurveTo
Draws a quad bezier curve from the current position to point.
strong::cpoint1:: is a control point determining the curve's curvature.
argument:: endPoint
An instance of link::Classes/Point::
argument:: cPoint1
An instance of link::Classes/Point::
method:: arcTo
Draws an arc of a circle using a radius and tangent points.
argument:: point1
The end point of the first tangent line. Its start point is the current position. An instance of link::Classes/Point::
argument:: point2
The end point of the second tangent line. Its start point is point1. An instance of link::Classes/Point::
argument:: radius
The radius of the arc.
discussion::
example:
code::
(
var w = Window("arcTo", Rect(100, 200, 300, 300)).front;
var r= 15;
w.drawFunc = { |v|
Pen.fillColor = Color.blue;
Pen.strokeColor = Color.red;
Pen.moveTo(150@150);
Pen.arcTo(200@150, 200@225, r);
Pen.arcTo(200@225, 100@225, r);
Pen.arcTo(100@225, 100@150, r);
Pen.arcTo(100@150, 150@150, r);
Pen.lineTo(150@150);
Pen.fillStroke;
};
)
::
method:: addArc
Draw an arc around the link::Classes/Point:: strong::center::, at strong::radius:: number of pixels. strong::startAngle:: and strong::arcAngle:: refer to the starting angle and the extent of the arc, and are in radians [0..2pi].
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
Pen.translate(100, 100);
10.do{
// set the Color
Pen.color = Color.red(rrand(0.0, 1), rrand(0.0, 0.5));
Pen.addArc((100.rand)@(100.rand), rrand(10, 100), 2pi.rand, pi);
Pen.perform([\stroke, \fill].choose);
}
};
w.refresh;
)
::
method:: addWedge
Draw a wedge around the link::Classes/Point:: strong::center::, at strong::radius:: number of pixels. strong::startAngle:: and strong::sweepLength:: refer to the starting angle and the extent of the arc, and are in radians [0..2pi].
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
Pen.translate(100, 100);
10.do{
// set the Color
Pen.color = Color.blue(rrand(0.0, 1), rrand(0.0, 0.5));
Pen.addWedge((100.rand)@(100.rand), rrand(10, 100), 2pi.rand, 2pi.rand);
Pen.perform([\stroke, \fill].choose);
}
};
w.refresh;
)
::
method:: addAnnularWedge
Draw an annular wedge around the link::Classes/Point:: strong::center::, from strong::innerRadius:: to strong::outerRadius:: in pixels. strong::startAngle:: and strong::sweepLength:: refer to the starting angle and the extent of the arc, and are in radians [0..2pi].
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
Pen.translate(100, 100);
1000.do{
// set the Color
Pen.color = Color.green(rrand(0.0, 1), rrand(0.0, 0.5));
Pen.addAnnularWedge(
(100.rand)@(100.rand),
rrand(10, 50),
rrand(51, 100),
2pi.rand,
2pi.rand
);
Pen.perform([\stroke, \fill].choose);
}
};
w.refresh;
)
::
method:: addRect
Adds a link::Classes/Rect:: to the drawing.
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
80.do{
// set the Color
Pen.color = Color.green(rrand(0.0, 1), rrand(0.0, 0.5));
Pen.addRect(
Rect(20, 20, (w.bounds.width-40).rand, (w.bounds.height-40).rand)
);
Pen.perform([\stroke, \fill].choose);
}
};
w.refresh;
)
::
method:: addOval
Adds an Oval shape that fits inside the link::Classes/Rect:: to the current path.
subsection:: Draw the path
method:: stroke
Outline the previous defined path.
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
// set the Color
Pen.strokeColor = Color.red;
Pen.moveTo(200@100);
Pen.lineTo(250@200);
Pen.lineTo(300@200);
Pen.lineTo(200@250);
Pen.lineTo(100@200);
Pen.lineTo(150@200);
Pen.lineTo(200@100);
Pen.stroke
};
w.refresh;
)
::
method:: fill
Fill the previous defined path.
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
// set the Color
Pen.fillColor = Color.red;
Pen.moveTo(200@100);
Pen.lineTo(250@200);
Pen.lineTo(300@200);
Pen.lineTo(200@250);
Pen.lineTo(100@200);
Pen.lineTo(150@200);
Pen.lineTo(200@100);
Pen.fill
};
w.refresh;
)
::
method:: draw
Draw the previous defined path using any of the following options:
argument:: style
table::
## 0 || fill
## 1 || fill using even-odd rule
## 2 || stroke
## 3 || fill and stroke the current path
## 4 || fill and stroke using even-odd rule
::
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
// set the Color
Pen.fillColor = Color.red;
Pen.moveTo(200@100);
Pen.lineTo(250@200);
Pen.lineTo(300@200);
Pen.lineTo(200@250);
Pen.lineTo(100@200);
Pen.lineTo(150@200);
Pen.lineTo(200@100);
Pen.draw(4); // fill and then stroke
};
w.refresh;
)
::
method:: fillStroke
Fill and stroke the current path. Shortcut to the draw(3) method.
subsection:: Construct and draw
These methods do not require separate calls to link::#*stroke:: or link::#*fill::.
method:: strokeRect
Strokes a link::Classes/Rect:: into the window.
method:: fillRect
Draws a filled link::Classes/Rect:: into the window.
method:: strokeOval
Strokes an oval into the window.
method:: fillOval
Draws a filled oval into the window.
subsection:: Gradients
method:: fillAxialGradient
Fills an Axial gradient.
discussion::
example:
code::
(
w = Window.new.front;
w.drawFunc = {
// fill the gradient
Pen.addRect(w.view.bounds.insetBy(30));
Pen.fillAxialGradient(w.view.bounds.leftTop, w.view.bounds.rightBottom, Color.rand, Color.rand);
};
w.refresh;
)
::
method:: fillRadialGradient
Fills a Radial gradient.
discussion::
example:
code::
(
w = Window.new.front;
w.drawFunc = {
// fill the gradient
Pen.addOval(w.view.bounds.insetBy(30));
Pen.fillRadialGradient(w.view.bounds.center,
w.view.bounds.center, 0, w.bounds.width, Color.rand, Color.rand);
};
w.refresh;
)
::
subsection:: Graphics State Methods
The following commands transform the graphics state, i.e. they effect all subsequent drawing commands. These transformations are cumulative, i.e. each command applies to the previous graphics state, not the original one.
method:: translate
Translate the coordinate system to have its origin moved by x,y
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
Pen.strokeColor = Color.blue;
Pen.translate(200,100); // 0@0 is now 200@100
Pen.moveTo(0@0);
Pen.lineTo(50@100);
Pen.lineTo(100@100);
Pen.lineTo(0@150);
Pen.lineTo(-100@100);
Pen.lineTo(-50@100);
Pen.lineTo(0@0);
Pen.stroke
};
w.refresh;
)
::
Cumulative translations:
code::
(
w = Window.new.front;
w.view.background_(Color.clear);
w.drawFunc = {
Pen.strokeColor = Color.black;
35.do { // draw 35 lines
Pen.moveTo(0@0);
Pen.lineTo(50@350);
Pen.translate(10, 0); // shift 10 to the right every time
Pen.stroke
}
};
w.refresh;
)
::
method:: scale
Scales subsequent drawing. x and y are scaling factors (i.e. 1 is normal, 2 is double size, etc.).
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
Pen.strokeColor = Color.green;
Pen.translate(200,100);
Pen.scale(0.5, 2);
Pen.moveTo(0@0); // you have to set a starting point...
Pen.lineTo(50@100);
Pen.lineTo(100@100);
Pen.lineTo(0@150);
Pen.lineTo(-100@100);
Pen.lineTo(-50@100);
Pen.lineTo(0@0);
Pen.stroke
};
w.refresh;
)
::
method:: skew
Skews subsequent drawing. x and y are skewing factors (i.e. 1 is normal).
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
Pen.fillColor = Color.green(0.5, 0.8);
Pen.translate(200,100);
Pen.skew(0.5, 0.2);
Pen.moveTo(0@0); // you have to set a starting point...
Pen.lineTo(50@100);
Pen.lineTo(100@100);
Pen.lineTo(0@150);
Pen.lineTo(-100@100);
Pen.lineTo(-50@100);
Pen.lineTo(0@0);
Pen.fill
};
w.refresh;
)
::
method:: rotate
Rotates subsequent drawing around the link::Classes/Point:: code::x@y:: by the amount strong::angle:: in radians [0..2pi].
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
c = 0;
w.drawFunc = {
Pen.translate(220, 200);
10.do({
Pen.translate(0,10);
Pen.fillColor = Color.hsv(c.fold(0, 1), 1, 1, 0.5);
Pen.moveTo(0@0); // you have to set a starting point...
Pen.lineTo(50@100);
Pen.lineTo(100@100);
Pen.lineTo(0@150);
Pen.lineTo(-100@100);
Pen.lineTo(-50@100);
Pen.lineTo(0@0);
Pen.fill;
Pen.rotate(0.2pi);
c = c + 0.1;
});
};
w.refresh;
)
::
method:: matrix
Gets or sets the coordinate system transformation matrix.
See link::#Matrix example:: for an example.
argument:: matrixArray
An array of the form code::[ zoomX, shearingY, shearingX, zoomY, translateX, translateY ]::
method:: width
Sets the width of the Pen for the whole stroke
method:: use
Draw function, and then revert to the previous graphics state. This allows you to make complex transformations of the graphics state without having to explicitly revert to get back to 'normal'.
discussion::
example:
code::
(
// modified by an example of Stefan Wittwer
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
//paint origin
Pen.fillColor = Color.gray(0, 0.5);
Pen.addArc(0@0, 20, 0, 2pi);
Pen.fill;
Pen.width = 10;
Pen.use { // draw something complex...
Pen.width = 0.5;
Pen.translate(100,100);
Pen.fillColor = Color.blue;
Pen.addArc(0@0, 10, 0, 2pi);
Pen.fill;
20.do{
Pen.moveTo(0@0);
Pen.lineTo(100@0);
Pen.strokeColor = Color.red(0.8, rrand(0.7, 1));
Pen.stroke;
Pen.skew(0, 0.1);
};
};
// now go on with all params as before
// translation, skewing, width, and color modifications do not apply
Pen.line(10@120, 300@120);
Pen.stroke;
};
w.refresh
)
::
method:: path
Make a path, consisting of the drawing made in function.
note::
Unfortunately not working for now...
(there's no Pen.endPath which currently is used in this method)
::
method:: beginPath
Discard any previous path.
method:: beginTransparencyLayer
Begins a new transparency layer. Transparency layers are useful when you want to apply an effect to a group of objects or create a composite graphic. See link::#Transparency layer example::.
method:: endTransparencyLayer
Ends the current transparency layer.
method:: clip
Use the previously defined path as a clipping path.
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
w.drawFunc = {
// outline the clipping path
Pen.moveTo(110@110);
Pen.lineTo(290@110);
Pen.lineTo(290@240);
Pen.lineTo(110@240);
Pen.lineTo(110@110);
// now clip
Pen.clip;
// everything else we draw is now clipped
Pen.fillColor = Color.yellow;
Pen.fillRect(Rect(0,0,400,400));
Pen.fillColor = Color.red;
Pen.moveTo(200@100);
Pen.lineTo(250@200);
Pen.lineTo(300@200);
Pen.lineTo(200@250);
Pen.lineTo(100@200);
Pen.lineTo(150@200);
Pen.fill;
};
w.refresh;
)
::
method:: smoothing
Turns on/off anti-aliasing.
discussion::
example:
code::
(
var w = Window("smoothing", Rect(100, 200, 500, 300)).front;
w.view.background_(Color.white);
w.drawFunc = { |v|
Pen.strokeColor = Color.grey(0.25);
Pen.smoothing_(false); //no anti-aliasing
50.do{|i|
Pen.moveTo(50@50.rrand(250));
Pen.lineTo(250@50.rrand(250));
};
Pen.stroke;
Pen.smoothing_(true); //anti-aliasing (default)
50.do{|i|
Pen.moveTo(250@50.rrand(250));
Pen.lineTo(450@50.rrand(250));
};
Pen.stroke;
};
)
::
method:: setShadow
Will fill the current path with a shadow.
You should use this option between Pen.push / Pen.pop (or Pen.use)
method:: joinStyle
Set the lines joining style according to the defined options:
table::
## 0 || miter
## 1 || round
## 2 || bevel
::
method:: capStyle
Set the lines joining style according to the defined options:
table::
## 0 || butt
## 1 || round
## 2 || square
::
discussion::
example:
code::
(
w = Window.new.front;
w.view.background_(Color.white);
StaticText(w, Rect(0,0,180,20))
.string_(" Change Line Cap & Join Styles: ");
y = PopUpMenu(w, Rect(180,0,130,20))
.items_(["Butt - Miter", "Round - Round", "Square - Bevel"])
.action_({w.refresh});
w.drawFunc = {
Pen.strokeColor = Color.red;
Pen.width_(8.0);
Pen.capStyle_(y.value);
Pen.joinStyle_(y.value);
Pen.moveTo(200@100);
Pen.lineTo(250@200);
Pen.lineTo(300@200);
Pen.lineTo(200@250);
Pen.lineTo(100@200);
Pen.lineTo(150@200);
Pen.lineTo(200@100);
Pen.stroke;
};
w.refresh;
)
::
method:: alpha
Set the opacity level.
method:: blendMode
Set the blending mode to use.
See link::#Blending modes:: for more information.
method:: lineDash
Set the line dash pattern.
pattern should be a link::Classes/FloatArray:: of values that specify the lengths of the painted segments and not painted segments.
method:: drawImage
Draw a bitmap image using the Image class.
discussion::
example:
code::
w = Window.new.front;
w.view.background = Color.red;
i = Image.open(SCDoc.helpSourceDir +/+ "images/Swamp.png");
w.drawFunc_({
Pen.drawImage( Point(140, 140), i, operation: 'sourceOver', opacity:1);
});
examples::
Simple rotating and scaling:
code::
(
w = Window("Pen Rotation and Scaling", Rect(128, 64, 360, 360));
w.drawFunc = {
Pen.use {
// use the same rect for everything, just scale and rotate
var r = Rect(0,0,200,80);
Pen.fillColor = Color.black;
// offset all subsequent co-ordinates
Pen.translate(80,20);
Pen.fillRect(r);
Pen.fillColor = Color.red;
// scale all subsequent co-ordinates
Pen.scale(0.8, 0.8);
Pen.translate(8,10);
// rotate all subsequent co-ordinates
Pen.rotate(0.1pi);
Pen.fillRect(r);
Pen.strokeColor = Color.blue;
// lather, rinse, repeat
Pen.scale(0.8, 0.8);
Pen.rotate(0.1pi);
Pen.width = 3;
Pen.strokeRect(r);
Pen.fillColor = Color.yellow(1,0.5);
Pen.scale(0.8, 0.8);
Pen.rotate(0.1pi);
Pen.translate(20,-20);
Pen.fillOval(r);
}
};
w.front;
)
::
Redraw at random interval, different every time:
code::
(
var w, run = true;
w = Window("my name is... panel", Rect(128, 64, 800, 800));
w.view.background = Color.white;
w.onClose = { run = false; };
w.front;
w.drawFunc = {
Pen.use {
Pen.width = 0.2;
400.do {
Pen.beginPath;
Pen.moveTo(Point(10.rand * 80 + 40, 10.rand * 80 + 40));
Pen.lineTo(Point(10.rand * 80 + 40, 10.rand * 80 + 40));
Pen.stroke;
};
};
};
{ while { run } { w.refresh; 1.0.rand.wait } }.fork(AppClock)
)
::
code::
(
var w, run = true;
w = Window("my name is... panel", Rect(128, 64, 800, 500));
w.view.background = Color.white;
w.onClose = { run = false; };
w.front;
w.drawFunc = {
Pen.use {
Pen.width = 2;
80.do {
Pen.width = rrand(0,4) + 0.5;
Pen.beginPath;
Pen.moveTo(Point(800.rand, 500.rand));
Pen.lineTo(Point(800.rand, 500.rand));
Pen.stroke;
};
};
};
{ while { run } { w.refresh; 1.0.rand.wait } }.fork(AppClock)
)
::
subsection:: Animation
Uses random seed to 'store' data
By reseting the seed each time the same random values and shapes are generated for each 'frame'
These can then be subjected to cumulative rotation, etc., by simply incrementing the phase var.
code::
(
// By James McCartney
var w, h = 700, v = 700, seed, run = true, phase = 0;
w = Window("wedge", Rect(40, 40, h, v), false);
w.view.background = Color.rand(0,0.3);
w.onClose = { run = false }; // stop the thread on close
w.front;
// store an initial seed value for the random generator
seed = Date.seed;
w.drawFunc = {
Pen.width = 2;
Pen.use {
// reset this thread's seed for a moment
thisThread.randSeed = Date.seed;
// now a slight chance of a new seed or background color
if (0.006.coin) { seed = Date.seed; };
if (0.02.coin) { w.view.background = Color.rand(0,0.3); };
// either revert to the stored seed or set the new one
thisThread.randSeed = seed;
// the random values below will be the same each time if the seed has not changed
// only the phase value has advanced
Pen.translate(h/2, v/2);
// rotate the whole image
// negative random values rotate one direction, positive the other
Pen.rotate(phase * 1.0.rand2);
// scale the rotated y axis in a sine pattern
Pen.scale(1, 0.3 * sin(phase * 1.0.rand2 + 2pi.rand) + 0.5 );
// create a random number of annular wedges
rrand(6,24).do {
Pen.color = Color.rand(0.0,1.0).alpha_(rrand(0.1,0.7));
Pen.beginPath;
Pen.addAnnularWedge(Point(0,0), a = rrand(60,300), a + 50.rand2, 2pi.rand
+ (phase * 2.0.rand2), 2pi.rand);
if (0.5.coin) {Pen.stroke}{Pen.fill};
};
};
};
// fork a thread to update 20 times a second, and advance the phase each time
{ while { run } { w.refresh; 0.05.wait; phase = phase + 0.01pi;} }.fork(AppClock)
)
::
code::
(
var w, phase = 0, seed = Date.seed, run = true;
w = Window("my name is... panel", Rect(128, 64, 800, 800));
w.view.background = Color.blue(0.4);
w.onClose = { run = false; };
w.front;
w.drawFunc = {
Pen.use {
if (0.02.coin) { seed = Date.seed; };
thisThread.randSeed = seed;
Pen.strokeColor = Color.white;
200.do {
var a = 4.rand;
var b = 24.rand;
var r1 = 230 + (50 * a);
var a1 = 2pi / 24 * b + phase;
var r2 = 230 + (50 * (a + 1.rand2).fold(0,3));
var a2 = 2pi / 24 * (b + (3.rand2)).wrap(0,23) + phase;
Pen.width = 0.2 + 1.0.linrand;
Pen.beginPath;
Pen.moveTo(Polar(r1, a1).asPoint + Point(400,400));
Pen.lineTo(Polar(r2, a2).asPoint + Point(400,400));
Pen.stroke;
};
thisThread.randSeed = Date.seed;
40.do {
var a = 4.rand;
var b = 24.rand;
var r1 = 230 + (50 * a);
var a1 = 2pi / 24 * b + phase;
var r2 = 230 + (50 * (a + 1.rand2).fold(0,3));
var a2 = 2pi / 24 * (b + (3.rand2)).wrap(0,23) + phase;
Pen.width = 0.2 + 1.5.linrand;
Pen.beginPath;
Pen.moveTo(Polar(r1, a1).asPoint + Point(400,400));
Pen.lineTo(Polar(r2, a2).asPoint + Point(400,400));
Pen.stroke;
};
};
};
{ while { run } { w.refresh; 0.1.wait; phase = phase + (2pi/(20*24)) } }.fork(AppClock)
)
::
code::
(
var w, h = 800, v = 600, seed = Date.seed, phase = 0, zoom = 0.7, zoomf = 1, run = true;
w = Window("affines", Rect(40, 40, h, v));
w.view.background = Color.blue(0.4);
w.onClose = { run = false };
w.front;
w.drawFunc = {
thisThread.randSeed = Date.seed;
if (0.0125.coin) { seed = Date.seed; phase = 0; zoom = 0.7; zoomf = exprand(1/1.01, 1.01); }
{ phase = phase + (2pi/80); zoom = zoom * zoomf; };
thisThread.randSeed = seed;
Pen.use {
var p1 = Point(20.rand2 + (h/2), 20.rand2 + (v/2));
var p2 = Point(20.rand2 + (h/2), 20.rand2 + (v/2));
var xscales = { exprand(2** -0.1, 2**0.1) } ! 2;
var yscales = { exprand(2** -0.1, 2**0.1) } ! 2;
var xlates = { 8.rand2 } ! 2;
var ylates = { 8.rand2 } ! 2;
var rots = { 2pi.rand + phase } ! 2;
var xform;
xscales = (xscales ++ (1/xscales)) * 1;
yscales = (yscales ++ (1/yscales)) * 1;
xlates = xlates ++ xlates.neg;
ylates = ylates ++ xlates.neg;
rots = rots ++ rots.neg;
xform = {|i| [xlates[i], ylates[i], rots[i], xscales[i], yscales[i]] } ! 4;
Pen.strokeColor = Color.grey(1,0.5);
Pen.width = 8.linrand + 1;
Pen.translate(400, 400);
Pen.scale(zoom, zoom);
Pen.translate(-400, -400);
1200.do {
var p, rot, xlate, ylate, xscale, yscale;
Pen.width = 8.linrand + 1;
Pen.beginPath;
#rot, xlate, ylate, xscale, yscale = xform.choose;
Pen.translate(xlate, ylate);
Pen.rotate(rot, h/2, v/2);
Pen.scale(xscale, yscale);
Pen.moveTo(p1);
Pen.lineTo(p2);
Pen.stroke;
};
};
};
{ while { run } { w.refresh; 0.05.wait; } }.fork(AppClock)
)
::
subsection:: Matrix example
code::
(
var controlWindow, w;
var r, a, b, c, d, matrix = [1, 0, 0, 1, 10, 10];
var sliders, spex, name;
w = Window.new.front;
w.view.background_(Color.white);
// create a controller-window
controlWindow = Window("matrix controls", Rect(400,200,350,120));
controlWindow.front;
// determine the rectangle to be drawn
r = Rect.fromPoints(a = 0 @ 0, c = 180 @ 180);
b = r.leftBottom;
d = r.rightTop;
// the drawFunc
w.drawFunc = {
Pen.strokeColor = Color.red;
Pen.matrix = matrix;
Pen.width = 5;
Pen.strokeRect(r);
Pen.strokeOval(r);
Pen.color = Color.blue;
Pen.width = 0.1;
Pen.line(a, c);
Pen.line(b, d);
Pen.stroke;
Pen.font = Font( "Helvetica-Bold", 12 );
Pen.stringAtPoint( "A", a - 6 );
Pen.stringAtPoint( "B", b - 6 );
Pen.stringAtPoint( "C", c - (0@6) );
Pen.stringAtPoint( "D", d - (0@6) );
Pen.font = Font( "Helvetica", 10 );
Pen.stringInRect( "a matrix test", r.moveBy( 50, 50 ));
};
controlWindow.view.decorator = sliders = FlowLayout(controlWindow.view.bounds);
spex = [
[0, 2.0].asSpec,
[0, 2.0].asSpec,
[0, 2.0].asSpec,
[0, 2.0].asSpec,
[0, 200.0].asSpec,
[0, 200.0].asSpec
];
name = #[zoomX, shearingY, shearingX, zoomY, translateX, translateY];
6.do { |i|
EZSlider(controlWindow, 300 @ 14, name[i], spex[i], { |ez| var val;
val = ez.value;
[i, val.round(10e-4)].postln;
matrix.put(i, val);
w.refresh; // reevaluate drawFunc function
}, matrix[i]);
sliders.nextLine;
};
w.refresh;
)
::
subsection:: Transparency layer example
code::
(
w = Window.new("Transparency Layer test", Rect(400,400,430,450)).front;
w.drawFunc = {
Color.blue.setStroke;
Pen.use {
Pen.setShadow(2@2, 10, Color.black);
Pen.beginTransparencyLayer;
Color.red.setFill;
Pen.addOval(Rect(20,40,100,100));
Pen.fill;
Color.green.setFill;
Pen.addOval(Rect(30,70,100,100));
Pen.fill;
Color.blue.setFill;
Pen.addOval(Rect(60,40,100,100));
Pen.fill;
"With Transparency Layer".drawCenteredIn(Rect(30, 40, 100, 100), Font.default, Color.white);
Pen.endTransparencyLayer;
};
Pen.use {
Pen.translate(200, 0);
Pen.setShadow(2@2, 10, Color.black);
Color.red.setFill;
Pen.addOval(Rect(20,40,100,100));
Pen.fill;
Color.green.setFill;
Pen.addOval(Rect(30,70,100,100));
Pen.fill;
Color.blue.setFill;
Pen.addOval(Rect(60,40,100,100));
Pen.fill;
"Without Transparency Layer".drawCenteredIn(Rect(30, 40, 100, 100), Font.default, Color.white);
};
Pen.use {
Pen.translate(0, 200);
Pen.setShadow(2@2, 10, Color.black);
Pen.beginTransparencyLayer;
Pen.push;
Pen.addOval(Rect(20,40,170,170));
Pen.fillAxialGradient(w.view.bounds.leftTop, w.view.bounds.rightBottom, Color.rand, Color.rand);
Pen.pop;
"With Transparency Layer".drawCenteredIn(Rect(20,40,170,170), Font.default, Color.white);
Pen.endTransparencyLayer;
};
Pen.use {
Pen.translate(200, 200);
Pen.setShadow(2@2, 10, Color.black);
Pen.addOval(Rect(20,40,170,170));
Pen.fillAxialGradient(w.view.bounds.leftTop, w.view.bounds.rightBottom, Color.rand, Color.rand);
"Without Transparency Layer".drawCenteredIn(Rect(20,40,170,170), Font.default, Color.white);
};
};
w.refresh;
)
::
subsection:: Blending modes
code::
/*
different blend modes:
OS X 10.4 and > Only
--------------------
0 - Normal
1 - Multiply
2 - Screen
3 - Overlay
4 - Darken
5 - Lighten
6 - ColorDodge
7 - ColorBurn
8 - SoftLight
9 - HardLight
10 - Difference
11 - Exclusion
12 - Hue
13 - Saturation
14 - Color
15 - Luminosity
OS 10.5 and > Only
--------------------
16 - Clear
17 - Copy
18 - SourceIn
19 - SourceOut
20 - SourceATop
21 - DestinationOver
22 - DestinationIn
23 - DestinationATop
24 - XOR
25 - PlusDarker
26 - PlusLighter
*/
(
var blendMode=0, blendString="Normal";
w = Window.new.front;
m = SCPopUpMenu(w, Rect(10, w.view.bounds.height - 30, 150, 20));
m.items = [
"0 - Normal",
"1 - Multiply",
"2 - Screen",
"3 - Overlay",
"4 - Darken",
"5 - Lighten",
"6 - ColorDodge",
"7 - ColorBurn",
"8 - SoftLight",
"9 - HardLight",
"10 - Difference",
"11 - Exclusion",
"12 - Hue",
"13 - Saturation",
"14 - Color",
"15 - Luminosity"
];
m.action_({|view|
blendMode = view.value;
blendString = view.items.at(blendMode);
w.refresh;
});
w.drawFunc = {
80.do{|i|
Pen.use {
Pen.blendMode_(blendMode);
Pen.color = Color.green(0.6, 0.10);
Pen.addRect(
Rect(20, 20, 20 + (i*4), 20 + (i*4));
);
Pen.fill;
};
}
};
w.refresh;
)
::
|