BlazorExtensions / Canvas

HTML5 Canvas API implementation for Microsoft Blazor
MIT License
620 stars 144 forks source link

Support for toDataURL #72

Open erikthysell opened 4 years ago

erikthysell commented 4 years ago

Using canvas with an image that refreshes very often (showing simulation real time) I am using a library that requires an url to the image. Therefore I would love a canvas.toDataURL() function.

Leaflet.ImageOverlay.Rotated.js ->

var overlay = L.imageOverlay.rotated(...        }); 
// routine that updates image
overlay.setUrl(canvas.toDataURL());
g00fy88 commented 1 year ago

I agree! Would be nice, if this library supports the toDataUrlfunctionality out of the box. The workaround I am currently using goes like this:

index.html

<script>CanvasDataUrl = (canvas, dataType) => { return canvas.toDataURL(dataType); }</script>

MyCanvas.razor.cs

[Inject]
public IJSRuntime JSRuntime { get; set; }

protected BECanvasComponent _beRef;

string myCanvasDataUrl = "";

public async Task StoreCanvasImage()
{
   this.myCanvasDataUrl = await this.JSRuntime.InvokeAsync<string>("CanvasDataUrl", this._beRef.CanvasReference, "image/jpeg");
}