Example of Drawing Tools in ASP.NET MVC Diagram Control
Drawing Tools: choose a shape or a connector, then drag on the canvas to draw it (a connector dragged from a node starts there, and ends on the node it is let go over). After one drawing the tool goes back to Select, unless Keep drawing is on. Escape stops a drawing.
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Drawing Tools: choose a shape or a connector, then drag on the canvas to draw it (a connector dragged from a node starts there, and
ends on the node it is let go over). After one drawing the tool goes back to Select, unless Keep drawing is on. Escape stops a drawing.
</p>
<div class="d-flex flex-wrap gap-2 align-items-center mb-2">
<div role="toolbar" aria-label="Drawing tools">
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-draw="select" aria-pressed="true">Select</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-draw="pan" aria-pressed="false">Pan</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-draw="rectangle" aria-pressed="false">Rectangle</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-draw="ellipse" aria-pressed="false">Ellipse</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-draw="process" aria-pressed="false">Process</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-draw="decision" aria-pressed="false">Decision</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-draw="straight" aria-pressed="false">Straight connector</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-1" data-draw="orthogonal" aria-pressed="false">Orthogonal connector</button>
</div>
<div class="form-check"><input class="form-check-input" type="checkbox" id="continuous"><label class="form-check-label" for="continuous">Keep drawing</label></div>
</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('drawingDiagram'); }
var objects = {
rectangle: { id: 'rectangle', shape: { type: 'Basic', shape: 'Rectangle' }, style: { fill: '#e8f0fe' } },
ellipse: { id: 'ellipse', shape: { type: 'Basic', shape: 'Ellipse' }, style: { fill: '#e6f4ea' } },
process: { id: 'process', shape: { type: 'Flow', shape: 'Process' }, annotations: [{ content: 'Process' }] },
decision: { id: 'decision', shape: { type: 'Flow', shape: 'Decision' }, annotations: [{ content: 'Decision' }] },
straight: { id: 'straight', type: 'Straight', sourcePoint: { x: 0, y: 0 }, targetPoint: { x: 100, y: 100 } },
orthogonal: { id: 'orthogonal', type: 'Orthogonal', sourcePoint: { x: 0, y: 0 }, targetPoint: { x: 100, y: 100 } }
};
function press(name) {
Array.prototype.forEach.call(document.querySelectorAll('[data-draw]'), function (b) {
b.setAttribute('aria-pressed', b.getAttribute('data-draw') === name ? 'true' : 'false');
});
}
document.addEventListener('click', function (e) {
var b = e.target.closest ? e.target.closest('[data-draw]') : null;
if (!b) { return; }
var name = b.getAttribute('data-draw');
var d = diagram();
press(name);
if (name === 'select') { d.setTool('Default'); status('Select: click or drag a rectangle to select.'); }
else if (name === 'pan') { d.setTool('ZoomPan'); status('Pan: drag the canvas to move the view.'); }
else {
d.setTool(document.getElementById('continuous').checked ? 'ContinuousDraw' : 'DrawOnce', objects[name]);
status('Drag on the canvas to draw: ' + b.textContent + '.');
}
});
/** OnCollectionChange: what was drawn; after one drawing the tool is Select again. */
function collection(args) {
if (args.type !== 'Addition') { return; }
status('Drawn: ' + args.element.id + '.');
window.setTimeout(function () { if (diagram().getTool() === 'Default') { press('select'); } }, 0);
}
return { collection: collection };
})();
</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/DrawingTools.cshtml; it draws what ViewData holds.
public ActionResult DrawingTools()
{
ViewData["diagram"] = new UiDiagramOptions
{
Id = "drawingDiagram",
Height = "480px",
AriaLabel = "Diagram to draw on",
SnapSettings = new UiDiagramSnapSettings { ShowGrid = true, GridSize = 20 },
Nodes = new List<UiDiagramNode> { Node("start", 120, 80, "Start", UiDiagramShapeType.Flow, "Terminator") },
OnCollectionChange = "DiagramSamples.collection"
};
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;
}
}
}
@* 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>