-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.js
75 lines (75 loc) · 2.61 KB
/
gen.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
var starsInfo=[[0, 1, 2, 3, 4, 5], [1, 2, 4], [1, 4], [2], [4]];
var currentStarFrame=0, frameLeng=0, starAnimationCycle=50;
var backgroundImages=[], backgroundStars=[];
var starColors, mainColors;
var genGrid;
function setPixel(x, y, color){
ctx.fillStyle=color;
ctx.fillRect(y, x, 1, 1);
}
function createBackground(n, m, seed, theme, nr){
mainColors=theme[0], starColors=theme[1];
can.width=m, can.height=n;
GNN=n; GMM=m;
setSeed(seed)
genGrid=makeGrid(GNN, GMM);
var LOG=Math.round(n*m/10000);
var sizes=[LOG, LOG*2, LOG*3, LOG*3, LOG*2];
backgroundStars[nr]=[];
for (var i=0; i<sizes.length; i++)
for (var j=0; j<sizes[i]; j++)
{
var CX=getRandom(1, n), CY=getRandom(1, m);
var ok=true;
if (CX<10 || CX>n-10 || CY<10 || CY>m-10)
ok=false;
for (var star of backgroundStars[nr])
if (Math.abs(star.x-CX)+Math.abs(star.y-CY)<10)
ok=false;
if (ok) backgroundStars[nr].push({x: CX, y: CY, size: i});
else j--;
}
for (var i=0; i<GNN; i++)
for (var j=0; j<GMM; j++)
setPixel(0+i, 0+j, mainColors[genGrid[i][j]]);
backgroundImages[nr]=new Image();
backgroundImages[nr].src=can.toDataURL('image/jpeg', 1.0);
}
function rePaintBackground(ft, bt, scene){
document.body.style.backgroundColor=mainColors[0];
ctx.fillStyle=mainColors[0];
//ctx.fillRect(0, 0, 400, 400);
frameLeng++;
if (frameLeng%30==0)
currentStarFrame++;
var genStars;
ctx.drawImage(backgroundImages[scene], bt, ft), genStars=backgroundStars[scene];
var GNN=backgroundImages[scene].height, GMM=backgroundImages[scene].width;
for (var star of genStars)
{
for (var i=0; i<starsInfo[star.size].length; i++)
{
var currentColor;
if ((currentStarFrame+star.x+star.y)%starAnimationCycle==i && i!=0)
currentColor=starColors[starsInfo[star.size][i]-1];
else
currentColor=starColors[starsInfo[star.size][i]];
setPixel(ft+((i*1+star.x+GNN)%GNN), bt+((star.y+GMM)%GMM), currentColor);
setPixel(ft+((star.x+GNN)%GNN), bt+((i+star.y+GMM)%GMM), currentColor);
setPixel(ft+((i*(-1)+star.x+GNN)%GNN), bt+((star.y+GMM)%GMM), currentColor);
setPixel(ft+((star.x+GNN)%GNN), bt+((i*(-1)+star.y+GMM)%GMM), currentColor);
}
if (star.size==0)
{
var currentColor;
if ((currentStarFrame+star.x+star.y)%starAnimationCycle==2)
currentColor=starColors[3];
else
currentColor=starColors[4];
setPixel(ft+((1+star.x+GNN)%GNN), bt+((1+star.y+GMM)%GMM), currentColor);
setPixel(ft+((-1+star.x+GNN)%GNN), bt+((1+star.y+GMM)%GMM), currentColor);
setPixel(ft+((1+star.x+GNN)%GNN), bt+((-1+star.y+GMM)%GMM), currentColor);
setPixel(ft+((-1+star.x+GNN)%GNN), bt+(((-1)+star.y+GMM)%GMM), currentColor);
}
}
}