Skip to content

Commit 7b5aa44

Browse files
committed
Updated Headless Loading
1 parent f5e83d3 commit 7b5aa44

8 files changed

Lines changed: 98 additions & 57 deletions

File tree

README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,49 +63,54 @@ And the resulting `/build/two.js` and `/build/two.min.js` will be updated to you
6363

6464
### Running in Headless Environments
6565

66-
As of version `v0.7.x` Two.js can also run in a headless environment, namely running on the server with the help of a library called [Node Canvas](https://github.com/Automattic/node-canvas). We don't add Node Canvas to dependencies of Two.js because it's _not necessary_ to run it in the browser. However, it has all the hooks setup to run in a cloud environment. To get started follow the installation instructions on Automattic's readme. After you've done that run:
66+
As of version `v0.7.x` Two.js can also run in a headless environment, namely running on the server with the help of a library called [Node Canvas](https://github.com/Automattic/node-canvas). We don't add Node Canvas to dependencies of Two.js because it's _not necessary_ to run it in the browser. However, it has all the hooks setup to run in a cloud environment. To get started follow the installation instructions on Automattic's [readme](https://github.com/Automattic/node-canvas#installation). After you've done that run:
6767

6868
```
6969
npm install canvas
70+
npm intsall two.js
7071
```
7172

7273
Now in a JavaScript file setup your Two.js scenegraph and save out frames whenever you need to:
7374

7475
```javascript
75-
var Two = require('../build/two.js'); // Or from npm, `require('two.js');`
76-
var Canvas = require('canvas');
77-
var Image = Canvas.Image;
76+
var { createCanvas, Image } = require('canvas');
77+
var Two = require('two.js')
78+
7879
var fs = require('fs');
7980
var path = require('path');
8081

8182
var width = 800;
8283
var height = 600;
8384

84-
var canvas = Two.Utils.shim(Canvas);
85+
var canvas = createCanvas(width, height);
86+
Two.Utils.shim(canvas, Image);
8587

8688
var time = Date.now();
8789

8890
var two = new Two({
89-
type: Two.Types.canvas,
90-
width: 800,
91-
height: 600,
91+
width: width,
92+
height: height,
9293
domElement: canvas
9394
});
9495

9596
var rect = two.makeRectangle(width / 2, height / 2, 50, 50);
9697
rect.fill = 'rgb(255, 100, 100)';
9798
rect.noStroke();
9899

99-
two.update();
100+
two.render();
100101

101-
fs.writeFileSync(path.resolve(__dirname, './images/rectangle.png'), canvas.toBuffer());
102+
var settings = { compressionLevel: 3, filters: canvas.PNG_FILTER_NONE };
103+
fs.writeFileSync(path.resolve(__dirname, './images/rectangle.png'), canvas.toBuffer('image/png', settings));
102104
console.log('Finished rendering. Time took: ', Date.now() - time);
103105

104106
process.exit();
107+
105108
```
106109

107110
## Change Log
108111
<!-- For the latest nightly changes checkout the `dev` branch [here](../../tree/dev). -->
112+
### November 18, 2018 [v0.7.0-beta.2](https://github.com/jonobr1/two.js/releases/tag/v0.7.0-beta.2)
113+
+ Updated Two.js compatibility with webpack and node-canvas 2.0.0+
109114

110115
#### November 3, 2018 [v0.7.0-beta.1](https://github.com/jonobr1/two.js/releases/tag/v0.7.0-beta.1)
111116
+ Altered `Two.Path.clone` and `Two.Text.clone` to use references where possible and to `_update()` on return

build/two.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -456,13 +456,13 @@ SOFTWARE.
456456
* @name Two.Version
457457
* @property {String} - The current working version of the library.
458458
*/
459-
Version: 'v0.7.0-beta.1',
459+
Version: 'v0.7.0-beta.2',
460460

461461
/**
462462
* @name Two.PublishDate
463463
* @property {String} - The automatically generated publish date in the build process to verify version release candidates.
464464
*/
465-
PublishDate: '2018-11-03T10:28:50+01:00',
465+
PublishDate: '2018-11-18T10:50:17+01:00',
466466

467467
/**
468468
* @name Two.Identifier
@@ -580,14 +580,16 @@ SOFTWARE.
580580
/**
581581
* @name Two.Utils.shim
582582
* @function
583-
* @param {Canvas} CanvasModule - The `Canvas` object provided by `node-canvas`.
584-
* @returns {Canvas} Returns an instanced canvas object from the provided `node-canvas` `Canvas` object.
583+
* @param {canvas} canvas - The instanced `Canvas` object provided by `node-canvas`.
584+
* @param {Image} [Image] - The prototypical `Image` object provided by `node-canvas`. This is only necessary to pass if you're going to load bitmap imagery.
585+
* @returns {canvas} Returns the instanced canvas object you passed from with additional attributes needed for Two.js.
585586
* @description Convenience method for defining all the dependencies from the npm package `node-canvas`. See [node-canvas]{@link https://github.com/Automattic/node-canvas} for additional information on setting up HTML5 `<canvas />` drawing in a node.js environment.
586587
*/
587-
shim: function(CanvasModule) {
588-
var canvas = new CanvasModule();
588+
shim: function(canvas, Image) {
589589
Two.CanvasRenderer.Utils.shim(canvas);
590-
Two.Utils.Image = CanvasModule.Image;
590+
if (!_.isUndefined(Image)) {
591+
Two.Utils.Image = Image;
592+
}
591593
Two.Utils.isHeadless = true;
592594
return canvas;
593595
},
@@ -11552,6 +11554,14 @@ SOFTWARE.
1155211554
return anchor.href;
1155311555
},
1155411556

11557+
loadHeadlessBuffer: new Function('texture', 'loaded', [
11558+
'var fs = require("fs");',
11559+
'var buffer = fs.readFileSync(texture.src);',
11560+
11561+
'texture.image.src = buffer;',
11562+
'loaded();'
11563+
].join('\n')),
11564+
1155511565
getImage: function(src) {
1155611566

1155711567
var absoluteSrc = Texture.getAbsoluteURL(src);
@@ -11634,11 +11644,7 @@ SOFTWARE.
1163411644

1163511645
if (Two.Utils.isHeadless) {
1163611646

11637-
var fs = require('fs');
11638-
var buffer = fs.readFileSync(texture.src);
11639-
11640-
texture.image.src = buffer;
11641-
loaded();
11647+
Texture.loadHeadlessBuffer(texture, loaded);
1164211648

1164311649
} else {
1164411650

0 commit comments

Comments
 (0)