Styles & animation
This chapter will introduce how to set element styles and create animation effects in Cytoscape.js.
Open a standalone demo to switch between the live preview and its source files:
Basic Style Setup
Initialize a graph instance with basic styles:
const cy = cytoscape({
container: document.getElementById("cy"),
elements: [
// Nodes
{ data: { id: "a", label: "Node A", type: "special" } },
{ data: { id: "b", label: "Node B", weight: 75 } },
{ data: { id: "c", label: "Node C", parent: "compound" } },
// Compound node
{ data: { id: "compound", label: "Compound Node" } },
// Edges
{
data: {
id: "ab",
source: "a",
target: "b",
label: "Edge AB",
weight: 2,
},
},
{
data: {
id: "bc",
source: "b",
target: "c",
label: "Edge BC",
weight: 1,
},
},
{
data: {
id: "ca",
source: "c",
target: "a",
label: "Edge CA",
weight: 3,
},
},
],
style: [
// Basic node style
{
selector: "node",
style: {
"background-color": "#666",
label: "data(label)",
"text-valign": "center",
"text-halign": "center",
width: 60,
height: 60,
// Font style
"font-size": 12,
"font-weight": "normal",
"font-family": "Helvetica",
// Border style
"border-width": 2,
"border-color": "#000",
"border-opacity": 0.5,
},
},
// Special node style
{
selector: "node[type='special']",
style: {
"background-color": "#e74c3c",
shape: "diamond",
},
},
// Data-driven node style
{
selector: "node[weight]",
style: {
width: "data(weight)",
height: "data(weight)",
},
},
// Compound node style
{
selector: "node:parent",
style: {
"background-opacity": 0.333,
"background-color": "#ad1a66",
"border-width": 3,
"border-color": "#ad1a66",
},
},
// Basic edge style
{
selector: "edge",
style: {
width: 3,
"line-color": "#999",
"curve-style": "bezier",
"target-arrow-shape": "triangle",
label: "data(label)",
"text-rotation": "autorotate",
// Edge label style
"text-margin-y": -10,
"text-background-color": "#fff",
"text-background-opacity": 1,
"text-background-padding": 4,
// Arrow style
"target-arrow-color": "#999",
"source-arrow-color": "#999",
"arrow-scale": 1.5,
},
},
// Data-driven edge style
{
selector: "edge[weight]",
style: {
width: "data(weight)",
"line-style": "data(weight)",
},
},
// Interaction state styles
{
selector: ":selected",
style: {
"background-color": "#ff0",
"line-color": "#ff0",
"target-arrow-color": "#ff0",
"source-arrow-color": "#ff0",
},
},
{
selector: ":active",
style: {
"overlay-color": "#ff0",
"overlay-padding": 10,
"overlay-opacity": 0.25,
},
},
{
selector: ":grabbed",
style: {
"overlay-color": "#0f0",
},
},
],
layout: { name: "circle" },
});Style Control
Node Styles
Basic Shapes
function changeNodeShape(shape) {
cy.nodes().style({
shape: shape, // Available values:
// Basic shapes: 'ellipse', 'triangle', 'rectangle', 'diamond'
// Polygons: 'pentagon', 'hexagon', 'heptagon', 'octagon'
// Special shapes: 'star', 'vee', 'rhomboid'
// Custom: 'polygon' with 'shape-polygon-points'
});
}
// Custom polygon
function setCustomPolygon() {
cy.nodes().style({
shape: "polygon",
"shape-polygon-points": [
-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, 0, 0.8, -0.5, 0.5,
],
});
}Compound Styles
function setCompoundStyle() {
// Parent node style
cy.nodes(":parent").style({
"background-opacity": 0.333,
"background-color": "#ad1a66",
"border-width": 3,
"border-color": "#ad1a66",
shape: "roundrectangle",
padding: 10,
});
// Child node style
cy.nodes(":child").style({
"background-color": "#f1f1f1",
shape: "rectangle",
});
}Gradient Effects
function setGradient() {
cy.nodes().style({
"background-gradient-stop-colors": "#ff0000 #00ff00",
"background-gradient-stop-positions": "0% 100%",
"background-gradient-direction": "to-bottom-right",
});
}Edge Styles
Line Types and Arrows
function setEdgeStyle() {
cy.edges().style({
// Line type
"curve-style": "bezier", // 'haystack', 'segments', 'straight', 'taxi'
"line-style": "solid", // 'dotted', 'dashed'
"line-dash-pattern": [6, 3], // Custom dash pattern
"line-dash-offset": 1,
// Arrows
"source-arrow-shape": "triangle",
"mid-source-arrow-shape": "diamond",
"target-arrow-shape": "triangle",
"source-arrow-fill": "hollow", // 'filled'
"target-arrow-fill": "filled",
// Endpoints
"source-endpoint": "outside-to-node",
"target-endpoint": "outside-to-node",
// Control points
"control-point-step-size": 40,
"control-point-weights": [0.25, 0.75],
"control-point-distances": [-50, 50],
});
}Compound Edges
function setCompoundEdge() {
// Edges from node to compound node
cy.edges('[source = "compound"]').style({
"curve-style": "bezier",
"source-endpoint": "inside-to-node",
});
// Edges within compound node
cy.edges()
.filter(
(edge) => edge.source().parent().id() === edge.target().parent().id()
)
.style({
"curve-style": "haystack",
});
}Animation System
Basic Animations
function basicAnimation() {
// Single property animation
cy.nodes().animate({
style: {
backgroundColor: "#ff0000",
width: 75,
height: 75,
},
duration: 1000,
easing: "ease-in-out-cubic",
});
// Position animation
cy.nodes().animate({
position: { x: 100, y: 100 },
duration: 1000,
});
// Combined style and position animation
cy.nodes().animate({
style: { backgroundColor: "#ff0000" },
position: { x: 100, y: 100 },
duration: 1000,
});
}Complex Animation Sequences
function complexAnimation() {
// Create animations
const a1 = cy.nodes().animation({
style: { backgroundColor: "#ff0000" },
duration: 1000,
});
const a2 = cy.nodes().animation({
style: { width: 75, height: 75 },
duration: 1000,
});
// Parallel animations
Promise.all([a1.play().promise(), a2.play().promise()]).then(() => {
// Operations after animations complete
});
// Sequential animations
a1.play()
.promise("complete")
.then(() => a2.play());
// Repeat animation
a1.play()
.promise("complete")
.then(() => {
a1.reverse().play();
});
}Animation Queue
function queueAnimation() {
const queue = cy.nodes();
// Add to queue
queue
.animation({
style: { backgroundColor: "#ff0000" },
duration: 1000,
})
.play();
queue
.animation({
style: { width: 75 },
duration: 1000,
})
.play();
// Clear queue
queue.stop();
}Performance-Optimized Animations
function performantAnimation() {
// Batch processing
cy.batch(() => {
cy.nodes().forEach((node) => {
node
.animation({
style: { backgroundColor: "#ff0000" },
duration: 1000,
})
.play();
});
});
// Use requestAnimationFrame
let frame = 0;
const animate = () => {
const progress = frame / 60; // 60fps
cy.nodes().style("width", 50 + Math.sin(progress) * 25);
frame++;
if (frame < 60) {
requestAnimationFrame(animate);
}
};
requestAnimationFrame(animate);
}Key Concepts Explanation
-
Style Selectors
node,edge: All nodes/edges#id: Elements with specific ID.className: Elements with specific class[attributeName]: Elements with specific attributes:selected,:active,:grabbed: State selectors:parent,:child: Compound node selectors>,~,+: Relationship selectors
-
Node Style Properties
- Basic Properties
shape: Node shapebackground-color: Background colorwidth,height: Dimensionslabel: Label text
- Border and Shadow
border-width: Border widthborder-style: Border styleborder-color: Border colorshadow-blur: Shadow blurshadow-color: Shadow color
- Text Style
font-family: Font familyfont-size: Font sizetext-valign: Vertical alignmenttext-halign: Horizontal alignmenttext-wrap: Text wrapping
- Gradient and Opacity
background-opacity: Background opacitybackground-gradient-direction: Gradient directionbackground-gradient-stop-colors: Gradient colorsbackground-gradient-stop-positions: Gradient positions
- Basic Properties
-
Edge Style Properties
- Line Properties
line-style: Line styleline-color: Line colorwidth: Line widthcurve-style: Curve style
- Arrow Properties
source-arrow-shape: Source arrowtarget-arrow-shape: Target arrowarrow-scale: Arrow sizearrow-fill: Arrow fill
- Label Properties
text-rotation: Text rotationtext-margin-x/y: Text margintext-background-color: Text background colortext-border-width: Text border width
- Line Properties
-
Animation System
- Animation Properties
duration: Durationeasing: Easing functiondelay: Delay timequeue: Whether to queue
- Animation Control
play(): Play animationpause(): Pause animationstop(): Stop animationrewind(): Reset animationreverse(): Reverse playback
- Animation Events
complete: Complete eventstep: Step eventframe: Frame event
- Performance Optimization
- Use
batch() - Avoid frequent style updates
- Use
requestAnimationFrame - Use animation queue appropriately
- Use
- Animation Properties
-
Performance Optimization
- Use class selectors instead of attribute selectors
- Cache frequently used selector results
- Batch process style updates
- Use
rafinstead ofsetInterval - Avoid unnecessary animations
- Set reasonable animation frame rates
Preserve causal and interaction meanings
Add category styles through styleOptions.baseRules on CausalWorkspace, or createCausalStyle for GraphView. Preserve polarity signs, arrows and selection highlights. The native examples teach selectors and animation; replacing the complete framework stylesheet would also replace its interaction rules.
Last updated on