{"id":25338352,"url":"https://github.com/lavolpecheprogramma/gcode-context","last_synced_at":"2025-10-29T09:31:15.348Z","repository":{"id":57245441,"uuid":"238414257","full_name":"lavolpecheprogramma/gcode-context","owner":"lavolpecheprogramma","description":"A simple wrapper for canvas context to export a g-code file starting from your web sketch","archived":false,"fork":false,"pushed_at":"2020-04-25T17:58:09.000Z","size":22,"stargazers_count":17,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-31T00:17:27.943Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lavolpecheprogramma.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-05T09:37:27.000Z","updated_at":"2025-01-28T18:23:27.000Z","dependencies_parsed_at":"2022-09-01T04:31:24.217Z","dependency_job_id":null,"html_url":"https://github.com/lavolpecheprogramma/gcode-context","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lavolpecheprogramma%2Fgcode-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lavolpecheprogramma%2Fgcode-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lavolpecheprogramma%2Fgcode-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lavolpecheprogramma%2Fgcode-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lavolpecheprogramma","download_url":"https://codeload.github.com/lavolpecheprogramma/gcode-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238798461,"owners_count":19532276,"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":[],"created_at":"2025-02-14T07:00:57.139Z","updated_at":"2025-10-29T09:31:08.531Z","avatar_url":"https://github.com/lavolpecheprogramma.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gcode-context\n\nA simple wrapper for canvas context to export a g-code file starting from your web sketch!\nVery useful to print your drawings with a penplot.\n\n### Installation\n\nYou can grab it as an `npm` package\n```bash\nnpm i --save @gcode-context\n```\n\n### Example\nYou can use gcode-context with [canvas-sketch](https://github.com/mattdesl/canvas-sketch) by Matt DesLauriers (@mattdesl).\n\nCheck [https://github.com/lavolpecheprogramma/gcode-file](https://github.com/lavolpecheprogramma/gcode-file) to set correctly your gcode file.\n\n```javascript\nconst canvasSketch = require('canvas-sketch');\nconst GCodeContext = require('gcode-context');\n\nconst gcodeSettings = {\n  feedRate: 4000,\n  seekRate: 4000,\n  flipX: true,\n  paperSize: [210, 297],\n  margin: 5,\n  fileName: name + '.gcode',\n}\n\nconst settings = {\n  dimensions: [\n    gcodeSettings.paperSize[0] - (gcodeSettings.margin*2),\n    gcodeSettings.paperSize[1] - (gcodeSettings.margin*2)\n  ],\n  units: 'mm',\n}\n\nconst sketch = ({context}) =\u003e {\n  const ctx = new GCodeContext({context, gcodeSettings})\n  return ({ width, height, data }) =\u003e {\n    ctx.gCode.updateCoordsArea(width, height)\n    ctx.clear()\n    \n    ctx.beginPath()\n    ctx.rect(0, 0, width, height)\n    ctx.stroke()\n\n    // with CMD+S you can download your gcode file \n    // or\n    ctx.saveFile()\n  }\n}\n\ncanvasSketch(sketch, settings);\n```\n\nor in plain javascript: \n\n```javascript\nconst GCodeContext = require('gcode-context');\n\n// for more information on gcode file settings see https://github.com/lavolpecheprogramma/gcode-file\nconst gcodeSettings = {\n  feedRate: 4000,\n  seekRate: 4000,\n  paperSize: [ 210, 297],\n  margin: 10,\n  fileName: 'sketch.gcode'\n}\n\nconst canvas = document.getElementById(\"myCanvas\");\nconst context = canvas.getContext(\"2d\");\nconst ctx = new GCodeContext({context, gcodeSettings})\n// ... your canvas setup ...\nctx.gCode.updateCoordsArea(canvas.width, canvas.height);\n\nctx.beginPath();\nctx.rect(20, 20, 150, 100);\nctx.stroke();\n\n// with CMD+S you can download your gcode file \n// or\nctx.saveFile()\n\n```\n\nYou can pass some options to costructor:\n\n- `context`, the canvas context\n- `autoBind`, if you want bind automatically CMD+S command to save the file (default is `true`)\n- `gcodeSettings`, the options passed to gcode file class (see [https://github.com/lavolpecheprogramma/gcode-file](https://github.com/lavolpecheprogramma/gcode-file) )\n\n```javascript\nconst ctx = new GCodeContext({\n  context,\n  autoBind: true,\n  gcodeSettings\n})\n```\n\n*GCodeContext* has the following drawing functions that match those in Canvas2D contexts.\nUnder the hood, it use the original canvas context to render and [createPath](https://github.com/mattdesl/canvas-sketch-util/blob/master/docs/penplot.md#createPath) to store the paths for gcode file.\n\n- `beginPath()`\n- `closePath()`\n- `moveTo(x, y)`\n- `lineTo(x, y)`\n- `quadraticCurveTo(cpx, cpy, x, y)`\n- `bezierCurveTo(cpx1, cpy1, cpx2, cpy2, x, y)`\n- `arcTo(x1, y1, x2, y2, radius)`\n- `arc(x, y, radius, startAngle, endAngle[, anticlockwise])`\n- `rect(x, y, width, height)`\n- `stroke()`\n\n### Other methods are available:\n\n#### `clear()`\nClear the canvas and remove all the saved paths\n\n#### `addLayer()`\nAdd layer in gcode file instance\n\n#### `saveFile()`\nDownload the gcode file\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flavolpecheprogramma%2Fgcode-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flavolpecheprogramma%2Fgcode-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flavolpecheprogramma%2Fgcode-context/lists"}