/
patterns.js
46 lines (36 loc) · 1.14 KB
/
patterns.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
36
37
38
39
40
41
42
43
44
45
46
import shapes, { getRandomShape } from './shapes/index';
export function draw (
shapeType = 'square',
backgroundColor,
patternColor,
size
) {
const patternCanvas = document.createElement('canvas');
const patternContext = patternCanvas.getContext('2d');
const outerSize = size * 2;
const Shape = shapes[shapeType];
const shape = new Shape(size, backgroundColor, patternColor);
const pattern = patternContext.createPattern(shape.drawTile(), 'repeat');
patternCanvas.width = outerSize;
patternCanvas.height = outerSize;
pattern.shapeType = shapeType;
return pattern;
}
export function generate(colorList) {
let firstShapeType;
let previousShapeType;
return colorList.map((color, index, list) => {
let shapeType;
if (index === 0) {
shapeType = getRandomShape();
previousShapeType = shapeType;
firstShapeType = previousShapeType;
} else if (index === list.length - 1) {
shapeType = getRandomShape([previousShapeType, firstShapeType]);
} else {
shapeType = getRandomShape([previousShapeType]);
previousShapeType = shapeType;
}
return draw(shapeType, color);
});
}