forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlacedSymbol.js
More file actions
137 lines (126 loc) · 3.73 KB
/
Copy pathPlacedSymbol.js
File metadata and controls
137 lines (126 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name PlacedSymbol
*
* @class A PlacedSymbol represents an instance of a symbol which has been
* placed in a Paper.js project.
*
* @extends Item
*/
var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
_class: 'PlacedSymbol',
_transformContent: false,
// PlacedSymbol uses strokeBounds for bounds
_boundsGetter: { getBounds: 'getStrokeBounds' },
_boundsSelected: true,
_serializeFields: {
symbol: null
},
/**
* Creates a new PlacedSymbol Item.
*
* @param {Symbol} symbol the symbol to place
* @param {Point} [point] the center point of the placed symbol
*
* @example {@paperscript split=true height=240}
* // Placing 100 instances of a symbol:
* // Create a star shaped path at {x: 0, y: 0}:
* var path = new Path.Star({
* center: new Point(0, 0),
* points: 6,
* radius1: 5,
* radius2: 13,
* fillColor: 'white',
* strokeColor: 'black'
* });
*
* // Create a symbol from the path:
* var symbol = new Symbol(path);
*
* // Remove the path:
* path.remove();
*
* // Place 100 instances of the symbol:
* for (var i = 0; i < 100; i++) {
* // Place an instance of the symbol in the project:
* var instance = new PlacedSymbol(symbol);
*
* // Move the instance to a random position within the view:
* instance.position = Point.random() * view.size;
*
* // Rotate the instance by a random amount between
* // 0 and 360 degrees:
* instance.rotate(Math.random() * 360);
*
* // Scale the instance between 0.25 and 1:
* instance.scale(0.25 + Math.random() * 0.75);
* }
*/
initialize: function PlacedSymbol(arg0, arg1) {
// Support two forms of item initialization: Passing one object literal
// describing all the different properties to be set, or a symbol (arg0)
// and a point where it should be placed (arg1).
// If _initialize can set properties through object literal, we're done.
// Otherwise we need to set symbol from arg0.
if (!this._initialize(arg0,
arg1 !== undefined && Point.read(arguments, 1)))
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
},
_equals: function(item) {
return this._symbol === item._symbol;
},
/**
* The symbol that the placed symbol refers to.
*
* @type Symbol
* @bean
*/
getSymbol: function() {
return this._symbol;
},
setSymbol: function(symbol) {
// Remove from previous symbol's instances
if (this._symbol)
delete this._symbol._instances[this._id];
this._symbol = symbol;
// Add to the new one's
symbol._instances[this._id] = this;
},
clone: function(insert) {
return this._clone(new PlacedSymbol({
symbol: this.symbol,
insert: false
}), insert);
},
isEmpty: function() {
return this._symbol._definition.isEmpty();
},
_getBounds: function(getter, matrix) {
// Redirect the call to the symbol definition to calculate the bounds
// TODO: Implement bounds caching through passing on of cacheItem, so
// that Symbol#_changed() notification become unnecessary!
return this.symbol._definition._getCachedBounds(getter, matrix);
},
_hitTest: function(point, options, matrix) {
var result = this._symbol._definition._hitTest(point, options, matrix);
// TODO: When the symbol's definition is a path, should hitResult
// contain information like HitResult#curve?
if (result)
result.item = this;
return result;
},
_draw: function(ctx, param) {
this.symbol._definition.draw(ctx, param);
}
// TODO: PlacedSymbol#embed()
});