package ocaml-canvas

  1. Overview
  2. Docs

Module V1.CanvasSource

Canvas manipulation functions

Sourcetype t

An abstract type representing a canvas

Comparison and hash functions

In order to ease identification of canvas or building collections of canvas, the usual comparison and hash functions are provided. These functions simply operate on the canvas' unique ids. Also, standard structural and physical comparison operators can be used to compare canvases.

Sourceval compare : t -> t -> int

compare c1 c2 is equivalent to compare (getId c1) (getId c2)

Sourceval hash : t -> int

hash c returns a unique integer value for canvas c, which is computed as Hashtbl.hash (Canvas.getId c)

Creation

Sourceval createOnscreen : ?autocommit:bool -> ?decorated:bool -> ?resizeable:bool -> ?minimize:bool -> ?maximize:bool -> ?close:bool -> ?title:string -> ?pos:(int * int) -> size:(int * int) -> unit -> t

createOnscreen ?autocommit ?decorated ?resizeable ?minimize ?maximize ?close ?title ?pos ~size () creates a windowed canvas of size size. The window title and position can be specified using the optional arguments title and pos. The window decorations, which are active by default, can be disabled using the optional arguments decorated, resizeable, minimize, maximize, and close. The decorated argument has a higher priority: if set to false, all other decoration arguments will be ignored (considered to be false), and all decorations will be removed from the window. The autocommit option, which is active by default, indicates whether the canvas should be automatically presented after each frame event. See Canvas.commit for more info on autocommit.

Exceptions:

Sourceval createOffscreen : size:(int * int) -> unit -> t

createOffscreen ~size creates an offscreen canvas of size size

Exceptions:

Sourceval createOffscreenFromImageData : ImageData.t -> t

createOffscreenFromImageData id creates an offscreen canvas with the contents of image data id

Exceptions:

Sourceval createOffscreenFromPNG : string -> t React.event

createOffscreen filename creates an offscreen canvas with the contents of PNG file filename. The returned event will be triggered once the image is loaded.

Exceptions:

Visibility

Sourceval show : t -> unit

show c makes the canvas c visible on screen. Does nothing on offscreen canvases.

Sourceval hide : t -> unit

hide c makes the canvas c invisible. Does nothing offscreen canvases.

Sourceval close : t -> unit

close c closes the canvas c, i.e. it permanently removes it from the screen and prevents it to receive events ; however it can still be used as an offscreen canvas. Does nothing on offscreen canvases.

Rendering

Sourceval commit : t -> unit

commit c informs the backend that the canvas has been modified and should be presented on screen. This is not necessary if the canvas has been created with autocommit set to true, as in this case the canvas will be automatically presented after each frame event. Note that it is also useless when using the Javascript backend ; however, to maintain consistent behavior between the various backends, do remember to use commit on any canvas created with autocommit set to false. Does nothing on offscreen canvases.

Configuration

Sourceval getId : t -> int

getId c returns the unique id of canvas c

Sourceval getSize : t -> int * int

getSize c returns the size of canvas c

Sourceval setSize : t -> (int * int) -> unit

setSize c size sets the size of canvas c

Exceptions:

Sourceval getPosition : t -> int * int

getPosition c returns the position of canvas c. Returns (0, 0) when used on offscreen cavnas.

Sourceval setPosition : t -> (int * int) -> unit

setPosition c pos sets the position of canvas c. Does nothing on offscreen canvases.

State

Sourceval save : t -> unit

save c pushes the current state of canvas c onto the state stack

Sourceval restore : t -> unit

restore c pops the current state of canvas c from the state stack

Transformations

Sourceval setTransform : t -> Transform.t -> unit

setTransform c t sets the current transformation matrix of canvas c. The matrix t = { a, b, c, d, e, f } is of the following form:

        a b 0
        c d 0
        e f 1 
Sourceval transform : t -> Transform.t -> unit

transform c t apply the given arbitrary transformation to the current transformation matrix of canvas c

Sourceval translate : t -> Vector.t -> unit

translate c vec apply the given translation transform to the current transformation matrix of canvas c

Sourceval scale : t -> Vector.t -> unit

scale c vec apply the given scale transform to the current transformation matrix of canvas c

Sourceval shear : t -> Vector.t -> unit

shear c vec apply the given shear transform to the current transformation matrix of canvas c

Sourceval rotate : t -> float -> unit

rotate c theta apply the given rotation transform to the current transformation matrix of canvas c

Style

Sourceval getLineWidth : t -> float

getLineWidth c returns the current line width of canvas c

Sourceval setLineWidth : t -> float -> unit

setLineWidth c w sets the current line width of canvas c to w

Sourceval getLineJoin : t -> Join.t

getLineJoin c returns the current line join type of canvas c

Sourceval setLineJoin : t -> Join.t -> unit

setLineJoin c j sets the current line join type of canvasc to j

Sourceval getLineCap : t -> Cap.t

