Simplify the way the paper namespace is created and populated.

This commit is contained in:
Jürg Lehni 2011-03-04 13:34:31 +00:00
parent c6d79f964c
commit 92bf51a7cc
29 changed files with 38 additions and 50 deletions

View file

@ -3,7 +3,7 @@
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
var Matrix = Base.extend({ var Matrix = this.Matrix = Base.extend({
beans: true, beans: true,
/** /**

View file

@ -3,7 +3,7 @@
* Paper.js document. It is also used to represent two dimensional vector * Paper.js document. It is also used to represent two dimensional vector
* objects. * objects.
*/ */
var Point = Base.extend({ var Point = this.Point = Base.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {

View file

@ -1,4 +1,4 @@
var Rectangle = Base.extend({ var Rectangle = this.Rectangle = Base.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {

View file

@ -1,4 +1,4 @@
var Size = Base.extend({ var Size = this.Size = Base.extend({
initialize: function() { initialize: function() {
if (arguments.length == 2) { if (arguments.length == 2) {
this.width = arguments[0]; this.width = arguments[0];

View file

@ -40,14 +40,4 @@ var paper = new function() {
//#include "util/MathUtils.js" //#include "util/MathUtils.js"
//#include "util/PaperScript.js" //#include "util/PaperScript.js"
// Inject all prototypes from the paper scope into the paper object.
return Base.each(['Point', 'Size', 'Rectangle', 'Matrix', 'DocumentView',
'Document', 'Symbol', 'Item', 'Group', 'Layer', 'Raster', 'PlacedSymbol',
'PathStyle', 'Segment', 'Curve', 'PathItem', 'Path', 'CompoundPath',
'Color', 'RGBColor', 'GrayColor', 'GradientColor', 'Gradient',
'GradientStop', 'ToolEvent', 'ToolHandler', 'Tool'],
function(name) {
this[name] = eval(name);
}, paper);
}; };

View file

@ -1,4 +1,4 @@
var Color = Base.extend({ var Color = this.Color = Base.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {

View file

@ -1,4 +1,4 @@
var Gradient = Base.extend({ var Gradient = this.Gradient = Base.extend({
initialize: function() { initialize: function() {
this.stops = [ this.stops = [
new GradientStop('white', 0), new GradientStop('white', 0),

View file

@ -1,4 +1,4 @@
var GradientColor = Color.extend({ var GradientColor = this.GradientColor = Color.extend({
beans: true, beans: true,
initialize: function(gradient, origin, destination, hilite) { initialize: function(gradient, origin, destination, hilite) {

View file

@ -1,4 +1,4 @@
var GradientStop = Base.extend({ var GradientStop = this.GradientStop = Base.extend({
beans: true, beans: true,
// TODO: support midPoint? (initial tests didn't look nice) // TODO: support midPoint? (initial tests didn't look nice)

View file

@ -1,4 +1,4 @@
var GrayColor = Color.extend({ var GrayColor = this.GrayColor = Color.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {

View file

@ -1,4 +1,4 @@
var RGBColor = Color.extend(new function() { var RGBColor = this.RGBColor = Color.extend(new function() {
// TODO: convert hex codes to [r,g,b]? // TODO: convert hex codes to [r,g,b]?
var namedColors = { var namedColors = {
lightpink: 'ffb6c1', pink: 'ffc0cb', crimson: 'dc143c', lightpink: 'ffb6c1', pink: 'ffc0cb', crimson: 'dc143c',

View file

@ -1,4 +1,4 @@
var Document = Base.extend({ var Document = this.Document = Base.extend({
beans: true, beans: true,
initialize: function(canvas) { initialize: function(canvas) {

View file

@ -1,4 +1,4 @@
var DocumentView = Base.extend({ var DocumentView = this.DocumentView = Base.extend({
beans: true, beans: true,
initialize: function(document) { initialize: function(document) {

View file

@ -1,4 +1,4 @@
var Symbol = Base.extend({ var Symbol = this.Symbol = Base.extend({
beans: true, beans: true,
initialize: function(item) { initialize: function(item) {

View file

@ -1,4 +1,4 @@
var Group = Item.extend({ var Group = this.Group = Item.extend({
beans: true, beans: true,
initialize: function(items) { initialize: function(items) {
this.base(); this.base();

View file

@ -1,4 +1,4 @@
var Item = Base.extend({ var Item = this.Item = Base.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {

View file

@ -1,4 +1,4 @@
var Layer = Group.extend({ var Layer = this.Layer = Group.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {

View file

@ -1,4 +1,4 @@
var PathStyle = Base.extend(new function() { var PathStyle = this.PathStyle = Base.extend(new function() {
var keys = ['windingRule', 'resolution', 'strokeColor', 'strokeWidth', var keys = ['windingRule', 'resolution', 'strokeColor', 'strokeWidth',
'strokeCap', 'strokeJoin', 'dashOffset','dashArray', 'miterLimit', 'strokeCap', 'strokeJoin', 'dashOffset','dashArray', 'miterLimit',
'strokeOverprint', 'fillColor', 'fillOverprint']; 'strokeOverprint', 'fillColor', 'fillOverprint'];

View file

@ -1,4 +1,4 @@
var PlacedSymbol = Item.extend({ var PlacedSymbol = this.PlacedSymbol = Item.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {

View file

@ -1,4 +1,4 @@
var Raster = Item.extend({ var Raster = this.Raster = Item.extend({
beans: true, beans: true,
// TODO: implement url / type, width, height // TODO: implement url / type, width, height

View file

@ -1,10 +1,8 @@
var paper = new Base().inject({ this.document = null;
document: null, this.documents = [];
documents: [],
install: function(scope) { this.install = function(scope) {
for (var i in paper) { for (var i in this) {
scope[i] = paper[i]; scope[i] = this[i];
} }
} };
});

View file

@ -1,4 +1,4 @@
var CompoundPath = PathItem.extend({ var CompoundPath = this.CompoundPath = PathItem.extend({
initialize: function(items) { initialize: function(items) {
this.base(); this.base();
this.children = []; this.children = [];

View file

@ -1,4 +1,4 @@
var Curve = Base.extend({ var Curve = this.Curve = Base.extend({
initialize: function() { initialize: function() {
} }
}); });

View file

@ -1,4 +1,4 @@
var Path = PathItem.extend({ var Path = this.Path = PathItem.extend({
beans: true, beans: true,
initialize: function(/* segments */) { initialize: function(/* segments */) {
@ -12,8 +12,10 @@ var Path = PathItem.extend({
if (!segments || !Array.isArray(segments) if (!segments || !Array.isArray(segments)
|| typeof segments[0] != 'object') || typeof segments[0] != 'object')
segments = arguments; segments = arguments;
for (var i = 0, l = segments.length; i < l; i++) for (var i = 0, l = segments.length; i < l; i++) {
this._add(Segment.read(segments, i, 1)); var seg = Segment.read(segments, i, 1);
this._add(seg);
}
}, },
/** /**

View file

@ -1,4 +1,4 @@
var PathItem = Item.extend({ var PathItem = this.PathItem = Item.extend({
beans: true, beans: true,
initialize: function() { initialize: function() {

View file

@ -1,4 +1,4 @@
var Segment = Base.extend({ var Segment = this.Segment = Base.extend({
initialize: function() { initialize: function() {
if (arguments.length == 0) { if (arguments.length == 0) {
this.point = new Point(); this.point = new Point();

View file

@ -1,4 +1,4 @@
var Tool = ToolHandler.extend(new function() { var Tool = this.Tool = ToolHandler.extend(new function() {
function viewToArtwork(event, document) { function viewToArtwork(event, document) {
var point = Point.create(event.offset.x, event.offset.y); var point = Point.create(event.offset.x, event.offset.y);
// TODO: always the active view? // TODO: always the active view?

View file

@ -1,4 +1,4 @@
var ToolHandler = Base.extend({ var ToolHandler = this.ToolHandler = Base.extend({
beans: true, beans: true,
/** /**
@ -48,7 +48,7 @@ var ToolHandler = Base.extend({
getFixedDistance: function() { getFixedDistance: function() {
if (this._minDistance != null if (this._minDistance != null
&& this._minDistance == this._maxDistance) && this._minDistance.equals(this._maxDistance))
return this._minDistance; return this._minDistance;
return null; return null;
}, },

View file

@ -114,9 +114,7 @@ var PaperScript = new function() {
} }
function run(code) { function run(code) {
// Use paper.extend() to create a paper scope within which the code is with (paper) {
// evaluated.
with (paper.extend()) {
var tool = /onMouse(?:Up|Down|Move|Drag)/.test(code) && new Tool(); var tool = /onMouse(?:Up|Down|Move|Drag)/.test(code) && new Tool();
var res = eval(compile(code)); var res = eval(compile(code));
if (tool) { if (tool) {