Raphaël: JavaScript Vector Graphics Library
Raphaël is a JavaScript library designed to simplify working with vector graphics on the web. It uses SVG for modern browsers (Firefox, Safari, Chrome, Opera) and VML for older Internet Explorer, providing a consistent API across different rendering engines. The current stable version is 2.3.0, released in 2018. The library is largely considered feature-complete but is no longer actively maintained, with the last code changes dating back several years. Key differentiators include its cross-browser compatibility, especially its VML fallback for legacy IE, and its relatively small footprint. Development is dormant, with releases historically occurring sporadically as PRs were gathered and tested. It relies on the `eve` library for custom event handling.
Common errors
-
Raphael is not defined
cause The Raphaël library script was not loaded or executed before being accessed, or it's being used in an environment where the global `Raphael` variable is not available (e.g., an ES Module context without proper bridging).fixEnsure `<script src="path/to/raphael.js"></script>` is placed before any code attempting to use `Raphael`. For bundled environments, use `const Raphael = require('raphael');` and ensure your bundler processes it correctly. -
Uncaught ReferenceError: eve is not defined
cause This error typically occurs when using `raphael.no-deps.js` (or `.min.js`) without explicitly loading the `eve` library beforehand.fixLoad `eve.js` from `https://cdnjs.cloudflare.com/ajax/libs/eve/0.4.2/eve.min.js` (or a similar source) before loading `raphael.no-deps.js`. Alternatively, use the standard `raphael.js` (or `.min.js`) distribution, which includes `eve`. -
TypeError: paper.path is not a function
cause This usually indicates that `paper` is not a valid Raphaël canvas object, or it was not initialized correctly, perhaps due to a DOM element not existing when `Raphael()` was called.fixEnsure the target DOM element for the Raphaël canvas exists and is accessible when `Raphael(element, width, height)` is called. Wrap your Raphaël initialization code in a `window.onload` or `DOMContentLoaded` event listener if executed from the `<head>`.
Warnings
- breaking The `R.ninja` method, previously used for certain internal operations, will now throw a strict error if called directly. This indicates an internal change that might affect highly customized implementations.
- gotcha Raphaël is largely unmaintained. While functional, it may encounter compatibility issues with very new browser versions or modern JavaScript features. Security updates are highly unlikely.
- gotcha The default build (`raphael.js`, `raphael.min.js`) includes the `eve` dependency. If using `raphael.no-deps.js` or `raphael.no-deps.min.js`, `eve` must be provided separately, or core functionality (like event handling) will fail.
- deprecated The original project website, `raphaeljs.com`, is no longer active. The official documentation and project information can now be found on the GitHub Pages site.
Install
-
npm install raphael -
yarn add raphael -
pnpm add raphael
Imports
- Raphael
import Raphael from 'raphael';
<script src="path/to/raphael.js"></script> // Then Raphael is available globally
- Raphael (CommonJS)
import * as Raphael from 'raphael';
const Raphael = require('raphael'); - Raphael (AMD)
require(['raphael'], function(R) {});define([ "path/to/raphael" ], function(Raphael) { // Use Raphael here });
Quickstart
<html>
<head>
<title>Raphaël Quickstart</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.3.0/raphael.min.js"></script>
</head>
<body>
<div id="holder"></div>
<script>
window.onload = function () {
// Create a canvas with a width of 320px and a height of 200px
// and place it in the element with the ID 'holder'.
var paper = Raphael(document.getElementById('holder'), 320, 200);
// Draw a circle at x=50, y=50 with a radius of 40
var circle = paper.circle(50, 50, 40);
circle.attr({
fill: '#f00',
stroke: '#fff',
'stroke-width': 5
});
// Draw a rectangle at x=100, y=20 with width=100, height=60
var rect = paper.rect(100, 20, 100, 60);
rect.attr({
fill: '#0f0',
stroke: '#000',
'stroke-width': 2,
opacity: 0.7
});
// Add text
var text = paper.text(250, 100, "Hello Raphaël");
text.attr({
font: '20px Arial',
fill: '#00f'
});
};
</script>
</body>
</html>