{"id":21473694,"url":"https://github.com/lucasepe/g2d","last_synced_at":"2025-07-15T08:32:31.554Z","repository":{"id":41300183,"uuid":"318522349","full_name":"lucasepe/g2d","owner":"lucasepe","description":"Craft beautiful geometric art using code.","archived":false,"fork":false,"pushed_at":"2020-12-28T10:58:28.000Z","size":4667,"stargazers_count":45,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-06-19T01:49:05.319Z","etag":null,"topics":["canvas","geometry","golang","graphics2d","interpreter","programming-language","scripting-language"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucasepe.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"lucasepe"}},"created_at":"2020-12-04T13:19:00.000Z","updated_at":"2024-03-18T19:51:35.000Z","dependencies_parsed_at":"2022-09-15T19:01:34.068Z","dependency_job_id":null,"html_url":"https://github.com/lucasepe/g2d","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasepe%2Fg2d","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasepe%2Fg2d/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasepe%2Fg2d/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucasepe%2Fg2d/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucasepe","download_url":"https://codeload.github.com/lucasepe/g2d/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226027899,"owners_count":17562156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["canvas","geometry","golang","graphics2d","interpreter","programming-language","scripting-language"],"created_at":"2024-11-23T10:18:30.553Z","updated_at":"2024-11-23T10:18:31.323Z","avatar_url":"https://github.com/lucasepe.png","language":"Go","funding_links":["https://github.com/sponsors/lucasepe"],"categories":[],"sub_categories":[],"readme":"\n![](./logo.png)\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/lucasepe/g2d)](https://goreportcard.com/report/github.com/lucasepe/g2d) \u0026nbsp;\u0026nbsp;\u0026nbsp; [![Go Coverage](https://gocover.io/_badge/github.com/lucasepe/g2d?nocache=g2d)](https://gocover.io/_badge/github.com/lucasepe/g2d?nocache=g2d)\n\n\n# `g2D` - Geometry art built coding\n\nUse the code basics like loops, control flow and specialized functions to generate your geometry artworks.\n\n## Examples of g2D Geometry Art\n\n- source code in [_examples](./_examples) folder\n\n![](./_examples/heart.png) \u0026nbsp; ![](./_examples/circles.png) \u0026nbsp; ![](./_examples/maurer.png)\n\n![](./_examples/targets.png) \u0026nbsp; ![](./_examples/lines.png) \u0026nbsp; ![](./_examples/spiral.png)\n\n---\n\n## Installation\n\n\n### [Go](https://golang.org/doc/install) get\n\n```bash\n$ go get -u github.com/lucasepe/g2d\n```\n\n## Ready-To-Use Releases \n\n[Here you can find `g2d` already compiled](https://github.com/lucasepe/g2d/releases/latest) for:\n\n- MacOS \n- Linux \n- Windows\n\n---\n\n## How to use\n\nTo execute a local `g2d` script:\n\n```bash\n$ g2d /path/to/my-script.g2d\n```\n\nTo execute a `g2d` script stored somewhere on the web:\n\n```bash\n$ g2d http://my-cool-site.com/remote/path/to/my-script.g2d\n```\n\nUse the `--directory` (or the shorter `-d`) flag to specify a destination folder for the generated PNG images.\n\n---\n\n\n## The `g2D` Programming Language Syntax\n\nExample-programs can be found beneath [examples/](examples/) which demonstrate these things, as well as parts of the standard-library.\n\n### Types\n\ng2D has the following data types: `bool`, `int`, `float`, `str`, `array`, and `fn`.\n\nType      | Syntax                                    | Notes                                           |\n--------- | ----------------------------------------- | ----------------------------------------------- |\nbool      | `true false`                              |                                                 |\nint       | `0 42 1234 -5`                            | is a signed 64-bit integer                      |\nfloat     | `0.5 4.2 1.234 -5.5`                      | is a 64-bit double-precision floating point     |\nstr       | `\"\" \"foo\" \"\\\"quotes\\\" and a\\nline break\"` | are immutable arrays of bytes                   |\narray     | `[] [1, 2] [1, 2, 3]`                     | grow-able arrays (*use the `append()` builtin*) |\nfn        | `fn(a, b) { ... }`                        | defines a custom function                       |\n\n\n### Bindings\n\nVariables are bounded using the `:=` operator.\n\n```go\na := 3\nb := 1.2\n```\n\nVariables may be integers, floats, strings, or arrays/hashes.\n\nTo update a variable you can simply specify the equals `=` operator:\n\n```go\na := 3    // Binding\na = a + 5 // Updating\n```\n\n### Arithmetic operations\n\n`g2D` supports all the basic arithmetic operation of `int` and `float` types.\n\n```go\na := 5\nb := 3\n\nc := a + b\nd := c / 2\ne := d * d\n```\n\n### Builtin containers\n\n`g2d` has one builtin containers: `array`.\n\n#### Arrays\n\nAn array is a list which organizes items by linear sequence.  Arrays can hold multiple types.\n\n```go\na := [1, 2.3, \"hello!\"]\nb := [false, true, \"Hello World\", 3, 3.13]\n```\n\nAdding to an array is done via the `push` builtin function:\n\n```go\na = append(a, \"another\")\n```\n\nYou can iterate over the contents of an array like so:\n\n```go\ni := 0\nwhile( i \u003c len(a) ) {\n    print( \"Array index \", i, \" contains \", a[i], \"\\n\")\n    i = i + 1\n}\n```\n\nWith the definition we included that produces this output:\n\n```text\nArray index 0 contains 1\nArray index 1 contains 2.3\nArray index 2 contains hello!\nArray index 3 contains another\n```\n\n### Functions\n\n`g2D` uses `fn` to define a function which will be assigned to a variable for naming/invocation purposes:\n\n```go\nsum := fn(a, b) { return a + b }\n\nprint(sum(5,3), \"\\n\")       // Outputs: 8\nprint(sum(2.5,7.5), \"\\n\")   // Outputs: 10\n```\n\nFunctions can be passed as values to others functions:\n\n```go\naddTwo := fn(a, b, f) { \n    return 2 + f(a, b) \n}\n\ntot := addTwo(68, 1, sum)\nprint(tot, \"\\n\")            // Outputs: 71\n```\n\nFunctions inside functions\n\n```go\nmultiplier := fn(q) {\n    return fn(x) {\n        return x*q\n    }\n}\n\nmultThree := multiplier(3)\n\nprint(multThree(2), \"\\n\")  // Outputs: 6\nprint(multThree(3), \"\\n\")  // Outputs: 9\nprint(multThree(4), \"\\n\")  // Outputs: 12\n```\n\n### If-else statements\n\n`g2D` supports `if-else` statements.\n\n```go\nmax := fn(a, b) {\n    if (a \u003e b) {\n        return a;\n    } else {\n        return b;\n    }\n}\n\nprint( max(1, 2) )  // Outputs: 2\n```\n\n### Switch Statements\n\n`g2D` supports the `switch` and `case` expressions:\n\n```go\nswitch n := randi(10) {\n    case n % 2 == 0 {\n        print(n, \" is even\", \"\\n\")\n    }\n    default {\n        print(n, \" is odd\", \"\\n\")\n    }\n}\n```\n\n### While Loops\n\n`g2D` supports only one looping construct, the `while` loop:\n\n```go\ni := 30\nwhile (i \u003e 0) {\n    print(i, \" \")\n    i = i - 10\n}\n// 30 20 10\n```\n\n---\n\n## Builtin functions\n\n### Core\n\nFunction               | Description\n---------------------- | -------------------------------------------------------------------------- | \n`exit([status])`       | exits the program immediately with the optional status or 0                |\n`input([prompt]`       | reads a line from standard input optionally printing the specified prompt  |\n`print(...)`           | output a string to stdout                                                  |\n`printf(pattern, ...)` | output a string to stdout (formatted according the specified pattern)      |\n`sprintf(pattern, ...)`| like `printf(...)` but returns a _string_                                  |\n`bool(val)`            | converts value to a bool                                                   |\n`float(val)`           | converts decimal value str to _float_ - if _val_ is invalid returns _null_ |\n`int(val)`             | converts decimal value str to _int_ - if _val_ is invalid returns _null_   |\n`str(val)`             | returns the string representation of _val_                                 |\n`len(iterable)`        | returns the length of the iterable (_string_ or _array_)           |\n`append(array, val)`   | returns a new array with value pushed onto the end of array                |\n\n\n### Calculation\n\nFunction                | Description\n----------------------- | -------------------------------------------------------------------------- | \n`abs(x)`                | returns the absolute value of _x_                                          |\n`atan(x)`               | returns the arc tangent, in radians, of _x_                                |\n`atan2(x, y)`           | returns the arc tangent of _y/x_                                           |\n`cos(x)`                | returns the cosine of the radian argument _x_                              |\n`degrees(angle)`        | converts radians into degrees                                              | \n`hypot(p, q)`           | returns `sqrt(p*p + q*q)`                                                  |\n`lerp(start, stop, amt)`| calculates a number between two numbers at a specific increment            |\n`map(v, b1, e1, b2, e2)`| re-maps a number from one range to another                                 |\n`max(v1....vn)`         | returns the largest value in a sequence of numbers                         |\n`min(v1....vn)`         | returns the smallest value in a sequence of numbers                        |\n`pow(x, y)`             | returns `x**y`, the base _x_ exponential of _y_                            |\n`sin(x)`                | returns the sine of the radian argument _x_                                |\n`sqrt(x)`               | returns the square root of _x_                                             |\n`radians(angle)`        | converts a degree measurement to its corresponding value in radians        |\n`randf([min], [max])`   | returns a random float between min and max - by default min=0.0 and max=1.0|\n`randi([min], [max])`   | returns a random int between min and max                                   |\n\n### Basic graphic functions\n\nFunction                              | Description\n------------------------------------- | ------------------------------------------------------------------------------------- | \n`size(w,[h])`                         | sets the size of the drawing; when both _w_ and _h_ are specified creates a rectangular image otherwise creates a squared one |\n`viewport(xMin, xMax, yMin, yMax, xOffset, yOffset)` | sets up user-defined coordinate system; performs a screen reset (drawings are cleared)|\n`clear()`                             | fills the entire image with the current color; clear all drawings                     |\n`fillColor(hexcolor)`               | sets the fill color to the specified _hexcolor_; example _fillColor(\"#ff0000\")_         |\n`fillColor(r, g, b, [a])`           | sets the fill color to _r,g,b,a_ values - should be between 0 and 255, inclusive        |\n`strokeColor(hexcolor)`               | sets the stroke color to the specified _hexcolor_; example _strokeColor(\"#ff0000\")_   |\n`strokeColor(r, g, b, [a])`           | sets the stroke color to _r,g,b,a_ values - should be between 0 and 255, inclusive    |\n`strokeWeight(weight)`                | sets the stroke thickness to the specified _width_                                    |\n`dashes([s1, s2, ...sn])`             | sets the current dash pattern to use (call with zero arguments to disable dashes)     |\n`stroke()`                            | strokes the current path with the current stroek color and line width the path is cleared after this operation |\n`fill()`                              | fills the current path with the current fill color; open subpaths are implicity closed.\u003cbr/\u003e The path is cleared after this operation |\n`fillAndStroke()`                     | fills the current path with the current fill color and strokes it with the current stroke color; the path is cleared after this operation |\n`push()`                              | saves the current state of the graphic context by pushing it onto a stack |\n`pop()`                               | restores the last saved graphic context state from the stack |\n`snapshot([filename])`                | creates a PNG image with the current drawings. \u003cbr/\u003eIf _filename_ is omitted, it will be autogenerated with a progressive counter, that will be incremented on each \u003cbr/\u003e `snapshot()` invocation; this is useful if you wants to generate an animation later (using all the generated PNG images). |\n`xpos()`                              | returns the current X position (if there is a current point) |\n`ypos()`                              | returns the current Y position (if there is a current point) |\n\n### Graphic primitives\n\nFunction                              | Description\n------------------------------------- | ------------------------------------------------------------------------------------- | \n`arc(x, y, r, sa, ea)`                | draws a circular arc centered at _(x, y)_ with a radius of _r_.\u003cbr/\u003e The path starts at _sa_ angle_, ends at _ea_ angle, and travels in the direction given by anticlockwise |\n`circle(x, y, r)`                     | draws a circle centered at _[x, y]_ coordinates and with the radius _r_               |\n`ellipse(x, y, rx ,ry)`               | draws an ellipse centered at [x, y] coordinates and with the radii _rx_ and _ry_      |\n`line(x1, y1, x2, y2)`                | draws a line from point _(x1, y1)_ to point _(x2, y2)_                                |\n`point(x, y)`                         | draws a point at specified coordinates (the size is equal to the stroke weight)       |\n`quad(x1, y1, x2,y2, x3,y3, x4,y4)`   | draws a a four sided polygon using the provided vertices                              |\n`rect(x, y, w, h, [tl, tr, br, bl])`  | draws a (w x h) rectangle with upper left corner located at _(x, y)_.\u003cbr/\u003e If only one radius is specified, all the corners have the same bending, if _tl_, _tr_, _br_, _bl_ are specified, each corner can have a different curvature        |\n`triangle(x1,y1, x2,y2, x3,y3)`       | draws a triangle using the provided vertices                                          |\n`star(cx, cy, n, or, ir)`             | draws a star _cx_, _cy_ is the center, _n_ the number of spikes, _or_ and _ir_ the outer and inner radius |\n\n### Paths\n\nFunction                              | Description\n------------------------------------- | -------------------------------------------------------------------------------------- | \n`beginPath()`                         | starts a new path                                                                      |\n`closePath()`                         | adds a line segment from the current point to the beginning of the current subpath     |\n`moveTo(x, y)`                        | sets the begin of a new subpath starting at the specified _x_, _y_ point               |\n`lineTo(x, y)`                        | adds a line segment to the current path starting at the current point                  |\n`arcTo(x1, y1, x2, y2, r)`            | adds a circular arc to the current sub-path, using the given control points and radius |\n`quadraticCurveTo(x1, y1, x2, y2)`    | adds a quadratic Bézier curve to the current sub-path; _x1_, _y1_ is the control point and _x2_, _y2_ is the end point |\n\n### Transform\n\nFunction                              | Description\n------------------------------------- | -------------------------------------------------------------------------------------- | \n`rotate(angle, [x, y] )`              | updates the current matrix with a anticlockwise rotation; when _x, y_ are specified, rotation occurs about this point, otherwise rotation occurs about the origin (_angle_ is in radians) |\n`scale(sx, sy, [x, y])`               | updates the current matrix with _sx_, _sy_ scaling factor; when _x_,_y_ are specified, scaling occurs about this point, otherwise scaling occurs about origin. |\n`translate(x, y)`                     | updates the current matrix with a translation to _x_ and _y_                           |\n`identity()`                          | resets the current transformation matrix to the identity matrix                        |\n`transform(x, y)`                     | multiplies the point _x_, _y_ by the current matrix, returning a transformed position  | \n\n### Text\n\nFunction                              | Description\n------------------------------------- | -------------------------------------------------------------------------------------- | \n`text(str, x, y, [ax, ay])`           | draws the specified text _str_ at the specified anchor point _x_, _y_; the anchor point is _x - w * ax_, _y - h * ay_, where _w_, _h_ is the size of the text (by default _ax=0.5_, _ay=0.5_ to center the text at the specified point)        |\n`textWidth(str)`                      | returns the rendered width of the specified text _str_ given the current font face     |\n`fontSize(size)`                      | sets the font height                                                                   |\n\n### Images\n\nFunction                              | Description\n------------------------------------- | -------------------------------------------------------------------------------------- | \n`imageGet(path/to/png)`               | loads a PNG image from the local filesystem                                            |\n`imageAt(im, x, y, [ax, ay])`         | draws the specified image _im_ at the specified anchor point _x_, _y_; (_ax_ and _ay_ are the x and y offsets) use ax=0.5, ay=0.5 to center the image at the specified point  |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucasepe%2Fg2d","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucasepe%2Fg2d","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucasepe%2Fg2d/lists"}