Page
Library
Module
Module type
Parameter
Class
Class type
Source
V1.Canvas
SourceCanvas manipulation functions
An abstract type representing a canvas
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.
hash c
returns a unique integer value for canvas c
, which is computed as Hashtbl.hash (Canvas.getId c)
val 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:
Exception.Not_initialized
if Backend.init
was not calledInvalid_argument
if either component of size
is outside the range 1-32767createOffscreen ~size
creates an offscreen canvas of size size
Exceptions:
Exception.Not_initialized
if Backend.init
was not calledInvalid_argument
if either component of size
is outside the range 1-32767createOffscreenFromImageData id
creates an offscreen canvas with the contents of image data id
Exceptions:
Exception.Not_initialized
if Backend.init
was not calledcreateOffscreen filename
creates an offscreen canvas with the contents of PNG file filename
. The returned event will be triggered once the image is loaded.
Exceptions:
Exception.Not_initialized
if Backend.init
was not calledException.Read_png_failed
if the PNG file could not be readshow c
makes the canvas c
visible on screen. Does nothing on offscreen canvases.
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.
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.
setSize c size
sets the size of canvas c
Exceptions:
Invalid_argument
if either component of size
is outside the range 1-32767getPosition c
returns the position of canvas c
. Returns (0, 0) when used on offscreen cavnas.
setPosition c pos
sets the position of canvas c
. Does nothing on offscreen canvases.
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
transform c t
apply the given arbitrary transformation to the current transformation matrix of canvas c
translate c vec
apply the given translation transform to the current transformation matrix of canvas c
scale c vec
apply the given scale transform to the current transformation matrix of canvas c
shear c vec
apply the given shear transform to the current transformation matrix of canvas c
rotate c theta
apply the given rotation transform to the current transformation matrix of canvas c
setLineWidth c w
sets the current line width of canvas c
to w
setLineJoin c j
sets the current line join type of canvasc
to j
setLineJoin c j
sets the current line cap type of canvasc
to j
getMiterLimit c m
sets the current miter limit of canvas c
to m
setLineDashOffset c t
sets the current line offset of c
to t
setLineDash c t
sets the current line dash pattern of c
to t
getStrokeColor c
returns the current stroke color of canvas c
setStrokeColor c col
sets the current stroke color of canvas c
to col
setStrokeGradient c grad
sets the current stroke style of canvas c
to the gradient grad
setStrokePattern c pat
sets the current stroke style of canvas c
to the pattern pat
getStrokeStyle c
returns the current stroke style of canvas c
setStrokeStyle c style
sets the current stroke style of canvas c
to style style
setFillColor c col
sets the current fill color of canvas c
to col
setFillGradient c grad
sets the current fill style of canvas c
to the gradient grad
setFillPattern c pat
sets the current fill style of canvas c
to the pattern pat
setFillStyle c style
sets the current fill style of canvas c
to style style
setGlobalAlpha c a
sets the global alpha value of canvasc
to a
getGlobalCompositeOperation c
returns the global composite or blending operation of canvasc
setGlobalCompositeOperation c o
sets the global composite or blending operation of canvasc
to o
setShadowColor c col
sets the canvas c
's shadow color to col
setShadowBlur c b
sets the shadow blur radius of canvas c
to b
setShadowOffset c
returns the offset of the shadows drawn in c
setShadowOffset c o
sets the offset of the shadows drawn in c
to o
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
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.
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.
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
.
val 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.
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.
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
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
rect c ~pos ~size
adds the rectangle specified by pos
and size
) to the current subpath of canvas c
val 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
fill c ~nonzero
fills the current subpath of canvas c
using the current fill color and the specified fill rule
fillPath c p ~nonzero
fills the path p
on canvas c
using the current fill style and the specified fill rule
stroke c
draws the outline of the current subpath of canvas c
using the current stroke color and line width
strokePath c p
draws the outline of the path p
on canvas c
using the current stroke style and line width
clipPath c p ~nonzero
intersects the current subpath of c
on canvas c
's clip region using the specified fill rule
clipPath c p ~nonzero
intersects the filled path p
on canvas c
's clip region using the specified fill rule
fillRect c ~pos ~size
immediatly fills the rectangle specified by pos
and size
to the canvas c
using the current fill color
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
fillText c text pos
immediatly draws the text text
at position pos
on the canvas c
using the current fill color
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
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:
Invalid_argument
if either component of size
is outside the range 1-32767Warning: 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.
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.
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.
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:
Invalid_argument
if either component of size
is outside the range 1-32767val 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:
Invalid_argument
if either component of size
is outside the range 1-32767importPNG 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:
Exception.Read_png_failed
if the PNG file could not be readexportPNG c filename
saves the contents of canvas c
to a file with name filename
Exceptions:
Exception.Write_png_failed
if the PNG file could not be written