x4js gallery

SvgComponent

An <svg> element whose drawing is made with an SvgBuilder: shapes, paths, texts, gradients, groups, all in code.

import { SvgComponent, SvgBuilder } from "x4js"

Shapes

An SvgBuilder draws rectangles, circles, paths and texts; fill and stroke chain on each shape.

export function shapes( ) {
	const svg = new SvgBuilder( );
	svg.rect( 10, 10, 100, 60 ).fill( "#569cd6" ).stroke( "#2f6fa8", 2 );
	svg.circle( 170, 40, 30 ).fill( "#e8590c" );
	svg.ellipse( 260, 40, 40, 20 ).fill( "none" ).stroke( "#2f9e44", 3 );
	svg.path( ).moveTo( 10, 110 ).lineTo( 80, 90 ).lineTo( 150, 120 ).lineTo( 300, 85 ).stroke( "#888", 2 ).no_fill( );
	svg.text( 10, 150, "rect, circle, ellipse, path, text" ).fontSize( 14 ).fill( "#888" );

	return new SvgComponent( { width: 320, height: 160, svg } );
}
svg.svgcomponent {
	border: 1px solid var( --border );
}

Interactive

A shape takes DOM events (addDOMEvent) and a tooltip: its attribute tooltip, shown as html by initTooltips. The hover is in the style.

export function interactive( ) {
	const months: [ string, number ][] = [ [ "Jan", 42 ], [ "Feb", 58 ], [ "Mar", 35 ], [ "Apr", 71 ], [ "May", 64 ], [ "Jun", 80 ] ];
	const info = new Label( { text: "Click a bar" } );
	const svg = new SvgBuilder( );

	months.forEach( ( [ month, value ], i ) => {
		svg.rect( 10 + i * 50, 100 - value, 36, value )
			.fill( "#569cd6" )
			.addClass( "bar" )
			.setAttr( "tooltip", `<b>${month}</b>: ${value} orders` )
			.addDOMEvent( "click", ( ) => info.setText( `${month}: ${value} orders` ) );

		svg.text( 28 + i * 50, 116, month ).fontSize( 11 ).textAlign( "center" ).fill( "#888" );
	} );

	return new VBox( { content: [ new SvgComponent( { width: 320, height: 120, svg } ), info ] } );
}
// a shape of svg takes a style as an element of html: here its hover
.bar {
	cursor: pointer;

	&:hover {
		fill: #e8590c;
	}
}

Drawing with the mouse

A click on the drawing adds a point where the pointer is (no viewbox: a point of the drawing is a pixel); a click on a point removes it.

export function dots( ) {
	const svg = new SvgBuilder( );
	const view = new SvgComponent( { cls: "dots", width: 320, height: 160, svg } );

	view.addDOMEvent( "click", ( ev ) => {
		// on a point, its own click removes it
		if( ev.target !== view.dom ) {
			return;
		}

		const dot = svg.circle( ev.offsetX, ev.offsetY, 8 ).fill( "#e8590c" ).addClass( "dot" );
		dot.addDOMEvent( "click", ( ) => dot.getDom( ).remove( ) );
	} );

	return view;
}
svg.dots {
	border: 1px dashed var( --border );
	cursor: crosshair;

	.dot {
		cursor: pointer;
	}
}

Gradient

linear_gradient gives a gradient with its stops; its id is the fill of a shape.

export function gradient( ) {
	const svg = new SvgBuilder( );
	const grad = svg.linear_gradient( "0%", "0%", "100%", "0%" )
		.addStop( 0, "#569cd6" )
		.addStop( 100, "#e8590c" );

	svg.rect( 0, 0, 320, 60 ).fill( grad.id );

	return new SvgComponent( { width: 320, height: 60, svg } );
}

Arcs

path().arc draws an arc, its angles in degrees from the top: a gauge at 70 %.

export function arcs( ) {
	const svg = new SvgBuilder( );
	svg.path( ).arc( 60, 60, 45, -120, 120 ).stroke( "#ccc", 12 ).strokeCap( "round" ).no_fill( );
	svg.path( ).arc( 60, 60, 45, -120, 48 ).stroke( "#569cd6", 12 ).strokeCap( "round" ).no_fill( );
	svg.text( 60, 66, "70 %" ).fontSize( 18 ).textAlign( "center" ).fill( "#888" );

	return new SvgComponent( { width: 120, height: 120, svg } );
}

viewBox and transforms

A drawing of 100 x 100 shown at 200 x 200 by its viewbox; a group rotated, an icon in it (its svg needs its xmlns).

export function transforms( ) {
	const svg = new SvgBuilder( );
	const group = svg.group( );
	group.rect( 30, 30, 40, 40 ).fill( "#569cd6" );
	group.icon( heart, 40, 40, 20, 20 ).fill( "#fff" );
	group.rotate( 30, 50, 50 );

	return new SvgComponent( { width: 200, height: 200, viewbox: "0 0 100 100", svg } );
}

setSvg

setSvg replaces the whole drawing: new bars at each click, in the accent of the theme (a variable, by setStyle).

export function setSvg( ) {
	const view = new SvgComponent( { width: 320, height: 100 } );

	const draw = ( ) => {
		const svg = new SvgBuilder( );
		for( let i = 0; i < 10; i++ ) {
			const h = 10 + Math.random( ) * 85;
			// the accent of the theme: a variable, in the style of the shape
			svg.rect( i * 32, 100 - h, 26, h ).setStyle( "fill", "var( --accent-background )" );
		}
		view.setSvg( svg );
	};

	draw( );
	return new VBox( { content: [
		view,
		new HBox( { content: new Button( { label: "Draw again", click: draw } ) } ),
	] } );
}

For an assistant: llms.txt, llms-full.txt.