litecanvas 0.91.0 → 0.92.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,594 @@
1
+ import './types'
2
+
3
+ export default function litecanvas(settings?: LitecanvasOptions): LitecanvasInstance
4
+
5
+ declare global {
6
+ function litecanvas(settings?: LitecanvasOptions): LitecanvasInstance
7
+
8
+ /** The game screen width */
9
+ var W: number
10
+ /** The game screen height */
11
+ var H: number
12
+ /** the amount of time (in seconds) since the game started */
13
+ var T: number
14
+ /** The current mouse's horizontal (X) position or -1 (if the mouse was not used or detected) */
15
+ var MX: number
16
+ /** The current mouse's vertical (Y) position or -1 (if the mouse was not used or detected) */
17
+ var MY: number
18
+
19
+ /** MATH API */
20
+ /**
21
+ * The value of the mathematical constant PI (π).
22
+ * Approximately 3.14159
23
+ */
24
+ var PI: number
25
+ /**
26
+ * Twice the value of the mathematical constant PI (π).
27
+ * Approximately 6.28318
28
+ *
29
+ * Note: TWO_PI radians equals 360°, PI radians equals 180°,
30
+ * HALF_PI radians equals 90°, and HALF_PI/2 radians equals 45°.
31
+ */
32
+ var TWO_PI: number
33
+ /**
34
+ * Half the value of the mathematical constant PI (π).
35
+ * Approximately 1.57079
36
+ */
37
+ var HALF_PI: number
38
+ /**
39
+ * Calculates a linear (interpolation) value over t%.
40
+ *
41
+ * @param start
42
+ * @param end
43
+ * @param t The progress in percentage, where 0 = 0% and 1 = 100%.
44
+ * @returns The unterpolated value
45
+ * @tutorial https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
46
+ */
47
+ function lerp(start: number, end: number, t: number): number
48
+ /**
49
+ * Convert degrees to radians
50
+ *
51
+ * @param degs
52
+ * @returns the value in radians
53
+ */
54
+ function deg2rad(degs: number): number
55
+ /**
56
+ * Convert radians to degrees
57
+ *
58
+ * @param rads
59
+ * @returns the value in degrees
60
+ */
61
+ function rad2deg(rads: number): number
62
+ /**
63
+ * Returns the rounded value of an number to optional precision (number of digits after the decimal point).
64
+ *
65
+ * @param n number to round.
66
+ * @param [precision] number of decimal digits to round to, default is 0.
67
+ * @returns the rounded number.
68
+ */
69
+ function round(n: number, precision?: number): number
70
+ /**
71
+ * Constrains a number between `min` and `max`.
72
+ *
73
+ * @param value
74
+ * @param min
75
+ * @param max
76
+ * @returns
77
+ */
78
+ function clamp(value: number, min: number, max: number): number
79
+ /**
80
+ * Wraps a number between `min` (inclusive) and `max` (exclusive).
81
+ *
82
+ * @param value
83
+ * @param min
84
+ * @param max
85
+ * @returns the wrapped number
86
+ */
87
+ function wrap(value: number, min: number, max: number): number
88
+ /**
89
+ * Re-maps a number from one range to another.
90
+ *
91
+ * @param value the value to be remapped.
92
+ * @param start1 lower bound of the value's current range.
93
+ * @param stop1 upper bound of the value's current range.
94
+ * @param start2 lower bound of the value's target range.
95
+ * @param stop2 upper bound of the value's target range.
96
+ * @param [withinBounds=false] constrain the value to the newly mapped range
97
+ * @returns the remapped number
98
+ */
99
+ function map(
100
+ value: number,
101
+ start1: number,
102
+ stop1: number,
103
+ start2: number,
104
+ stop2: number,
105
+ withinBounds?: boolean
106
+ ): number
107
+ /**
108
+ * Maps a number from one range to a value between 0 and 1.
109
+ * Identical to `map(value, min, max, 0, 1)`.
110
+ * Note: Numbers outside the range are not clamped to 0 and 1.
111
+ *
112
+ * @param value
113
+ * @param start
114
+ * @param stop
115
+ * @returns the normalized number.
116
+ */
117
+ function norm(value: number, start: number, stop: number): number
118
+ /**
119
+ * Interpolate between 2 values using a periodic function.
120
+ *
121
+ * @param from - the lower bound
122
+ * @param to - the higher bound
123
+ * @param t - the value passed to the periodic function
124
+ * @param fn= - the periodic function (which default to `Math.sin`)
125
+ */
126
+ function wave(from: number, to: number, t: number, fn?: (n: number) => number): number
127
+ /**
128
+ * Returns the sine of a number in radians
129
+ */
130
+ function sin(n: number): number
131
+ /**
132
+ * Returns the cosine of a number in radians
133
+ */
134
+ function cos(n: number): number
135
+ /**
136
+ * Returns the angle in the plane (in radians) between the positive x-axis and the ray from (0, 0) to the point (x, y)
137
+ */
138
+ function atan2(y: number, x: number): number
139
+ /**
140
+ * Returns the square root of the sum of squares of its arguments.
141
+ */
142
+ function hypot(...ns: number[]): number
143
+ /**
144
+ * Returns the tangent of a number in radians.
145
+ */
146
+ function tan(n: number): number
147
+ /**
148
+ * Returns the absolute value of a number.
149
+ */
150
+ function abs(n: number): number
151
+ /**
152
+ * Always rounds up and returns the smallest integer greater than or equal to a given number.
153
+ */
154
+ function ceil(n: number): number
155
+ /**
156
+ * Always rounds down and returns the largest integer less than or equal to a given number.
157
+ */
158
+ function floor(n: number): number
159
+ /**
160
+ * Returns the integer part of a number by removing any fractional digits.
161
+ */
162
+ function trunc(n: number): number
163
+ /**
164
+ * Returns the smallest of the numbers given as input parameters, or `Infinity` if there are no parameters.
165
+ */
166
+ function min(...ns: number[]): number
167
+ /**
168
+ * Returns the largest of the numbers given as input parameters, or `-Infinity` if there are no parameters.
169
+ */
170
+ function max(...ns: number[]): number
171
+ /**
172
+ * Returns the value of a base raised to a power.
173
+ */
174
+ function pow(base: number, exponent: number): number
175
+ /**
176
+ * Returns the square root of a number.
177
+ */
178
+ function sqrt(n: number): number
179
+ /**
180
+ * Returns 1 or -1, indicating the sign of the number passed as argument.
181
+ * If the input is 0 or -0, it will be returned as-is.
182
+ */
183
+ function sign(n: number): number
184
+ /**
185
+ * Returns the Euler's number raised to the power of a number.
186
+ */
187
+ function exp(exponent: number): number
188
+
189
+ /** RNG API */
190
+ /**
191
+ * Generates a pseudorandom float between min (inclusive) and max (exclusive)
192
+ *
193
+ * @param [min=0.0]
194
+ * @param [max=1.0]
195
+ * @returns the random number
196
+ */
197
+ function rand(min?: number, max?: number): number
198
+ /**
199
+ * Generates a pseudorandom integer between min (inclusive) and max (inclusive)
200
+ *
201
+ * @param [min=0]
202
+ * @param [max=1]
203
+ * @returns the random number
204
+ */
205
+ function randi(min?: number, max?: number): number
206
+ /**
207
+ * Initializes the random number generator with an explicit seed value.
208
+ *
209
+ * Note: The seed should be a integer number greater than or equal to zero.
210
+ *
211
+ * @param value
212
+ */
213
+ function rseed(value: number): void
214
+
215
+ /** BASIC GRAPHICS API */
216
+ /**
217
+ * Clear the game screen with an optional color
218
+ *
219
+ * @param color The background color index or `null`
220
+ */
221
+ function cls(color: number | null): void
222
+
223
+ /**
224
+ * Draw a rectangle outline
225
+ *
226
+ * @param x
227
+ * @param y
228
+ * @param width
229
+ * @param height
230
+ * @param [color=0] the color index
231
+ * @param [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
232
+ *
233
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
234
+ */
235
+ function rect(
236
+ x: number,
237
+ y: number,
238
+ width: number,
239
+ height: number,
240
+ color?: number,
241
+ radii?: number | number[]
242
+ ): void
243
+ /**
244
+ * Draw a color-filled rectangle
245
+ *
246
+ * @param x
247
+ * @param y
248
+ * @param width
249
+ * @param height
250
+ * @param [color=0] the color index
251
+ * @param [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
252
+ */
253
+ function rectfill(
254
+ x: number,
255
+ y: number,
256
+ width: number,
257
+ height: number,
258
+ color?: number,
259
+ radii?: number | number[]
260
+ ): void
261
+ /**
262
+ * Draw a circle outline
263
+ *
264
+ * @param x
265
+ * @param y
266
+ * @param radius
267
+ * @param [color=0] the color index
268
+ */
269
+ function circ(x: number, y: number, radius: number, color?: number): void
270
+ /**
271
+ * Draw a color-filled circle
272
+ *
273
+ * @param x
274
+ * @param y
275
+ * @param radius
276
+ * @param [color=0] the color index
277
+ */
278
+ function circfill(x: number, y: number, radius: number, color?: number): void
279
+ /**
280
+ * Draw a ellipse outline
281
+ *
282
+ * @param x
283
+ * @param y
284
+ * @param radiusX
285
+ * @param radiusY
286
+ * @param [color=0] the color index
287
+ */
288
+ function oval(x: number, y: number, radiusX: number, radiusY: number, color?: number): void
289
+ /**
290
+ * Draw a color-filled ellipse
291
+ *
292
+ * @param x
293
+ * @param y
294
+ * @param radiusX
295
+ * @param radiusY
296
+ * @param [color=0] the color index
297
+ */
298
+ function ovalfill(x: number, y: number, radiusX: number, radiusY: number, color?: number): void
299
+ /**
300
+ * Draw a line
301
+ *
302
+ * @param x1
303
+ * @param y1
304
+ * @param x2
305
+ * @param y2
306
+ * @param [color=0] the color index
307
+ */
308
+ function line(x1: number, y1: number, x2: number, y2: number, color?: number): void
309
+ /**
310
+ * Sets the thickness of lines
311
+ *
312
+ * @param value
313
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
314
+ */
315
+ function linewidth(value: number): void
316
+ /**
317
+ * Sets the line dash pattern used when drawing lines
318
+ *
319
+ * @param segments the line dash pattern
320
+ * @param [offset=0] the line dash offset, or "phase".
321
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash
322
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
323
+ */
324
+ function linedash(segments: number[], offset?: number): void
325
+
326
+ /** TEXT RENDERING API */
327
+ /**
328
+ * Draw text
329
+ *
330
+ * @param x
331
+ * @param y
332
+ * @param message the text message
333
+ * @param [color=3] the color index
334
+ * @param [fontStyle="normal"] can be "normal" (default), "italic" and/or "bold"
335
+ */
336
+ function text(x: number, y: number, message: string, color?: number, fontStyle?: string): void
337
+ /**
338
+ * Set the font family
339
+ *
340
+ * @param fontFamily
341
+ */
342
+ function textfont(fontFamily: string): void
343
+ /**
344
+ * Set the font size
345
+ *
346
+ * @param size
347
+ */
348
+ function textsize(size: number): void
349
+ /**
350
+ * Sets the alignment used when drawing texts
351
+ *
352
+ * @param align the horizontal alignment. Possible values: "left", "right", "center", "start" or "end"
353
+ * @param baseline the vertical alignment. Possible values: "top", "bottom", "middle", "hanging" or "ideographic"
354
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline
355
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign
356
+ */
357
+ function textalign(align: CanvasTextAlign, baseline: CanvasTextBaseline): void
358
+
359
+ /** IMAGE GRAPHICS API */
360
+ /**
361
+ * Draw an image
362
+ *
363
+ * @param x
364
+ * @param y
365
+ * @param source
366
+ */
367
+ function image(
368
+ x: number,
369
+ y: number,
370
+ source: OffscreenCanvas | HTMLImageElement | HTMLCanvasElement
371
+ ): void
372
+ /**
373
+ * Draw in an OffscreenCanvas and returns its image.
374
+ *
375
+ * @param width
376
+ * @param height
377
+ * @param drawing
378
+ * @param [options]
379
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
380
+ */
381
+ function paint(
382
+ width: number,
383
+ height: number,
384
+ drawing: string[] | drawCallback,
385
+ options?: {
386
+ scale?: number
387
+ canvas?: OffscreenCanvas
388
+ }
389
+ ): ImageBitmap
390
+
391
+ /** ADVANCED GRAPHICS API */
392
+ /**
393
+ * Get or set the canvas context 2D
394
+ *
395
+ * @param [context] an new canvas context
396
+ * @returns the current canvas context
397
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
398
+ */
399
+ function ctx(
400
+ context?: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D
401
+ ): CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D
402
+ /**
403
+ * saves the current drawing style settings and transformations
404
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
405
+ */
406
+ function push(): void
407
+ /**
408
+ * restores the drawing style settings and transformations
409
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/restore
410
+ */
411
+ function pop(): void
412
+ /**
413
+ * Adds a translation transformation to the current matrix
414
+ *
415
+ * @param x
416
+ * @param y
417
+ */
418
+ function translate(x: number, y: number): void
419
+ /**
420
+ * Adds a scaling transformation to the canvas units horizontally and/or vertically.
421
+ *
422
+ * @param x
423
+ * @param [y]
424
+ */
425
+ function scale(x: number, y?: number): void
426
+ /**
427
+ * Adds a rotation to the transformation matrix
428
+ *
429
+ * @param radians
430
+ */
431
+ function rotate(radians: number): void
432
+ /**
433
+ * Sets the alpha (transparency) value to apply when drawing new shapes and images
434
+ *
435
+ * @param value float from 0 to 1 (e.g: 0.5 = 50% transparent)
436
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
437
+ */
438
+ function alpha(value: number): void
439
+ /**
440
+ * Fills the current path with a given color.
441
+ *
442
+ * @param color
443
+ */
444
+ function fill(color: number): void
445
+ /**
446
+ * Outlines the current path with a given color.
447
+ *
448
+ * @param color
449
+ */
450
+ function stroke(color: number): void
451
+ /**
452
+ * Turns a path (in the callback) into the current clipping region.
453
+ *
454
+ * @param callback
455
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
456
+ */
457
+ function clip(callback: clipCallback): void
458
+
459
+ /** SOUND API */
460
+ /**
461
+ * Play a sound effects using ZzFX library.
462
+ * If the first argument is omitted, plays an default sound.
463
+ *
464
+ * @param [zzfxParams] a ZzFX array of params
465
+ * @param [pitchSlide=0] a value to increment/decrement the pitch
466
+ * @param [volumeFactor=1] the volume factor
467
+ * @returns The sound that was played or `false`
468
+ *
469
+ * @see https://github.com/KilledByAPixel/ZzFX
470
+ */
471
+ function sfx(
472
+ zzfxParams?: number[],
473
+ pitchSlide?: number,
474
+ volumeFactor?: number
475
+ ): number[] | boolean
476
+ /**
477
+ * Set the ZzFX's global volume factor.
478
+ * Note: use 0 to mute all sound effects.
479
+ *
480
+ * @param value
481
+ */
482
+ function volume(value: number): void
483
+
484
+ /** UTILS API */
485
+ /**
486
+ * Checks if a which key is pressed on the keyboard.
487
+ * Note: use `iskeydown()` to check for any key pressed.
488
+ *
489
+ * @param key
490
+ * @returns `true` if the which key is down
491
+ */
492
+ function iskeydown(key: string): boolean
493
+ /**
494
+ * Checks if a which key just got pressed on the keyboard.
495
+ * Note: use `iskeypressed()` to check for any key.
496
+ *
497
+ * @param [key]
498
+ * @returns `true` if the which key was pressed
499
+ */
500
+ function iskeypressed(key: string): boolean
501
+
502
+ /** PLUGINS API */
503
+ /**
504
+ * Returns the canvas
505
+ */
506
+ function canvas(): HTMLCanvasElement
507
+ /**
508
+ * Prepares a plugin to be loaded
509
+ *
510
+ * @param callback
511
+ */
512
+ function use(callback: pluginCallback): void
513
+ /**
514
+ * Add a game loop event listener
515
+ *
516
+ * @param event The game event type
517
+ * @param callback the function that is called when the event occurs
518
+ * @returns a function to remove the listener
519
+ */
520
+ function listen(event: string, callback: Function): Function
521
+ /**
522
+ * Call all listeners attached to a game event
523
+ *
524
+ * @param event The game event type
525
+ * @param [arg1] any data to be passed over the listeners
526
+ * @param [arg2] any data to be passed over the listeners
527
+ * @param [arg3] any data to be passed over the listeners
528
+ * @param [arg4] any data to be passed over the listeners
529
+ */
530
+ function emit(event: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any): void
531
+ /**
532
+ * Set or reset the color palette
533
+ *
534
+ * @param [colors]
535
+ */
536
+ function pal(colors?: string[]): void
537
+ /**
538
+ * Define or update a instance property
539
+ *
540
+ * @param key
541
+ * @param value
542
+ */
543
+ function def(key: string, value: any): void
544
+ /**
545
+ * The scale of the game's delta time (dt).
546
+ * Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
547
+ * A value of 0 freezes time and is effectively equivalent to pausing.
548
+ *
549
+ * @param value
550
+ */
551
+ function timescale(value: number): void
552
+ /**
553
+ * Set the target FPS (frames per second).
554
+ *
555
+ * @param fps
556
+ */
557
+ function framerate(fps: number): void
558
+ /**
559
+ * Returns information about that engine instance.
560
+ *
561
+ * - n = 0: the settings passed to that instance
562
+ * - n = 1: returns true if the "init" event has already been emitted
563
+ * - n = 2: the current delta time (dt)
564
+ * - n = 3: the current canvas element scale (not the context 2D scale)
565
+ * - n = 4: the attached event callbacks
566
+ * - n = 5: the current color palette
567
+ * - n = 6: the default sound used by `sfx()`
568
+ * - n = 7: the current time scale
569
+ * - n = 8: the current volume used by ZzFX
570
+ * - n = 9: the current RNG state
571
+ * - n = 10: the current font size
572
+ * - n = 11: the current font family
573
+ * - n = *any other value*: probably returns undefined
574
+ *
575
+ * @param n
576
+ */
577
+ function stat(n: number): any
578
+ /**
579
+ * Shutdown the litecanvas instance and remove all event listeners.
580
+ */
581
+ function quit(): void
582
+ /**
583
+ * Pauses the engine loop (update & draw).
584
+ */
585
+ function pause(): void
586
+ /**
587
+ * Resumes (if paused) the engine loop.
588
+ */
589
+ function resume(): void
590
+ /**
591
+ * Returns `true` if the engine loop is paused.
592
+ */
593
+ function paused(): boolean
594
+ }