paper.js/src/item/PlacedSymbol.js

116 lines
3.2 KiB
JavaScript
Raw Normal View History

2011-03-06 19:50:44 -05:00
/*
* Paper.js
*
* 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-03-07 20:41:50 -05:00
* Distributed under the MIT license. See LICENSE file for details.
*
2011-03-06 19:50:44 -05:00
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
2011-03-07 20:41:50 -05:00
* All rights reserved.
2011-03-06 19:50:44 -05:00
*/
var PlacedSymbol = this.PlacedSymbol = Item.extend({
2011-05-23 13:48:03 -04:00
/** @lends PlacedSymbol# */
beans: true,
2011-05-23 13:48:03 -04:00
/**
* Creates a new PlacedSymbol Item.
*
* @param {Symbol} symbol the symbol to place
* @param {Point|Matrix} [matrixOrOffset] the center point of the placed
* symbol or a {@link Matrix} transformation to transform the placed symbol
* with.
*
* @example {@paperscript split=true}
* // 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'
* };
*
* // Create a symbol from the path:
* // (the original path is removed from the project)
* var symbol = new Symbol(path);
*
* // 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);
* }
*
* @class A PlacedSymbol represents an instance of a symbol which has been
* placed in a Paper.js project.
2011-05-23 13:48:03 -04:00
*
* @extends Item
* @constructs PlacedSymbol
*/
initialize: function(symbol, matrixOrOffset) {
this.base();
2011-05-07 08:12:46 -04:00
this.symbol = symbol instanceof Symbol ? symbol : new Symbol(symbol);
this.matrix = matrixOrOffset !== undefined
? matrixOrOffset instanceof Matrix
? matrixOrOffset
: new Matrix().translate(Point.read(arguments, 1))
: new Matrix();
},
2011-05-23 13:48:03 -04:00
/**
* The symbol that the placed symbol refers to:
2011-05-23 13:48:03 -04:00
*
* @name PlacedSymbol#symbol
* @type Symbol
*/
clone: function() {
2011-05-20 13:41:53 -04:00
return this._clone(new PlacedSymbol(this.symbol, this.matrix.clone()));
},
_transform: function(matrix, flags) {
// In order to set the right context transformation when drawing the
// raster, simply preconcatenate the internal matrix with the provided
// one.
this.matrix.preConcatenate(matrix);
},
getBounds: function() {
2011-05-05 06:20:37 -04:00
var bounds = this.symbol._definition.getStrokeBounds(this.matrix);
return LinkedRectangle.create(this, 'setBounds',
bounds.x, bounds.y, bounds.width, bounds.height);
},
getStrokeBounds: function() {
return this.getBounds();
},
draw: function(ctx, param) {
2011-04-21 09:48:21 -04:00
if (param.selection) {
Item.drawSelectedBounds(this.symbol._definition.getStrokeBounds(),
2011-04-21 09:48:21 -04:00
ctx, this.matrix);
} else {
ctx.save();
this.matrix.applyToContext(ctx);
Item.draw(this.symbol.getDefinition(), ctx, param);
ctx.restore();
}
}
2011-05-23 13:48:03 -04:00
// TODO: PlacedSymbol#embed()
2011-03-03 11:32:55 -05:00
});