1Graphics(3) OCaml library Graphics(3)
2
3
4
6 Graphics - Machine-independent graphics primitives.
7
9 Module Graphics
10
12 Module Graphics
13 : sig end
14
15
16 Machine-independent graphics primitives.
17
18
19
20
21
22
23 exception Graphic_failure of string
24
25
26 Raised by the functions below when they encounter an error.
27
28
29
30
31 === Initializations ===
32
33
34 val open_graph : string -> unit
35
36 Show the graphics window or switch the screen to graphic mode. The
37 graphics window is cleared and the current point is set to (0, 0). The
38 string argument is used to pass optional information on the desired
39 graphics mode, the graphics window size, and so on. Its interpretation
40 is implementation-dependent. If the empty string is given, a sensible
41 default is selected.
42
43
44
45 val close_graph : unit -> unit
46
47 Delete the graphics window or switch the screen back to text mode.
48
49
50
51 val set_window_title : string -> unit
52
53 Set the title of the graphics window.
54
55
56
57 val resize_window : int -> int -> unit
58
59 Resize and erase the graphics window.
60
61
62
63 val clear_graph : unit -> unit
64
65 Erase the graphics window.
66
67
68
69 val size_x : unit -> int
70
71 See Graphics.size_y .
72
73
74
75 val size_y : unit -> int
76
77 Return the size of the graphics window. Coordinates of the screen pix‐
78 els range over 0 .. size_x()-1 and 0 .. size_y()-1 . Drawings outside
79 of this rectangle are clipped, without causing an error. The origin
80 (0,0) is at the lower left corner. Some implementation (e.g. X Win‐
81 dows) represent coordinates by 16-bit integers, hence wrong clipping
82 may occur with coordinates below -32768 or above 32676 .
83
84
85
86
87 === Colors ===
88
89
90 type color = int
91
92
93 A color is specified by its R, G, B components. Each component is in
94 the range 0..255 . The three components are packed in an int : 0xRRGGBB
95 , where RR are the two hexadecimal digits for the red component, GG for
96 the green component, BB for the blue component.
97
98
99
100 val rgb : int -> int -> int -> color
101
102
103 rgb r g b returns the integer encoding the color with red component r ,
104 green component g , and blue component b . r , g and b are in the
105 range 0..255 .
106
107
108
109 val set_color : color -> unit
110
111 Set the current drawing color.
112
113
114
115 val background : color
116
117 See Graphics.foreground .
118
119
120
121 val foreground : color
122
123 Default background and foreground colors (usually, either black fore‐
124 ground on a white background or white foreground on a black back‐
125 ground). Graphics.clear_graph fills the screen with the background
126 color. The initial drawing color is foreground .
127
128
129
130
131 === Some predefined colors ===
132
133
134 val black : color
135
136
137
138
139 val white : color
140
141
142
143
144 val red : color
145
146
147
148
149 val green : color
150
151
152
153
154 val blue : color
155
156
157
158
159 val yellow : color
160
161
162
163
164 val cyan : color
165
166
167
168
169 val magenta : color
170
171
172
173
174
175 === Point and line drawing ===
176
177
178 val plot : int -> int -> unit
179
180 Plot the given point with the current drawing color.
181
182
183
184 val plots : (int * int) array -> unit
185
186 Plot the given points with the current drawing color.
187
188
189
190 val point_color : int -> int -> color
191
192 Return the color of the given point in the backing store (see "Double
193 buffering" below).
194
195
196
197 val moveto : int -> int -> unit
198
199 Position the current point.
200
201
202
203 val rmoveto : int -> int -> unit
204
205
206 rmoveto dx dy translates the current point by the given vector.
207
208
209
210 val current_x : unit -> int
211
212 Return the abscissa of the current point.
213
214
215
216 val current_y : unit -> int
217
218 Return the ordinate of the current point.
219
220
221
222 val current_point : unit -> int * int
223
224 Return the position of the current point.
225
226
227
228 val lineto : int -> int -> unit
229
230 Draw a line with endpoints the current point and the given point, and
231 move the current point to the given point.
232
233
234
235 val rlineto : int -> int -> unit
236
237 Draw a line with endpoints the current point and the current point
238 translated of the given vector, and move the current point to this
239 point.
240
241
242
243 val curveto : int * int -> int * int -> int * int -> unit
244
245
246 curveto b c d draws a cubic Bezier curve starting from the current
247 point to point d , with control points b and c , and moves the current
248 point to d .
249
250
251
252 val draw_rect : int -> int -> int -> int -> unit
253
254
255 draw_rect x y w h draws the rectangle with lower left corner at x,y ,
256 width w and height h . The current point is unchanged. Raise
257 Invalid_argument if w or h is negative.
258
259
260
261 val draw_poly_line : (int * int) array -> unit
262
263
264 draw_poly_line points draws the line that joins the points given by the
265 array argument. The array contains the coordinates of the vertices of
266 the polygonal line, which need not be closed. The current point is
267 unchanged.
268
269
270
271 val draw_poly : (int * int) array -> unit
272
273
274 draw_poly polygon draws the given polygon. The array contains the
275 coordinates of the vertices of the polygon. The current point is
276 unchanged.
277
278
279
280 val draw_segments : (int * int * int * int) array -> unit
281
282
283 draw_segments segments draws the segments given in the array argument.
284 Each segment is specified as a quadruple (x0, y0, x1, y1) where (x0,
285 y0) and (x1, y1) are the coordinates of the end points of the segment.
286 The current point is unchanged.
287
288
289
290 val draw_arc : int -> int -> int -> int -> int -> int -> unit
291
292
293 draw_arc x y rx ry a1 a2 draws an elliptical arc with center x,y , hor‐
294 izontal radius rx , vertical radius ry , from angle a1 to angle a2 (in
295 degrees). The current point is unchanged. Raise Invalid_argument if rx
296 or ry is negative.
297
298
299
300 val draw_ellipse : int -> int -> int -> int -> unit
301
302
303 draw_ellipse x y rx ry draws an ellipse with center x,y , horizontal
304 radius rx and vertical radius ry . The current point is unchanged.
305 Raise Invalid_argument if rx or ry is negative.
306
307
308
309 val draw_circle : int -> int -> int -> unit
310
311
312 draw_circle x y r draws a circle with center x,y and radius r . The
313 current point is unchanged. Raise Invalid_argument if r is negative.
314
315
316
317 val set_line_width : int -> unit
318
319 Set the width of points and lines drawn with the functions above.
320 Under X Windows, set_line_width 0 selects a width of 1 pixel and a
321 faster, but less precise drawing algorithm than the one used when
322 set_line_width 1 is specified. Raise Invalid_argument if the argument
323 is negative.
324
325
326
327
328 === Text drawing ===
329
330
331 val draw_char : char -> unit
332
333 See Graphics.draw_string .
334
335
336
337 val draw_string : string -> unit
338
339 Draw a character or a character string with lower left corner at cur‐
340 rent position. After drawing, the current position is set to the lower
341 right corner of the text drawn.
342
343
344
345 val set_font : string -> unit
346
347 Set the font used for drawing text. The interpretation of the argument
348 to set_font is implementation-dependent.
349
350
351
352 val set_text_size : int -> unit
353
354 Set the character size used for drawing text. The interpretation of
355 the argument to set_text_size is implementation-dependent.
356
357
358
359 val text_size : string -> int * int
360
361 Return the dimensions of the given text, if it were drawn with the cur‐
362 rent font and size.
363
364
365
366
367 === Filling ===
368
369
370 val fill_rect : int -> int -> int -> int -> unit
371
372
373 fill_rect x y w h fills the rectangle with lower left corner at x,y ,
374 width w and height h , with the current color. Raise Invalid_argument
375 if w or h is negative.
376
377
378
379 val fill_poly : (int * int) array -> unit
380
381 Fill the given polygon with the current color. The array contains the
382 coordinates of the vertices of the polygon.
383
384
385
386 val fill_arc : int -> int -> int -> int -> int -> int -> unit
387
388 Fill an elliptical pie slice with the current color. The parameters are
389 the same as for Graphics.draw_arc .
390
391
392
393 val fill_ellipse : int -> int -> int -> int -> unit
394
395 Fill an ellipse with the current color. The parameters are the same as
396 for Graphics.draw_ellipse .
397
398
399
400 val fill_circle : int -> int -> int -> unit
401
402 Fill a circle with the current color. The parameters are the same as
403 for Graphics.draw_circle .
404
405
406
407
408 === Images ===
409
410
411 type image
412
413
414 The abstract type for images, in internal representation. Externally,
415 images are represented as matrices of colors.
416
417
418
419 val transp : color
420
421 In matrices of colors, this color represent a 'transparent' point: when
422 drawing the corresponding image, all pixels on the screen corresponding
423 to a transparent pixel in the image will not be modified, while other
424 points will be set to the color of the corresponding point in the
425 image. This allows superimposing an image over an existing background.
426
427
428
429 val make_image : color array array -> image
430
431 Convert the given color matrix to an image. Each sub-array represents
432 one horizontal line. All sub-arrays must have the same length; other‐
433 wise, exception Graphic_failure is raised.
434
435
436
437 val dump_image : image -> color array array
438
439 Convert an image to a color matrix.
440
441
442
443 val draw_image : image -> int -> int -> unit
444
445 Draw the given image with lower left corner at the given point.
446
447
448
449 val get_image : int -> int -> int -> int -> image
450
451 Capture the contents of a rectangle on the screen as an image. The
452 parameters are the same as for Graphics.fill_rect .
453
454
455
456 val create_image : int -> int -> image
457
458
459 create_image w h returns a new image w pixels wide and h pixels tall,
460 to be used in conjunction with blit_image . The initial image contents
461 are random, except that no point is transparent.
462
463
464
465 val blit_image : image -> int -> int -> unit
466
467
468 blit_image img x y copies screen pixels into the image img , modifying
469 img in-place. The pixels copied are those inside the rectangle with
470 lower left corner at x,y , and width and height equal to those of the
471 image. Pixels that were transparent in img are left unchanged.
472
473
474
475
476 === Mouse and keyboard events ===
477
478
479 type status = {
480 mouse_x : int ; (* X coordinate of the mouse
481 *)
482 mouse_y : int ; (* Y coordinate of the mouse
483 *)
484 button : bool ; (* true if a mouse button is pressed
485 *)
486 keypressed : bool ; (* true if a key has been pressed
487 *)
488 key : char ; (* the character for the key pressed
489 *)
490 }
491
492
493 To report events.
494
495
496 type event =
497 | Button_down (* A mouse button is pressed
498 *)
499 | Button_up (* A mouse button is released
500 *)
501 | Key_pressed (* A key is pressed
502 *)
503 | Mouse_motion (* The mouse is moved
504 *)
505 | Poll (* Don't wait; return immediately
506 *)
507
508
509 To specify events to wait for.
510
511
512
513 val wait_next_event : event list -> status
514
515 Wait until one of the events specified in the given event list occurs,
516 and return the status of the mouse and keyboard at that time. If Poll
517 is given in the event list, return immediately with the current status.
518 If the mouse cursor is outside of the graphics window, the mouse_x and
519 mouse_y fields of the event are outside the range 0..size_x()-1,
520 0..size_y()-1 . Keypresses are queued, and dequeued one by one when the
521 Key_pressed event is specified and the Poll event is not specified.
522
523
524
525 val loop_at_exit : event list -> (status -> unit) -> unit
526
527 Loop before exiting the program, the list given as argument is the list
528 of handlers and the events on which these handlers are called. To exit
529 cleanly the loop, the handler should raise Exit. Any other exception
530 will be propagated outside of the loop.
531
532
533 Since 4.01
534
535
536
537
538 === Mouse and keyboard polling ===
539
540
541 val mouse_pos : unit -> int * int
542
543 Return the position of the mouse cursor, relative to the graphics win‐
544 dow. If the mouse cursor is outside of the graphics window, mouse_pos()
545 returns a point outside of the range 0..size_x()-1, 0..size_y()-1 .
546
547
548
549 val button_down : unit -> bool
550
551 Return true if the mouse button is pressed, false otherwise.
552
553
554
555 val read_key : unit -> char
556
557 Wait for a key to be pressed, and return the corresponding character.
558 Keypresses are queued.
559
560
561
562 val key_pressed : unit -> bool
563
564 Return true if a keypress is available; that is, if read_key would not
565 block.
566
567
568
569
570 === Sound ===
571
572
573 val sound : int -> int -> unit
574
575
576 sound freq dur plays a sound at frequency freq (in hertz) for a dura‐
577 tion dur (in milliseconds).
578
579
580
581
582 === Double buffering ===
583
584
585 val auto_synchronize : bool -> unit
586
587 By default, drawing takes place both on the window displayed on screen,
588 and in a memory area (the 'backing store'). The backing store image is
589 used to re-paint the on-screen window when necessary.
590
591 To avoid flicker during animations, it is possible to turn off
592 on-screen drawing, perform a number of drawing operations in the back‐
593 ing store only, then refresh the on-screen window explicitly.
594
595
596 auto_synchronize false turns on-screen drawing off. All subsequent
597 drawing commands are performed on the backing store only.
598
599
600 auto_synchronize true refreshes the on-screen window from the backing
601 store (as per synchronize ), then turns on-screen drawing back on. All
602 subsequent drawing commands are performed both on screen and in the
603 backing store.
604
605 The default drawing mode corresponds to auto_synchronize true .
606
607
608
609 val synchronize : unit -> unit
610
611 Synchronize the backing store and the on-screen window, by copying the
612 contents of the backing store onto the graphics window.
613
614
615
616 val display_mode : bool -> unit
617
618 Set display mode on or off. When turned on, drawings are done in the
619 graphics window; when turned off, drawings do not affect the graphics
620 window. This occurs independently of drawing into the backing store
621 (see the function Graphics.remember_mode below). Default display mode
622 is on.
623
624
625
626 val remember_mode : bool -> unit
627
628 Set remember mode on or off. When turned on, drawings are done in the
629 backing store; when turned off, the backing store is unaffected by
630 drawings. This occurs independently of drawing onto the graphics win‐
631 dow (see the function Graphics.display_mode above). Default remember
632 mode is on.
633
634
635
636
637
638OCamldoc 2019-02-02 Graphics(3)