'use strict';
const Rac = require('./Rac');
/**
* This namespace lists utility functions attached to an instance of
* `{@link Rac}` during initialization. Each drawable and style class gets
* a corresponding function like [`rac.Point`]{@link instance.Point} or
* [`rac.Color`]{@link instance.Color}.
*
* Drawable and style objects require for construction a reference to a
* `rac` instance in order to perform drawing operations. The attached
* functions build new objects using the calling `Rac` instance.
*
* These functions are also setup with ready-made convenience objects for
* many usual values like [`rac.Angle.north`]{@link instance.Angle#north} or
* [`rac.Point.zero`]{@link instance.Point#zero}.
*
* @namespace instance
*/
// Attaches the convenience functions to create objects with this instance
// of Rac. These functions are attached as properties (instead of into the
// prototype) because these are later populated with more properties and
// methods, and thus need to be independent for each instance.
//
// Intended to receive the a Rac instance as parameter.
module.exports = function attachInstanceFunctions(rac) {
/**
* Convenience function that creates a new `Color` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Color}`.
*
* @param {number} r
* @param {number} g
* @param {number} b
* @param {number=} a
*
* @returns {Rac.Color}
*
* @see instance.Color
*
* @function Color
* @memberof Rac#
*/
rac.Color = function makeColor(r, g, b, a = 1) {
return new Rac.Color(this, r, g, b, a);
};
/**
* Convenience function that creates a new `Stroke` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Stroke}`.
*
* @param {?number} weight
* @param {?Rac.Color} color
*
* @returns {Rac.Stroke}
*
* @see instance.Stroke
*
* @function Stroke
* @memberof Rac#
*/
rac.Stroke = function makeStroke(weight, color = null) {
return new Rac.Stroke(this, weight, color);
};
/**
* Convenience function that creates a new `Fill` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Fill}`.
*
* @param {Rac.Color=} color
* @returns {Rac.Fill}
*
* @see instance.Fill
*
* @function Fill
* @memberof Rac#
*/
rac.Fill = function makeFill(color = null) {
return new Rac.Fill(this, color);
};
/**
* Convenience function that creates a new `Style` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Style}`.
*
* @param {?Rac.Stroke} stroke
* @param {?Rac.Fill} fill
*
* @returns {Rac.Style}
*
* @see instance.Style
*
* @function Style
* @memberof Rac#
*/
rac.Style = function makeStyle(stroke = null, fill = null) {
return new Rac.Style(this, stroke, fill);
};
/**
* Convenience function that creates a new `Angle` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Angle}`.
*
* @param {number} turn - The turn value of the angle, in the range `[O,1)`
* @returns {Rac.Angle}
*
* @see instance.Angle
*
* @function Angle
* @memberof Rac#
*/
rac.Angle = function makeAngle(turn) {
return new Rac.Angle(this, turn);
};
/**
* Convenience function that creates a new `Point` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Point}`.
*
* @param {number} x - The x coordinate
* @param {number} y - The y coordinate
*
* @returns {Rac.Point}
*
* @see instance.Point
*
* @function Point
* @memberof Rac#
*/
rac.Point = function makePoint(x, y) {
return new Rac.Point(this, x, y);
};
/**
* Convenience function that creates a new `Ray` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Ray}`.
*
* @param {number} x
* @param {number} y
* @param {Rac.Angle|number} angle
*
* @returns {Rac.Ray}
*
* @see instance.Ray
*
* @function Ray
* @memberof Rac#
*/
rac.Ray = function makeRay(x, y, angle) {
const start = new Rac.Point(this, x, y);
angle = Rac.Angle.from(this, angle);
return new Rac.Ray(this, start, angle);
};
/**
* Convenience function that creates a new `Segment` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Segment}`.
*
* @param {number} x
* @param {number} y
* @param {Rac.Angle|number} angle
* @param {number} length
*
* @returns {Rac.Segment}
*
* @see instance.Segment
*
* @function Segment
* @memberof Rac#
*/
rac.Segment = function makeSegment(x, y, angle, length) {
const start = new Rac.Point(this, x, y);
angle = Rac.Angle.from(this, angle);
const ray = new Rac.Ray(this, start, angle);
return new Rac.Segment(this, ray, length);
};
/**
* Convenience function that creates a new `Arc` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Arc}`.
*
* @param {number} x - The _x_ coordinate for the arc center
* @param {number} y - The _y_ coordinate for the arc center
* @param {Rac.Angle|number} start - The start of the arc
* @param {?Rac.Angle|number} [end=null] - The end of the arc; when
* ommited or set to `null`, `start` is used instead
* @param {boolean} [clockwise=true] The orientation of the arc
*
* @returns {Rac.Arc}
*
* @see instance.Arc
*
* @function Arc
* @memberof Rac#
*/
rac.Arc = function makeArc(x, y, radius, start = this.Angle.zero, end = null, clockwise = true) {
const center = new Rac.Point(this, x, y);
start = Rac.Angle.from(this, start);
end = end === null
? start
: Rac.Angle.from(this, end);
return new Rac.Arc(this, center, radius, start, end, clockwise);
};
/**
* Convenience function that creates a new `Text` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Text}`.
*
* @param {number} x - The x coordinate location for the drawn text
* @param {number} y - The y coordinate location for the drawn text
* @param {string} string - The string to draw
* @param {Rac.Text.Format} format - The format for the drawn text
*
* @returns {Rac.Text}
*
* @see instance.Text
*
* @function Text
* @memberof Rac#
*/
rac.Text = function makeText(x, y, string, format = this.Text.Format.topLeft) {
const point = new Rac.Point(this, x, y);
return new Rac.Text(this, point, string, format);
};
/**
* Convenience function that creates a new `Text.Format` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Text.Format}`.
*
* @param {string} hAlign - The horizontal alignment, left-to-right; one
* of the values from [`horizontalAlign`]{@link Rac.Text.Format.horizontalAlign}
* @param {string} vAlign - The vertical alignment, top-to-bottom; one of
* the values from [`verticalAlign`]{@link Rac.Text.Format.verticalAlign}
* @param {Rac.Angle} [angle=[rac.Angle.zero]{@link instance.Angle#zero}]
* The angle towards which the text is drawn
* @param {string} [font=null] - The font name
* @param {number} [size=null] - The font size
*
* @returns {Rac.Text.Format}
*
* @see instance.Text.Format
*
* @function Format
* @memberof instance.Text#
*/
rac.Text.Format = function makeTextFormat(
hAlign,
vAlign,
angle = rac.Angle.zero,
font = null,
size = null)
{
// This functions uses `rac` instead of `this`, since `this` points to
// `rac.Text` here and to `rac` in the `TextFormat` alias
angle = Rac.Angle.from(rac, angle);
return new Rac.Text.Format(
rac,
hAlign, vAlign,
angle, font, size);
};
/**
* Alias of [`rac.Text.Format`]{@link instance.Text#Format}.
*
* To display in documentation along the rest of
* [utility instance functions]{@link instance}.
*
* @function TextFormat
* @memberof Rac#
*/
rac.TextFormat = rac.Text.Format;
/**
* Convenience function that creates a new `Bezier` setup with `this`.
*
* The function also contains additional methods and properties listed in
* `{@link instance.Bezier}`.
*
* @param {number} startX
* @param {number} startY
* @param {number} startAnchorX
* @param {number} startAnchorY
* @param {number} endAnchorX
* @param {number} endAnchorY
* @param {number} endX
* @param {number} endY
*
* @returns {Rac.Bezier}
*
* @see instance.Bezier
*
* @function Bezier
* @memberof Rac#
*/
rac.Bezier = function makeBezier(
startX, startY, startAnchorX, startAnchorY,
endAnchorX, endAnchorY, endX, endY)
{
const start = new Rac.Point(this, startX, startY);
const startAnchor = new Rac.Point(this, startAnchorX, startAnchorY);
const endAnchor = new Rac.Point(this, endAnchorX, endAnchorY);
const end = new Rac.Point(this, endX, endY);
return new Rac.Bezier(this, start, startAnchor, endAnchor, end);
};
}; // attachInstanceFunctions