1. Products
  2.   CAD
  3.   Node.js
  4.   JSketcher
 
  

Free Node.js Library to Create Sketches & Export to DWG

Open Source Node.js 2D and 3D CAD Modeler Library allows to Create, Edit and Convert 2D sketches to STL, DWG & SVG Programmatically

What is JSketcher Library?

jSketcher is a powerful Node.js library designed to provide a simple yet powerful API for creating, editing, and exporting sketches directly within your web application. Built with performance and ease-of-use in mind, it leverages the asynchronous nature of Node.js to handle interactive drawing tasks efficiently. JSketcher is a lightweight, open-source JavaScript library designed to create and manipulate 2D sketches programmatically. It provides a simple and intuitive API for drawing shapes, lines, and curves, making it an excellent choice for developers who need to generate diagrams or sketches dynamically. The project is maintained on GitHub, welcoming contributions and community feedback, making it an excellent choice for developers who value open source innovation.

JSketcher provides a developer-friendly interface that makes setting up and managing a drawing canvas simple. The library provides built-in tools for freehand drawing, shape creation (e.g., rectangles, circles), text insertion, and more. It includes advanced geometric operations such as intersections, offsets, and transformations (e.g., scaling, rotation, and translation). These features make it possible to create complex diagrams with ease. The library is highly extensible, allowing developers to add custom shapes, tools, and functionality as needed. Whether you’re building educational tools, collaborative whiteboards, or creative design platforms, jSketcher offers a robust, flexible, and modular solution to spark your project’s creativity.

Previous Next

Getting Started with JSketcher

The recommended way to install JSketcher is using npm. Please use the following command a smooth installation.

Install JSketcher via npm

 npm install jsketcher  

You can also install it manually; download the latest release files directly from GitHub repository.

Create Simple sketches via JavaScript

The JSketcher library provides a clean and easy-to-use API that allows software developers to create sketches with minimal code. The library is designed to be beginner-friendly while still offering advanced features for experienced developers. The follow example demonstrates how software developers can create a new sketch, add a circle and a rectangle, and then export the sketch as an SVG string.

How to Create New Sketch and Draw Basic Shapes inside Node.js Apps?

const { Sketch, Shapes } = require('jsketcher');

// Create a new sketch
const sketch = new Sketch();

// Add a circle
const circle = Shapes.circle(50, 50, 30); // (x, y, radius)
sketch.add(circle);

// Add a rectangle
const rectangle = Shapes.rectangle(100, 100, 80, 40); // (x, y, width, height)
sketch.add(rectangle);

// Export the sketch as SVG
const svg = sketch.toSVG();
console.log(svg);

Exporting and Saving Sketches in Node.js

The open source JSketcher library makes it easy for software developers to load and export their CAD sketches to various other supported file formats with just a couple of lines of code. The library allows to export your sketches in various formats, including PDf, SVG and JSON. This makes it easy to integrate the library with other tools or display the sketches in web applications or beneficial for applications that require persistent storage or sharing capabilities. The following example shows how to create a polygon and export the sketch as a JSON object.

How to Create a Polygon and Export the Sketch as a JSON Object inside Node.js?

const { Sketch, Shapes } = require('jsketcher');

// Create a new sketch
const sketch = new Sketch();

// Add a polygon
const polygon = Shapes.polygon([
  [10, 10],
  [50, 30],
  [30, 70],
  [5, 50]
]);
sketch.add(polygon);

// Export the sketch as JSON
const json = sketch.toJSON();
console.log(json);

Real-Time Collaboration

While jSketcher handles the core drawing functionality, its integration with Node.js makes it a great candidate for real-time collaborative applications. By combining it with libraries like Socket.io, you can broadcast drawing events to multiple users, enabling a shared whiteboard experience. The following server-side snippet sets up a basic express application that uses Socket.io to relay drawing events, paving the way for multi-user collaborative sketching.

How to Integrate jSketcher Library with Socket.io?

const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const JSketcher = require('jsketcher');

const app = express();
const server = http.createServer(app);
const io = socketIO(server);

app.use(express.static('public')); // Serve client-side files

// When a client connects, set up real-time event broadcasting
io.on('connection', (socket) => {
  console.log('A client connected.');

  // Relay drawing data to all clients except the sender
  socket.on('drawing', (data) => {
    socket.broadcast.emit('drawing', data);
  });

  socket.on('disconnect', () => {
    console.log('A client disconnected.');
  });
});

server.listen(3000, () => {
  console.log('Server is running on port 3000.');
});

Advanced Geometric Operations & Shape Customization

The open source JSketcher library has included advanced geometric operations allows software developers to perform operations, such as intersections, offsets, and transformations (e.g., scaling, rotation, and translation). These features make it possible to create complex diagrams with ease. Moreover, for CAD applications, customization is key. jSketcher allows developers to add shapes with detailed parameters—such as dimensions, coordinates, and color attributes—ensuring that every element can be precisely controlled.