mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Switch from options.browser and options.node to options.environment
This commit is contained in:
parent
6b45a9382c
commit
c68a7d33da
10 changed files with 42 additions and 39 deletions
|
@ -30,9 +30,9 @@ then
|
|||
mkdir ../dist/
|
||||
fi
|
||||
|
||||
./preprocess.sh $MODE ../src/paper.js "-o '{ \"browser\": true }' -i '../src/constants.js'" ../dist/paper-full.js
|
||||
./preprocess.sh $MODE ../src/paper.js "-o '{ \"browser\": true, \"paperscript\": false }' -i '../src/constants.js'" ../dist/paper-core.js
|
||||
./preprocess.sh $MODE ../src/paper.js "-o '{ \"browser\": false, \"node\": true }' -i '../src/constants.js'" ../dist/paper-node.js
|
||||
./preprocess.sh $MODE ../src/paper.js "-i '../src/constants.js'" ../dist/paper-full.js
|
||||
./preprocess.sh $MODE ../src/paper.js "-o '{ \"paperscript\": false }' -i '../src/constants.js'" ../dist/paper-core.js
|
||||
./preprocess.sh $MODE ../src/paper.js "-o '{ \"environment\": \"node\" }' -i '../src/constants.js'" ../dist/paper-node.js
|
||||
|
||||
# Remove the existing file and copy paper-full.js to paper.js now
|
||||
if [ -f ../dist/paper.js ]
|
||||
|
|
|
@ -22,12 +22,12 @@ var CanvasProvider = {
|
|||
if (this.canvases.length) {
|
||||
canvas = this.canvases.pop();
|
||||
} else {
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
canvas = document.createElement('canvas');
|
||||
/*#*/ } else { // !options.browser
|
||||
/*#*/ } else { // !options.environment == 'browser'
|
||||
canvas = new Canvas(size.width, size.height);
|
||||
init = false; // It's already initialized through constructor.
|
||||
/*#*/ } // !options.browser
|
||||
/*#*/ } // !options.environment == 'browser'
|
||||
|
||||
}
|
||||
var ctx = canvas.getContext('2d');
|
||||
|
|
|
@ -288,7 +288,7 @@ paper.PaperScope.prototype.PaperScript = (function(root) {
|
|||
return res;
|
||||
}
|
||||
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
// Code borrowed from Coffee Script:
|
||||
function request(url, scope) {
|
||||
var xhr = new (window.ActiveXObject || XMLHttpRequest)(
|
||||
|
@ -354,8 +354,8 @@ paper.PaperScope.prototype.PaperScript = (function(root) {
|
|||
load: load
|
||||
};
|
||||
|
||||
/*#*/ } else { // !options.browser
|
||||
/*#*/ if (options.node) {
|
||||
/*#*/ } else { // !options.environment == 'browser'
|
||||
/*#*/ if (options.environment == 'node') {
|
||||
|
||||
// Register the .pjs extension for automatic compilation as PaperScript
|
||||
|
||||
|
@ -374,12 +374,12 @@ paper.PaperScope.prototype.PaperScript = (function(root) {
|
|||
module.exports = scope;
|
||||
};
|
||||
|
||||
/*#*/ } // options.node
|
||||
/*#*/ } // options.environment == 'node'
|
||||
|
||||
return {
|
||||
compile: compile,
|
||||
evaluate: evaluate
|
||||
};
|
||||
|
||||
/*#*/ } // !options.browser
|
||||
/*#*/ } // !options.environment == 'browser'
|
||||
})(this);
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
// First add Base and a couple of other objects that are not automatically
|
||||
// exported to exports (Numerical, Key, etc), then inject all exports into
|
||||
// PaperScope, and create the initial paper object, all in one statement:
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
|
||||
paper = new (PaperScope.inject(Base.merge(Base.exports, {
|
||||
// Mark fields as enumeralbe so PaperScope.inject can pick them up
|
||||
|
@ -32,7 +32,7 @@ paper = new (PaperScope.inject(Base.merge(Base.exports, {
|
|||
if (typeof define === 'function' && define.amd)
|
||||
define('paper', paper);
|
||||
|
||||
/*#*/ } else if (options.node) {
|
||||
/*#*/ } else if (options.environment == 'node') {
|
||||
|
||||
paper = new (PaperScope.inject(Base.merge(Base.exports, {
|
||||
// Mark fields as enumeralbe so PaperScope.inject can pick them up
|
||||
|
@ -49,4 +49,4 @@ paper = new (PaperScope.inject(Base.merge(Base.exports, {
|
|||
// Export the paper scope.
|
||||
module.exports = paper;
|
||||
|
||||
/*#*/ } // options.node
|
||||
/*#*/ } // options.environment == 'node'
|
||||
|
|
|
@ -287,7 +287,7 @@ var Raster = Item.extend(/** @lends Raster# */{
|
|||
},
|
||||
|
||||
setSource: function(src) {
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
var that = this,
|
||||
// src can be an URL or a DOM ID to load the image from
|
||||
image = document.getElementById(src) || new Image();
|
||||
|
@ -317,7 +317,7 @@ var Raster = Item.extend(/** @lends Raster# */{
|
|||
image.src = src;
|
||||
}
|
||||
this.setImage(image);
|
||||
/*#*/ } else if (options.node) {
|
||||
/*#*/ } else if (options.environment == 'node') {
|
||||
var image = new Image();
|
||||
// If we're running on the server and it's a string,
|
||||
// check if it is a data URL
|
||||
|
@ -331,7 +331,7 @@ var Raster = Item.extend(/** @lends Raster# */{
|
|||
image.src = fs.readFileSync(src);
|
||||
}
|
||||
this.setImage(image);
|
||||
/*#*/ } // options.node
|
||||
/*#*/ } // options.environment == 'node'
|
||||
},
|
||||
|
||||
// DOCS: document Raster#getElement
|
||||
|
@ -385,7 +385,7 @@ var Raster = Item.extend(/** @lends Raster# */{
|
|||
toDataURL: function() {
|
||||
// See if the linked image is base64 encoded already, if so reuse it,
|
||||
// otherwise try using canvas.toDataURL()
|
||||
/*#*/ if (options.node) {
|
||||
/*#*/ if (options.environment == 'node') {
|
||||
if (this._data)
|
||||
return this._data;
|
||||
/*#*/ } else {
|
||||
|
|
|
@ -42,8 +42,7 @@ if (typeof window === 'object') {
|
|||
prepro.include('../src/options.js');
|
||||
// Override node specific options.
|
||||
prepro.setOptions({
|
||||
browser: false,
|
||||
node: true,
|
||||
environment: 'node',
|
||||
stats: false
|
||||
});
|
||||
// Load Paper.js library files.
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
var options = {
|
||||
parser: 'acorn',
|
||||
version: 'dev',
|
||||
browser: true,
|
||||
environment: 'browser',
|
||||
stats: true,
|
||||
svg: true,
|
||||
fatline: true,
|
||||
|
|
14
src/paper.js
14
src/paper.js
|
@ -93,19 +93,19 @@ var paper = new function(undefined) {
|
|||
/*#*/ include('style/GradientStop.js');
|
||||
/*#*/ include('style/Style.js');
|
||||
|
||||
/*#*/ if (options.node) {
|
||||
/*#*/ if (options.environment == 'node') {
|
||||
/*#*/ include('dom/node.js');
|
||||
/*#*/ } // options.node
|
||||
/*#*/ } // options.environment == 'node'
|
||||
/*#*/ include('dom/DomElement.js');
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
// DomEvent doesn't make sense outside of the browser (yet)
|
||||
/*#*/ include('dom/DomEvent.js');
|
||||
/*#*/ } // options.browser
|
||||
/*#*/ } // options.environment == 'browser'
|
||||
|
||||
/*#*/ include('ui/View.js');
|
||||
/*#*/ include('ui/CanvasView.js');
|
||||
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
/*#*/ include('ui/Event.js');
|
||||
/*#*/ include('ui/KeyEvent.js');
|
||||
/*#*/ include('ui/Key.js');
|
||||
|
@ -116,13 +116,13 @@ var paper = new function(undefined) {
|
|||
|
||||
/*#*/ include('tool/ToolEvent.js');
|
||||
/*#*/ include('tool/Tool.js');
|
||||
/*#*/ } // options.browser
|
||||
/*#*/ } // options.environment == 'browser'
|
||||
|
||||
/*#*/ include('canvas/CanvasProvider.js');
|
||||
/*#*/ include('canvas/BlendMode.js');
|
||||
/*#*/ if (options.version == 'dev') {
|
||||
/*#*/ include('canvas/ProxyContext.js');
|
||||
/*#*/ } // options.browser
|
||||
/*#*/ } // options.environment == 'browser'
|
||||
|
||||
/*#*/ if (options.svg) {
|
||||
/*#*/ include('svg/SVGStyles.js');
|
||||
|
|
|
@ -188,7 +188,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
|||
};
|
||||
});
|
||||
|
||||
/*#*/ if (options.node) {
|
||||
/*#*/ if (options.environment == 'node') {
|
||||
// Node.js based image exporting code.
|
||||
CanvasView.inject(new function() {
|
||||
// Utility function that converts a number to a string with
|
||||
|
@ -273,4 +273,4 @@ CanvasView.inject(new function() {
|
|||
}
|
||||
};
|
||||
});
|
||||
/*#*/ } // options.node
|
||||
/*#*/ } // options.environment == 'node'
|
||||
|
|
|
@ -29,7 +29,7 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
this._project = paper.project;
|
||||
this._element = element;
|
||||
var size;
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
// Generate an id for this view / element if it does not have one
|
||||
this._id = element.getAttribute('id');
|
||||
if (this._id == null)
|
||||
|
@ -86,11 +86,11 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
style.top = offset.y + 'px';
|
||||
document.body.appendChild(stats);
|
||||
}
|
||||
/*#*/ } else if (options.node) {
|
||||
/*#*/ } else if (options.environment == 'node') {
|
||||
// Generate an id for this view
|
||||
this._id = 'view-' + View._id++;
|
||||
size = new Size(element.width, element.height);
|
||||
/*#*/ } // options.node
|
||||
/*#*/ } // options.environment == 'node'
|
||||
// Keep track of views internally
|
||||
View._views.push(this);
|
||||
// Link this id to our view
|
||||
|
@ -122,9 +122,11 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
// Unlink from project
|
||||
if (this._project.view == this)
|
||||
this._project.view = null;
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
// Uninstall event handlers again for this view.
|
||||
DomEvent.remove(this._element, this._viewHandlers);
|
||||
DomEvent.remove(window, this._windowHandlers);
|
||||
/*#*/ } // options.environment == 'browser'
|
||||
this._element = this._project = null;
|
||||
// Removing all onFrame handlers makes the onFrame handler stop
|
||||
// automatically through its uninstall method.
|
||||
|
@ -144,14 +146,14 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
*/
|
||||
onFrame: {
|
||||
install: function() {
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
// Request a frame handler straight away to initialize the
|
||||
// sequence of onFrame calls.
|
||||
if (!this._requested) {
|
||||
this._animate = true;
|
||||
this._requestFrame();
|
||||
}
|
||||
/*#*/ } // options.browser
|
||||
/*#*/ } // options.environment == 'browser'
|
||||
},
|
||||
|
||||
uninstall: function() {
|
||||
|
@ -170,6 +172,7 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
_count: 0,
|
||||
|
||||
_requestFrame: function() {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
var that = this;
|
||||
DomEvent.requestAnimationFrame(function() {
|
||||
that._requested = false;
|
||||
|
@ -181,6 +184,7 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
that._handleFrame();
|
||||
}, this._element);
|
||||
this._requested = true;
|
||||
/*#*/ } // options.environment == 'browser'
|
||||
},
|
||||
|
||||
_handleFrame: function() {
|
||||
|
@ -561,10 +565,10 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
_id: 0,
|
||||
|
||||
create: function(element) {
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
if (typeof element === 'string')
|
||||
element = document.getElementById(element);
|
||||
/*#*/ } // options.browser
|
||||
/*#*/ } // options.environment == 'browser'
|
||||
// Factory to provide the right View subclass for a given element.
|
||||
// Produces only CanvasViews for now:
|
||||
return new CanvasView(element);
|
||||
|
@ -572,7 +576,7 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
}
|
||||
}, new function() {
|
||||
// Injection scope for mouse events on the browser
|
||||
/*#*/ if (options.browser) {
|
||||
/*#*/ if (options.environment == 'browser') {
|
||||
var tool,
|
||||
prevFocus,
|
||||
tempFocus,
|
||||
|
@ -705,5 +709,5 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
updateFocus: updateFocus
|
||||
}
|
||||
};
|
||||
/*#*/ } // options.browser
|
||||
/*#*/ } // options.environment == 'browser'
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue