### Start Development Server Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Starts the development server. This is a prerequisite for some testing commands. ```bash npm start ``` -------------------------------- ### Build and Serve Documentation Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Commands to build the project's documentation. Use `open-docs` to start a server and view the built documentation, or `build-and-open-docs` to perform both actions. ```bash npm run build-docs ``` ```bash npm run open-docs ``` ```bash npm run open-docs-no-start ``` ```bash npm run build-and-open-docs ``` ```bash npm run build-and-open-docs-no-start ``` -------------------------------- ### Open ESM Editor with All Extensions Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Launches the ESM editor with all extensions. Use `npm run open-all-ext-no-start` if a start process is already running. ```bash npm run open-all-ext ``` ```bash npm run open-all-ext-no-start ``` -------------------------------- ### Hello World Extension Example Source: https://github.com/svg-edit/svgedit/blob/master/docs/tutorials/ExtensionDocs.md A basic 'Hello World' extension demonstrating how to add a button, define event handlers like 'mouseDown' and 'mouseUp', and specify SVG icons and internationalization data. ```javascript export default { name: 'helloworld', init () { return { svgicons: 'extensions/helloworld-icon.xml', buttons: [ { /* ... */ } ], mouseDown () { // ... }, mouseUp (_opts) { // ... } } } } ``` -------------------------------- ### Run End-to-End Playwright Tests Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Builds the app, starts `vite preview`, and executes Playwright end-to-end tests against the editor. ```bash npm run test:e2e ``` -------------------------------- ### Open Compiled Editor for Production Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Launches a pre-compiled editor suitable for production environments. Requires running the `prep` script beforehand. Use `npm run open-compiled-no-start` if a start process is already running. ```bash npm run open-compiled ``` ```bash npm run open-compiled-no-start ``` -------------------------------- ### Open ESM Editor with Default Extensions Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Launches the ESM editor with default extensions. Use `npm run open-no-start` if a start process is already running. ```bash npm run open ``` ```bash npm run open-no-start ``` -------------------------------- ### Open Embedded ESM Editor Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Launches an embedded ESM editor, also starting a separate server for cross-origin testing. Use `npm run open-embedded-no-start` if a start process is already running. ```bash npm run open-embedded ``` ```bash npm run open-embedded-no-start ``` -------------------------------- ### Open HTML Test Coverage Report Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Opens the HTML-based test coverage report in the browser, showing coverage status for all lines. Use `npm run open-cov-no-start` if a start process is already running. ```bash npm run open-cov ``` ```bash npm run open-cov-no-start ``` -------------------------------- ### Configure SVG-Edit with User Extensions Source: https://github.com/svg-edit/svgedit/blob/master/README.md Configure SVG-Edit to load custom user extensions. This example shows how to specify the path to a React-based extension bundle. ```javascript svgEditor.setConfig({ allowInitialUserOverride: true, extensions: [], noDefaultExtensions: false, userExtensions: ['./react-extensions/react-test/dist/react-test.js'] }) ``` -------------------------------- ### Install SVG-Edit Canvas Source: https://github.com/svg-edit/svgedit/blob/master/README.md Install the SVG-Edit canvas package using npm. This package provides the core functionality for SVG manipulation. ```bash npm i -s "@svgedit/svgcanvas" ``` -------------------------------- ### Build Project Source: https://github.com/svg-edit/svgedit/blob/master/docs/ReleaseInstructions.md Build all workspaces and the main editor from the root directory. This must pass before a version bump. ```bash npm run build ``` -------------------------------- ### Import SVG-Edit Canvas Source: https://github.com/svg-edit/svgedit/blob/master/README.md Import the SVG-Edit canvas into your application after installation. This makes the canvas available for use in your framework. ```javascript import svgCanvas from '@svgedit/svgcanvas' ``` -------------------------------- ### Initialize SvgCanvas and Basic Configuration Source: https://github.com/svg-edit/svgedit/blob/master/packages/svgcanvas/demos/canvas.html Initializes the SvgCanvas with a container element and configuration options. Use this to set up the drawing environment. ```javascript /* globals canvas */ import SvgCanvas from '@svgedit/svgcanvas' const container = document.querySelector('#editorContainer') const { width, height } = { width: 500, height: 300 } window.width = width window.height = height const hiddenTextTagId = 'text' const config = { initFill: { color: 'FFFFFF', opacity: 1 }, initStroke: { color: '000000', opacity: 1, width: 1 }, text: { stroke_width: 0, font_size: 24, font_family: 'serif' }, initOpacity: 1, imgPath: '/src/editor/images', dimensions: [ width, height ], baseUnit: 'px' } window.canvas = new SvgCanvas(container, config) canvas.updateCanvas(width, height) ``` -------------------------------- ### Run Tests Source: https://github.com/svg-edit/svgedit/blob/master/docs/ReleaseInstructions.md Execute all tests, including accessibility tests, before proceeding with a version bump. Address or accept known failures. ```bash npm test ``` -------------------------------- ### Initialize and Configure SVG Editor Source: https://github.com/svg-edit/svgedit/blob/master/src/editor/index.html Import the Editor class and instantiate it with a container element. Configure editor options such as allowing user overrides, managing extensions, and specifying user extensions. Finally, initialize the editor. ```javascript import Editor from './Editor.js' /* for available options see the file `docs/tutorials/ConfigOptions.md` */ const svgEditor = new Editor(document.getElementById('container')) svgEditor.setConfig({ allowInitialUserOverride: true, extensions: [], noDefaultExtensions: false, userExtensions: [/* { pathName: '/packages/react-test/dist/react-test.js' } */] }) svgEditor.init() ``` -------------------------------- ### Run Full Release Checks Source: https://github.com/svg-edit/svgedit/blob/master/docs/ReleaseInstructions.md Execute comprehensive release checks including tests, documentation generation, and building the project. The script exits on failure. ```bash npm run test-build ``` -------------------------------- ### Integrate SVGEdit into a Web Application Source: https://github.com/svg-edit/svgedit/blob/master/README.md Include the SVGEdit CSS and initialize the editor within a specified HTML div. Ensure the div has numeric width and height. Configuration options are available in `docs/tutorials/ConfigOptions.md`. ```html
``` -------------------------------- ### Export SVG as Raster Image or PDF Source: https://context7.com/svg-edit/svgedit/llms.txt Export the current SVG drawing as a raster image (PNG, JPG, WEBP, BMP) or as a PDF. Results are returned asynchronously via the `exported` / `exportedPDF` canvas events. Listen for these events to get the exported data or document instance. ```javascript import SvgCanvas from '@svgedit/svgcanvas' const canvas = new SvgCanvas(container, config) // Listen for raster export completion canvas.bind('exported', (win, data) => { // data.output — the data URI string // data.type — MIME type, e.g., 'image/png' // data.issues — any export warnings console.log('Export type:', data.type) // Trigger a download const a = document.createElement('a') a.href = data.output a.download = 'drawing.png' document.body.appendChild(a) a.click() document.body.removeChild(a) }) // Listen for PDF export completion canvas.bind('exportedPDF', (win, data) => { // data is a jsPDF document instance data.save('drawing.pdf') }) // Export as PNG (default) canvas.rasterExport('PNG', null, 'drawing') // Export as JPG with quality setting canvas.rasterExport('JPG', null, 'drawing') // Export as WEBP canvas.rasterExport('WEBP', null, 'drawing') // Export as PDF canvas.exportPDF('drawing.pdf') ``` -------------------------------- ### Basic Extension Structure Source: https://github.com/svg-edit/svgedit/blob/master/docs/tutorials/ExtensionDocs.md All extensions must export an object with at least a 'name' and an 'init' function. The 'init' function receives editor methods and can return extension-specific data. ```javascript export default { name: 'extensionname', init (_methods) { return extensionData } } ``` -------------------------------- ### svgEditor.ready() / svgEditorReady event Source: https://context7.com/svg-edit/svgedit/llms.txt Listen for the editor to be fully initialized before interacting with the canvas programmatically. This is crucial for ensuring all editor components are ready. ```APIDOC ## `svgEditor.ready()` / `svgEditorReady` event — Reacting to editor initialization Listen for the editor to be fully initialized before interacting with the canvas programmatically, either from within the frame or from a parent/opener window. ```javascript // --- Within the same frame --- import Editor from './Editor.js' const svgEditor = new Editor(document.getElementById('container')) await svgEditor.init() svgEditor.ready(() => { // Canvas is fully initialized; safe to call canvas methods const canvas = svgEditor.svgCanvas console.log('Canvas ready, resolution:', canvas.getResolution()) // => { w: 640, h: 480, zoom: 1 } }) // --- From a parent window containing an SVGEdit iframe --- window.document.addEventListener('svgEditorReady', () => { const svgCanvas = document.querySelector('iframe.svgedit').contentWindow.svgCanvas // Load SVG content into the embedded editor const svgContent = ` ` svgCanvas.setSvgString(svgContent) }) ``` ``` -------------------------------- ### Publish to npm Source: https://github.com/svg-edit/svgedit/blob/master/docs/ReleaseInstructions.md Publish the new release to npm. This script confirms previous steps, runs full release checks, prompts for a release commit/tag, and publishes workspaces before the root package. ```bash npm run publish ``` -------------------------------- ### Manage Git Submodules Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Use these npm scripts to initialize and update Git submodules for managing different versions of the project. The init command is non-recursive to prevent nested submodule releases. ```bash npm run submodules-init ``` ```bash npm run submodules-update ``` -------------------------------- ### Initialize SVG-edit Editor with Xdomain Configuration Source: https://github.com/svg-edit/svgedit/blob/master/src/editor/xdomain-index.html Initializes the SVG-edit editor and applies xdomain-specific configurations if the XDOMAIN flag is set. This includes setting the canvas name and allowed origins for cross-domain communication. ```javascript import Editor from './Editor.js' const XDOMAIN = true const svgEditor = new Editor(document.getElementById('container')) svgEditor.setConfig({ allowInitialUserOverride: true, extensions: [], noDefaultExtensions: false, userExtensions: [] }) svgEditor.init() if (typeof XDOMAIN !== 'undefined' && XDOMAIN) { svgEditor.setConfig({ canvasName: 'xdomain', allowedOrigins: ['*'] }) console.info('xdomain config activated') } ``` -------------------------------- ### Miscellaneous Utility Scripts Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md A collection of utility scripts for development tasks including image compression, Markdown linting, code linting and fixing, and preparation steps for testing. ```bash npm run compress-images ``` ```bash npm run remark ``` ```bash npm run lint ``` ```bash npm run eslint-fix ``` ```bash npm run prep ``` ```bash npm run prep-no-core-rollup ``` ```bash npm run build-html ``` ```bash npm run build-by-config ``` ```bash npm run rollup ``` -------------------------------- ### Manage License Badges Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Scripts to update license information and generate badges. `license-badge` covers project and bundled dependencies, while `license-badge-dev` focuses on devDependencies. ```bash npm run license-badges ``` ```bash npm run license-badge ``` ```bash npm run license-badge-dev ``` -------------------------------- ### Use Standalone SvgCanvas for Custom Editors Source: https://context7.com/svg-edit/svgedit/llms.txt Initializes the core SvgCanvas drawing engine for headless usage. Configure canvas dimensions, initial fill/stroke properties, and image path. Includes basic button controls for mode switching and SVG retrieval. ```html
``` -------------------------------- ### loadFromString() / loadFromURL() / loadFromDataURI() Source: https://context7.com/svg-edit/svgedit/llms.txt Loads SVG content into the editor programmatically. These methods can be used before or after initialization. Setting `noStorageOnLoad: true` in the configuration prevents preloaded content from being overwritten by localStorage saves. ```APIDOC ## `loadFromString()` / `loadFromURL()` / `loadFromDataURI()` — Preloading SVG content Load SVG content into the editor programmatically before or after initialization. Set `noStorageOnLoad: true` in config if you want to guarantee the preloaded content is not overwritten by previous localStorage saves. ```javascript import Editor from './Editor.js' const svgEditor = new Editor(document.getElementById('container')) svgEditor.setConfig({ noStorageOnLoad: true // prevent localStorage from overwriting the preloaded file }) await svgEditor.init() // Load from an inline SVG string svgEditor.loadFromString(` Hello SVGEdit `) // Load from a URL svgEditor.loadFromURL('images/logo.svg') // Load from a base64 data URI const base64svg = btoa('...') svgEditor.loadFromDataURI(`data:image/svg+xml;base64,${base64svg}`) // Load via URL parameter (in browser address bar) // .../index.html?url=images%2Flogo.svg // .../index.html?source=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2C... ``` ``` -------------------------------- ### Read Test Coverage Reports Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Generates and displays test coverage reports. `npm run report` shows line numbers, while `npm run report-summary` provides only a summary. ```bash npm run report ``` ```bash npm run report-summary ``` -------------------------------- ### View Git Log for Changes Source: https://github.com/svg-edit/svgedit/blob/master/CHANGES.md Use this command to view the detailed git log between two specific commits to see all changes made in a release. ```console git log 4bb15e0..253b4bf ``` -------------------------------- ### Fill, Stroke, and Paint Source: https://context7.com/svg-edit/svgedit/llms.txt Set fill and stroke colors, widths, and gradients on selected elements, as well as opacity and rounded corners. ```APIDOC ## Fill, Stroke, and Paint - `setColor()`, `setStrokeWidth()`, `setGradient()` Set fill and stroke colors, widths, and gradients on selected elements. ### Methods - `setColor(type: 'fill' | 'stroke', color: string)`: Sets the fill or stroke color. - `setStrokeWidth(width: number)`: Sets the stroke width. - `setStrokeAttr(attr: string, value: string)`: Sets stroke style attributes like 'stroke-dasharray', 'stroke-linecap', 'stroke-linejoin'. - `setOpacity(opacity: number)`: Sets the overall opacity of selected elements (0 to 1). - `setPaint(type: 'fill' | 'stroke', paint: Paint)`: Applies a Paint object to fill or stroke. - `setGradient(type: 'fill' | 'stroke')`: Applies the current gradient to fill or stroke. - `setRectRadius(radius: number)`: Sets the rounded corner radius for rectangles. - `setBackground(color: string, imageUrl?: string)`: Sets the canvas background color and an optional image URL. ``` -------------------------------- ### Copy Development Dependencies Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Copies necessary devDependencies into the svgedit repository and npm package for hosting services. This avoids the need to bundle the entire node_modules directory. ```bash npm run copy ``` -------------------------------- ### Initialize SVG-edit Editor with IIFE Source: https://github.com/svg-edit/svgedit/blob/master/src/editor/iife-index.html Instantiates the SVG-edit editor and sets initial configuration options. Use this pattern when integrating SVG-edit as a standalone script. ```javascript const EditorCtor = (window.Editor && window.Editor.default) || window.Editor const svgEditor = new EditorCtor(document.getElementById('container')) svgEditor.setConfig({ allowInitialUserOverride: true, extensions: [], noDefaultExtensions: false, userExtensions: [] }) svgEditor.init() ``` -------------------------------- ### React to SVG Editor Initialization Source: https://context7.com/svg-edit/svgedit/llms.txt Use `svgEditor.ready()` to ensure the canvas is fully initialized before calling canvas methods. This is crucial for programmatic interactions. For parent windows embedding an SVGEdit iframe, listen for the `svgEditorReady` event. ```javascript import Editor from './Editor.js' const svgEditor = new Editor(document.getElementById('container')) await svgEditor.init() svgEditor.ready(() => { // Canvas is fully initialized; safe to call canvas methods const canvas = svgEditor.svgCanvas console.log('Canvas ready, resolution:', canvas.getResolution()) // => { w: 640, h: 480, zoom: 1 } }) ``` ```javascript window.document.addEventListener('svgEditorReady', () => { const svgCanvas = document.querySelector('iframe.svgedit').contentWindow.svgCanvas // Load SVG content into the embedded editor const svgContent = ` ` svgCanvas.setSvgString(svgContent) }) ``` -------------------------------- ### Set SVG-Edit Configuration Programmatically Source: https://github.com/svg-edit/svgedit/blob/master/docs/tutorials/ConfigOptions.md Use `svgEditor.setConfig()` to set initial options like dimensions, canvas expansion, and fill color. This must be run before the DOM is loaded. The default values can be overridden. ```javascript svgEditor.setConfig({ dimensions: [ 320, 240 ], canvas_expansion: 5, initFill: { color: '0000FF' } }) ``` -------------------------------- ### Embed SVGEdit Editor in a Web Application Source: https://context7.com/svg-edit/svgedit/llms.txt Mounts the full SVGEdit UI into a container div. Configure extensions, dimensions, and initial settings before initialization. The container must have explicit numeric width and height. ```html
``` -------------------------------- ### Run Tests After Instrumentation and Prep Source: https://github.com/svg-edit/svgedit/blob/master/docs/Development.md Re-runs tests after code modifications and instrumentation. Useful if tests were interrupted or for ESM-only testing without `prep`. ```bash npm run test-only ``` -------------------------------- ### setConfig() Source: https://context7.com/svg-edit/svgedit/llms.txt Sets configuration options for the SVG editor before initialization. It accepts an options object and an optional behavior object to control overwriting and URL parameter overrides. ```APIDOC ## `setConfig()` — Configuring the editor `setConfig(options, [behavior])` sets configuration before `init()` is called. Pass a second argument `{ overwrite: false }` to prevent overwriting previous config, or `{ allowInitialUserOverride: true }` to allow URL parameters to override programmatic config. ```javascript import Editor from './Editor.js' const svgEditor = new Editor(document.getElementById('container')) // Primary configuration svgEditor.setConfig({ // Canvas defaults dimensions: [1280, 720], baseUnit: 'px', canvas_expansion: 3, // Initial drawing styles initFill: { color: 'FFFFFF', opacity: 1 }, initStroke: { color: '333333', opacity: 1, width: 2 }, initOpacity: 1, text: { stroke_width: 0, font_size: 18, font_family: 'sans-serif' }, // Grid / rulers showRulers: true, gridSnapping: true, snappingStep: 10, gridColor: '#cccccc', // Initial tool initTool: 'select', // Extensions noDefaultExtensions: false, extensions: [], userExtensions: ['./react-extensions/react-test/dist/react-test.js'], // Security preventAllURLConfig: true, lockExtensions: false, // Storage behavior noStorageOnLoad: false, forceStorage: false, emptyStorageOnDecline: true }, { allowInitialUserOverride: true }) await svgEditor.init() ``` ``` -------------------------------- ### Importing Extension Locale Strings Source: https://github.com/svg-edit/svgedit/blob/master/docs/tutorials/ExtensionDocs.md Use `importSetGlobalDefault` to load extension-specific locale strings. Ensure the global property name avoids conflicts with the non-modular version of SVGEdit. ```javascript import { importSetGlobalDefault } from '../external/dynamic-import-polyfill/importModule.js'; // ... (async () => { const url = `${svgEditor.curConfig.extPath}ext-locale//.js` const localeStrings = await importSetGlobalDefault(url, { global: 'svgEditorExtensionLocale_imagelib_' + lang }) // Use `localeStrings` console.info(localeStrings) })() ```