-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
35 lines (31 loc) · 1.06 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Cria um canvas com as dimensões especificadas e adiciona ele a algum elemnto
* caso o parâmetro `appendTo` seja especificado
* @param {number} width Largura inicial do canvas
* @param {number} height Altura inicial do canvas
* @param {HTMLElement} [appendTo] O elemento especificado através dessa propriedade
* terá o canvas adicionado a sua lista de filhos
* @returns {HTMLCanvasElement} Canvas recém criado
*/
export function createCanvas(width, height, appendTo = null) {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
if (appendTo) {
appendTo.appendChild(canvas);
}
return canvas;
}
/**
* Cria uma matrize bidimencional do tamanho especificado
* @param {number} width largura da matriz
* @param {number} height altura da matriz
* @returns {number[][]} a matriz recém criada preenchida com zeros
*/
export function createMatrix(width, height) {
const matrix = [];
while (height--) {
matrix.push(new Array(width).fill(0));
}
return matrix;
}