Or use online code playgrounds
Edit in JSFiddle Edit in JSitorhttps://cdn.jsdelivr.net/npm/diagramatics@1.5/dist/diagramatics.js
https://cdn.jsdelivr.net/npm/diagramatics@1.5/dist/diagramatics.min.js
https://cdn.jsdelivr.net/npm/diagramatics@1.5/css/diagramatics.css
https://fonts.cdnfonts.com/css/latin-modern-math
<!DOCTYPE html>
<html>
<body>
<svg id="mysvg"></svg>
</body>
<script type="module">
import {square, draw_to_svg} from 'https://cdn.jsdelivr.net/npm/diagramatics@1.5/dist/diagramatics.min.js'
let mysvg = document.getElementById('mysvg');
let sq = square(10).fill('red');
draw_to_svg(mysvg, sq);
</script>
</html>
$ npm install diagramatics
After installing, you can import it into your project.
<!DOCTYPE html>
<html>
<body>
<svg id="mysvg"></svg>
</body>
<script type="module">
import {square, draw_to_svg} from 'diagramatics'
let mysvg = document.getElementById('mysvg');
let sq = square(10).fill('red');
draw_to_svg(mysvg, sq);
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<!-- css for 'Latin Modern Math' font -->
<link href="https://fonts.cdnfonts.com/css/latin-modern-math" rel="stylesheet">
<!-- optional css for interactive controls -->
<link href="diagramatics/css/diagramatics.css" rel="stylesheet">
</head>
<body>
<!-- svg component to draw the diagram -->
<svg id="mysvg"></svg>
<!-- optional div to hold interactive controls -->
<div id="controldiv"></div>
</body>
<script src="index.js" type="module"></script>
</html>
index.js
file.
// import the necessary functions from the library
import {square, draw_to_svg} from 'diagramatics'
// get the svg element
let mysvg = document.getElementById('mysvg');
// build the diagram
let sq = square(10).fill('red');
// draw the diagram to `mysvg`
draw_to_svg(mysvg, sq);
draw_to_svg()
only accept a single diagram object to draw. To draw multiple diagram objects, use diagram_combine()
to combine them into a single diagram object.
// import the necessary functions from the library
import {square, draw_to_svg, diagram_combine} from 'diagramatics'
// get the svg element
let mysvg = document.getElementById('mysvg');
// define the `draw` function
let draw = (...diagrams) => {
draw_to_svg(mysvg, diagram_combine(...diagrams));
};
// build the diagram objects
let sq = square(10).fill();
let sq2 = square(2).fill('red')
// draw the diagram to `mysvg`
draw(sq, sq2);
// import the necessary functions from the library
import {square, draw_to_svg, diagram_combine, V2, Interactive} from 'diagramatics'
// get the svg and control element
let mysvg = document.getElementById('mysvg');
let controldiv = document.getElementById('controldiv');
// define the `draw` function
let draw = (...diagrams) => {
draw_to_svg(mysvg, diagram_combine(...diagrams));
};
// create the interactive object
let int = new Interactive(controldiv, mysvg);
// build the diagram objects
int.draw_function = (inp) => {
let x = inp['x'];
let big_sq = square(10).fill();
let small_sq = square(2).fill('red').translate(V2(x,0));
draw(big_sq, small_sq);
}
int.slider('x', -10, 10, 0);
int.draw();
import
statement to import the necessary functions from the library one by one.
import {square, circle, V2} from 'diagramatics'
let sq = square(10).fill();
let circ = circle(2).fill('red').translate(V2(5,0));
*
wildcard and give it an alias.
import * as dg from 'diagramatics'
let sq = dg.square(10).fill();
let circ = dg.circle(2).fill('red').translate(dg.V2(5,0));
import * as dg from 'diagramatics'
Object.entries(dg).forEach(([name, exported]) => window[name] = exported);
let sq = square(10).fill();
let circ = circle(2).fill('red').translate(V2(5,0));
download_svg_as_svg()
and download_svg_as_png()
functions respectively.