Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I draw images? #73

Closed
SteveDunn opened this issue Apr 21, 2020 · 1 comment
Closed

How do I draw images? #73

SteveDunn opened this issue Apr 21, 2020 · 1 comment

Comments

@SteveDunn
Copy link

SteveDunn commented Apr 21, 2020

Hi, firstly thanks for a great library!

I am writing a Blazor WA game. When the game starts, it loads a big spritesheet and when the game runs, it draws rectangles from that spritesheet into the canvas using DrawImageAsync

My question is, what's the best way to initially load that big spritesheet? I've tried using <img id='spritesheet' src='....'/> but I get an exception saying Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLImageElement or SVGImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap or OffscreenCanvas)'

My call is:

            ElementReference reference = new ElementReference("spritesheet");

            await _canvas2DContext.DrawImageAsync(reference, spritePosition.X, spritePosition.Y);

When I inspect my spritesheet in the DOM, it is:

image

Am I setting ElementReference incorrectly?

Are there any example of using DrawImageAsync anywhere?

@SteveDunn SteveDunn changed the title How do I drawn images? How do I draw images? Apr 21, 2020
@SteveDunn
Copy link
Author

Sorry, I've answered my own question!

My confusion arose from the fact that I thought ElementReference was the ID of the DOM element.

In fact, it's the property of the Blazor page! e.g.

<img @ref="spritesheet" hidden id="spritesheet" src="Assets/spritesheet.png" />

<BECanvas Width="300" Height="400" @ref="_canvasReference"></BECanvas>

@code
{
    ElementReference spritesheet;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant