-
Notifications
You must be signed in to change notification settings - Fork 0
/
patterns.js
202 lines (163 loc) · 4.25 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function randomPatternDrawable() {
var patternDrawableFactories = [
function() { return new Drawable(); }, // empty
solidPatternFactory,
patternWithMaskFactory,
tunnelFactory,
spirographFactory,
otherDesignFactory
];
return randomPick(patternDrawableFactories)();
}
function flannie(ctx, op) {
var oldAlpha = ctx.globalAlpha;
ctx.beginPath();
ctx.rect(-.5, -.5, 0.5, 0.5);
op(ctx);
ctx.globalAlpha = .5;
ctx.beginPath();
ctx.rect(0, -.5, 0.5, 0.5);
op(ctx);
ctx.beginPath();
ctx.rect(-.5, 0, 0.5, 0.5);
op(ctx);
ctx.globalAlpha = oldAlpha;
}
function getPatternList() {
var s = getShapeList();
var details = [];
for (var i=0 ; i<s.length; ++i) {
details.push( { ratio: vary(.75, .2), pattern: s[i] } );
}
details.push( { ratio: 1, pattern: flannie } );
return details;
}
function randomPattern() {
return randomPick(getPatternList());
}
function solidPatternFactory() {
var details = randomPattern();
var pattern = new PatternDrawable(randomColor(), randomColor(), details.ratio, function(ctx) { details.pattern(ctx, fill); });
return pattern;
}
function patternWithMaskFactory() {
var s = randomShape();
var details = randomPattern();
var pattern = new PatternDrawable(randomColor(), randomColor(), details.ratio, function(ctx) { details.pattern(ctx, fill); });
pattern.scale = vary(.4, .1);
pattern.clipFunc = function(ctx) {
s(ctx, clip);
}
return pattern;
}
// have to do this to create new variables for JS
function makeShapeFunc(f) {
return function(ctx) { f(ctx, stroke); }
}
function tunnelFactory() {
var len = 6 + irand(6);
var funcs = [];
var sameShape = odds(.5);
var f = randomShape();
for (var i=0 ; i<len ; ++i) {
if (!sameShape) {
f = randomShape();
}
funcs.push(makeShapeFunc(f));
}
var pattern = new Drawable();
pattern.strokeStyle = randomColor();
pattern.func = function(ctx) {
ctx.scale(2, 2);
ctx.lineWidth = .05;
tunnel(ctx, .6, funcs);
}
return pattern;
}
function spirographFactory() {
var pattern = new Drawable();
var num = 1 + irand(3);
for (var i=0 ; i<num ; ++i) {
pattern.children.push( getSpirograph() );
}
return pattern;
}
// Taken from Mozilla Transformations demo - https://developer.mozilla.org/en/Canvas_tutorial/Transformations
function getSpirograph() {
var pattern = new Drawable();
pattern.strokeStyle = randomColor();
var R = 20*(irand(3)+2)/(irand(3)+1);
var r = -8*(irand(3)+3)/(irand(3)+1);
var O = 10;
pattern.scale = .008; // normally is 100
pattern.func = function(ctx) {
var x1 = R-O;
var y1 = 0;
var i = 1;
ctx.beginPath();
ctx.moveTo(x1,y1);
do {
if (i>20000) break;
var x2 = (R+r)*Math.cos(i*Math.PI/72) - (r+O)*Math.cos(((R+r)/r)*(i*Math.PI/72))
var y2 = (R+r)*Math.sin(i*Math.PI/72) - (r+O)*Math.sin(((R+r)/r)*(i*Math.PI/72))
ctx.lineTo(x2,y2);
x1 = x2;
y1 = y2;
i++;
} while (x2 != R-O && y2 != 0 );
ctx.stroke();
}
return pattern;
}
function otherDesignFactory() {
var pattern = new Drawable("rainbow");
pattern.func = randomDesign();
return pattern;
}
function randomDesign() {
return randomPick(getDesignList());
}
function getDesignList() {
return [
//rainbow,
flower
];
}
////////// Designs
// FIXME: We could make this into a proper rainbow by moving inwards on the curving points
function rainbow(ctx) {
var roygbiv = ["#FF0000", "#FFA500", "#FFFF00", "#008000", "#0000FF", "#4B0082", "#EE82EE"];
for (var c=0 ; c<roygbiv.length ; ++c) {
ctx.lineWidth = 0.1;
ctx.strokeStyle = roygbiv[c];
ctx.beginPath();
ctx.bezierCurveTo(-0.6, 0, 0, -0.5, 0.6, 0);
ctx.stroke();
ctx.translate(0.0, 0.1);
}
}
function flower(ctx) {
var numPetals = 13;
var centreRadius = .05;
var rot = Math.PI * 2 / numPetals;
var petal = { length: .15, width: .12 };
ctx.lineWidth = 0.005;
ctx.strokeStyle = "#000000";
ctx.fillStyle = "#FFFFFF";
for (var p=0 ; p<numPetals ; ++p) {
ctx.save();
ctx.rotate( p * rot );
ctx.translate(centreRadius, 0);
ctx.beginPath();
ctx.bezierCurveTo(0, 0, petal.length/2, -petal.width/2, petal.length, 0);
ctx.bezierCurveTo(petal.length, 0, petal.length/2, petal.width/2, 0, 0);
ctx.fill();
ctx.stroke();
ctx.restore();
}
ctx.lineWidth = 0.05;
ctx.fillStyle = "#fffc22";
var s = centreRadius * 3;
ctx.scale(s, s);
circle(ctx, strokeAndFill);
}