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