2011-05-16 09:15:47 -04:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-16 09:15:47 -04:00
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
|
|
|
* http://paperjs.org/
|
|
|
|
* http://scriptographer.org/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-16 09:15:47 -04: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-05-16 09:15:47 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* @name PointText
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @class A PointText item represents a piece of typography in your Paper.js
|
|
|
|
* project which starts from a certain point and extends by the amount of
|
|
|
|
* characters contained in it.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @extends TextItem
|
|
|
|
*/
|
|
|
|
var PointText = this.PointText = TextItem.extend(/** @lends PointText# */{
|
2011-05-23 13:13:51 -04:00
|
|
|
/**
|
|
|
|
* Creates a point text item
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-23 13:13:51 -04:00
|
|
|
* @param {Point} point the position where the text will start
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @example
|
|
|
|
* var text = new PointText(new Point(50, 100));
|
2011-06-20 19:12:47 -04:00
|
|
|
* text.justification = 'center';
|
2011-06-22 18:56:05 -04:00
|
|
|
* text.fillColor = 'black';
|
|
|
|
* text.content = 'The contents of the point text';
|
2011-05-23 13:13:51 -04:00
|
|
|
*/
|
2011-05-16 09:15:47 -04:00
|
|
|
initialize: function(point) {
|
|
|
|
this.base();
|
2011-08-02 11:08:00 -04:00
|
|
|
this._point = Point.read(arguments).clone();
|
|
|
|
this._matrix = new Matrix().translate(this._point);
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
|
|
|
|
2011-05-19 16:56:49 -04:00
|
|
|
clone: function() {
|
2011-05-20 03:54:44 -04:00
|
|
|
var copy = this._clone(new PointText(this._point));
|
2011-05-20 03:47:13 -04:00
|
|
|
// Use Matrix#initialize to easily copy over values.
|
2011-05-20 16:03:16 -04:00
|
|
|
copy._matrix.initialize(this._matrix);
|
2011-05-20 03:47:13 -04:00
|
|
|
return copy;
|
2011-05-19 16:56:49 -04:00
|
|
|
},
|
|
|
|
|
2011-05-23 13:13:51 -04:00
|
|
|
/**
|
|
|
|
* The PointText's anchor point
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-23 13:13:51 -04:00
|
|
|
* @type Point
|
|
|
|
* @bean
|
|
|
|
*/
|
2011-05-16 09:15:47 -04:00
|
|
|
getPoint: function() {
|
2011-08-02 11:08:00 -04:00
|
|
|
// Se Item#getPosition for an explanation why we create new LinkedPoint
|
|
|
|
// objects each time.
|
|
|
|
return LinkedPoint.create(this, 'setPoint',
|
|
|
|
this._point.x, this._point.y);
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
setPoint: function(point) {
|
2011-08-03 17:37:56 -04:00
|
|
|
this.translate(Point.read(arguments).subtract(this._point));
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2011-06-01 05:49:43 -04:00
|
|
|
// TODO: Position should be the center point of the bounds but we currently
|
2011-08-02 11:08:00 -04:00
|
|
|
// don't support bounds for PointText, so let's return the same as #point
|
|
|
|
// for the time being.
|
2011-05-16 09:15:47 -04:00
|
|
|
getPosition: function() {
|
2011-08-02 08:31:35 -04:00
|
|
|
return this.getPoint();
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2011-05-16 09:15:47 -04:00
|
|
|
setPosition: function(point) {
|
|
|
|
this.setPoint.apply(this, arguments);
|
|
|
|
},
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2011-05-16 09:15:47 -04:00
|
|
|
_transform: function(matrix, flags) {
|
2011-05-20 16:03:16 -04:00
|
|
|
this._matrix.preConcatenate(matrix);
|
2011-08-02 11:08:00 -04:00
|
|
|
// Also transform _point:
|
|
|
|
matrix._transformPoint(this._point, this._point);
|
2011-05-16 09:15:47 -04:00
|
|
|
},
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2011-05-16 09:15:47 -04:00
|
|
|
draw: function(ctx) {
|
2011-06-19 18:28:41 -04:00
|
|
|
if (!this._content)
|
2011-05-16 09:15:47 -04:00
|
|
|
return;
|
|
|
|
ctx.save();
|
2011-06-20 13:53:36 -04:00
|
|
|
ctx.font = this.getFontSize() + 'pt ' + this.getFont();
|
|
|
|
ctx.textAlign = this.getJustification();
|
2011-05-20 16:03:16 -04:00
|
|
|
this._matrix.applyToContext(ctx);
|
2011-06-30 06:01:51 -04:00
|
|
|
|
2011-05-16 09:15:47 -04:00
|
|
|
var fillColor = this.getFillColor();
|
|
|
|
var strokeColor = this.getStrokeColor();
|
|
|
|
if (!fillColor || !strokeColor)
|
2011-06-17 13:53:34 -04:00
|
|
|
ctx.globalAlpha = this._opacity;
|
2011-05-16 09:15:47 -04:00
|
|
|
if (fillColor) {
|
|
|
|
ctx.fillStyle = fillColor.getCanvasStyle(ctx);
|
2011-06-19 18:28:41 -04:00
|
|
|
ctx.fillText(this._content, 0, 0);
|
2011-05-16 09:15:47 -04:00
|
|
|
}
|
|
|
|
if (strokeColor) {
|
|
|
|
ctx.strokeStyle = strokeColor.getCanvasStyle(ctx);
|
2011-06-19 18:28:41 -04:00
|
|
|
ctx.strokeText(this._content, 0, 0);
|
2011-05-16 09:15:47 -04:00
|
|
|
}
|
|
|
|
ctx.restore();
|
|
|
|
}
|
|
|
|
});
|