getLineJoin c returns the current line cap type of canvas c

Sourceval setLineCap : t -> Cap.t -> unit

setLineJoin c j sets the current line cap type of canvasc to j

Sourceval getMiterLimit : t -> float

getMiterLimit c returns the current miter limit of canvas c

Sourceval setMiterLimit : t -> float -> unit

getMiterLimit c m sets the current miter limit of canvas c to m

Sourceval getLineDashOffset : t -> float

getLineDashOffset c returns the current line offset of c

Sourceval setLineDashOffset : t -> float -> unit

setLineDashOffset c t sets the current line offset of c to t

Sourceval getLineDash : t -> float array

getLineDash c t returns the current line dash pattern of c

Sourceval setLineDash : t -> float array -> unit

setLineDash c t sets the current line dash pattern of c to t

Sourceval getStrokeColor : t -> Color.t

getStrokeColor c returns the current stroke color of canvas c

Sourceval setStrokeColor : t -> Color.t -> unit

setStrokeColor c col sets the current stroke color of canvas c to col

Sourceval setStrokeGradient : t -> Gradient.t -> unit

setStrokeGradient c grad sets the current stroke style of canvas c to the gradient grad

Sourceval setStrokePattern : t -> Pattern.t -> unit

setStrokePattern c pat sets the current stroke style of canvas c to the pattern pat

Sourceval getStrokeStyle : t -> Style.t

getStrokeStyle c returns the current stroke style of canvas c

Sourceval setStrokeStyle : t -> Style.t -> unit

setStrokeStyle c style sets the current stroke style of canvas c to style style

Sourceval getFillColor : t -> Color.t

getFillColor c returns the current fill color of canvas c

Sourceval setFillColor : t -> Color.t -> unit

setFillColor c col sets the current fill color of canvas c to col

Sourceval setFillGradient : t -> Gradient.t -> unit

setFillGradient c grad sets the current fill style of canvas c to the gradient grad

Sourceval setFillPattern : t -> Pattern.t -> unit

setFillPattern c pat sets the current fill style of canvas c to the pattern pat

Sourceval getFillStyle : t -> Style.t

getFillStyle c return the current fill style of canvas c

Sourceval setFillStyle : t -> Style.t -> unit

setFillStyle c style sets the current fill style of canvas c to style style

Sourceval getGlobalAlpha : t -> float

getGlobalAlpha c returns the current global alpha of canvas c

Sourceval setGlobalAlpha : t -> float -> unit

setGlobalAlpha c a sets the global alpha value of canvasc to a

Sourceval getGlobalCompositeOperation : t -> CompositeOp.t

getGlobalCompositeOperation c returns the global composite or blending operation of canvasc

Sourceval setGlobalCompositeOperation : t -> CompositeOp.t -> unit

setGlobalCompositeOperation c o sets the global composite or blending operation of canvasc to o

Sourceval getShadowColor : t -> Color.t

setShadowColor c returns the canvas c's shadow color

Sourceval setShadowColor : t -> Color.t -> unit

setShadowColor c col sets the canvas c's shadow color to col

Sourceval getShadowBlur : t -> float

setShadowBlur c returns the shadow blur radius of canvas c

Sourceval setShadowBlur : t -> float -> unit

setShadowBlur c b sets the shadow blur radius of canvas c to b

Sourceval getShadowOffset : t -> Vector.t

setShadowOffset c returns the offset of the shadows drawn in c

Sourceval setShadowOffset : t -> Vector.t -> unit

setShadowOffset c o sets the offset of the shadows drawn in c to o

Sourceval setFont : t -> string -> size:Font.size -> slant:Font.slant -> weight:Font.weight -> unit

setFont c family ~size ~slant ~weight sets the current font of canvas c to the one specified by the given family, size, slant and weight

Path

Sourceval clearPath : t -> unit

clearPath c resets the path of canvas c

Sourceval closePath : t -> unit

closePath c closes the current subpath of canvas c, i.e. adds a line from the last point in the current subpath to the first point, and marks the subpath as closed. Does nothing if the subpath is empty or has a single point, or if the subpath is already closed.

Sourceval moveTo : t -> Point.t -> unit

moveTo c p starts a new subpath in canvas c containing the single point p. If the current subpath is empty, its first point is set to this point, instead of creating a new subpath. Likewise, if the current subpath has a single point, it is simply replaced by the given point.

Sourceval lineTo : t -> Point.t -> unit

lineTo c p adds the point p to the current subpath of canvas c. If the current subpath is empty, this behaves just like moveTo c ~p.

Sourceval arc : t -> center:Point.t -> radius:float -> theta1:float -> theta2:float -> ccw:bool -> unit

arc c ~center ~radius ~theta1 ~theta2 ~ccw adds an arc of the given radius, centered at center, between angle theta1 to theta2 to the current subpath of canvas c. If ccw is true, the arc will be drawn counterclockwise. Note that the last point in the subpath (if such point exists) will be connected to the first point of the arc by a straight line.

