GithubHelp home page GithubHelp logo

ivanpopelyshev / canvas2dtowebgl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jagenjo/canvas2dtowebgl

0.0 1.0 0.0 316 KB

Ports (almost) all Canvas2D functions to the GPU so it can be mixed with a WebGL canvas.

License: MIT License

JavaScript 98.74% CSS 0.05% HTML 1.21%

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

Watchers

 avatar

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.