UNPKG

graphics-ts

Version:

A porting of purescript-{canvas, free-canvas, drawing} featuring fp-ts

154 lines (153 loc) 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.monoidPath = exports.rect = exports.path = exports.closed = exports.ellipse = exports.composite = exports.circle = exports.arc = exports.point = exports.angle = exports.radians = exports.degrees = void 0; var RA = require("fp-ts/lib/ReadonlyArray"); var M = require("fp-ts/lib/Monoid"); // ------------------------------------------------------------------------------------- // constructors // ------------------------------------------------------------------------------------- /** * Constructs an angle specified in degrees. * * @category constructors * @since 1.0.0 */ exports.degrees = function (degrees) { return ({ _tag: 'Degrees', degrees: degrees }); }; /** * Constructs an angle specified in degrees. * * @category constructors * @since 1.0.0 */ exports.radians = function (radians) { return ({ _tag: 'Radians', radians: radians }); }; /** * Converts an angle into radians for use with the canvas. * * @category constructors * @since 1.0.0 */ exports.angle = function (angle) { switch (angle._tag) { case 'Degrees': return angle.degrees * (Math.PI / 180); case 'Radians': return angle.radians; } }; /** * Constructs a `Point` from x and y coordinates. * * @category constructors * @since 1.0.0 */ exports.point = function (x, y) { return ({ x: x, y: y }); }; /** * Constructs an `Arc` shape. * * @category constructors * @since 1.0.0 */ exports.arc = function (x, y, r, start, end, anticlockwise) { if (anticlockwise === void 0) { anticlockwise = false; } return ({ _tag: 'Arc', x: x, y: y, r: r, start: exports.angle(start), end: exports.angle(end), anticlockwise: anticlockwise }); }; /** * Constructs an `Arc` that forms a circle shape. * * @category constructors * @since 1.0.0 */ exports.circle = function (x, y, r) { return ({ _tag: 'Arc', x: x, y: y, r: r, start: exports.angle(exports.radians(0)), end: exports.angle(exports.radians(Math.PI * 2)), anticlockwise: false }); }; /** * Constructs a `Composite` shape. * * @category constructors * @since 1.0.0 */ exports.composite = function (shapes) { return ({ _tag: 'Composite', shapes: shapes }); }; /** * Constructs an `Ellipse` shape. * * @category constructors * @since 1.0.0 */ exports.ellipse = function (x, y, rx, ry, rotation, start, end, anticlockwise) { if (anticlockwise === void 0) { anticlockwise = false; } return ({ _tag: 'Ellipse', x: x, y: y, rx: rx, ry: ry, rotation: exports.angle(rotation), start: exports.angle(start), end: exports.angle(end), anticlockwise: anticlockwise }); }; function closed(F) { return function (fa) { return F.reduce(fa, exports.monoidPath.empty, function (b, a) { return ({ _tag: 'Path', closed: true, points: RA.snoc(b.points, a) }); }); }; } exports.closed = closed; function path(F) { return function (fa) { return F.reduce(fa, exports.monoidPath.empty, function (b, a) { return ({ _tag: 'Path', closed: false, points: RA.snoc(b.points, a) }); }); }; } exports.path = path; /** * Constructs a `Rectangle` shape. * * @category constructors * @since 1.0.0 */ exports.rect = function (x, y, width, height) { return ({ _tag: 'Rect', x: x, y: y, width: width, height: height }); }; // ------------------------------------------------------------------------------------- // instances // ------------------------------------------------------------------------------------- /** * The `Monoid` instance for a `Path`. * * @category instances * @since 1.0.0 */ exports.monoidPath = M.getStructMonoid({ _tag: { concat: function () { return 'Path'; }, empty: 'Path' }, closed: M.monoidAny, points: RA.getMonoid() });