Snap.svg - JavaScript SVG Library

0.5.1 · abandoned · verified Sun Apr 19

Snap.svg is a JavaScript library specifically designed for dynamic creation, manipulation, and animation of Scalable Vector Graphics (SVG) directly within the browser's DOM. It offers an intuitive, jQuery-like API for interacting with SVG elements, providing a powerful alternative to libraries that might render to canvas or abstract SVG too heavily. Its latest stable release, version 0.5.1, was published approximately seven years ago, indicating the project is effectively abandoned. There have been no significant updates, commits, or responses to issues in many years, making it unsuitable for new development requiring active maintenance, modern JavaScript module support, or contemporary security practices. Its key differentiator was its direct, low-level access and manipulation of native SVG DOM elements, facilitating complex interactive SVG graphics and animations without requiring a canvas.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load Snap.svg via a CDN and use its API to draw and animate basic SVG shapes (circle, rectangle, text) within an existing SVG element in an HTML document.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Snap.svg Quickstart</title>
    <style>
        body { margin: 0; display: flex; justify-content: center; align-items: center; min-height: 100vh; background-color: #f0f0f0; }
        svg { border: 1px solid #ccc; background-color: white; }
    </style>
</head>
<body>
    <svg id="my-svg" width="300" height="200"></svg>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/snap.svg/0.5.1/snap.svg-min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            // Create an SVG object using Snap.svg by referencing its ID
            const s = Snap('#my-svg');

            // Draw a circle
            const circle = s.circle(50, 50, 40);
            circle.attr({
                fill: "#bada55",
                stroke: "#000",
                strokeWidth: 5
            });

            // Draw a rectangle
            const rect = s.rect(100, 20, 150, 80);
            rect.attr({
                fill: "#007bff",
                stroke: "#000",
                strokeWidth: 3,
                rx: 10,
                ry: 10
            });

            // Animate the circle
            circle.animate({
                r: 60,
                cx: 250,
                cy: 150,
                fill: "#ff007b"
            }, 2000, mina.easeinout, () => {
                console.log('Circle animation complete!');
            });

            // Create a text element
            const text = s.text(150, 180, "Hello Snap!");
            text.attr({
                fontSize: 24,
                fontFamily: "Arial, sans-serif",
                fill: "#333",
                textAnchor: "middle"
            });
        });
    </script>
</body>
</html>

view raw JSON →