Example of Snapping in ASP.NET MVC Diagram Control
Snapping: the grid is shown; a node dragged, resized or rotated snaps to it (rotation in steps of the snap angle). Change the grid and the snapping below.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Snapping: the grid is shown; a node dragged, resized or rotated snaps to it (rotation in steps of the snap angle). Change the grid
and the snapping below.
</p>
<div class="d-flex flex-wrap gap-3 align-items-center mb-2">
<div class="form-check"><input class="form-check-input" type="checkbox" id="showGrid" checked><label class="form-check-label" for="showGrid">Show the grid</label></div>
<div class="form-check"><input class="form-check-input" type="checkbox" id="snapToGrid" checked><label class="form-check-label" for="snapToGrid">Snap to the grid</label></div>
<label class="d-flex align-items-center gap-1">Grid size
<select class="form-select form-select-sm" id="gridSize"><option>10</option><option selected>20</option><option>40</option></select>
</label>
<label class="d-flex align-items-center gap-1">Snap angle
<select class="form-select form-select-sm" id="snapAngle"><option value="0">Free</option><option selected>15</option><option>45</option></select>
</label>
</div>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p class="small text-muted mt-2" id="diagramStatus" role="status" aria-live="polite"></p>
<script>
window.DiagramSamples = (function () {
'use strict';
function status(text) {
var el = document.getElementById('diagramStatus');
if (el) { el.textContent = text; }
}
function diagram() { return Lumen.get('snapDiagram'); }
/** OnPositionChange: where a moved node's top left is (on the grid when snapping). */
function position(args) {
if (args.state !== 'Completed') { return; }
var n = args.source;
status(n.id + ': top left at ' + Math.round(n.offsetX - n.width / 2) + ', ' + Math.round(n.offsetY - n.height / 2) + '.');
}
document.addEventListener('change', function (e) {
var t = e.target;
if (!t || ['showGrid', 'snapToGrid', 'gridSize', 'snapAngle'].indexOf(t.id) < 0) { return; }
var s = diagram().setSnapSettings({
showGrid: document.getElementById('showGrid').checked,
snapToGrid: document.getElementById('snapToGrid').checked,
gridSize: Number(document.getElementById('gridSize').value),
snapAngle: Number(document.getElementById('snapAngle').value)
});
status('Grid ' + (s.showGrid ? 'shown' : 'hidden') + ', ' + s.gridSize + ' pixels; snapping ' + (s.snapToGrid ? 'on' : 'off') + '.');
});
return { position: position };
})();
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DiagramController : Controller
{
// The page is Views/Diagram/Snapping.cshtml; it draws what ViewData holds.
public ActionResult Snapping()
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "snapDiagram",
Height = "460px",
AriaLabel = "Diagram with a grid",
SnapSettings = new UiDiagramSnapSettings { ShowGrid = true, SnapToGrid = true, GridSize = 20, SnapAngle = 15 },
Nodes = new List<UiDiagramNode>
{
Node("first", 120, 100, "Drag me", UiDiagramShapeType.Basic, "Rectangle", 120, 60, "#e8f0fe"),
Node("second", 360, 200, "Resize me", UiDiagramShapeType.Basic, "Ellipse", 140, 80, "#e6f4ea"),
Node("third", 600, 120, "Rotate me", UiDiagramShapeType.Basic, "Rectangle", 120, 60, "#fff4e5")
},
Connectors = new List<UiDiagramConnector> { Link("s1", "first", "second") },
OnPositionChange = "DiagramSamples.position"
};
return View();
}
private static UiDiagramNode Node(string id, double x, double y, string text, UiDiagramShapeType type = UiDiagramShapeType.Flow, string shape = "Process",
double width = 120, double height = 54, string fill = null)
{
var node = new UiDiagramNode
{
Id = id, OffsetX = x, OffsetY = y, Width = width, Height = height,
Shape = new UiDiagramShape { Type = type, Shape = shape },
Style = new UiDiagramStyle { Fill = fill }
};
if (text != null) { node.Annotations.Add(new UiDiagramAnnotation { Content = text }); }
return node;
}
private static UiDiagramConnector Link(string id, string from, string to, string label = null,
UiDiagramConnectorType type = UiDiagramConnectorType.Orthogonal)
{
var c = new UiDiagramConnector { Id = id, SourceID = from, TargetID = to, Type = type };
if (label != null) { c.Annotations.Add(new UiDiagramAnnotation { Content = label, Offset = new UiDiagramPoint { X = 0.5, Y = 0 } }); }
return c;
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>