UNPKG

graphics-ts

Version:

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

396 lines (395 loc) 8.38 kB
import * as M from 'fp-ts/lib/Monoid' import * as O from 'fp-ts/lib/Option' import * as C from './Canvas' import { Color } from './Color' import { Font } from './Font' import { Point, Shape } from './Shape' /** * Represents a `Drawing` that has been clipped by a `Shape`. * * @category model * @since 1.0.0 */ export interface Clipped { readonly _tag: 'Clipped' /** * The shape to use for clipping. */ readonly shape: Shape /** * The drawing to be clipped. */ readonly drawing: Drawing } /** * Represents a filled `Shape` that can be drawn to the canvas. * * @category model * @since 1.0.0 */ export interface Fill { readonly _tag: 'Fill' /** * The filled `Shape`. */ readonly shape: Shape /** * The fill style applied to the `Shape`. */ readonly style: FillStyle } /** * Represents the styles applied to a filled `Shape`. * * @category model * @since 1.0.0 */ export interface FillStyle { /** * The fill color. */ readonly color: O.Option<Color> } /** * Represents an outlined `Shape` that can be drawn to the canvas. * * @category model * @since 1.0.0 */ export interface Outline { readonly _tag: 'Outline' /** * The outlined `Shape`. */ readonly shape: Shape /** * The outline style applied to the `Shape`. */ readonly style: OutlineStyle } /** * Represents the styles applied to an outlined `Shape`. * * @category model * @since 1.0.0 */ export interface OutlineStyle { /** * The outline color. */ readonly color: O.Option<Color> /** * The outline line width. */ readonly lineWidth: O.Option<number> } /** * Represents a collection of `Drawing`s that can be drawn to the canvas. * * @category model * @since 1.0.0 */ export interface Many { readonly _tag: 'Many' /** * The collection of drawings. */ readonly drawings: ReadonlyArray<Drawing> } /** * Represents a `Drawing` that has had its transform rotated. * * @category model * @since 1.0.0 */ export interface Rotate { readonly _tag: 'Rotate' /** * The angle of rotation. */ readonly angle: number /** * The drawing to be rotated. */ readonly drawing: Drawing } /** * Represents a `Drawing` that has had scale applied to its transform. * * @category model * @since 1.0.0 */ export interface Scale { readonly _tag: 'Scale' /** * The x-axis scale. */ readonly scaleX: number /** * The y-axis scale. */ readonly scaleY: number /** * The drawing to be scaled. */ readonly drawing: Drawing } /** * Represents text that can be drawn to the canvas. * * @category model * @since 1.0.0 */ export interface Text { readonly _tag: 'Text' /** * The font style applied to the text. */ readonly font: Font /** * The x-axis coordinate at which to begin drawing the text. */ readonly x: number /** * The y-axis coordinate at which to begin drawing the text. */ readonly y: number /** * The fill style applied to the text. */ readonly style: FillStyle /** * The HTML text string. */ readonly text: string } /** * Represents a `Drawing` that has had its transform translated. * * @category model * @since 1.0.0 */ export interface Translate { readonly _tag: 'Translate' /** * The x-axis translation. */ readonly translateX: number /** * The y-axis translation. */ readonly translateY: number /** * The drawing to be translated. */ readonly drawing: Drawing } /** * Represents a `Drawing` that has had a shadow applied to it. * * @category model * @since 1.0.0 */ export interface WithShadow { readonly _tag: 'WithShadow' /** * The shadow to be applied. */ readonly shadow: Shadow /** * The drawing to be shadowed. */ readonly drawing: Drawing } /** * Represents the shadow styles applied to a `Shape`. * * @category model * @since 1.0.0 */ export interface Shadow { /** * The shadow color. */ readonly color: O.Option<Color> /** * The shadow blur radius. */ readonly blur: O.Option<number> /** * The shadow offset. */ readonly offset: O.Option<Point> } /** * Represents a shape that can be drawn to the canvas. * * @category model * @since 1.0.0 */ export declare type Drawing = Clipped | Fill | Outline | Many | Rotate | Scale | Text | Translate | WithShadow /** * Clips a `Drawing` using the specified `Shape`. * * @category constructors * @since 1.0.0 */ export declare const clipped: (shape: Shape, drawing: Drawing) => Drawing /** * Constructs a `Drawing` from a `Fill` `Shape`. * * @category constructors * @since 1.0.0 */ export declare const fill: (shape: Shape, style: FillStyle) => Drawing /** * Constructs a `FillStyle`. * * @category constructors * @since 1.0.0 */ export declare const fillStyle: (color: Color) => FillStyle /** * Constructs a `Drawing` from an `Outline` `Shape`. * * @category constructors * @since 1.0.0 */ export declare const outline: (shape: Shape, style: OutlineStyle) => Drawing /** * Constructs an `OutlineStyle` from a `Color`. * * @category constructors * @since 1.0.0 */ export declare const outlineColor: (color: Color) => OutlineStyle /** * Constructs an `OutlineStyle` from a line width. * * @category constructors * @since 1.0.0 */ export declare const lineWidth: (lineWidth: number) => OutlineStyle /** * Construct a single `Drawing` from a collection of `Many` `Drawing`s. * * @category constructors * @since 1.0.0 */ export declare const many: (drawings: ReadonlyArray<Drawing>) => Drawing /** * Applies rotation to the transform of a `Drawing`. * * @category constructors * @since 1.0.0 */ export declare const rotate: (angle: number, drawing: Drawing) => Drawing /** * Applies scale to the transform of a `Drawing`. * * @category constructors * @since 1.0.0 */ export declare const scale: (scaleX: number, scaleY: number, drawing: Drawing) => Drawing /** * Constructs a `Drawing` from `Text`. * * @category constructors * @since 1.0.0 */ export declare const text: (font: Font, x: number, y: number, style: FillStyle, text: string) => Drawing /** * Applies translation to the transform of a `Drawing`. * * @category constructors * @since 1.0.0 */ export declare const translate: (translateX: number, translateY: number, drawing: Drawing) => Drawing /** * Applies `Shadow` to a `Drawing`. * * @category constructors * @since 1.0.0 */ export declare const withShadow: (shadow: Shadow, drawing: Drawing) => Drawing /** * Constructs a `Shadow` from a blur radius. * * @category constructors * @since 1.0.0 */ export declare const shadowBlur: (blurRadius: number) => Shadow /** * Constructs a `Shadow` from a `Color`. * * @category constructors * @since 1.0.0 */ export declare const shadowColor: (color: Color) => Shadow /** * Constructs a `Shadow` from an offset `Point`. * * @category constructors * @since 1.0.0 */ export declare const shadowOffset: (offsetPoint: Point) => Shadow /** * Renders a `Shape`. * * @category combinators * @since 1.1.0 */ export declare const renderShape: (shape: Shape) => C.Render<CanvasRenderingContext2D> /** * Renders a `Drawing`. * * @category combinators * @since 1.0.0 */ export declare const render: (drawing: Drawing) => C.Render<CanvasRenderingContext2D> /** * Gets a `Monoid` instance for `FillStyle`. * * @category instances * @since 1.0.0 */ export declare const monoidFillStyle: M.Monoid<FillStyle> /** * Gets a `Monoid` instance for `OutlineStyle`. * * @example * import * as O from 'fp-ts/lib/Option' * import * as M from 'fp-ts/lib/Monoid' * import * as Color from 'graphics-ts/lib/Color' * import * as D from 'graphics-ts/lib/Drawing' * * assert.deepStrictEqual( * M.fold(D.monoidOutlineStyle)([ * D.outlineColor(Color.black), * D.outlineColor(Color.white), * D.lineWidth(5) * ]), * { * color: O.some(Color.black), * lineWidth: O.some(5) * } * ) * * @category instances * @since 1.0.0 */ export declare const monoidOutlineStyle: M.Monoid<OutlineStyle> /** * Gets a `Monoid` instance for `Shadow`. * * @category instances * @since 1.0.0 */ export declare const monoidShadow: M.Monoid<Shadow> /** * Gets a `Monoid` instance for `Drawing`. * * @category instances * @since 1.0.0 */ export declare const monoidDrawing: M.Monoid<Drawing>