Sourceval arcTo : t -> p1:Point.t -> p2:Point.t -> radius:float -> unit

arcTo c ~p1 ~p2 ~radius adds an arc of the given radius using the control points p1 and p2 to the current subpath of canvas c. If the current subpath is empty, this behaves as if moveTo c ~p:p1 was called.

Sourceval quadraticCurveTo : t -> cp:Point.t -> p:Point.t -> unit

quadraticCurveTo c ~cp ~p adds a quadratic Bezier curve using the control point cp and the end point p to the current subpath of canvas c

Sourceval bezierCurveTo : t -> cp1:Point.t -> cp2:Point.t -> p:Point.t -> unit

bezierCurve c ~cp1 ~cp2 ~p adds a cubic Bezier curve using the control points cp1 and cp2 and the end point p to the current subpath of canvas c

Sourceval rect : t -> pos:Point.t -> size:Vector.t -> unit

rect c ~pos ~size adds the rectangle specified by pos and size) to the current subpath of canvas c

Sourceval ellipse : t -> center:Point.t -> radius:Vector.t -> rotation:float -> theta1:float -> theta2:float -> ccw:bool -> unit

ellipse c ~center ~radius ~rotation ~theta1 ~theta2 adds an ellipse with the given parameters to the current subpath of canvas c

Path stroking and filling

Sourceval fill : t -> nonzero:bool -> unit

fill c ~nonzero fills the current subpath of canvas c using the current fill color and the specified fill rule

Sourceval fillPath : t -> Path.t -> nonzero:bool -> unit

fillPath c p ~nonzero fills the path p on canvas c using the current fill style and the specified fill rule

Sourceval stroke : t -> unit

stroke c draws the outline of the current subpath of canvas c using the current stroke color and line width

Sourceval strokePath : t -> Path.t -> unit

strokePath c p draws the outline of the path p on canvas c using the current stroke style and line width

Sourceval clip : t -> nonzero:bool -> unit

clipPath c p ~nonzero intersects the current subpath of c on canvas c's clip region using the specified fill rule

Sourceval clipPath : t -> Path.t -> nonzero:bool -> unit

clipPath c p ~nonzero intersects the filled path p on canvas c's clip region using the specified fill rule

Immediate drawing

Sourceval fillRect : t -> pos:Point.t -> size:Vector.t -> unit

fillRect c ~pos ~size immediatly fills the rectangle specified by pos and size to the canvas c using the current fill color

Sourceval strokeRect : t -> pos:Point.t -> size:Vector.t -> unit

strokeRect c ~pos ~size immediatly draws the outline of the rectangle specified by pos and size to the canvas c using the current stroke color and line width

Sourceval fillText : t -> string -> Point.t -> unit

fillText c text pos immediatly draws the text text at position pos on the canvas c using the current fill color

Sourceval strokeText : t -> string -> Point.t -> unit

strokeText c text pos immediatly draws the outline of text text at position pos on the canvas c using the current stroke color and line width

Sourceval blit : dst:t -> dpos:(int * int) -> src:t -> spos:(int * int) -> size:(int * int) -> unit

blit ~dst ~dpos ~src ~spos ~size copies the area specified by spos and size from canvas src to canvas dst at position dpos. If the given position and size yield an inconsistent area, this has no effect.

Exceptions:

Direct pixel access

Warning: these functions (especially the per-pixel functions) can be slow and are not meant for updating the contents of a canvas in real-time. Better use them on offscreen canvas during loading phases.

Sourceval getPixel : t -> (int * int) -> Color.t

getPixel c pos returns the color of the pixel at position pos in canvas c. If pos is outside the canvas bounds, returns the transparent black color.

Sourceval putPixel : t -> (int * int) -> Color.t -> unit

putPixel c pos col sets the color of the pixel at position pos in canvas c to color col. If pos is outside the canvas bounds, this has no effect.

Sourceval getImageData : t -> pos:(int * int) -> size:(int * int) -> ImageData.t

getImageData c ~pos ~size returns a copy of the pixel data at position pos of size size in canvas c. Any pixel outside the canvas bounds is considered to be transparent black.

Exceptions:

Sourceval putImageData : t -> dpos:(int * int) -> ImageData.t -> spos:(int * int) -> size:(int * int) -> unit

setImageData c ~dpos id ~spos ~size overwrite the pixels at position dpos in canvas c with the provided pixel data starting at position spos and of size size. If the given position and size yield an inconsistent area, this has no effect.

Exceptions:

Sourceval importPNG : t -> pos:(int * int) -> string -> t React.event

importPNG c ~pos filename loads the file filename into canvas c at position pos. The returned event will be triggered once the image is loaded.

Exceptions:

Sourceval exportPNG : t -> string -> unit

exportPNG c filename saves the contents of canvas c to a file with name filename

Exceptions: