Recursive AI

Use a canvas as a .NET WebAssembly window

OnePlatform is a free, open-source library for windows, input, clipboard, displays and gamepads in .NET from Recursive AI. MIT or Apache-2.0, no native DLLs to ship.

Register the bundled script in wwwroot/index.html before starting C#.

If you don’t have the WebAssembly tools, run dotnet workload install wasm-tools before adding the package.

Terminal
dotnet add package RecursiveAI.OnePlatform

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

HTML
<!doctype html>
<meta charset="utf-8">
<title>Canvas window</title>
<canvas id="oneplatform-canvas"></canvas>
<script type="module">
  import { dotnet }
    from './_framework/dotnet.js';
  const platform = await import(
    './_content/RecursiveAI.OnePlatform/'
    + 'oneplatform.js');
 
  const runtime = await dotnet.create();
  runtime.setModuleImports(
    'oneplatform', platform);
  await runtime.runMain();
</script>

The example is the full program below: unzip it and dotnet run.

Open the HTTP App url printed by dotnet run, then open the browser’s developer console.

The full program

The download pairs the HTML above with Program.cs. Here’s CanvasWindow.csproj:

XML
<Project Sdk="Microsoft.NET.Sdk.WebAssembly">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="RecursiveAI.OnePlatform" Version="1.0.0" />
  </ItemGroup>
</Project>
C#
using System;
using OnePlatform;
 
using var app = PlatformApp.Create();
var window = app.CreateWindow(new WindowOptions { Title = "Canvas window" });
Console.WriteLine($"{app.Backend}: {window.Title}");

The browser console prints:

Text
Browser: Canvas window

The canvas is blank: the example attaches the window but doesn’t draw anything. If you skip setModuleImports, creating the window fails with a JavaScript interop error naming the missing module.

Variations

To use a different canvas ID, change the element in wwwroot/index.html:

HTML
<canvas id="preview"></canvas>

Then replace the PlatformApp.Create() line in Program.cs with:

C#
using var app = PlatformApp.Create(new()
{
    // The canvas element ID.
    DisplayName = "preview"
});

The console still prints Browser: Canvas window. The module name stays oneplatform; only the element ID changes.

The short answer

Put the canvas in the HTML and register the package’s JavaScript as the oneplatform module after dotnet.create() and before runtime.runMain(). PlatformApp.Create() selects the browser backend on browser-wasm. When C# calls CreateWindow, OnePlatform uses the element whose ID matches PlatformOptions.DisplayName, or oneplatform-canvas by default. The package supplies the JavaScript; you supply the canvas and give the runtime the import name that the C# library expects.

How do you load the canvas script in a WebAssembly app?

Load the page through the app’s HTTP server, not by opening index.html as a local file. The page imports two separate modules: the .NET loader from _framework and the OnePlatform asset from _content/RecursiveAI.OnePlatform. A WebAssembly SDK project that references the NuGet package serves oneplatform.js as a static web asset. You don’t have to copy the script into wwwroot or download it from another server. If the asset URL returns 404, check that the app serves static web assets and that the package ID in the path matches the one in the project file. If the asset loads but the interop error still names oneplatform, check that you registered it under that exact name before starting C#.

How does a WebAssembly canvas size relate to CSS pixels?

A canvas has an on-page size and a drawing buffer. Those sizes need not match: device pixel ratio describes how many physical pixels correspond to a CSS pixel. OnePlatform sizes the canvas for the visible part of the page and follows the browser’s device pixel ratio. Read the window’s dimensions for the drawing buffer rather than assuming that a CSS width is a pixel count. A browser page has one OnePlatform window, not one window per canvas tag: changing the ID picks a different canvas for that single window. OnePlatform connects the canvas and page input to the platform API; you still supply the rendering code that draws into it.

All oneplatform recipes