Code Monkey home page Code Monkey logo

canvas-cpp's Introduction

Canvas++

An implementation of the JavaScript canvas api, written in C++.

Modern browsers that support HTML5 comes with a handy element called the canvas. It provides a surface that can be drawn upon using JavaScript. Using this canvas all sorts of things can be created.

This library makes it possible to embed such a canvas into another application. This is especially useful for games that can use the canvas to draw it’s UI’s.

Building

OS X is the only supported platform at the moment, and you need to have both cmake and boost installed to be able to build the project.

To build the dependencies Skia and V8 execute the following command:
./deps/fetch_and_build.sh

Create a build folder, this step is optional, but it’s encouraged. It makes cleaning up a lot easier, especially because cmake creates a lot of files all over the place:
mkdir build
cd build

Generate makefiles for the project:
cmake -DCMAKE_BUILD_TYPE=Debug ..

Compile:
make

When this is done, the demo application can be located and started from the build/bin directory.
./build/bin/demo

Example

A simple example that creates a threaded canvas and load a JavaScript file.

Canvas * canvas = new Canvas(800, 600, Canvas::kARGB, true);
canvas->startWithFile("...");
//canvas->startWithCode("..."); We can just as well supply a string containing code.

It is only possible to call either startWithFile("...") or startWithCode("...") once. The second call to one of the functions will throw an assert.

We need to have some target that our canvas should paint to. This could for example be a SDL_Surface.

void * imageTarget = ...;

Then we ask the canvas to paint into our target.

if (canvas->isDirty())
  canvas->paint(imageTarget);

JavaScript API

There are some differences between the HTML5 canvas api and Canvas++. Most notably is the getContext() function. Instead of calling it on the canvas dom element, it’s located on the window object. So we need to call window.getContext('2d').

The same apply to the width and height attributes. Instead of being located on the dom element, they are accessed through the window object, window.width and window.height.

Window Object

Accessed through the global window object.

  • Object getContext(string type) – Returns the graphics context. The only valid context is ‘2d’.
  • int setInterval(function func) – Register a function that will be called when the canvas should repaint itself. Returns an integer identifying the callback function.
  • clearInterval(int id) – Removes the callback function with the corresponding id.
  • int width – The width of the window.
  • int height – The height of the window.

Console Object

Accessed through the global console object.

  • void log(string text) – Output a string to the log system.

Context object

List of all the methods and attributes that are currently implemented.

  • string fillStyle
  • string strokeStyle
  • float lineWidth
  • string lineCap
  • float globalAlpha
  • void scale(float x, float y)
  • void rotate(float angle)
  • void translate(float x, float y)
  • void drawImage(Object image, float x, float y, [Optional] float width, float height)
  • void beginPath()
  • void closePath()
  • void fill()
  • void stroke()
  • void moveTo(float x, float y)
  • void lineTo(float x, float y)
  • void quadraticCurveTo(float cpx, float cpy, float x, float y)
  • void bezierCurveTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y)
  • void arcTo(float x1, float y1, float x2, float y2, float radius)
  • void rect(float x, float y, float width, float height)
  • void fillRect(float x, float y, float width, float height)
  • void strokeRect(float x, float y, float width, float height)
  • void clearRect(float x, float y, float width, float height)
  • void clear() - Non standard function that clears the whole canvas.

License

See the LICENSE file for legal information.

canvas-cpp's People

Contributors

wibbe 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.