2011-03-06 19:50:44 -05:00
|
|
|
/*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-01-28 21:03:27 -05:00
|
|
|
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* @name PlacedSymbol
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @class A PlacedSymbol represents an instance of a symbol which has been
|
|
|
|
* placed in a Paper.js project.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2013-04-19 19:40:30 -04:00
|
|
|
* @extends Item
|
2011-06-22 18:56:05 -04:00
|
|
|
*/
|
2013-05-27 15:48:58 -04:00
|
|
|
var PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
|
2013-06-23 23:18:32 -04:00
|
|
|
_class: 'PlacedSymbol',
|
2013-06-18 18:50:11 -04:00
|
|
|
_transformContent: false,
|
2013-04-19 19:40:30 -04:00
|
|
|
// PlacedSymbol uses strokeBounds for bounds
|
|
|
|
_boundsGetter: { getBounds: 'getStrokeBounds' },
|
2013-02-24 18:53:37 -05:00
|
|
|
_boundsSelected: true,
|
2013-02-11 21:59:49 -05:00
|
|
|
_serializeFields: {
|
|
|
|
symbol: null
|
|
|
|
},
|
2013-03-01 13:08:17 -05:00
|
|
|
|
2011-05-23 13:48:03 -04:00
|
|
|
/**
|
|
|
|
* Creates a new PlacedSymbol Item.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-30 13:42:17 -04:00
|
|
|
* @param {Symbol} symbol the symbol to place
|
2012-12-25 11:57:04 -05:00
|
|
|
* @param {Point} [point] the center point of the placed symbol
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-03 16:25:37 -04:00
|
|
|
* @example {@paperscript split=true height=240}
|
2011-05-30 13:42:17 -04:00
|
|
|
* // Placing 100 instances of a symbol:
|
2013-03-03 14:10:25 -05:00
|
|
|
* // Create a star shaped path at {x: 0, y: 0}:
|
|
|
|
* var path = new Path.Star({
|
|
|
|
* center: new Point(0, 0),
|
2013-03-10 14:06:37 -04:00
|
|
|
* points: 6,
|
2013-03-03 14:10:25 -05:00
|
|
|
* radius1: 5,
|
|
|
|
* radius2: 13,
|
|
|
|
* fillColor: 'white',
|
|
|
|
* strokeColor: 'black'
|
|
|
|
* });
|
2013-05-25 01:25:22 -04:00
|
|
|
*
|
2011-05-23 13:48:03 -04:00
|
|
|
* // Create a symbol from the path:
|
|
|
|
* var symbol = new Symbol(path);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-03 16:25:37 -04:00
|
|
|
* // Remove the path:
|
|
|
|
* path.remove();
|
2013-05-25 01:25:22 -04:00
|
|
|
*
|
2011-05-23 13:48:03 -04:00
|
|
|
* // 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);
|
2013-05-25 01:25:22 -04:00
|
|
|
*
|
2011-05-23 13:48:03 -04:00
|
|
|
* // Move the instance to a random position within the view:
|
|
|
|
* instance.position = Point.random() * view.size;
|
2013-05-25 01:25:22 -04:00
|
|
|
*
|
2011-05-23 13:48:03 -04:00
|
|
|
* // Rotate the instance by a random amount between
|
|
|
|
* // 0 and 360 degrees:
|
|
|
|
* instance.rotate(Math.random() * 360);
|
2013-05-25 01:25:22 -04:00
|
|
|
*
|
2011-05-23 13:48:03 -04:00
|
|
|
* // Scale the instance between 0.25 and 1:
|
|
|
|
* instance.scale(0.25 + Math.random() * 0.75);
|
|
|
|
* }
|
|
|
|
*/
|
2013-05-27 15:48:58 -04:00
|
|
|
initialize: function PlacedSymbol(arg0, arg1) {
|
2012-12-25 16:12:25 -05:00
|
|
|
// 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).
|
2013-07-21 18:45:22 -04:00
|
|
|
// 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)))
|
2012-12-25 16:12:25 -05:00
|
|
|
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
|
2011-02-20 12:34:38 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2013-10-17 07:08:54 -04:00
|
|
|
_equals: function(item) {
|
|
|
|
return this._symbol === item._symbol;
|
|
|
|
},
|
|
|
|
|
2011-05-23 13:48:03 -04:00
|
|
|
/**
|
2011-07-09 03:28:49 -04:00
|
|
|
* The symbol that the placed symbol refers to.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-23 13:48:03 -04:00
|
|
|
* @type Symbol
|
2011-07-04 15:27:42 -04:00
|
|
|
* @bean
|
2011-05-23 13:48:03 -04:00
|
|
|
*/
|
2011-07-04 15:27:42 -04:00
|
|
|
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;
|
|
|
|
},
|
2011-05-23 13:48:03 -04:00
|
|
|
|
2013-07-19 21:27:00 -04:00
|
|
|
clone: function(insert) {
|
2013-07-19 21:42:13 -04:00
|
|
|
return this._clone(new PlacedSymbol({
|
|
|
|
symbol: this.symbol,
|
|
|
|
insert: false
|
|
|
|
}), insert);
|
2011-05-19 16:56:49 -04:00
|
|
|
},
|
|
|
|
|
2013-03-03 19:56:48 -05:00
|
|
|
isEmpty: function() {
|
|
|
|
return this._symbol._definition.isEmpty();
|
|
|
|
},
|
|
|
|
|
2012-12-15 11:19:10 -05:00
|
|
|
_getBounds: function(getter, matrix) {
|
2011-11-24 09:44:26 -05:00
|
|
|
// Redirect the call to the symbol definition to calculate the bounds
|
2011-12-18 10:55:56 -05:00
|
|
|
// TODO: Implement bounds caching through passing on of cacheItem, so
|
|
|
|
// that Symbol#_changed() notification become unnecessary!
|
2012-12-15 11:19:10 -05:00
|
|
|
return this.symbol._definition._getCachedBounds(getter, matrix);
|
2011-03-06 16:26:38 -05:00
|
|
|
},
|
|
|
|
|
2013-04-19 19:40:30 -04:00
|
|
|
_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;
|
|
|
|
},
|
|
|
|
|
2013-04-18 19:58:35 -04:00
|
|
|
_draw: function(ctx, param) {
|
2013-04-18 20:04:06 -04:00
|
|
|
this.symbol._definition.draw(ctx, param);
|
2011-02-20 12:34:38 -05:00
|
|
|
}
|
2013-03-01 18:55:19 -05:00
|
|
|
|
2011-05-23 13:48:03 -04:00
|
|
|
// TODO: PlacedSymbol#embed()
|
2011-03-03 11:32:55 -05:00
|
|
|
});
|