2011-03-06 19:50:44 -05:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://scriptographer.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* 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
|
|
|
*
|
2011-07-01 05:26:51 -04:00
|
|
|
* @extends PlacedItem
|
2011-06-22 18:56:05 -04:00
|
|
|
*/
|
2011-07-01 05:26:51 -04:00
|
|
|
var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol# */{
|
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
|
2011-05-30 14:09:46 -04:00
|
|
|
* @param {Point|Matrix} [matrixOrOffset] the center point of the placed
|
2011-05-30 13:42:17 -04:00
|
|
|
* symbol or a {@link Matrix} transformation to transform the placed symbol
|
|
|
|
* with.
|
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:
|
2011-05-23 13:48:03 -04:00
|
|
|
* var path = new Path.Star(new Point(0, 0), 6, 5, 13);
|
|
|
|
* path.style = {
|
|
|
|
* fillColor: 'white',
|
|
|
|
* strokeColor: 'black'
|
|
|
|
* };
|
2011-06-30 06:01:51 -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();
|
2011-06-30 06:01:51 -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);
|
2011-06-30 06:01:51 -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;
|
2011-06-30 06:01:51 -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);
|
2011-06-30 06:01:51 -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);
|
|
|
|
* }
|
|
|
|
*/
|
2011-03-08 12:21:05 -05:00
|
|
|
initialize: function(symbol, matrixOrOffset) {
|
2011-02-20 12:34:38 -05:00
|
|
|
this.base();
|
2011-07-04 15:27:42 -04:00
|
|
|
this.setSymbol(symbol instanceof Symbol ? symbol : new Symbol(symbol));
|
2011-07-01 05:32:09 -04:00
|
|
|
this._matrix = matrixOrOffset !== undefined
|
2011-05-07 08:12:46 -04:00
|
|
|
? matrixOrOffset instanceof Matrix
|
|
|
|
? matrixOrOffset
|
|
|
|
: new Matrix().translate(Point.read(arguments, 1))
|
|
|
|
: new Matrix();
|
2011-02-20 12:34:38 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
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
|
|
|
|
2011-05-19 16:56:49 -04:00
|
|
|
clone: function() {
|
2011-07-01 05:32:09 -04:00
|
|
|
return this._clone(new PlacedSymbol(this.symbol, this._matrix.clone()));
|
2011-05-19 16:56:49 -04:00
|
|
|
},
|
|
|
|
|
2011-11-24 09:13:21 -05:00
|
|
|
_calculateBounds: function(type, matrix) {
|
2011-11-24 09:44:26 -05:00
|
|
|
// Redirect the call to the symbol definition to calculate the bounds
|
2011-11-24 09:13:21 -05:00
|
|
|
return this.symbol._definition._getBounds(type, matrix);
|
2011-03-06 16:26:38 -05:00
|
|
|
},
|
|
|
|
|
2011-03-03 07:19:43 -05:00
|
|
|
draw: function(ctx, param) {
|
2011-04-21 09:48:21 -04:00
|
|
|
if (param.selection) {
|
2011-11-26 04:15:01 -05:00
|
|
|
Item.drawSelectedBounds(this._getBounds('bounds'), ctx, this._matrix);
|
2011-04-21 09:48:21 -04:00
|
|
|
} else {
|
|
|
|
ctx.save();
|
2011-07-01 05:32:09 -04:00
|
|
|
this._matrix.applyToContext(ctx);
|
2011-04-21 09:48:21 -04:00
|
|
|
Item.draw(this.symbol.getDefinition(), ctx, param);
|
|
|
|
ctx.restore();
|
2011-04-18 12:46:39 -04:00
|
|
|
}
|
2011-02-20 12:34:38 -05:00
|
|
|
}
|
2011-03-03 07:19:43 -05:00
|
|
|
|
2011-05-23 13:48:03 -04:00
|
|
|
// TODO: PlacedSymbol#embed()
|
2011-03-03 11:32:55 -05:00
|
|
|
});
|