This library packages the well-known 3D library Babylon.js into a Razor component that can be used in a C# Blazor project.
The library is intended to use for creation of molecules visualization and used limited API of Babylon library.
Demo application – Demo for showing different parts of library
Pubchem Viewer – Demo application which used library reference. Show chemical information from pubchem.ncbi.nlm.nih.gov
Getting Started
Prerequisites
To create Blazor Apps, install the latest version of Visual Studio with the ASP.NET and web development workload.
For Blazor WebAssembly you need at least Visual Studio 2019 16.6+.
Another alternative would be to use Visual Studio code. Click here for more information.
Library requires .NET 5 or higher as I use IJSObjectReference
Installing
After you have created your Blazor project, you need to do the following steps:
Install the latest NuGet Package
Using Package Manager
Install-Package Babylon.Blazor
Using .NET CLI
dotnet add package Babylon.Blazor
Using MS VS Manage NuGet Packages
Search for Babylon.Blazor
Add reference to babylon js library. Add 2 lines into index.html
” dir=”auto”>
<body> <div id="app">Loading...div> <div id="blazor-error-ui"> An unhandled error has occurred. <a href="" class="reload">Reloada> <a class="dismiss">🗙a> div> <script src="https://preview.babylonjs.com/babylon.js">script> <script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js">script> <script src="_framework/blazor.webassembly.js">script> <script>navigator.serviceWorker.register('service-worker.js');script> body>
Add InstanceCreator
to DI
Chemical formula of water is H2O
@code {
ChemicalData PanelData { get; } = new ChemicalData();
async Task InitDataAsync()
{
// Fake await line
await Task.FromResult(1);
PanelData.Atoms.Add(new AtomDescription() { Name = “O”, X = 2.5369, Y = -0.1550, Z = 0.0000 });
PanelData.Atoms.Add(new AtomDescription() { Name = “H”, X = 3.0739, Y = 0.1550, Z = 0.0000 });
PanelData.Atoms.Add(new AtomDescription() { Name = “H”, X = 2.0000, Y = 0.1550, Z = 0.0000 });
PanelData.Bonds.Add(new BondDescription(1, 2, BondDescription.BondType.Single));
PanelData.Bonds.Add(new BondDescription(1, 3, BondDescription.BondType.Single));
}
protected override async Task OnInitializedAsync()
{
await InitDataAsync();
}
}” dir=”auto”>
@page "/test" @using Babylon.Blazor.Chemical <h1>Water</h1> <p> Chemical formula of water is H<sub>2</sub>O</p> <div style="height: 600px;"> <BabylonCanvas CanvasId="Canvas1" SceneData=@PanelData/> </div> @code { ChemicalData PanelData { get; } = new ChemicalData(); async Task InitDataAsync() { // Fake await line await Task.FromResult(1); PanelData.Atoms.Add(new AtomDescription() { Name = "O", X = 2.5369, Y = -0.1550, Z = 0.0000 }); PanelData.Atoms.Add(new AtomDescription() { Name = "H", X = 3.0739, Y = 0.1550, Z = 0.0000 }); PanelData.Atoms.Add(new AtomDescription() { Name = "H", X = 2.0000, Y = 0.1550, Z = 0.0000 }); PanelData.Bonds.Add(new BondDescription(1, 2, BondDescription.BondType.Single)); PanelData.Bonds.Add(new BondDescription(1, 3, BondDescription.BondType.Single)); } protected override async Task OnInitializedAsync() { await InitDataAsync(); } }
Add to _Imports.razor
NOTE: You can skip this step
Demo Application
For demo application I implemented: Water, Benzene and Epinephrine.
All descriptions was get from PubChem catalog.
As I not found atom size into description – I not set it. The same is for double and triple bonds – the parallel lines rotation vector mostly oriented along the Y axis. Colors selected automatically from color palette.
If chemist sees something wrong then tell me, please. My target was to create C# interface to Java script library. Not to draw molecules absolute correctly.
Water H2O
Benzene C6H6
Epinephrine C9H13NO3
In addition I draw some tests
Test1
How it works?
With .NET 5.0 it is possible to transfer objects between Java Script and C# very easily. Call functions from JS object under C# is easy too.
You can read more in article Using JS Object References in Blazor WASM to wrap JS libraries
Here is the steps you need to wrap JS library for Blazor Web Assembly application:
- Create Razor library with LibraryName.
- Create under wwwroot js export file with functions like this:
export function functionName(parameters) { ... return javaObject; }
- Create library wrapper
NOTE: You can get _jSInstance into main application over dependency injection or service provider call
- Create C# wrapper