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

Question: Can you set canvas width and height to full document width and height? #18

Open
paularundel opened this issue Nov 13, 2018 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@paularundel
Copy link

Hi,

Is there a way to set canvas to full document width and height? Also can you retrieve the current canvas width and height in the component OnAfterRender()?

Thanks for any help

@galvesribeiro galvesribeiro self-assigned this Nov 26, 2018
@galvesribeiro galvesribeiro added the question Further information is requested label Nov 26, 2018
@galvesribeiro
Copy link
Member

Is there a way to set canvas to full document width and height?

You can set the width/height to whatever you want. Just set the properties on it.

Also can you retrieve the current canvas width and height in the component OnAfterRender()?

Today it doesn't support that as the ElementRef from Blazor doesn't allow you to invoke method/props on it.

@Blightbuster
Copy link

Blightbuster commented May 30, 2019

You can scale the canvas with css:

canvas {
    background-color: #ffffff;
    display: block;
    position: absolute;
    width: 100%;
    height: 100%;
}

Its not perfect since you will have issues with unproportional scaling (a square on a 100x100 canvas may be displayed as a rectangle) as long as you dont know the aspect ratio of the canvas.

@Blightbuster
Copy link

Blightbuster commented May 31, 2019

Ok so i tampered a bit and found a solution.
Put this into your css:

canvas {
    background-color: #ffffff;
    display: block;
    position: fixed;
}

And into your index.html you put this:

<script>
    function ResizeCanvas() {
        var canvas = document.querySelector('canvas');
        canvas.style.width = '100%';
        canvas.style.height = '100%';
        canvas.width = canvas.offsetWidth;
        canvas.height = canvas.offsetHeight;
        return { width: canvas.width, height: canvas.height };
    }
</script>

then call ResizeCanvas() with JSInterop either in the razor or in a cs file.

If you want to get the resulting size of the canvas you can use the return value of ResizeCanvas().

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

No branches or pull requests

3 participants