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
Add Canvas API #80
Add Canvas API #80
Conversation
/// | ||
/// MDN incorrectly documents this as a UIEvent, but in browsers it is actually | ||
/// just an Event. | ||
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/Events/resize) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check what the specs say?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was changed in the specs recently from a UIEvent to an Event to match expectations/browser implementations. Either way, this errors in all browsers if it's specified as a UIEvent rather than an Event.
src/webapi/html_elements/canvas.rs
Outdated
js! ( | ||
return @{self}.getContext(@{T::CONTEXT_TYPE}); | ||
).try_into().ok().and_then(|r| T::from_reference(r)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might not be flexible enough. E.g. for WebGL at some point in time in various browsers you could use one of the following names for what effectively was the same kind of a context: "webgl", "experimental-webgl", "webkit-3d", "moz-webgl"
, so this mechanism should support specifying a list. (Or perhaps we should just have the RenderingContext
export a get_context
method and just delegate calling getContext
to it completely?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will give RenderingContext
a from_canvas
method or something...
src/webapi/html_elements/canvas.rs
Outdated
/// cached on the disk or stored in memory at the discretion of the user agent. | ||
/// | ||
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob) | ||
pub fn to_blob<F: FnMut(Blob) + 'static>( &self, f: F, mime_type: Option<&str>, quality: Option<f64> ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FnOnce
(There is a wrapper called Once
which allows passing FnOnce
functions to js!
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, semi related question - do you know why the original JS API takes a callback here instead of returning a Blob
? Is it because the Blob
is done asynchronously?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
src/webcore/macros.rs
Outdated
@@ -654,6 +661,7 @@ macro_rules! reference_boilerplate { | |||
}; | |||
} | |||
|
|||
#[macro_export] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I assume you're exporting these to allow the context to be implemented externally? Hmmm.... Could we keep those private? I would actually be fine having the RenderingContext
s implementations inside stdweb
for now. (They're JS APIs after all!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, OK, but I think these macros should be exposed anyway - it's not uncommon to want to wrap new types of references.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not against exposing something like this macro in the future, but I'd rather keep it private for now. Unlike the js!
macro this macro wasn't designed to be used outside of stdweb
, so I will likely want to export something slightly different to the outside.
src/webapi/html_elements/canvas.rs
Outdated
use webapi::blob::Blob; | ||
|
||
/// The HTML image element is used to manipulate the layout and presentation of | ||
/// `<img>` elements. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc comment is wrong. (:
@koute is this good to go? |
Right, sorry! It's almost ready to go! Let me just put a few more comments. |
examples/canvas/src/main.rs
Outdated
} | ||
|
||
#[derive(Clone, Debug)] | ||
struct CanvasRenderingContext2D(Reference); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I wrote in one of the comments, I would be fine with having the RenderingContext
s implementations inside of stdweb
, and I think I'd actually prefer that right now, so please move this from the example to inside the stdweb
.
Also, please rename the 2D
to 2d
to keep with the PascalCase
naming convention we've decided the other day to stick to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, OK I will move it in - I'm not planning on expanding on the set of implemented methods though.
I do think these would work better as a separate crate: have you thought about splitting up stdweb?
eg.
- stdweb-core (contains just webcore)
- stdweb-common (contains bindings to core js APIs, not specific to environment)
- stdweb-browser (browser-specific bindings)
- stdweb-node (node-specific bindings)
Adding everything into stdweb
will increase compile times quite a bit, especially if you add the whole of canvas 2d, webgl and webgl 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's fine not adding every method there is right now.
Yes, I did think about splitting it up in the future! The first thing I'd like to split up is webapi
and webcore
directories, and then we'll go from there and see if/what would make sense to further split up. However, I don't want to do that just yet.
As far as the compile times go there is probably some room for improvement too without splitting it up. (E.g. once we make the js!
macro into a procedural macro)
src/lib.rs
Outdated
@@ -188,12 +190,16 @@ pub mod web { | |||
KeyupEvent, | |||
ClickEvent, | |||
DoubleClickEvent, | |||
MouseDownEvent, | |||
MouseUpEvent, | |||
MouseMoveEvent, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were already exported in another PR, so please get rid of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
src/webapi/html_elements/canvas.rs
Outdated
/// Trait implemented by rendering contexts which can be obtained from a canvas. | ||
pub trait RenderingContext { | ||
/// Name which identifies this kind of rendering context. | ||
fn from_canvas(canvas: &CanvasElement) -> Option<Self> where Self: Sized; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make more sense for this to return Result<Self, Self::Error>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
@koute updated. |
Looks good to me except you forgot to change But to not delay merging it I'll just search & replace it myself. Thanks a lot for the PR! |
Oops, the comment got lost somehow. Thanks! |
It only support |
@Boscop yeah, it was just laying the ground-work, no reason not to add more though! |
Can you please add |
No description provided.