---
title: "Use a canvas as a .NET WebAssembly window"
url: https://recursiveai.org/oneplatform/cookbook/host-dotnet-wasm-window-on-canvas/
description: "Use an HTML canvas as a .NET WebAssembly window. Register OnePlatform’s bundled JavaScript module before running C#, then open the example in a browser."
version: "1.0.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePlatform) · [GitHub](https://github.com/recursive-ai-oss/oneplatform-csharp) · [Docs](https://recursiveai.org/oneplatform/docs/) · [Download example (.zip)](https://recursiveai.org/oneplatform/cookbook/host-dotnet-wasm-window-on-canvas/host-dotnet-wasm-window-on-canvas.zip)

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.

```shell
dotnet add package RecursiveAI.OnePlatform
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (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](https://recursiveai.org/oneplatform/cookbook/host-dotnet-wasm-window-on-canvas/host-dotnet-wasm-window-on-canvas.zip) 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>
```

```csharp
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:

```csharp
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.

## Related recipes

- [Create a native cross-platform window](https://recursiveai.org/oneplatform/cookbook/create-native-cross-platform-window/)
- [Handle per-monitor DPI](https://recursiveai.org/oneplatform/cookbook/handle-per-monitor-dpi/)

## 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](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio) 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.
