Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Use mock canvas object replacing canvas #3518

Merged
merged 4 commits into from
Oct 13, 2017

Conversation

wimrijnders
Copy link
Contributor

Fixes #3515.

A mock canvas object is added to the unit tests, which makes usage of module canvas voluntary.

The issue with canvas is that it requires an external dependency to cairo. This complicates setting up a develop environment for vis.js

  • Removed canvas from package.json
  • Added section to README.md with instructions on how to install canvas instead.

Fixes almende#3515.

A mock canvas object is added to the unit tests, which makes usage of module `canvas` voluntary.

The issue with `canvas` is that it requires an external dependency to `cairo`. This complicates setting up a develop environment for `vis.js`

- Removed `canvas` from `package.json`
- Added section to README.md with instructions on how to install `canvas` instead.
package.json Outdated
@@ -33,30 +33,30 @@
"watch-dev": "gulp watch --bundle"
},
"dependencies": {
"canvas": "^1.6.7",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Canvas" is not needed als dependency because it is only used during development. Should be removed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I must have placed it there again during testing.

README.md Outdated
@@ -322,6 +322,31 @@ module: {

There is also an [demo-project](https://github.com/mojoaxel/vis-webpack-demo) showing the integration of vis.js using webpack.


## Using module `canvas`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this section should be better a sub-part of "Installation".

README.md Outdated
The unit tests use a mock object for canvas, which is severely limited in its functionality. It might be preferable
to use module `canvas` supplies a canvas object for usage with `node.js`. This has a functional implementation of the 2D context.

Reasons to use `canvas:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this "Reasons to use canvas" section at all.

README.md Outdated
The unit tests will only use a mock object for canvas, if there is no `canvas` element already supplied. So, if the `canvas` module is installed, that will take precedence.


The issue with `canvas` is that it has an external dependency to `cairo`. This needs to be installed outside of the regular install as done by `npm`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

README.md Outdated

The issue with `canvas` is that it has an external dependency to `cairo`. This needs to be installed outside of the regular install as done by `npm`.

- In order to fully install `canvas` on Windows, follow [these instructions](https://github.com/Automattic/node-canvas/wiki/Installation---Windows).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just link to the canvas-wiki and remove this whole section.

var {allOptions, configureOptions} = require('./../lib/network/options.js');
//var {printStyle} = require('./../lib/shared/Validator');




Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank lines.

Typically, I use 2 blank lines to separate top level class/function declarations, single blank line to separate import groups. Two blank lines to separate imports from constants. Single blank line between nested functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have my own guidelines which broadly match yours. But this was indeed a good observation.

@@ -353,6 +360,9 @@ describe('Network', function () {


describe('Node', function () {
before(function() {
// if (!this.canvasPresent) this.skip();
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These before functions should all be removed, imo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they should. Artifacts of a previous attempt at a fix.

* Adapted from: https://github.com/Cristy94/canvas-mock
*/

var myHackedInCanvas; // Use one global canvas instance for all calls to createElement('canvas');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why global?
If that's a good reason, can we call this globalCanvasMock?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, the comment might be a bit off. It's strictly speaking not a global because it doesn't reside in the global namespace. It's a static within the module. I'll adjust the comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the name is silly; changing it to canvasMock.

// Following added for vis.js unit tests
//

measureText: function(text) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing looks off

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this code is a module which I took from here and adapted it to own needs when I realized it wasn't completely fit for the purpose.

If think you mean this spacing?

      measureText: function(text) {
        return {
          width:>>  <<12*text.length,
          height: 14
        };
      },
};

Otherwise, I don't know what you mean. I have a habit of aligning related stuff, but indeed it's not needed here with only two lines.


Ah wait, there's tabs there. That's probably what you mean. Ah, right, that's from the original. I'll change it to spaces.

Copy link

@mbroad mbroad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@wimrijnders
Copy link
Contributor Author

@mbroad Thanks. It's admittedly a hack, but it only runs within the unit tests so I think we're OK.

@yotamberk
Copy link
Contributor

@mojoaxel can you please review again/accept changes so we can get this in?

@mbroad
Copy link

mbroad commented Oct 8, 2017

@wimrijnders - Seems like an appropriate use of a mock to me.

@wimrijnders
Copy link
Contributor Author

@mojoaxel I've handled your review points. Can you please have a look again?

@mojoaxel mojoaxel added this to the Patch Release v4.21.1 milestone Oct 13, 2017
@mojoaxel
Copy link
Member

We should release this quickly as a Patch-Release.

@yotamberk
Copy link
Contributor

@mojoaxel Another release will occur in exactly 2 weeks

@yotamberk yotamberk merged commit 06b2623 into almende:develop Oct 13, 2017
@wimrijnders wimrijnders deleted the mockCanvas branch October 14, 2017 02:50
mojoaxel pushed a commit to visjs/vis_original that referenced this pull request Jun 9, 2019
* Use mock canvas object replacing `canvas`

Fixes almende#3515.

A mock canvas object is added to the unit tests, which makes usage of module `canvas` voluntary.

The issue with `canvas` is that it requires an external dependency to `cairo`. This complicates setting up a develop environment for `vis.js`

- Removed `canvas` from `package.json`
- Added section to README.md with instructions on how to install `canvas` instead.

* Removed debugger statements

* Updates for review

* Fixes for review
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants