Git Product home page Git Product logo

canvas-5-polyfill's Introduction

Canvas 5 Polyfill

Canvas 5 Polyfill is a Javascript polyfill library to fill in new features for HTML 5 Canvas that browsers may not have implemented yet, such as Path2D objects and ellipse() on CanvasRenderingContext2D.

Installation

Canvas 5 Polyfill uses Bower to make installation easy:

    bower install --save canvas-5-polyfill

You can also get the code directly at GitHub.

Usage

<html>
<head>
    <title>Test Canvas Polyfill</title>
    <script
      src='bower_components/canvas-5-polyfill/canvas.js'
      type='text/javascript'></script>
</head>
<body>
  <canvas id=ID width=500 height=500>
  <script type='text/javascript' charset='utf-8'>
    var ctx = document.getElementById('ID').getContext('2d');
    var p = new Path2D();
    p.moveTo(100, 10);
    p.lineTo(10, 100);

    ctx.strokeStyle = '#555';
    ctx.lineWidth = 10;
    ctx.stroke(p);
  </script>
</body>
</html>

Status

Canvas 5 Polyfill adds the following classes and methods to an existing HTML Canvas implementation if they are missing, if they are not missing the native implementations are used:

The polyfill adds the following methods to CanvasRenderingContext2D:

void stroke(Path2D path);
void fill(Path2D path, optional CanvasFillRule fillRule = "nonzero");
void clip(Path2D path, optional CanvasFillRule fillRule = "nonzero");
boolean isPointInPath(Path2D path,  double x,
    double y, optional CanvasFillRule fillRule = "nonzero");
boolean isPointInStroke(Path2D path,  double x, double y);
void ellipse( double x, double y, double radiusX, double radiusY,
  double rotation, double startAngle, double endAngle,
  optional boolean anticlockwise = false);

It also adds Path2D with the following constructors:

Path2D()
Path2D(Path2D path, optional CanvasFillRule fillRule = "nonzero"),
Path2D(DOMString d)

Where Path2D has the following methods:

void addPath(Path2D path, SVGMatrix? transformation);
void closePath();
void moveTo(double x,  double y);
void lineTo(double x,  double y);
void quadraticCurveTo( double cpx, double cpy, double x, double y);
void bezierCurveTo( double cp1x, double cp1y, double cp2x, double cp2y,
  double x,  double y);
void arcTo(double x1, double y1, double x2, double y2, double radius);
void arcTo(double x1, double y1, double x2, double y2, double radiusX,
  double radiusY,  double rotation);
void rect(double x, double y, double w, double h);
void arc(double x, double y, double radius, double startAngle,
  double endAngle, optional boolean anticlockwise = false);
void ellipse(double x, double y, double radiusX, double radiusY,
  double rotation, double startAngle, double endAngle,
  optional boolean anticlockwise = false);

Caveats

With this polyfill installed, the calls to context.clip(path), context.isPointInPath(path, x, y) and context.isPointInStroke(path, x, y) all affect the current path.

When using the polyfill the best approach is to move strictly to using Path2D objects to describe paths and then use the path enabled calls on the context, such as ctx.fill(path). Do not mix and match such calls.

canvas-5-polyfill's People

Contributors

cartant avatar e98cuenc avatar jcgregorio avatar karanjitsingh avatar ksm2 avatar mortonfox 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

canvas-5-polyfill's Issues

Reference Error on function clamp()

The function clamp on line 2008 is as:

 function clamp(value, min, max) {
      return Math.min(Math.max(val, min),max);
  }

There is a val coming out of nowhere in the return method.
Just modify it to value and this is it!

