Code Monkey home page Code Monkey logo

webgl-2d's Introduction

WebGL-2D

HTML5 Canvas2D API in WebGL.

Authors:

This project aims to be a complete port of the Canvas2D API implemented in a WebGL context. WebGL-2D is a proof of concept and attempts to ascertain performance improvements over Canvas2D.

It should allow most Canvas2D applications to be switched to a WebGL context.

Check out a LIVE DEMO!

20110304-qfkhgf9hjin5uk3we9rmgpwtf8.jpg

Usage

Switching your Canvas2D sketch to a WebGL2D is very simple and only requires one additional line of code:

var cvs = document.getElementById("myCanvas");

WebGL2D.enable(cvs); // adds "webgl-2d" context to cvs

var ctx = cvs.getContext("webgl-2d"); // easily switch between "webgl-2d" and "2d" contexts

Supported Canvas2D Features

WebGL-2D is a work in progress and currently supports a very small subset of the Canvas2D API.

  • strokeStyle
  • fillStyle
  • strokeRect
  • fillRect
  • translate
  • rotate
  • scale
  • save
  • restore
  • lineWidth
  • drawImage
  • createImageData
  • getImageData
  • putImageData
  • beginPath
  • closePath
  • moveTo
  • lineTo
  • rect
  • fill*
  • stroke*

*fill and stroke only work on straight line paths supported above, arc, and curves are not yet supported.

All other properties or functions are currently stubbed. Properties are set to their defaults and functions are empty.

Benchmarks

WebGL-2D should be faster than Canvas2D at certain things and slower at others. It is important that you know what to expect when using WebGL-2D with your project.

The benchmarks directory contains benchmarks for testing performance

asteroidsbench/

Kevin Roast has graciously given permission to include this benchmark in the WebGL-2D project.

This benchmark is perfect for testing real world canvas usage in game engine. It relies heavily on 9 arg drawImage cropping to implement scrolling backgrounds and stripstrip animations.

Visit his website for other HTML5 canvas demos as well as the asteroids game

webgl-2d's People

Contributors

cjcliffe avatar corbanbrook avatar secretrobotron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webgl-2d's Issues

clearRect is missing

clearRect is missing and would be nice to have. You may have to fiddle around with the WebGL premultiplied attribs to make it work properly though.

drawImage Example is Broken

On opening the draw image example, I see no hearts, but when I switch tabs away and back, they show up.

Occurs in Chrome 11.0.696.57

Bezier curves

Hi,

I've added bezier support in my fork (https://github.com/nelsonsilva/webgl-2d).

I haven't submitted a pull request since I want to fix the filling of non convex polygons.
If I add each bezier as a subpath the rendering of each of these filled is not the same as filling the whole path.
If I add all to a single subpath the use of the triangle fan doesn't work properly for non convex polygons.
I have even tried to calculate the lower left of the path and use it as the first point but it still doesn't seem to work properly.
Should we add tesselation to fix this ?

the Live Demo is broken

The Live Demo is broken, it seems like it just renders the first frame and then it just becomes blank.

First I thought that it just were my system that had fracked up, as the desktop compositing has been disabled somehow (It did not like that I had to disconnect the secondary monitor in order to make the laptop portable for a while). But then I stumbled across this beautiful water demo, which uses WebGL. The Asteroids Demo does also work as it should.

I'm using Firefox 6.0.2 with Vimperator. I've not got Flash installed, or anything else special that I think could affect in some way, I've got the latest nVidia OpenGL drivers but I do not play much games, I use it mainly just to activate compositing.

Unit Tests

Contribution to this project seems unfortunately limited due the lack of a test suite. Are there any plans to include a definitive unit test suite?

Is there a Roadmap?

I tried pluggin' in a couple of old stuff and it's pretty flawless! I was wondering what you guys had plan to implement / not. Like for example save(), restore(), or clearRect()???

Really awesome — saves lotsa time

^^

getImageData throwing exception

No matter what I do, the call to getImageData() seems to throw the awesomely vague exception "Uncaught TypeError: Type error" on line 516.

The code looks like this:

this.canvas = canvas;
if ('WebGL2D' in window) {
    console.log("WebGL-2D Enabled!");
    WebGL2D.enable(this.canvas);
    this.context = canvas.getContext("webgl-2d");
} else {
    this.context = canvas.getContext("2d");
}

this.context.drawImage(image, 0, 0, image.width, image.height);

this.image_data = this.context.getImageData(0, 0, image.width, image.height);

clip() is missing

clip() is missing and would be useful. Implementation is hmm.. You could use the stencil buffer. Fill current path to stencil buffer, enable stencil test.

Demo is broken

Server error when one clicks the link on the homepage.

arc

arc can be done either by generating circle geometry with specified angle delta between two segments or by drawing a quad and using a fragment shader to reject pixels outside the circle (antialias by controlling opacity based on how much of a sample is inside the circle).

Weird output

Hi, I'm the developer of the Isogenic Game Engine and I thought it would be cool to test webgl-2d on our engine.

I've got some questions that might lead to a bug fix here.

Please check the screenshot here: http://www.isogenicengine.com/wp-content/uploads/2011/05/webgl-2d-bug.png

The image actually displays proper graphics for about 50 milliseconds then goes into this ghosted image view. My first suspicion is that upon resizing the canvas (which Isogenic does automatically when the browser loads and when it is resized) may be causing this issue. Does webgl-2d support changing the size of the canvas AFTER the context has been set up?

If I turn off automatic resizing of the canvas to fill the browser, all I get is a big white screen where I assume the clearRect cleared the canvas but nothing is drawn after that! This may also have something to do with the issue about the hearts not being drawn in the drawImage example - in that example the canvas stays completely white as well.

The expected result (and how it looks with the '2d' context is this): http://www.isogenicengine.com/wp-content/uploads/2011/05/webgl-2d-bug2.png - That shot has auto-resize of the canvas turned off, the first DOES NOT so although you see a similar sized box, the canvas actually fills up the entire screen in the first shot, this is why I was leaning to a resize issue, like the size of the redraw area stayed the same even after resizing the canvas.

All tests run in Chrome 11.0.696.65

Any thoughts? I would be happy to investigate further and post bug fixes if I can help out!

.width and .height don't work with webgl-2d canvas ?

Hello
I am trying to use webgl-2d instead of my usual canvas but I get a size problem.
I define .width and .height for resolution and .style.width and .style.height for size on screen. But with webgl-2d, the canvas is small in the bottom left corner.
Is there a specific way to give webgl-2d canvas their size ?

Need preserveDrawingBuffer: true

WebGL by default does not preserve the contents of the canvas across draw calls like canvas 2d does.

To fix this you need to pass in {preserveDrawingBuffer: true} to getContext

Any updated fork?

I'm curious if there is an up-to-date fork of this project, or a similar project. Seems like a great concept! But a lot of bugs exist now (maybe due to browser API changes?).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.