Skip to content
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

Merged
merged 2 commits into from Jan 20, 2018
Merged

Add Canvas API #80

merged 2 commits into from Jan 20, 2018

Conversation

Diggsey
Copy link
Contributor

@Diggsey Diggsey commented Jan 14, 2018

No description provided.

///
/// 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)
Copy link
Owner

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?

Copy link
Contributor Author

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.

js! (
return @{self}.getContext(@{T::CONTEXT_TYPE});
).try_into().ok().and_then(|r| T::from_reference(r))
}
Copy link
Owner

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?)

Copy link
Contributor Author

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...

/// 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> ) {
Copy link
Owner

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!)

Copy link
Owner

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

@@ -654,6 +661,7 @@ macro_rules! reference_boilerplate {
};
}

#[macro_export]
Copy link
Owner

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 RenderingContexts implementations inside stdweb for now. (They're JS APIs after all!)

Copy link
Contributor Author

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.

Copy link
Owner

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.

use webapi::blob::Blob;

/// The HTML image element is used to manipulate the layout and presentation of
/// `<img>` elements.
Copy link
Owner

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. (:

@Diggsey
Copy link
Contributor Author

Diggsey commented Jan 18, 2018

@koute is this good to go?

@koute
Copy link
Owner

koute commented Jan 18, 2018

Right, sorry!

It's almost ready to go! Let me just put a few more comments.

}

#[derive(Clone, Debug)]
struct CanvasRenderingContext2D(Reference);
Copy link
Owner

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 RenderingContexts 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.

Copy link
Contributor Author

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

Copy link
Owner

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,
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah

/// 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;
Copy link
Owner

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>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah

@Diggsey
Copy link
Contributor Author

Diggsey commented Jan 20, 2018

@koute updated.

@koute
Copy link
Owner

koute commented Jan 20, 2018

Looks good to me except you forgot to change 2D into 2d. (:

But to not delay merging it I'll just search & replace it myself.

Thanks a lot for the PR!

@koute koute merged commit 039efb4 into koute:master Jan 20, 2018
@Diggsey
Copy link
Contributor Author

Diggsey commented Jan 20, 2018

Oops, the comment got lost somehow. Thanks!

@Boscop
Copy link

Boscop commented Mar 29, 2018

It only support fill_rect right now, right?

@Diggsey
Copy link
Contributor Author

Diggsey commented Mar 29, 2018

@Boscop yeah, it was just laying the ground-work, no reason not to add more though!

@Diggsey Diggsey deleted the canvas branch March 29, 2018 15:11
@Boscop
Copy link

Boscop commented May 4, 2018

Can you please add clearRect and fillText, too? Then I can port my current polymer app to Rust :)
I could do clearRect myself but how to pass the string to js for fillText?
I'm interested in contributing to expand the canvas API coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants