-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
166 lines (132 loc) · 4.33 KB
/
script.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var width = 600;
var height = 400;
var styles = ["horizontalBars", "verticalBars", "cross"]
var features = [true,false]
var flagColours = ["e0201b","a22b2b","b1793a","25369d","eeede9","4690c9","100e10","3d9139","626051","f5d126","ffffff","e6194B","3cb44b","ffe119","4363d8","f58231","42d4f4","469990","800000","aaffc3","000075"]
var usedColours = []
class Flag{
constructor(style,feature,colours){
this.style = style;
this.feature = feature;
this.colours = colours;
}
getStyle(){
return this.style;
}
getFeature(){
return this.feature;
}
getcolours(){
return this.colours;
}
}
function getRandomColor() {
var colour = flagColours[Math.floor(Math.random()*flagColours.length)]
return "#" + colour;
}
function buildFlag(){
var style = styles[Math.floor(Math.random()*styles.length)];
var feature = features[Math.floor(Math.random()*features.length)];
var colours = Math.floor(Math.random() * 3)+ 1;
if(colours <3){
feature = true;
}
flag = new Flag(style,feature,colours)
drawFlag(flag)
}
function drawFlag(flag){
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
style = flag.getStyle();
colours = flag.getcolours();
feature = flag.getFeature();
usedColours = []
//draw vertical flag
if(style == "verticalBars"){
if(colours == 1){
c = getRandomColor();
usedColours.push(c)
ctx.fillStyle = c;
ctx.fillRect(0,0, width,height);
} else {
for(i=0; i < colours; i++){
c = getRandomColor();
usedColours.push(c)
ctx.fillStyle = c;
ctx.fillRect(0,0, width/colours,height);
c2 = getRandomColor();
usedColours.push(c2)
ctx.fillStyle = c2;
ctx.fillRect((width/colours)*i, 0, width/colours,height);
}
if(colours == 3){
usedColours = [];
}
}
}
//draw horizontal flag
if(style == "horizontalBars"){
if(colours == 1){
c = getRandomColor();
usedColours.push(c)
ctx.fillStyle = c;
ctx.fillRect(0,0, width,height);
} else {
for(i=0; i < colours; i++){
c = getRandomColor();
usedColours.push(c)
ctx.fillStyle = c;
ctx.fillRect(0,0, width, height/colours);
c2 = getRandomColor();
usedColours.push(c2)
ctx.fillStyle = c2;
ctx.fillRect(0, (height/colours)*i, width, height/colours);
}
}
}
if(style == "cross"){
ctx.fillStyle = getRandomColor();
ctx.fillRect(0, 0, width, height);
ctx.beginPath();
ctx.rect(0, height/3, width, height/3);
ctx.fillStyle = getRandomColor();
ctx.fill();
ctx.beginPath();
ctx.rect(225 , 0, 150,height);
ctx.fill();
}
//draw feature
if(feature){
drawFeature()
}
}
function drawFeature(){
var canvas = document.getElementById("canvas");
var canvas2 = document.getElementById("canvas2");
var ctx = canvas.getContext("2d");
var ctx2 = canvas2.getContext("2d");
var centerX = width/2;
var centerY = height/2;
var radius = 120;
var option = Math.floor((Math.random() * 37) + 1);
var imgPath = "images/" + option + ".png";
var img = new Image();
img.src = imgPath;
img.onload = function(){
ogWidth = img.naturalWidth;
ogHeight = img.naturalHeight;
newHeight = 180;
reduction = newHeight/ogHeight
newWidth = ogWidth*reduction
c = getRandomColor();
while(usedColours.includes(c)){
c = getRandomColor();
}
ctx2.fillStyle = c
ctx2.fillRect(0, 0, width, height);
ctx2.globalCompositeOperation = "destination-in";
ctx2.drawImage(img,centerX-(newWidth/2),centerY-(newHeight/2),newWidth,newHeight);
ctx.drawImage(canvas2,0,0)
};
}
buildFlag()