fruta 0.0.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.
- package/DOCUMENTATION.MD +874 -0
- package/README.md +54 -0
- package/dist/main.js +1 -0
- package/index.html +9 -0
- package/package.json +37 -0
- package/settings.json +6 -0
- package/src/core/create/_fontCreator.js +11 -0
- package/src/core/create/_saveOrRestore.js +22 -0
- package/src/core/create/_shapeCreator.js +20 -0
- package/src/core/create/fontCreatorMixin.js +167 -0
- package/src/core/create/shapeCreatorMixin.js +656 -0
- package/src/core/fruta.js +22 -0
- package/src/core/game/game.js +30 -0
- package/src/core/game/scene.js +29 -0
- package/src/core/game/tick.js +42 -0
- package/src/core/utils/logStyle.js +8 -0
- package/src/core/utils/utils.js +0 -0
- package/src/methods/constants.js +0 -0
- package/src/methods/creator/_scene.js +0 -0
- package/src/methods/creator/creator.js +0 -0
- package/webpack.config.js +47 -0
package/DOCUMENTATION.MD
ADDED
|
@@ -0,0 +1,874 @@
|
|
|
1
|
+
### Methods
|
|
2
|
+
|
|
3
|
+
We created an additional layer on top of Canvas functionalities to enable method nesting and perform actions in a single line, as well as to encapsulate shapes and behaviors in a simpler and more organized way.
|
|
4
|
+
|
|
5
|
+
Next, I will go through each method to which we have applied this encapsulation approach.
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
/**
|
|
9
|
+
/* SHAPE - METHODS */
|
|
10
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillStyle
|
|
11
|
+
* @description property of the Canvas 2D API specifies the color, gradient,
|
|
12
|
+
* or pattern to use inside shapes. The default style is #000 (black
|
|
13
|
+
*
|
|
14
|
+
* @param {Color} style - CanvasGradient | CanvasPattern
|
|
15
|
+
*
|
|
16
|
+
* @example - "red" | rgb(25,25,25)
|
|
17
|
+
*
|
|
18
|
+
* @return ShapeCreator class
|
|
19
|
+
*/
|
|
20
|
+
fillStyleShape(style) {
|
|
21
|
+
this.context.fillStyle = style
|
|
22
|
+
return this
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @alert
|
|
27
|
+
* I think we should separe this methods, but I'm no pretty sure
|
|
28
|
+
*
|
|
29
|
+
* @param {Array<numbers>} fill - create a rectangle by x, y, width, height
|
|
30
|
+
* @param {Array<numbers>} stroke draw a stroke rectangle x, y, width, height
|
|
31
|
+
* @param {Array<numbers>} clear - erases the pixels in a rectangular area by setting them to transparent black. side by x, y, width, height
|
|
32
|
+
*
|
|
33
|
+
* @return ShapeCreator class
|
|
34
|
+
*/
|
|
35
|
+
drawRectShape(
|
|
36
|
+
clear = [0, 0, 0, 0],
|
|
37
|
+
fill = [0, 0, 0, 0],
|
|
38
|
+
stroke = [0, 0, 0, 0]
|
|
39
|
+
) {
|
|
40
|
+
this.context.clearRect(...clear)
|
|
41
|
+
this.context.fillRect(...fill)
|
|
42
|
+
this.context.strokeRect(...stroke)
|
|
43
|
+
|
|
44
|
+
return this
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
|
|
48
|
+
* @description method of the Canvas 2D API adds a rounded rectangle to the current path.
|
|
49
|
+
*
|
|
50
|
+
* @param {*} x - x-axis coordinate of the rectangle's starting point, in pixels.
|
|
51
|
+
* @param {*} y - y-axis coordinate of the rectangle's starting point, in pixels.
|
|
52
|
+
* @param {*} width - rectangle's width. Positive values are to the right, and negative to the left.
|
|
53
|
+
* @param {*} height - rectangle's height. Positive values are down, and negative are up.
|
|
54
|
+
* @param {Array<number>} radius position by index in array Ex. top-left | top-right | bottom-right | bottom-left
|
|
55
|
+
*
|
|
56
|
+
* @return ShapeCreator class
|
|
57
|
+
*/
|
|
58
|
+
roundRectShape(x, y, width, height, radius) {
|
|
59
|
+
this.context.roundRect(x, y, width, height, radius)
|
|
60
|
+
return this
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/beginPath
|
|
65
|
+
* @description method of the Canvas 2D API starts a new path by emptying the list of
|
|
66
|
+
* sub-paths. Call this method when you want to create a new path.
|
|
67
|
+
*
|
|
68
|
+
* @return ShapeCreator class
|
|
69
|
+
*/
|
|
70
|
+
startShape() {
|
|
71
|
+
this.context.beginPath()
|
|
72
|
+
return this
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/stroke
|
|
77
|
+
* @description method of the Canvas 2D API strokes (outlines) the current or given
|
|
78
|
+
* path with the current stroke style
|
|
79
|
+
*
|
|
80
|
+
* @param {Path2D} path - A Path2D path to fill.
|
|
81
|
+
*
|
|
82
|
+
* @return ShapeCreator class
|
|
83
|
+
*/
|
|
84
|
+
strokeShape(...path) {
|
|
85
|
+
this.context.stroke(...path)
|
|
86
|
+
return this
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/moveTo
|
|
91
|
+
* @description method of the Canvas 2D API begins a new sub-path
|
|
92
|
+
* at the point specified by the given (x, y) coordinates
|
|
93
|
+
*
|
|
94
|
+
* @param {number} x - new horizontal draw position
|
|
95
|
+
* @param {number} y - new vertical draw position
|
|
96
|
+
*
|
|
97
|
+
* @return ShapeCreator class
|
|
98
|
+
*/
|
|
99
|
+
moveTo(x, y) {
|
|
100
|
+
this.context.moveTo(x, y)
|
|
101
|
+
return this
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineTo
|
|
106
|
+
* @description method lineTo(), part of the Canvas 2D API,
|
|
107
|
+
* adds a straight line to the current sub-path by connecting
|
|
108
|
+
* the sub-path's last point to the specified (x, y) coordinates
|
|
109
|
+
*
|
|
110
|
+
* @param {number} x horizontal line position
|
|
111
|
+
* @param {number} y vertical line position
|
|
112
|
+
*
|
|
113
|
+
* @return ShapeCreator class
|
|
114
|
+
*/
|
|
115
|
+
lineTo(x, y) {
|
|
116
|
+
this.context.lineTo(x, y)
|
|
117
|
+
return this
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fill
|
|
122
|
+
* @description - method of the Canvas 2D API fills the current or given path with the current
|
|
123
|
+
*
|
|
124
|
+
* @param {string} region algorithm by which to determine if a point is inside or outside the filling region. Possible values:
|
|
125
|
+
* nonzero | evenodd.
|
|
126
|
+
*
|
|
127
|
+
* @return ShapeCreator class
|
|
128
|
+
*/
|
|
129
|
+
fillShape(...param) {
|
|
130
|
+
this.context.fill(...param)
|
|
131
|
+
return this
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arc
|
|
136
|
+
* @description method of the Canvas 2D API adds a circular arc to the current sub-path.
|
|
137
|
+
*
|
|
138
|
+
* @param {number} x - horizontal line position
|
|
139
|
+
* @param {number} y - vertical line position
|
|
140
|
+
* @param {number} radius - arc radius
|
|
141
|
+
* @param {number} startAngle - starting point of the angle.
|
|
142
|
+
* @param {number} endAngle - ending point of the angle.
|
|
143
|
+
* @param {boolean} counterclockwise - flip draw angle
|
|
144
|
+
*
|
|
145
|
+
* @return ShapeCreator class
|
|
146
|
+
*/
|
|
147
|
+
arcShape(x, y, radius, startAngle, endAngle, counterclockwise) {
|
|
148
|
+
this.context.arc(x, y, radius, startAngle, endAngle, counterclockwise)
|
|
149
|
+
return this
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @alert
|
|
154
|
+
* This can we broke in Firefox
|
|
155
|
+
*
|
|
156
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/quadraticCurveTo
|
|
157
|
+
* @description method of the Canvas 2D API adds a quadratic
|
|
158
|
+
* Bézier curve to the current sub-path. It requires two points:
|
|
159
|
+
* the first one is a control point and the second one is the end point.
|
|
160
|
+
* The starting point is the latest point in the current path,
|
|
161
|
+
* which can be changed using moveTo() before creating the quadratic
|
|
162
|
+
* Bézier curve.
|
|
163
|
+
*
|
|
164
|
+
* @param {number} cpx - x-axis coordinate of the control point
|
|
165
|
+
* @param {number} cpy - y-axis coordinate of the control point
|
|
166
|
+
* @param {number} x - x-axis coordinate of the end point
|
|
167
|
+
* @param {number} y - y-axis coordinate of the end point
|
|
168
|
+
*
|
|
169
|
+
* @return ShapeCreator class
|
|
170
|
+
*/
|
|
171
|
+
quadraticCurveTo(cpx, cpy, x, y) {
|
|
172
|
+
this.context.quadraticCurveTo(cpx, cpy, x, y)
|
|
173
|
+
return this
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/bezierCurveTo
|
|
178
|
+
* @description method of the Canvas 2D API adds a cubic Bézier
|
|
179
|
+
* curve to the current sub-path. It requires three points: the
|
|
180
|
+
* first two are control points and the third one is the end point.
|
|
181
|
+
* The starting point is the latest point in the current path,
|
|
182
|
+
* which can be changed using moveTo() before creating the Bézier curve
|
|
183
|
+
*
|
|
184
|
+
* @param {*} cp1x - x-axis coordinate of the first control point
|
|
185
|
+
* @param {*} cp1y - y-axis coordinate of the first control point
|
|
186
|
+
* @param {*} cp2x - x-axis coordinate of the second control point
|
|
187
|
+
* @param {*} cp2y - y-axis coordinate of the second control point
|
|
188
|
+
* @param {*} x - x-axis coordinate of the end point
|
|
189
|
+
* @param {*} y - y-axis coordinate of the end point
|
|
190
|
+
*
|
|
191
|
+
* @return ShapeCreator class
|
|
192
|
+
*/
|
|
193
|
+
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
|
194
|
+
this.context.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
|
|
195
|
+
return this
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/arcTo
|
|
200
|
+
* @description method of the Canvas 2D API adds a circular arc to the current sub-path,
|
|
201
|
+
* using the given control points and radius. The arc is automatically connected to the
|
|
202
|
+
* path's latest point with a straight line, if necessary for the specified parameters
|
|
203
|
+
*
|
|
204
|
+
* @param {number} x1 x-axis coordinate of the first control point
|
|
205
|
+
* @param {number} y1 y-axis coordinate of the first control point
|
|
206
|
+
* @param {number} x2 x-axis coordinate of the second control point
|
|
207
|
+
* @param {number} y2 y-axis coordinate of the second control point
|
|
208
|
+
* @param {number} radius arc's radius. Must be non-negative
|
|
209
|
+
*
|
|
210
|
+
* @return ShapeCreator class
|
|
211
|
+
*/
|
|
212
|
+
arcTo(x1, y1, x2, y2, radius) {
|
|
213
|
+
this.context.arcTo(x1, y1, x2, y2, radius)
|
|
214
|
+
return this
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* @todo - I think we can improve this - we need to figure out how to
|
|
219
|
+
*
|
|
220
|
+
* @description The Path2D() constructor returns a newly instantiated Path2D object,
|
|
221
|
+
* optionally with another path as an argument (creates a copy), or optionally with
|
|
222
|
+
* a string consisting of SVG path data.
|
|
223
|
+
*
|
|
224
|
+
* @param {...any} args (new Path2D, domMatrix)
|
|
225
|
+
*
|
|
226
|
+
* @return Path2D Class
|
|
227
|
+
*/
|
|
228
|
+
path2DShape(...args) {
|
|
229
|
+
return new Path2D(...args)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @todo - I think we can improve this - we need to figure out how to
|
|
234
|
+
*
|
|
235
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/DOMMatrix
|
|
236
|
+
* @description The DOMMatrix interface represents 4×4 matrices, suitable for 2D and 3D operations
|
|
237
|
+
* including rotation and translation. It is a mutable version of the DOMMatrixReadOnly interface.
|
|
238
|
+
*
|
|
239
|
+
* @return DOMMatrix Class
|
|
240
|
+
*/
|
|
241
|
+
domMatrixShape() {
|
|
242
|
+
return new DOMMatrix()
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/Path2D/addPath
|
|
247
|
+
* @description method of the Canvas 2D API adds one Path2D object to another Path2D object.
|
|
248
|
+
*
|
|
249
|
+
* @param {*} callerPath path used to call addPatch
|
|
250
|
+
* @param {...any} args config Path and domMatrix
|
|
251
|
+
*
|
|
252
|
+
* @return Path2D class
|
|
253
|
+
*/
|
|
254
|
+
addPathShape(callerPath, ...args) {
|
|
255
|
+
return callerPath.addPath(...args)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/ellipse
|
|
260
|
+
* @description method of the Canvas 2D API adds an elliptical arc to the current sub-path.
|
|
261
|
+
*
|
|
262
|
+
* @param {number} x horizontal position
|
|
263
|
+
* @param {number} y vertical position
|
|
264
|
+
* @param {number} radiusX ellipse's major-axis radius. Must be non-negative
|
|
265
|
+
* @param {number} radiusY ellipse's minor-axis radius. Must be non-negative
|
|
266
|
+
* @param {number} rotation rotation of the ellipse, expressed in radians
|
|
267
|
+
* @param {number} startAngle eccentric angle at which the ellipse starts, measured clockwise from
|
|
268
|
+
* the positive x-axis and expressed in radians
|
|
269
|
+
* @param {number} endAngle same ↑
|
|
270
|
+
* @param {number} counterclockwise An optional boolean value which, if true, draws the ellipse counterclockwise
|
|
271
|
+
* (anticlockwise). The default value is false (clockwise)
|
|
272
|
+
*
|
|
273
|
+
* @example - (100, 100, 50, 75, Math.PI / 4, 0, 2 * Math.PI)
|
|
274
|
+
*
|
|
275
|
+
* @return ShapeCreator class
|
|
276
|
+
*/
|
|
277
|
+
ellipseShape(
|
|
278
|
+
x,
|
|
279
|
+
y,
|
|
280
|
+
radiusX,
|
|
281
|
+
radiusY,
|
|
282
|
+
rotation,
|
|
283
|
+
startAngle,
|
|
284
|
+
endAngle,
|
|
285
|
+
counterclockwise
|
|
286
|
+
) {
|
|
287
|
+
this.context.ellipse(
|
|
288
|
+
x,
|
|
289
|
+
y,
|
|
290
|
+
radiusX,
|
|
291
|
+
radiusY,
|
|
292
|
+
rotation,
|
|
293
|
+
startAngle,
|
|
294
|
+
endAngle,
|
|
295
|
+
counterclockwise
|
|
296
|
+
)
|
|
297
|
+
return this
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @message You can use addColorStop here
|
|
302
|
+
*
|
|
303
|
+
* @doc https://www.w3schools.com/tags/canvas_addcolorstop.asp
|
|
304
|
+
* @description Define a gradient from black to white and use it to fill a rectangle:
|
|
305
|
+
*
|
|
306
|
+
* @param {...number} rgba red,green,blue,alpha colors
|
|
307
|
+
*
|
|
308
|
+
* @example (r,g,b,a) -> (255,255,255,1)
|
|
309
|
+
*
|
|
310
|
+
* @return createLinearGradient
|
|
311
|
+
*/
|
|
312
|
+
createLinearGradient(...rgba) {
|
|
313
|
+
return this.context.createLinearGradient(...rgba)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* @message You can use addColorStop here
|
|
318
|
+
*
|
|
319
|
+
* @doc https://www.w3schools.com/tags/canvas_createradialgradient.asp
|
|
320
|
+
* @description Define a radial gradient
|
|
321
|
+
*
|
|
322
|
+
* @param {...number} position
|
|
323
|
+
*
|
|
324
|
+
* @example (x0, y0, r0, x1, y1, r1a) -> (75, 50, 5, 90, 60, 100)
|
|
325
|
+
*
|
|
326
|
+
* @return createRadialGradient
|
|
327
|
+
*/
|
|
328
|
+
createRadialGradient(...position) {
|
|
329
|
+
return this.context.createRadialGradient(...position)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* @doc https://www.w3schools.com/tags/canvas_addcolorstop.asp
|
|
334
|
+
* @description Define a gradient from black to white and use it to fill a rectangle:
|
|
335
|
+
*
|
|
336
|
+
* @param {createLinearGradient} gradient
|
|
337
|
+
* @param {Array<{color: string<color>}} stops
|
|
338
|
+
*
|
|
339
|
+
* @example (gradient, {color:"red"})
|
|
340
|
+
*
|
|
341
|
+
* @return ShapeCreator Class
|
|
342
|
+
*/
|
|
343
|
+
addColorStop(gradient, stops) {
|
|
344
|
+
for (let i = 0; i < stops.length; i++) {
|
|
345
|
+
const element = stops[i]
|
|
346
|
+
gradient.addColorStop(i, element.color)
|
|
347
|
+
}
|
|
348
|
+
return this
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* @doc https://www.w3schools.com/tags/canvas_createpattern.asp
|
|
353
|
+
* @description create a patter by an image
|
|
354
|
+
*
|
|
355
|
+
* @param {Image} img new Image() class
|
|
356
|
+
* @param {*} repeatType type of repetition
|
|
357
|
+
*
|
|
358
|
+
* @example (new Image(), repeat | repeat-x | repeat-y | no-repeat)
|
|
359
|
+
*
|
|
360
|
+
* @retunr ShapeCreator Class
|
|
361
|
+
*/
|
|
362
|
+
createPattern(img, repeatType = 'repeat') {
|
|
363
|
+
this.context.createPattern(img, repeatType)
|
|
364
|
+
return this
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* @doc https://www.w3schools.com/tags/canvas_linecap.asp
|
|
369
|
+
* @description Draw a line with rounded end caps
|
|
370
|
+
*
|
|
371
|
+
* @param {string} style line style
|
|
372
|
+
*
|
|
373
|
+
* @example butt|round|square
|
|
374
|
+
*
|
|
375
|
+
* @return ShapeCreator Class
|
|
376
|
+
*/
|
|
377
|
+
lineCapShape(style) {
|
|
378
|
+
this.context.lineCap = style
|
|
379
|
+
return this
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* @doc https://www.w3schools.com/tags/canvas_linejoin.asp
|
|
384
|
+
* @description Create a rounded corner where the two lines meet
|
|
385
|
+
*
|
|
386
|
+
* @param {string} style line style
|
|
387
|
+
*
|
|
388
|
+
* @example bevel|round|miter
|
|
389
|
+
*
|
|
390
|
+
* @return ShapeCreator Class
|
|
391
|
+
*/
|
|
392
|
+
lineJoinShape(style) {
|
|
393
|
+
this.context.lineJoin = style
|
|
394
|
+
return this
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
|
|
398
|
+
* @description property of the Canvas 2D API sets the thickness of lines
|
|
399
|
+
*
|
|
400
|
+
* @param {number} width - define the thickness of lines
|
|
401
|
+
*
|
|
402
|
+
* @retun ShapeCreator class
|
|
403
|
+
*/
|
|
404
|
+
lineWidthShape(width) {
|
|
405
|
+
this.context.lineWidth = width
|
|
406
|
+
return this
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* @doc https://www.w3schools.com/tags/canvas_miterlimit.asp
|
|
411
|
+
* @description Draw lines with the maximum miter length of 5
|
|
412
|
+
*
|
|
413
|
+
* @param {number} limit define the limit
|
|
414
|
+
*
|
|
415
|
+
* @retun ShapeCreator class
|
|
416
|
+
*/
|
|
417
|
+
miterLimitShape(limit) {
|
|
418
|
+
this.context.miterLimit = limit
|
|
419
|
+
return this
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* @doc https://www.w3schools.com/tags/canvas_shadowblur.asp
|
|
424
|
+
* @description Draw a red rectangle with a black shadow, with blur level of 20
|
|
425
|
+
*
|
|
426
|
+
* @param {number} size define the blur size
|
|
427
|
+
*
|
|
428
|
+
* @retun ShapeCreator class
|
|
429
|
+
*/
|
|
430
|
+
shadowBlurOfShape(size) {
|
|
431
|
+
this.context.shadowBlur = size
|
|
432
|
+
return this
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* @doc https://www.w3schools.com/tags/canvas_shadowcolor.asp
|
|
437
|
+
* @description Draw a red rectangle with a black shadow:
|
|
438
|
+
*
|
|
439
|
+
* @param {string} style shadow color
|
|
440
|
+
*
|
|
441
|
+
* @return ShapeCreator class
|
|
442
|
+
*/
|
|
443
|
+
shadowColorOfShape(style) {
|
|
444
|
+
this.context.shadowColor = style
|
|
445
|
+
return this
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
shadowOffSetXY(x, y) {
|
|
449
|
+
this.context.shadowOffsetX = x
|
|
450
|
+
this.context.shadowOffsetX = y
|
|
451
|
+
return this
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/strokeStyle
|
|
456
|
+
* @description property of the Canvas 2D API specifies the color, gradient, or pattern
|
|
457
|
+
* to use for the strokes (outlines) around shapes. The default is #000 (black)
|
|
458
|
+
*
|
|
459
|
+
* @param {Color | CanvasGradient | CanvasPattern} style - property to specify color,
|
|
460
|
+
* gradient or pattern
|
|
461
|
+
*
|
|
462
|
+
* @return ShapeCreator class
|
|
463
|
+
*/
|
|
464
|
+
strokeStyle(style) {
|
|
465
|
+
this.context.strokeStyle = style
|
|
466
|
+
return this
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* @doc https://www.w3schools.com/tags/canvas_scale.asp
|
|
471
|
+
* @description change the size of shape
|
|
472
|
+
*
|
|
473
|
+
* @param {number} x horizontal scale
|
|
474
|
+
* @param {number} y vertical scale
|
|
475
|
+
*
|
|
476
|
+
* @return ShapeCreator class
|
|
477
|
+
*/
|
|
478
|
+
scaleShape(x, y) {
|
|
479
|
+
this.context.scale(x, y)
|
|
480
|
+
return this
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* @doc https://www.w3schools.com/tags/canvas_rotate.asp
|
|
485
|
+
* @description Rotate the rectangle
|
|
486
|
+
*
|
|
487
|
+
* @param {number} angle the angle to rotate
|
|
488
|
+
*
|
|
489
|
+
* @return ShapeCreator class
|
|
490
|
+
*/
|
|
491
|
+
rotateShape(angle) {
|
|
492
|
+
this.context.rotate(angle)
|
|
493
|
+
return this
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* @doc https://www.w3schools.com/tags/canvas_translate.asp
|
|
498
|
+
* @description change the position of shape
|
|
499
|
+
*
|
|
500
|
+
* @param {number} x horizontal trasnform
|
|
501
|
+
* @param {number} y vertical trasnform
|
|
502
|
+
*
|
|
503
|
+
* @return ShapeCreator class
|
|
504
|
+
*/
|
|
505
|
+
translateShape(x, y) {
|
|
506
|
+
this.context.translate(x, y)
|
|
507
|
+
return this
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* @doc https://www.w3schools.com/tags/canvas_transform.asp
|
|
512
|
+
* @description method scales, rotates, moves, and skews the context
|
|
513
|
+
*
|
|
514
|
+
* @param {Array<number[posA, posB, posC, posD, posE, posF]>} pos
|
|
515
|
+
*
|
|
516
|
+
* @return ShapeCreator class
|
|
517
|
+
*/
|
|
518
|
+
transformShape(x, y) {
|
|
519
|
+
this.context.transform(x, y)
|
|
520
|
+
return this
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* @doc https://www.w3schools.com/tags/canvas_transform.asp
|
|
525
|
+
* @description method scales, rotates, moves, and skews the context
|
|
526
|
+
*
|
|
527
|
+
* @param {Array<number[posA, posB, posC, posD, posE, posF]>} pos
|
|
528
|
+
*
|
|
529
|
+
* @return ShapeCreator Class
|
|
530
|
+
*/
|
|
531
|
+
setTransformShape(x, y) {
|
|
532
|
+
this.context.setTransform(x, y)
|
|
533
|
+
return this
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* @doc https://www.w3schools.com/tags/canvas_globalalpha.asp
|
|
538
|
+
* @description change the global alpha
|
|
539
|
+
*
|
|
540
|
+
* @param {number} alpha change the global alpha
|
|
541
|
+
*
|
|
542
|
+
* @return ShapeCreator Class
|
|
543
|
+
*/
|
|
544
|
+
globalAlpha(alpha) {
|
|
545
|
+
this.context.globalAlpha(alpha)
|
|
546
|
+
return this
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* @doc https://www.w3schools.com/tags/canvas_globalcompositeoperation.asp
|
|
551
|
+
* @description property sets or returns how a source are drawn over a destination.
|
|
552
|
+
*
|
|
553
|
+
* @param {string} composition
|
|
554
|
+
*
|
|
555
|
+
* @example source-over | source-atop | source-in | source-out | destination-over |
|
|
556
|
+
* destination-atop | destination-in | destination-out | lighter | copy | xor
|
|
557
|
+
*
|
|
558
|
+
* @return globalCompositeOperation() method
|
|
559
|
+
*/
|
|
560
|
+
globalCompositeOperation(composition) {
|
|
561
|
+
return (this.context.globalCompositeOperation = composition)
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
|
|
566
|
+
* @description method of the Canvas 2D API turns the current or given path into the
|
|
567
|
+
* current clipping region.
|
|
568
|
+
*
|
|
569
|
+
* @return clip
|
|
570
|
+
*/
|
|
571
|
+
clipShape() {
|
|
572
|
+
return this.context.clip()
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/* FONT - METHODS */
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/FontFace
|
|
579
|
+
* @description You can load, give a name and styles to the fonts using this method
|
|
580
|
+
* it is like the main for fonts.
|
|
581
|
+
*
|
|
582
|
+
* @param {...any} config - see the doc for the configs
|
|
583
|
+
*
|
|
584
|
+
* @return parent class
|
|
585
|
+
*/
|
|
586
|
+
loadFont(...config) {
|
|
587
|
+
new FontFace(...config)
|
|
588
|
+
return this
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* @message - very important method, you should read the doc below
|
|
593
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textRendering
|
|
594
|
+
* @param {string} config
|
|
595
|
+
* @example - auto | optimizeSpeed | optimizeLegibility | geometricPrecision
|
|
596
|
+
*
|
|
597
|
+
* @return FontCreator class
|
|
598
|
+
*/
|
|
599
|
+
fontRenderingType(config) {
|
|
600
|
+
this.context.textRendering = config
|
|
601
|
+
return this
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* @param {*} message - current text that you want to show
|
|
606
|
+
* @param {*} xPos - horizontal position of the text
|
|
607
|
+
* @param {*} yPos - vertical position of the text
|
|
608
|
+
*
|
|
609
|
+
* @example - ("Hello world", 10, 10)
|
|
610
|
+
*
|
|
611
|
+
* @return FontCreator class
|
|
612
|
+
*/
|
|
613
|
+
fontFill(message, xPos, yPos) {
|
|
614
|
+
this.context.fillText(message, xPos, yPos)
|
|
615
|
+
return this
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* @message - you could change the maxWith, we put it in 20 just like a
|
|
620
|
+
* default value
|
|
621
|
+
*
|
|
622
|
+
* @param {string} text - current text that you want to show (only stroke)
|
|
623
|
+
* @param {number} xPos - horizontal position of the text
|
|
624
|
+
* @param {number} yPos - vertical position of the text
|
|
625
|
+
* @param {number} maxWidth - maxWith stroke
|
|
626
|
+
*
|
|
627
|
+
* @example - ("Hello",10,10,20)
|
|
628
|
+
*
|
|
629
|
+
* @return FontCreator class
|
|
630
|
+
*/
|
|
631
|
+
fontStrokeText(...config) {
|
|
632
|
+
this.context.strokeText(...config)
|
|
633
|
+
return this
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* @param {string} style css value for fonts (you should know a little bit of css)
|
|
638
|
+
* @example - bold 48px serif
|
|
639
|
+
* @return FontCreator class
|
|
640
|
+
*/
|
|
641
|
+
fontStyle(style) {
|
|
642
|
+
this.context.font = style
|
|
643
|
+
return this
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* @param {string} style
|
|
648
|
+
* @example - left | right | center | start | end
|
|
649
|
+
|
|
650
|
+
* @return FontCreator class
|
|
651
|
+
*/
|
|
652
|
+
fontAlign(style) {
|
|
653
|
+
this.context.textAlign = style
|
|
654
|
+
return this
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/direction
|
|
659
|
+
* @description with this method you will be able to change the direction of the
|
|
660
|
+
* font - Ex: !Hi -> Hi!
|
|
661
|
+
* @param {string} style
|
|
662
|
+
* @exameple - ltr | rtl | inherit
|
|
663
|
+
*
|
|
664
|
+
* @return FontCreator class
|
|
665
|
+
*/
|
|
666
|
+
fontDirection(style) {
|
|
667
|
+
this.context.direction = style
|
|
668
|
+
return this
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline
|
|
673
|
+
* @description property of the Canvas 2D API specifies the current
|
|
674
|
+
* text baseline used when drawing text
|
|
675
|
+
* @param {string} style - top | hanging | middle | alphabetic | ideographic | bottom
|
|
676
|
+
*
|
|
677
|
+
* @return FontCreator class
|
|
678
|
+
*/
|
|
679
|
+
fontBaseline(style) {
|
|
680
|
+
this.context.textBaseline = style
|
|
681
|
+
return this
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fontVariantCaps
|
|
686
|
+
* @param {string} style - normal | small-caps | all-small-caps | petite-caps | all-petite-caps
|
|
687
|
+
* | unicase | titling-caps
|
|
688
|
+
*
|
|
689
|
+
* @return FontCreator class
|
|
690
|
+
*/
|
|
691
|
+
fontVariantCaps(style) {
|
|
692
|
+
this.context.fontVariantCaps = style
|
|
693
|
+
return this
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fontKerning
|
|
698
|
+
* @param {string} style kerning property to adjust the space between characters
|
|
699
|
+
* @example - auto | normal | none
|
|
700
|
+
* @return FontCreator class
|
|
701
|
+
*/
|
|
702
|
+
fontKerning(style) {
|
|
703
|
+
this.context.fontKerning = style
|
|
704
|
+
return this
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* @alert the style prop should be a number by default but it is a string but number
|
|
709
|
+
* @param {string (number)} style - spacing of between the words
|
|
710
|
+
* @return FontCreator class
|
|
711
|
+
*/
|
|
712
|
+
fontWordSpacing(style) {
|
|
713
|
+
this.context.wordSpacing = style
|
|
714
|
+
return this
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* @warning - could no be tested
|
|
719
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fontStretch
|
|
720
|
+
* @param {string} style - fontStretch property
|
|
721
|
+
* @example - ultra-condensed | extra-condensed | condensed | semi-condensed | normal |
|
|
722
|
+
* semi-expanded | expanded | extra-expanded | ultra-expanded
|
|
723
|
+
*
|
|
724
|
+
* @return FontCreator class
|
|
725
|
+
*/
|
|
726
|
+
fontS(style) {
|
|
727
|
+
this.context.fontStretch = style
|
|
728
|
+
return this
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/* IMAGE - METHODS */
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* @param { src: string, img: [sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight] } config
|
|
735
|
+
* Configuration could be long or short, but right now we are showing as example the long
|
|
736
|
+
* way. You could try adding just: dx, dy, dWidth, dHeight or dx, dy (for the img property)
|
|
737
|
+
*
|
|
738
|
+
* @example - {src: imgPath, img: [sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight]}
|
|
739
|
+
*
|
|
740
|
+
* @return ShapeCreator Class
|
|
741
|
+
*/
|
|
742
|
+
createImage(config) {
|
|
743
|
+
const img = new Image()
|
|
744
|
+
img.src = config.src
|
|
745
|
+
this.context.drawImage(img, ...config.img)
|
|
746
|
+
return this
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
/**
|
|
750
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createImageData
|
|
751
|
+
* @description method of the Canvas 2D API creates a new, blank ImageData object with the
|
|
752
|
+
* specified dimensions. All of the pixels in the new object are transparent black
|
|
753
|
+
*
|
|
754
|
+
* @param {object<width | height | settings | ImageData>} imgConfig
|
|
755
|
+
*
|
|
756
|
+
* @example width | height | settings -> object{colorSpace} or existing ImageData
|
|
757
|
+
*
|
|
758
|
+
* @return createImageData method
|
|
759
|
+
*/
|
|
760
|
+
createImageData(...imgConfig) {
|
|
761
|
+
return this.context.createImageData(...imgConfig)
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* @doc https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData
|
|
766
|
+
* @description method getImageData() of the Canvas 2D API returns an ImageData object
|
|
767
|
+
* representing the underlying pixel data for a specified portion of the canvas.
|
|
768
|
+
*
|
|
769
|
+
* @param {object<sx | xy | sw | sh | setting>} imgConfig
|
|
770
|
+
*
|
|
771
|
+
* @example sx | sy | sw | sh or settings -> object<colorSpace>
|
|
772
|
+
*
|
|
773
|
+
* @return ShapeCreator Class
|
|
774
|
+
*/
|
|
775
|
+
getImgData(...imgConfig) {
|
|
776
|
+
return this.context.getImageData(...imgConfig)
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* @doc https://www.w3schools.com/tags/canvas_putimagedata.asp
|
|
781
|
+
* @description method puts the image data (from a specified ImageData object) back onto the canvas
|
|
782
|
+
*
|
|
783
|
+
* @param {object<imgData> } imgData The ImageData object to put on the context.
|
|
784
|
+
* @param {...} pos xPos and yPos
|
|
785
|
+
*
|
|
786
|
+
* @example {imgData, x, y, dirtyX, dirtyY, dirtyWidth, dirtyHeight}
|
|
787
|
+
* @return ShapeCreator Class
|
|
788
|
+
*/
|
|
789
|
+
putImageData(imgData, ...pos) {
|
|
790
|
+
this.context.putImageData(imgData, ...pos)
|
|
791
|
+
return this
|
|
792
|
+
}
|
|
793
|
+
```
|
|
794
|
+
|
|
795
|
+
# Examples
|
|
796
|
+
|
|
797
|
+
Now we will show you some examples we made with the library. Remember that everything you can do with canvas, you will be able to do with Fruta. Also, remember that we are still in development and in the early stages. We want to do many things, but it will take time. Nevertheless, feel free to give your opinion in order to improve what we currently have.
|
|
798
|
+
|
|
799
|
+
### Init Config
|
|
800
|
+
|
|
801
|
+
```js
|
|
802
|
+
const config = {
|
|
803
|
+
scene: {
|
|
804
|
+
w: 500,
|
|
805
|
+
h: 500,
|
|
806
|
+
id: 'main',
|
|
807
|
+
},
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
const { FontCreator, ShapeCreator, Game } = Fruta(config)
|
|
811
|
+
|
|
812
|
+
const canvasW = ShapeCreator.canvas.width
|
|
813
|
+
const canvasH = ShapeCreator.canvas.height
|
|
814
|
+
```
|
|
815
|
+
|
|
816
|
+
## Ping Pong
|
|
817
|
+
|
|
818
|
+
```js
|
|
819
|
+
let x = 50
|
|
820
|
+
let y = 50
|
|
821
|
+
|
|
822
|
+
let dx = 7
|
|
823
|
+
let dy = 5
|
|
824
|
+
|
|
825
|
+
Game.tick.OnRender((tick) => {
|
|
826
|
+
x += dx
|
|
827
|
+
y += dy
|
|
828
|
+
|
|
829
|
+
ShapeCreator.drawRectShape(
|
|
830
|
+
[0, 0, canvasW, canvasH],
|
|
831
|
+
[x, y, 50, 50],
|
|
832
|
+
[0, 0, canvasW, canvasH]
|
|
833
|
+
).fillStyleShape('blue')
|
|
834
|
+
|
|
835
|
+
if (x + 50 >= canvasW || x <= 0) {
|
|
836
|
+
dx = -dx
|
|
837
|
+
}
|
|
838
|
+
if (y + 50 >= canvasH || y <= 0) {
|
|
839
|
+
dy = -dy
|
|
840
|
+
}
|
|
841
|
+
})
|
|
842
|
+
```
|
|
843
|
+
|
|
844
|
+
https://github.com/karttofer/Fruta/assets/34972636/165c74fa-424b-4c4e-8ec8-45514d7274bc
|
|
845
|
+
|
|
846
|
+
## Simple Font Generation With Animation
|
|
847
|
+
|
|
848
|
+
```js
|
|
849
|
+
const drawMap = [
|
|
850
|
+
[0, 0, 0],
|
|
851
|
+
[0, 0, 0],
|
|
852
|
+
[0, 0, 0],
|
|
853
|
+
]
|
|
854
|
+
const yOffset = 100
|
|
855
|
+
const rotation = 0.01
|
|
856
|
+
|
|
857
|
+
ShapeCreator.shadowColorOfShape('blue')
|
|
858
|
+
.shadowOffSetXY(50, 10)
|
|
859
|
+
.translateShape(250, 250)
|
|
860
|
+
|
|
861
|
+
Game.tick.OnRender((tick) => {
|
|
862
|
+
ShapeCreator.drawRectShape([0, 0, canvasW, canvasH]).rotateShape(rotation)
|
|
863
|
+
|
|
864
|
+
for (let i = 0; i < drawMap.length; i++) {
|
|
865
|
+
for (let j = 0; j < drawMap[i].length; j++) {
|
|
866
|
+
const x = j * 50 // Adjust the horizontal spacing between characters
|
|
867
|
+
const y = i * 50 + yOffset // Adjust the vertical spacing between lines of text
|
|
868
|
+
FontCreator.fontStyle('bold 20px serif').fontFill('Hello', x, y)
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
})
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
https://github.com/karttofer/Fruta/assets/34972636/5ac156d2-9e08-44de-8308-dacd30657c25
|