Giter Club home page Giter Club logo

canvas2dtowebgl's Introduction

Canvas2DtoWebGL.js

Canvas2DtoWebGL.js ports almost all the methods from the regular Canvas2D context (CanvasRenderingContext2D) of HTML5 to WebGL calls, this allows to mix 3D in your 2D Canvas or the opposite (to create GUIs, debug info, etc), and in some cases it could even improve the performance of your regular Canvas2D (when working with big images). It uses litegl.js as the base WebGL library. To improve performance it doesn't generate garbage (reuses the same containers). It can work with non power of two textures (no mipmaps obviously).

Fully supported functions:

  • translate, rotate, scale, transform, setTransform, save, restore
  • clearRect
  • strokeStyle, fillStyle, globalAlpha
  • drawImage you can use images or canvas (video not tested)
  • beginPath, lineTo, moveTo, closePath, stroke, rect, strokeRect, fillRect, arc
  • fill (limited to convex shapes)
  • createPattern with images
  • bezierCurveTo and quadraticCurveTo
  • fillText (it creates a texture atlas with all the characters)
  • lineWidth (only one mode supported)
  • imageSmoothingEnabled
  • getImageData and putImageData (not fully tested)
  • shadows (not blurred)
  • createLinearGradient
  • clip

Not supported (yet):

  • globalCompositeOperation
  • concave polygon shapes

Won't be supported:

  • Blurred shadows

It is easy to tweak, all the parameters are publicly available inside the context (matrix, colors, etc). Textures handlers are cached inside the Image object itself, this means that reusing the same images between different Canvas2D context would have a performance penalty.

Extra features

It not only provide the Canvas2D functions, it also comes with some extra ones that you can call directly to speed up some processes (like setting colors, modifying the matrix) or even creating some FX that would be hard in a regular Canvas (applying a shader to an image, colorizing an image).

Demos & Benchmark

Demos are included in demo folder, you can test it here

Usage

Include the library and dependencies

<script src="js/gl-matrix-min.js"></script>
<script src="js/litegl.min.js"></script>
<script src="js/Canvas2DtoWebGL.js"></script>

Once you have your Canvas created (and before a CanvasRenderingContext2D has been obtained), call this function:

var ctx = enableWebGLCanvas( mycanvas );

During your rendering you must call this two functions, it helps set the flags accordingly.

ctx.start2D();

//your rendering code
//...

ctx.finish2D();

Feedback

You can write any feedback to [email protected]

canvas2dtowebgl's People

Contributors

jagenjo avatar mattdoiron 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

canvas2dtowebgl's Issues

[disclaimer] It won't boost your app

If you got here and think it will boost your 2d context it won't. Cloning and changing render_benchmark_fillRect example to iterate over 10000 instead of 1000, 2d context handles better then using webgl. There is no dreaming 2d api to perform like gpu rendering - sad =/

// modified example from this repo
function render_benchmark_fillRect(canvas, ctx) {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for (var i = 0; i < 10000; i++) {
        ctx.fillRect(500, 500, 2, 2);
    }
}

Implementing shadowblur

Hi, awesome project!
are there plans to implement the shadowblur from the canvas2d/is there any issues implementing it?

performance and color?

Hi Javi,

I am trying to use this library in my project for drawing canvas.
I noticed the drawing speed is actually slower than 2d canvas. Am I doing something wrong? or this library is not suitable for my purpose?
Thank you.

not support Chinese!

Wow, great job.
I simply tested some functions and found that the filltext() function does not support Chinese double bytes. I hope it can be repaired as soon as possible. I will use it in my game project. Please!

filltext( ‘你好中文’,50,50,50)

WebGL2 support

Will this ever support webgl2? I'd like to work with some gl.RGBA16UI data.

Drawing between canvases

Thanks for your cool lib!
I tried it for fun as a drop-in replacement for canvas2D just to see if we could get a performance boost on our game.
Everything worked actually really fine out of the box except for when we draw stuff between canvases (since we're using only canvas I've made some functions to apply effects to images, like brighten, darken, dissolve etc, and here we use a buffer canvas where we manipulate the pixels directly). Of course, this could (and should, if we're using webgl) converted to just use fragment shaders instead, but I was wondering if you've tried to draw a canvas on another context with your solution?

What happens is that it basically seems to cache the original image and then all other calls to our per-pixel functions that uses the same canvas only get that same image back, however we manipulate the pixel data.

I could put together a super-basic minimal example of the problem (our game is not super-basic sadly) if this is something you would be interested in.

the benchmark problem

just now, i try it in windows, the all of webgl benchmark is 60fps, but in osx, it really slower than canvas, i read your code, it cache the texture but it call draw method everytime, so i check the inspector result with webgl inspector, i find drawArray too often.

the same time, i test the pixi's bunny benchmark example, i find it call drawElement method once and it has prepare module to prevent upload too many textures in one requestAnimationFrame call.

so, it case the benchmark performance bad?

Resize

Hi what an awesome project!
Maybe you can add resizing support?
I added gl.viewport(0, 0, gl.canvas.width, gl.canvas.height); into start2d function.
Is it good idea?

Does not support fonts with Cyrillic.

Hello! Your library is very convenient and supports many functions, but does not support fonts with Cyrillic or does not have the ability to change the font via ctx.font. Can this be fixed somehow?

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.