{"id":50903007,"url":"https://github.com/stacksjs/ts-svg","last_synced_at":"2026-06-16T04:31:37.121Z","repository":{"id":354054567,"uuid":"1221950978","full_name":"stacksjs/ts-svg","owner":"stacksjs","description":"Pure-TypeScript SVG parser, rasterizer, and PNG encoder.","archived":false,"fork":false,"pushed_at":"2026-06-10T11:52:24.000Z","size":24384,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-10T13:21:44.433Z","etag":null,"topics":["optimizer","parser","svg","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stacksjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["stacksjs","chrisbbreuer"],"open_collective":"stacksjs"}},"created_at":"2026-04-26T22:12:12.000Z","updated_at":"2026-06-10T11:52:44.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/stacksjs/ts-svg","commit_stats":null,"previous_names":["stacksjs/ts-svg"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/stacksjs/ts-svg","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stacksjs%2Fts-svg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stacksjs%2Fts-svg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stacksjs%2Fts-svg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stacksjs%2Fts-svg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stacksjs","download_url":"https://codeload.github.com/stacksjs/ts-svg/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stacksjs%2Fts-svg/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34391702,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-16T02:00:06.860Z","response_time":126,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["optimizer","parser","svg","typescript"],"created_at":"2026-06-16T04:31:35.739Z","updated_at":"2026-06-16T04:31:37.116Z","avatar_url":"https://github.com/stacksjs.png","language":"TypeScript","funding_links":["https://github.com/sponsors/stacksjs","https://github.com/sponsors/chrisbbreuer","https://opencollective.com/stacksjs"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\u003cimg src=\".github/art/cover.jpg\" alt=\"Social Card of this repo\"\u003e\u003c/p\u003e\n\n[![npm version][npm-version-src]][npm-version-href]\n[![GitHub Actions][github-actions-src]][github-actions-href]\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n\n# ts-svg\n\nPure-TypeScript SVG parser, rasterizer, and PNG encoder for Bun \u0026 Node — no native bindings, no resvg WASM, no Skia. Ships a typed element-tree API and a drop-in `Resvg`-compatible class shim.\n\n```ts\nimport { svgToPng } from 'ts-svg'\nimport { writeFileSync } from 'node:fs'\n\nconst svg = `\u003csvg xmlns=\"http://www.w3.org/2000/svg\" width=\"200\" height=\"80\"\u003e\n  \u003crect width=\"100%\" height=\"100%\" fill=\"#0ea5e9\"/\u003e\n  \u003ctext x=\"100\" y=\"50\" text-anchor=\"middle\" font-family=\"sans-serif\" font-size=\"24\" fill=\"white\"\u003ets-svg\u003c/text\u003e\n\u003c/svg\u003e`\n\nwriteFileSync('out.png', svgToPng(svg, { scale: 2 }))\n```\n\n## Install\n\n```bash\nbun add ts-svg\n# or\nnpm install ts-svg\n```\n\n## Features\n\n- **Pure TS / no native deps.** Runs anywhere Bun or Node runs.\n- **Drop-in `Resvg` shim.** `import { Resvg } from 'ts-svg'` replaces `@resvg/resvg-js` for the common path.\n- **Typed element tree.** `parseSVG(svg)` produces `SVGRoot` you can walk, mutate, and re-serialise.\n- **Analytical AA rasterizer.** 4× horizontal sub-sampling for smooth edges; non-zero fill rule.\n- **Real path support.** Full `M m L l H h V v C c S s Q q T t A a Z z` grammar with adaptive cubic / quadratic / arc flattening.\n- **Gradients, clip-paths, masks, `\u003cuse\u003e`** — including `objectBoundingBox` units, `mask-type=\"alpha\"`, and `xlink:href` chaining for gradients.\n- **Stroke styling.** `stroke-width`, `stroke-linecap`, `stroke-linejoin`, `miter-limit`, `dasharray` + `dashoffset`.\n- **Bunfig-powered config.** Drop a `svg.config.ts` next to your `package.json` and library defaults pick it up automatically.\n\n## API\n\n### Convenience pipeline\n\n```ts\nimport { svgToPng } from 'ts-svg'\nconst png: Buffer = svgToPng(svgString, { scale: 2 })\n```\n\n### Element tree\n\n```ts\nimport { parseSVG, rasterize, encodePng } from 'ts-svg'\n\nconst root = parseSVG(svgString)        // typed SVGRoot\nconst fb = rasterize(root, { scale: 2 }) // Framebuffer (RGBA Uint8Array)\nconst png = encodePng(fb)                // Buffer\n```\n\n### Resvg-compatible shim\n\n```ts\nimport { Resvg } from 'ts-svg'\n\nconst resvg = new Resvg(svgString, {\n  fitTo: { mode: 'width', value: 1024 },\n  background: '#fff',\n})\nconst img = resvg.render()\nimg.width()      // number\nimg.height()     // number\nimg.pixels()     // Uint8Array (RGBA, top-to-bottom; copy of the framebuffer)\nimg.asPng()      // Buffer\n```\n\n`fitTo.mode` accepts `'original'`, `'zoom'`, `'width'`, or `'height'`. The shim caches the parsed tree, so calling `render()` twice doesn't re-parse.\n\n### `RenderOptions`\n\n| key | type | default | notes |\n| --- | --- | --- | --- |\n| `width` | `number` | intrinsic | Output width in px (overrides `scale`). |\n| `height` | `number` | intrinsic | Output height in px (overrides `scale`). |\n| `scale` | `number` | `1` | Multiplier on the SVG's intrinsic size. |\n| `background` | `string \\| RGBA` | transparent | CSS color or RGBA literal. |\n| `tolerance` | `number` | `0.25` | Bezier flattening tolerance in user units. |\n| `currentColor` | `string` | `black` | Resolves `currentColor` references. |\n| `fontResolver` | `FontResolver` | — | Function that maps `font-family` to a font; without it `\u003ctext\u003e` is skipped. |\n\n## CLI\n\n```bash\nts-svg render logo.svg -o logo.png --scale 2\nts-svg render logo.svg --width 1024 --background \"#fff\"\nts-svg to-png logo.svg\n```\n\n## Configuration\n\nCreate `svg.config.ts` in your project root:\n\n```ts\nimport type { SvgConfig } from 'ts-svg'\n\nconst config: Partial\u003cSvgConfig\u003e = {\n  tolerance: 0.5,        // coarser flattening for huge documents\n  background: '#ffffff',\n  currentColor: '#1f2937',\n  maxUseDepth: 8,\n  verbose: true,\n}\n\nexport default config\n```\n\nPowered by [bunfig](https://github.com/stacksjs/bunfig). The file is auto-discovered at import time.\n\n## Scope\n\n- **Supported:** `svg`, `g`, `defs`, `rect`, `circle`, `ellipse`, `line`, `polygon`, `polyline`, `path`, `text` (with a font resolver), `use`, `linearGradient`, `radialGradient`, `clipPath`, `mask`.\n- **Out of scope (today):** `\u003cstyle\u003e`/CSS selectors, `\u003cfilter\u003e` (no Gaussian blur etc.), `\u003cimage\u003e`, `\u003cpattern\u003e`, `\u003csymbol\u003e` advanced semantics, `\u003ctspan\u003e` per-glyph positioning.\n\n## Testing\n\n```bash\nbun test\n```\n\nThe `fixtures.test.ts` suite asserts pixel-level structural facts (specific colours at specific coordinates) for every supported element so regressions are caught immediately.\n\n## Changelog\n\nPlease see our [releases](https://github.com/stacksjs/ts-svg/releases) page for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## License\n\nThe MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.\n\nMade with 💙\n\n\u003c!-- Badges --\u003e\n[npm-version-src]: https://img.shields.io/npm/v/ts-svg?style=flat-square\n[npm-version-href]: https://npmjs.com/package/ts-svg\n[github-actions-src]: https://img.shields.io/github/actions/workflow/status/stacksjs/ts-svg/ci.yml?style=flat-square\u0026branch=main\n[github-actions-href]: https://github.com/stacksjs/ts-svg/actions?query=workflow%3Aci\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstacksjs%2Fts-svg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstacksjs%2Fts-svg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstacksjs%2Fts-svg/lists"}