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

Added WebGL Rendering Context #110

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target
Cargo.lock
examples/*/target
examples/*/Cargo.lock
.idea/
7 changes: 7 additions & 0 deletions examples/webgl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "webgl"
version = "0.1.0"
authors = ["Liana Pigeot <[email protected]>"]

[dependencies]
stdweb = { path = "../.." }
Empty file added examples/webgl/README.md
Empty file.
26 changes: 26 additions & 0 deletions examples/webgl/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extern crate stdweb;

use stdweb::unstable::TryInto;
use stdweb::web::{
IParentNode,
IHtmlElement,
document,
WebGLRenderingContext
};

use stdweb::web::html_element::CanvasElement;

fn main() {
stdweb::initialize();

let canvas: CanvasElement = document().query_selector( "#canvas" ).unwrap().unwrap().try_into().unwrap();
let context: WebGLRenderingContext = canvas.get_context().unwrap();

canvas.set_width(canvas.offset_width() as u32);
canvas.set_height(canvas.offset_height() as u32);

context.clear_color(0.7, 0.2, 0.5, 1.0);
context.clear();

stdweb::event_loop();
}
20 changes: 20 additions & 0 deletions examples/webgl/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>stdweb • Canvas</title>
<style>
html, body, canvas {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="js/app.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ pub mod web {
pub use webapi::history::History;
pub use webapi::web_socket::{WebSocket, SocketCloseCode};
pub use webapi::rendering_context::{RenderingContext, CanvasRenderingContext2d};
pub use webapi::webgl_rendering_context::WebGLRenderingContext;
pub use webapi::mutation_observer::{MutationObserver, MutationObserverHandle, MutationObserverInit, MutationRecord};
pub use webapi::xml_http_request::{XmlHttpRequest, XhrReadyState};

Expand Down
1 change: 1 addition & 0 deletions src/webapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod xml_http_request;
pub mod history;
pub mod web_socket;
pub mod rendering_context;
pub mod webgl_rendering_context;
pub mod mutation_observer;
pub mod error;
pub mod dom_exception;
Expand Down
45 changes: 45 additions & 0 deletions src/webapi/webgl_rendering_context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use webcore::value::{Reference, ConversionError};
use webcore::try_from::TryInto;
use webapi::rendering_context::RenderingContext;
use webapi::html_elements::CanvasElement;

/// Used for drawing Webgl content onto the canvas element.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext)
// https://html.spec.whatwg.org/#webglrenderingcontext
pub struct WebGLRenderingContext(Reference);
Copy link
Owner

Choose a reason for hiding this comment

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

To be consistent with the convention we've picked in the rest of the library this should be named WebGlRenderingContext (lowercase L)


reference_boilerplate! {
Copy link
Owner

Choose a reason for hiding this comment

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

This needs to be updated to the newest master where this macro was replaced by #[derive(ReferenceType)]

WebGLRenderingContext,
instanceof WebGLRenderingContext
Copy link
Owner

Choose a reason for hiding this comment

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

Also, we use spaces everywhere instead of tabs. (:

}

impl RenderingContext for WebGLRenderingContext {
type Error = ConversionError;
fn from_canvas(canvas: &CanvasElement) -> Result<Self, ConversionError> {
js!(
return @{canvas}.getContext("webgl");
).try_into()
}
}

impl WebGLRenderingContext {
/// This specifies what color values to use when calling the clear() method.
/// The values are clamped between 0 and 1.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/clearColor)
pub fn clear_color(&self, red: f64, green: f64, blue: f64, alpha: f64) {
Copy link
Owner

Choose a reason for hiding this comment

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

Please add link to the IDL entry in the specs (just as for our other APIs.)

js! { @(no_return)
@{&self.0}.clearColor(@{red}, @{green}, @{blue}, @{alpha});
}
}

/// The WebGLRenderingContext.clear() method of the WebGL API clears buffers to preset values.
///
/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/clear)
pub fn clear(&self) {
Copy link
Owner

Choose a reason for hiding this comment

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

This should take an enum as an argument instead of having the COLOR_BUFFER_BIT hardcoded.

js! { @(no_return)
@{&self.0}.clear(@{&self.0}.COLOR_BUFFER_BIT);
}
}
}