HtmlCanvasElement
sealed class HtmlCanvasElement : HtmlElement, IHtmlCanvasElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers
Represents the HTML canvas element.
See: http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html
Alternative: http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#the-canvas-element
using AngleSharp.Dom.Css;
using AngleSharp.Dom.Events;
using AngleSharp.Dom.Media;
using AngleSharp.Extensions;
using AngleSharp.Html;
using AngleSharp.Network;
using AngleSharp.Services;
using System;
using System.IO;
namespace AngleSharp.Dom.Html
{
internal sealed class HtmlCanvasElement : HtmlElement, IHtmlCanvasElement, IHtmlElement, IElement, INode, IEventTarget, IMarkupFormattable, IParentNode, IChildNode, INonDocumentTypeChildNode, IElementCssInlineStyle, IGlobalEventHandlers
{
private enum ContextMode : byte
{
None,
Direct2d,
DirectWebGl,
Indirect,
Proxied
}
private ContextMode _mode;
private IRenderingContext _current;
public int Width {
get {
return this.GetOwnAttribute(AttributeNames.Width).ToInteger(300);
}
set {
this.SetOwnAttribute(AttributeNames.Width, value.ToString(), false);
}
}
public int Height {
get {
return this.GetOwnAttribute(AttributeNames.Height).ToInteger(150);
}
set {
this.SetOwnAttribute(AttributeNames.Height, value.ToString(), false);
}
}
public HtmlCanvasElement(Document owner, string prefix = null)
: base(owner, TagNames.Canvas, prefix, NodeFlags.None)
{
_mode = ContextMode.None;
}
public IRenderingContext GetContext(string contextId)
{
if (_current == null || contextId.Isi(_current.ContextId)) {
foreach (IRenderingService service in base.Owner.Options.GetServices<IRenderingService>()) {
if (service.IsSupportingContext(contextId)) {
IRenderingContext renderingContext = service.CreateContext(this, contextId);
if (renderingContext != null) {
_mode = GetModeFrom(contextId);
_current = renderingContext;
}
return renderingContext;
}
}
return null;
}
return _current;
}
public bool IsSupportingContext(string contextId)
{
foreach (IRenderingService service in base.Owner.Options.GetServices<IRenderingService>()) {
if (service.IsSupportingContext(contextId))
return true;
}
return false;
}
public void SetContext(IRenderingContext context)
{
if (_mode != 0 && _mode != ContextMode.Indirect)
throw new DomException(DomError.InvalidState);
if (context.IsFixed)
throw new DomException(DomError.InvalidState);
if (context.Host != this)
throw new DomException(DomError.InUse);
_current = context;
_mode = ContextMode.Indirect;
}
public string ToDataUrl(string type = null)
{
return Convert.ToBase64String(GetImageData(type));
}
public void ToBlob(Action<Stream> callback, string type = null)
{
MemoryStream obj = new MemoryStream(GetImageData(type));
callback(obj);
}
private byte[] GetImageData(string type)
{
return _current?.ToImage(type ?? MimeTypeNames.Plain) ?? new byte[0];
}
private static ContextMode GetModeFrom(string contextId)
{
if (contextId.Isi("2d"))
return ContextMode.Direct2d;
if (contextId.Isi("webgl"))
return ContextMode.DirectWebGl;
return ContextMode.None;
}
}
}