Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created May 28, 2019 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mzsima/d655e43219666dafe31859db938ea37d to your computer and use it in GitHub Desktop.
Save mzsima/d655e43219666dafe31859db938ea37d to your computer and use it in GitHub Desktop.
Blazor Random Rect
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>web3</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/site.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.5.1/pixi.min.js"></script>
<script type="text/javascript">
const app = new PIXI.Application({width: 256, height: 256});;
const graphics = new PIXI.Graphics();
window.CreatePIXI = (params) => {
console.log("test app")
document.getElementById('mypixi').appendChild(app.view);
console.log(app)
}
window.DrawRect = (params) => {
graphics.clear();
graphics.lineStyle(2, 0xFEEB77, 1);
graphics.beginFill(0x650A5A);
graphics.drawRect(params.x, params.y, 100, 100);
graphics.endFill();
app.stage.addChild(graphics);
}
</script>
</head>
<body>
<app>Loading...</app>
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>
@page "/"
@inject IJSRuntime JsRuntime;
<h1>Call JavaScript Function Example</h1>
<button type="button" class="btn btn-primary" onclick="@DrawRect">
DrawRect
</button>
@functions {
private async void DrawRect()
{
Random rnd = new Random();
int x = rnd.Next(10, 200);
int y = rnd.Next(10, 200);
await JsRuntime.InvokeAsync<object>("DrawRect", new {x = x, y = y});
}
}
@inherits LayoutComponentBase
@inject IJSRuntime JsRuntime;
<div class="sidebar">
<NavMenu />
</div>
<div class="main">
<div class="top-row px-4">
<a href="http://blazor.net" target="_blank" class="ml-md-auto">About</a>
</div>
<div class="content px-4">
<div id="mypixi"></div>
@Body
</div>
</div>
@functions {
protected override void OnAfterRender()
{
JsRuntime.InvokeAsync<object>("CreatePIXI","");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment