mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Update HTMLCanvasElement WebIDL definition
This commit is contained in:
parent
3528ef30e5
commit
462a488d55
2 changed files with 37 additions and 40 deletions
|
@ -17,7 +17,7 @@ use dom::bindings::inheritance::Castable;
|
||||||
use dom::bindings::num::Finite;
|
use dom::bindings::num::Finite;
|
||||||
use dom::bindings::reflector::DomObject;
|
use dom::bindings::reflector::DomObject;
|
||||||
use dom::bindings::root::{Dom, DomRoot, LayoutDom};
|
use dom::bindings::root::{Dom, DomRoot, LayoutDom};
|
||||||
use dom::bindings::str::DOMString;
|
use dom::bindings::str::{DOMString, USVString};
|
||||||
use dom::canvasrenderingcontext2d::{CanvasRenderingContext2D, LayoutCanvasRenderingContext2DHelpers};
|
use dom::canvasrenderingcontext2d::{CanvasRenderingContext2D, LayoutCanvasRenderingContext2DHelpers};
|
||||||
use dom::document::Document;
|
use dom::document::Document;
|
||||||
use dom::element::{AttributeMutation, Element, RawLayoutElementHelpers};
|
use dom::element::{AttributeMutation, Element, RawLayoutElementHelpers};
|
||||||
|
@ -191,12 +191,12 @@ impl HTMLCanvasElement {
|
||||||
pub fn get_or_init_webgl_context(
|
pub fn get_or_init_webgl_context(
|
||||||
&self,
|
&self,
|
||||||
cx: *mut JSContext,
|
cx: *mut JSContext,
|
||||||
attrs: Option<HandleValue>
|
options: HandleValue,
|
||||||
) -> Option<DomRoot<WebGLRenderingContext>> {
|
) -> Option<DomRoot<WebGLRenderingContext>> {
|
||||||
if self.context.borrow().is_none() {
|
if self.context.borrow().is_none() {
|
||||||
let window = window_from_node(self);
|
let window = window_from_node(self);
|
||||||
let size = self.get_size();
|
let size = self.get_size();
|
||||||
let attrs = Self::get_gl_attributes(cx, attrs)?;
|
let attrs = Self::get_gl_attributes(cx, options)?;
|
||||||
let maybe_ctx = WebGLRenderingContext::new(&window, self, WebGLVersion::WebGL1, size, attrs);
|
let maybe_ctx = WebGLRenderingContext::new(&window, self, WebGLVersion::WebGL1, size, attrs);
|
||||||
|
|
||||||
*self.context.borrow_mut() = maybe_ctx.map( |ctx| CanvasContext::WebGL(Dom::from_ref(&*ctx)));
|
*self.context.borrow_mut() = maybe_ctx.map( |ctx| CanvasContext::WebGL(Dom::from_ref(&*ctx)));
|
||||||
|
@ -212,7 +212,7 @@ impl HTMLCanvasElement {
|
||||||
pub fn get_or_init_webgl2_context(
|
pub fn get_or_init_webgl2_context(
|
||||||
&self,
|
&self,
|
||||||
cx: *mut JSContext,
|
cx: *mut JSContext,
|
||||||
attrs: Option<HandleValue>
|
options: HandleValue,
|
||||||
) -> Option<DomRoot<WebGL2RenderingContext>> {
|
) -> Option<DomRoot<WebGL2RenderingContext>> {
|
||||||
if !PREFS.is_webgl2_enabled() {
|
if !PREFS.is_webgl2_enabled() {
|
||||||
return None
|
return None
|
||||||
|
@ -220,7 +220,7 @@ impl HTMLCanvasElement {
|
||||||
if self.context.borrow().is_none() {
|
if self.context.borrow().is_none() {
|
||||||
let window = window_from_node(self);
|
let window = window_from_node(self);
|
||||||
let size = self.get_size();
|
let size = self.get_size();
|
||||||
let attrs = Self::get_gl_attributes(cx, attrs)?;
|
let attrs = Self::get_gl_attributes(cx, options)?;
|
||||||
let maybe_ctx = WebGL2RenderingContext::new(&window, self, size, attrs);
|
let maybe_ctx = WebGL2RenderingContext::new(&window, self, size, attrs);
|
||||||
|
|
||||||
*self.context.borrow_mut() = maybe_ctx.map( |ctx| CanvasContext::WebGL2(Dom::from_ref(&*ctx)));
|
*self.context.borrow_mut() = maybe_ctx.map( |ctx| CanvasContext::WebGL2(Dom::from_ref(&*ctx)));
|
||||||
|
@ -243,12 +243,8 @@ impl HTMLCanvasElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
fn get_gl_attributes(cx: *mut JSContext, attrs: Option<HandleValue>) -> Option<GLContextAttributes> {
|
fn get_gl_attributes(cx: *mut JSContext, options: HandleValue) -> Option<GLContextAttributes> {
|
||||||
let webgl_attributes = match attrs {
|
match unsafe { WebGLContextAttributes::new(cx, options) } {
|
||||||
Some(attrs) => attrs,
|
|
||||||
None => return Some(GLContextAttributes::default()),
|
|
||||||
};
|
|
||||||
match unsafe { WebGLContextAttributes::new(cx, webgl_attributes) } {
|
|
||||||
Ok(ConversionResult::Success(ref attrs)) => Some(From::from(attrs)),
|
Ok(ConversionResult::Success(ref attrs)) => Some(From::from(attrs)),
|
||||||
Ok(ConversionResult::Failure(ref error)) => {
|
Ok(ConversionResult::Failure(ref error)) => {
|
||||||
unsafe { throw_type_error(cx, &error); }
|
unsafe { throw_type_error(cx, &error); }
|
||||||
|
@ -310,36 +306,39 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-canvas-height
|
// https://html.spec.whatwg.org/multipage/#dom-canvas-height
|
||||||
make_uint_setter!(SetHeight, "height", DEFAULT_HEIGHT);
|
make_uint_setter!(SetHeight, "height", DEFAULT_HEIGHT);
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-canvas-getcontext
|
// https://html.spec.whatwg.org/multipage/#dom-canvas-getcontext
|
||||||
unsafe fn GetContext(&self,
|
#[allow(unsafe_code)]
|
||||||
|
unsafe fn GetContext(
|
||||||
|
&self,
|
||||||
cx: *mut JSContext,
|
cx: *mut JSContext,
|
||||||
id: DOMString,
|
id: DOMString,
|
||||||
attributes: Vec<HandleValue>)
|
options: HandleValue,
|
||||||
-> Option<RenderingContext> {
|
) -> Option<RenderingContext> {
|
||||||
match &*id {
|
match &*id {
|
||||||
"2d" => {
|
"2d" => {
|
||||||
self.get_or_init_2d_context()
|
self.get_or_init_2d_context()
|
||||||
.map(RenderingContext::CanvasRenderingContext2D)
|
.map(RenderingContext::CanvasRenderingContext2D)
|
||||||
}
|
}
|
||||||
"webgl" | "experimental-webgl" => {
|
"webgl" | "experimental-webgl" => {
|
||||||
self.get_or_init_webgl_context(cx, attributes.get(0).cloned())
|
self.get_or_init_webgl_context(cx, options)
|
||||||
.map(RenderingContext::WebGLRenderingContext)
|
.map(RenderingContext::WebGLRenderingContext)
|
||||||
}
|
}
|
||||||
"webgl2" | "experimental-webgl2" => {
|
"webgl2" | "experimental-webgl2" => {
|
||||||
self.get_or_init_webgl2_context(cx, attributes.get(0).cloned())
|
self.get_or_init_webgl2_context(cx, options)
|
||||||
.map(RenderingContext::WebGL2RenderingContext)
|
.map(RenderingContext::WebGL2RenderingContext)
|
||||||
}
|
}
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
|
||||||
// https://html.spec.whatwg.org/multipage/#dom-canvas-todataurl
|
// https://html.spec.whatwg.org/multipage/#dom-canvas-todataurl
|
||||||
unsafe fn ToDataURL(&self,
|
#[allow(unsafe_code)]
|
||||||
|
unsafe fn ToDataURL(
|
||||||
|
&self,
|
||||||
_context: *mut JSContext,
|
_context: *mut JSContext,
|
||||||
_mime_type: Option<DOMString>,
|
_mime_type: Option<DOMString>,
|
||||||
_arguments: Vec<HandleValue>) -> Fallible<DOMString> {
|
_quality: HandleValue,
|
||||||
|
) -> Fallible<USVString> {
|
||||||
// Step 1.
|
// Step 1.
|
||||||
if let Some(CanvasContext::Context2d(ref context)) = *self.context.borrow() {
|
if let Some(CanvasContext::Context2d(ref context)) = *self.context.borrow() {
|
||||||
if !context.origin_is_clean() {
|
if !context.origin_is_clean() {
|
||||||
|
@ -349,7 +348,7 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
||||||
|
|
||||||
// Step 2.
|
// Step 2.
|
||||||
if self.Width() == 0 || self.Height() == 0 {
|
if self.Width() == 0 || self.Height() == 0 {
|
||||||
return Ok(DOMString::from("data:,"));
|
return Ok(USVString("data:,".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3.
|
// Step 3.
|
||||||
|
@ -363,13 +362,13 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
||||||
Some(CanvasContext::WebGL(ref context)) => {
|
Some(CanvasContext::WebGL(ref context)) => {
|
||||||
match context.get_image_data(self.Width(), self.Height()) {
|
match context.get_image_data(self.Width(), self.Height()) {
|
||||||
Some(data) => data,
|
Some(data) => data,
|
||||||
None => return Ok("data:,".into()),
|
None => return Ok(USVString("data:,".into())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(CanvasContext::WebGL2(ref context)) => {
|
Some(CanvasContext::WebGL2(ref context)) => {
|
||||||
match context.base_context().get_image_data(self.Width(), self.Height()) {
|
match context.base_context().get_image_data(self.Width(), self.Height()) {
|
||||||
Some(data) => data,
|
Some(data) => data,
|
||||||
None => return Ok("data:,".into()),
|
None => return Ok(USVString("data:,".into())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
@ -388,7 +387,7 @@ impl HTMLCanvasElementMethods for HTMLCanvasElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
let encoded = base64::encode(&encoded);
|
let encoded = base64::encode(&encoded);
|
||||||
Ok(DOMString::from(format!("data:{};base64,{}", mime_type, encoded)))
|
Ok(USVString(format!("data:{};base64,{}", mime_type, encoded)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,20 +5,18 @@
|
||||||
// https://html.spec.whatwg.org/multipage/#htmlcanvaselement
|
// https://html.spec.whatwg.org/multipage/#htmlcanvaselement
|
||||||
typedef (CanvasRenderingContext2D or WebGLRenderingContext or WebGL2RenderingContext) RenderingContext;
|
typedef (CanvasRenderingContext2D or WebGLRenderingContext or WebGL2RenderingContext) RenderingContext;
|
||||||
|
|
||||||
[HTMLConstructor]
|
[Exposed=Window,
|
||||||
|
HTMLConstructor]
|
||||||
interface HTMLCanvasElement : HTMLElement {
|
interface HTMLCanvasElement : HTMLElement {
|
||||||
[CEReactions, Pure]
|
[CEReactions, Pure] attribute unsigned long width;
|
||||||
attribute unsigned long width;
|
[CEReactions, Pure] attribute unsigned long height;
|
||||||
[CEReactions, Pure]
|
|
||||||
attribute unsigned long height;
|
|
||||||
|
|
||||||
RenderingContext? getContext(DOMString contextId, any... arguments);
|
RenderingContext? getContext(DOMString contextId, optional any options = null);
|
||||||
//boolean probablySupportsContext(DOMString contextId, any... arguments);
|
|
||||||
|
|
||||||
//void setContext(RenderingContext context);
|
|
||||||
//CanvasProxy transferControlToProxy();
|
|
||||||
|
|
||||||
[Throws]
|
[Throws]
|
||||||
DOMString toDataURL(optional DOMString type, any... arguments);
|
USVString toDataURL(optional DOMString type, optional any quality);
|
||||||
//void toBlob(FileCallback? _callback, optional DOMString type, any... arguments);
|
//void toBlob(BlobCallback _callback, optional DOMString type, optional any quality);
|
||||||
|
//OffscreenCanvas transferControlToOffscreen();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//callback BlobCallback = void (Blob? blob);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue