Skip to content

Commit

Permalink
Use convertToBlob for OffscreenCanvas (#3341)
Browse files Browse the repository at this point in the history
* Use `convertToBlob` for OffscreenCanvas

* Generate code

* add ImageEncodeOptions

* Update Cargo.toml
  • Loading branch information
namse authored May 8, 2023
1 parent ceaf996 commit c600fbe
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 30 deletions.
1 change: 1 addition & 0 deletions crates/web-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ ImageDecodeOptions = []
ImageDecodeResult = []
ImageDecoder = []
ImageDecoderInit = []
ImageEncodeOptions = []
ImageOrientation = []
ImageTrack = ["EventTarget"]
ImageTrackList = []
Expand Down
57 changes: 57 additions & 0 deletions crates/web-sys/src/features/gen_ImageEncodeOptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#![allow(unused_imports)]
use super::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
# [wasm_bindgen (extends = :: js_sys :: Object , js_name = ImageEncodeOptions)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[doc = "The `ImageEncodeOptions` dictionary."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `ImageEncodeOptions`*"]
pub type ImageEncodeOptions;
}
impl ImageEncodeOptions {
#[doc = "Construct a new `ImageEncodeOptions`."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `ImageEncodeOptions`*"]
pub fn new() -> Self {
#[allow(unused_mut)]
let mut ret: Self = ::wasm_bindgen::JsCast::unchecked_into(::js_sys::Object::new());
ret
}
#[doc = "Change the `quality` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `ImageEncodeOptions`*"]
pub fn quality(&mut self, val: f64) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(
self.as_ref(),
&JsValue::from("quality"),
&JsValue::from(val),
);
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self
}
#[doc = "Change the `type` field of this object."]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `ImageEncodeOptions`*"]
pub fn type_(&mut self, val: &str) -> &mut Self {
use wasm_bindgen::JsValue;
let r = ::js_sys::Reflect::set(self.as_ref(), &JsValue::from("type"), &JsValue::from(val));
debug_assert!(
r.is_ok(),
"setting properties should never fail on our dictionary objects"
);
let _ = r;
self
}
}
impl Default for ImageEncodeOptions {
fn default() -> Self {
Self::new()
}
}
46 changes: 18 additions & 28 deletions crates/web-sys/src/features/gen_OffscreenCanvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ extern "C" {
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `OffscreenCanvas`*"]
pub fn new(width: u32, height: u32) -> Result<OffscreenCanvas, JsValue>;
# [wasm_bindgen (catch , method , structural , js_class = "OffscreenCanvas" , js_name = convertToBlob)]
#[doc = "The `convertToBlob()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas/convertToBlob)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `OffscreenCanvas`*"]
pub fn convert_to_blob(this: &OffscreenCanvas) -> Result<::js_sys::Promise, JsValue>;
#[cfg(feature = "ImageEncodeOptions")]
# [wasm_bindgen (catch , method , structural , js_class = "OffscreenCanvas" , js_name = convertToBlob)]
#[doc = "The `convertToBlob()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas/convertToBlob)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `ImageEncodeOptions`, `OffscreenCanvas`*"]
pub fn convert_to_blob_with_options(
this: &OffscreenCanvas,
options: &ImageEncodeOptions,
) -> Result<::js_sys::Promise, JsValue>;
# [wasm_bindgen (catch , method , structural , js_class = "OffscreenCanvas" , js_name = getContext)]
#[doc = "The `getContext()` method."]
#[doc = ""]
Expand All @@ -67,34 +85,6 @@ extern "C" {
context_id: &str,
context_options: &::wasm_bindgen::JsValue,
) -> Result<Option<::js_sys::Object>, JsValue>;
# [wasm_bindgen (catch , method , structural , js_class = "OffscreenCanvas" , js_name = toBlob)]
#[doc = "The `toBlob()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas/toBlob)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `OffscreenCanvas`*"]
pub fn to_blob(this: &OffscreenCanvas) -> Result<::js_sys::Promise, JsValue>;
# [wasm_bindgen (catch , method , structural , js_class = "OffscreenCanvas" , js_name = toBlob)]
#[doc = "The `toBlob()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas/toBlob)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `OffscreenCanvas`*"]
pub fn to_blob_with_type(
this: &OffscreenCanvas,
type_: &str,
) -> Result<::js_sys::Promise, JsValue>;
# [wasm_bindgen (catch , method , structural , js_class = "OffscreenCanvas" , js_name = toBlob)]
#[doc = "The `toBlob()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas/toBlob)"]
#[doc = ""]
#[doc = "*This API requires the following crate features to be activated: `OffscreenCanvas`*"]
pub fn to_blob_with_type_and_encoder_options(
this: &OffscreenCanvas,
type_: &str,
encoder_options: &::wasm_bindgen::JsValue,
) -> Result<::js_sys::Promise, JsValue>;
#[cfg(feature = "ImageBitmap")]
# [wasm_bindgen (catch , method , structural , js_class = "OffscreenCanvas" , js_name = transferToImageBitmap)]
#[doc = "The `transferToImageBitmap()` method."]
Expand Down
6 changes: 6 additions & 0 deletions crates/web-sys/src/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4030,6 +4030,12 @@ mod gen_ImageDecoderInit;
#[cfg(feature = "ImageDecoderInit")]
pub use gen_ImageDecoderInit::*;

#[cfg(feature = "ImageEncodeOptions")]
#[allow(non_snake_case)]
mod gen_ImageEncodeOptions;
#[cfg(feature = "ImageEncodeOptions")]
pub use gen_ImageEncodeOptions::*;

#[cfg(feature = "ImageOrientation")]
#[allow(non_snake_case)]
mod gen_ImageOrientation;
Expand Down
8 changes: 6 additions & 2 deletions crates/web-sys/webidls/enabled/OffscreenCanvas.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
* https://wiki.whatwg.org/wiki/OffscreenCanvas
*/

dictionary ImageEncodeOptions {
DOMString type = "image/png";
unrestricted double quality;
};

[Constructor(unsigned long width, unsigned long height),
Exposed=(Window,Worker),
Func="mozilla::dom::DOMPrefs::OffscreenCanvasEnabled"]
Expand All @@ -23,8 +28,7 @@ interface OffscreenCanvas : EventTarget {
[Throws]
ImageBitmap transferToImageBitmap();
[Throws]
Promise<Blob> toBlob(optional DOMString type = "",
optional any encoderOptions);
Promise<Blob> convertToBlob(optional ImageEncodeOptions options = {});
};

// OffscreenCanvas includes Transferable;

0 comments on commit c600fbe

Please sign in to comment.