-
Notifications
You must be signed in to change notification settings - Fork 178
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ target | |
Cargo.lock | ||
examples/*/target | ||
examples/*/Cargo.lock | ||
.idea/ |
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 = "../.." } |
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(); | ||
} |
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> |
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); | ||
|
||
reference_boilerplate! { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be updated to the newest |
||
WebGLRenderingContext, | ||
instanceof WebGLRenderingContext | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should take an enum as an argument instead of having the |
||
js! { @(no_return) | ||
@{&self.0}.clear(@{&self.0}.COLOR_BUFFER_BIT); | ||
} | ||
} | ||
} |
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.
To be consistent with the convention we've picked in the rest of the library this should be named
WebGlRenderingContext
(lowercaseL
)