I don't have a Git environment available right now else I could have PR this one, :'(.

Br.

Issues with SVG path data that relies on accurate floating points values.

It seems the parser breaks down any float it encounters to integer values.
I don't fully understand how the generated parser code works and I've never used peg before, but I think I found the general direction.

http://jsfiddle.net/z1mxLnp7/

I pasted in a modified version of the polyfill, if you run that fiddle in a browser that has native support for Path2D (I tested chrome) you should see the broken (polyfill) svg to the left and the working (native) one to the right.

Around line 1510 of the generated code I added some console.logs that show that the generated code somehow fails to parse floats to be floats and just truncates them instead, resulting in the distortion.

What's the use of canvasv5.js and svgpath.js

Hello,
I don't understand the use of the files canvasv5.js and svgpath.js ?
It seems that only canvas.js is used when I install the library...

Any help ?
Thanks,
Best regards.

Relative subpath uses the wrong starting position

Given a path like this:
m 0,0 50,0 0,-50 z m 10,10 -50,-20 20,10
the second subpath should start at the location 10,10. Relative to the first position of the previous subpath. The polyfill starts the second subpath at 60,-40. Relative to the last position of the previous path. This breaks svg that rely on such relative positions.

The polyfill needs to track the first position of the current path and use that position to determine the position of a relative next subpath.

Broken example, left is the polyfill, right is the native implementation: http://jsfiddle.net/z1mxLnp7/7/
Hacky fix, implemented without really fully understanding the whole code of the polyfill, so may fail for some datasets I dont use right now: http://jsfiddle.net/z1mxLnp7/6/

After update to 0.1.2 version getting error

I am getting error after updating to newer version,
ERROR in .//canvas-5-polyfill/canvas.js
Module not found: Error: Can't resolve 'canvas' in 'D:\src\node_modules\canvas-5-polyfill'
@ ./
/canvas-5-polyfill/canvas.js 14:29-46

complex paths are not rendered, no way to debug?

I have the following path:

M53.681 45.983a5.33 5.33 0 1 0-5.33-5.33 5.34 5.34 0 0 0 5.33 5.33zm0-7.66a2.33 2.33 0 1 1-2.33 2.33 2.33 2.33 0 0 1 2.33-2.33zm-24.26 19.43a1.5 1.5 0 0 0 1.63-.33l10.83-10.84 9.41 9.41a1.54 1.54 0 0 0 2.12 0l5.32-5.32 7.37 7.37a1.5 1.5 0 0 0 2.56-1.06v-24.3a1.5 1.5 0 0 0-1.5-1.5h-37.16a1.5 1.5 0 0 0-1.5 1.5v23.69a1.5 1.5 0 0 0 .92 1.38zm2.08-23.57h34.16v19.2l-5.08-5.08a2.62 2.62 0 0 0-3.7 0l-4.53 4.53-8.62-8.62a2.68 2.68 0 0 0-3.7 0l-8.53 8.53zm-29 19l1.8 1.6a4.63 4.63 0 0 1 1.19 5.28l-.91 2.23a7.64 7.64 0 0 0 4.92 10.29l2.32.66a4.63 4.63 0 0 1 3.36 4.24l.12 2.41a7.62 7.62 0 0 0 7.62 7.28 7.74 7.74 0 0 0 1.31-.11l2.35-.38a4.69 4.69 0 0 1 4.87 2.36l1.15 2.12a7.63 7.63 0 0 0 11.16 2.58l2-1.4a4.71 4.71 0 0 1 5.41 0l2 1.41a7.63 7.63 0 0 0 11.17-2.52l1.16-2.12a4.65 4.65 0 0 1 4.88-2.33l2.38.42a7.63 7.63 0 0 0 9-7.11l.13-2.41a4.63 4.63 0 0 1 3.38-4.22l2.33-.65a7.64 7.64 0 0 0 5-10.3l-1.02-2.22a4.63 4.63 0 0 1 1.22-5.27l1.78-1.59a7.64 7.64 0 0 0 0-11.45l-1.8-1.6a4.63 4.63 0 0 1-1.19-5.28l.99-2.23a7.64 7.64 0 0 0-4.93-10.29l-2.32-.66a4.63 4.63 0 0 1-3.36-4.24l-.12-2.41a7.62 7.62 0 0 0-7.62-7.28 7.76 7.76 0 0 0-1.31.11l-2.34.38a4.58 4.58 0 0 1-.79.07 4.63 4.63 0 0 1-4.08-2.43l-1.18-2.12a7.64 7.64 0 0 0-11.16-2.58l-2 1.4a4.72 4.72 0 0 1-5.41 0l-2-1.41a7.64 7.64 0 0 0-11.09 2.5l-1.16 2.12a4.68 4.68 0 0 1-4.88 2.34l-2.38-.43a7.72 7.72 0 0 0-1.35-.12 7.63 7.63 0 0 0-7.62 7.23l-.13 2.41a4.63 4.63 0 0 1-3.39 4.22l-2.32.65a7.64 7.64 0 0 0-5 10.3l.94 2.24a4.64 4.640 0 1-1.22 5.27l-1.78 1.59a7.64 7.64 0 0 0 0 11.45zm2-9.2l1.82-1.59a7.63 7.63 0 0 0 2-8.68l-.91-2.23a4.64 4.64 0 0 1 3-6.26l2.36-.64a7.63 7.63 0 0 0 5.58-7l.13-2.41a4.63 4.63 0 0 1 4.62-4.39 4.68 4.68 0 0 1 .83.07l2.38.43a7.72 7.72 0 0 0 1.34.12 7.63 7.63 0 0 0 6.69-4l1.16-2.12a4.64 4.64 0 0 1 4.08-2.33 4.59 4.59 0 0 1 2.71.88l2 1.41a7.76 7.76 0 0 0 8.91 0l2-1.4a4.63 4.63 0 0 1 6.77 1.56l1.07 2.18a7.63 7.63 0 0 0 8 3.89l2.38-.41a4.63 4.63 0 0 1 5.42 4.35l.12 2.41a7.63 7.63 0 0 0 5.54 7l2.32.66a4.64 4.64 0 0 1 3 6.27l-.94 2.22a7.63 7.63 0 0 0 2 8.69l1.8 1.6a4.64 4.64 0 0 1 0 7l-1.81 1.59a7.63 7.63 0 0 0-2 8.68l.93 2.23a4.64 4.64 0 0 1-3 6.26l-2.41.56a7.63 7.63 00 0-5.58 7l-.13 2.41a4.65 4.65 0 0 1-5.45 4.32l-2.38-.42a7.68 7.68 0 0 0-8 3.85l-1.16 2.12a4.64 4.64 0 0 1-4.11 2.35 4.59 4.59 0 0 1-2.71-.88l-2-1.41a7.64 7.64 0 0 0-8.91 0l-2 1.4a4.63 4.63 0 0 1-6.77-1.56l-1.07-2.18a7.64 7.64 0 0 0-8-3.89l-2.38.41a4.64 4.64 0 0 1-5.43-4.35l-.12-2.41a7.63 7.63 0 0 0-5.54-7l-2.32-.66a4.64 4.64 0 0 1-3-6.27l.94-2.22a7.63 7.63 0 0 0-2-8.69l-1.8-1.6a4.64 4.64 0 0 1 0-7zm24.54 26.09h39.08a6.65 6.65 0 0 0 6.64-6.64v-31.7a6.65 6.65 0 0 0-6.64-6.64h-39.08a6.65 6.65 0 0 0-6.64 6.64v31.7a6.65 6.65 0 0 0 6.64 6.64zm-3.64-38.34a3.64 3.64 0 0 1 3.64-3.64h39.08a3.64 3.64 0 0 1 3.64 3.64v31.7a3.64 3.64 0 0 1-3.64 3.64h-39.08a3.64 3.64 0 0 1-3.64-3.64z

and it doesn't seem to work, silently fails, any way for me to debug what is causing this?

Update the canvas.js file from the repository

I found out the canvas.js file relies on old and broken svg parser.
See for example #18.

And still after fixing the reference/syntax errors, the polyfill results is bad (paths are not properly respected, etc.).

When using canvasv5.js + svgpath.js together, this polyfill works as expected.
When coming on this repo it's not clear which file should be used if we do not want to rely on bower to get the file, and even the code snippet tells us to rely on canvas.js.
To avoid frustration just update the canvas.js with the latest svg parser.

Br.

Canvas v5 ellipse broken in Firefox 35.0.1

Using the Canvas v5 polyfill in Firefox 35.0.1, the ellipse method is undefined (both on context and the prototype). This was working in Firefox 30, and the same code works in IE10 on the same machine.

Likely up to Mozilla to fix/allow, but posting this here to help others.

Confirmed on multiple PCs. Have not tried other polyfill methods.

Temporary workaround:

// ctx = CanvasRenderingContext2D
drawEllipse: function(ctx, x, y, radiusX, radiusY, rotation, startAngle, endAngle, antiClockwise) {
    ctx.save();
    ctx.translate(x, y);
    ctx.rotate(rotation);
    ctx.scale(radiusX, radiusY);
    ctx.arc(0, 0, 1, startAngle, endAngle, antiClockwise);
    ctx.restore();
}

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.