-
Notifications
You must be signed in to change notification settings - Fork 3
/
guilloche.js
190 lines (164 loc) · 3.97 KB
/
guilloche.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
function Guilloche() {
var _steps = 300+Math.round(Math.random()*2400); // Divide a circle this many times
var _R = 50; // The major ripple
var _rr = 0.25; // The minor ripple
var _p = 25; // Radius type effect
var _m = 1; // Angle multiplier
var _amplitude = 4.5; // Scale of final drawing
var _opacity = 0.2; // Line opacity
var _color1 = 0x000000; // Line start colour
var _color2 = 0x000000; // Line end colour
var _thickness = 0.5; // Line thickness
var section_length = 1; // Number of sections for each line
var deg2rad = Math.PI / 180; // Factor to convert degs to radians
var _colorCycle = 1; // The number of times the colour repeats
var colorPalette = []; // For storing a gradient array of colours
this.plot = function(ctx) {
var l;
var x, y, ox, oy;
var sl = 0;
var theta = 0;
var thetaStep = 2 * Math.PI / _steps;
var s = (_R + _rr) / _rr;
var rR = _rr + _R;
var rp = _rr + _p;
var cpl = colorPalette.length;
var c = _color1;
for(var t = 0; t <= _steps; t++) {
x = rR * Math.cos(_m * theta) + rp * Math.cos(s * _m * theta);
y = rR * Math.sin(_m * theta) + rp * Math.sin(s * _m * theta);
x *= _amplitude;
y *= _amplitude;
if(sl == 0) {
c = (_color1 == _color2 || cpl < 2) ? _color1 : colorPalette[t % cpl];
ctx.beginPath();
if(t == 0) {
ctx.moveTo(x, y);
} else {
ctx.moveTo(ox, oy);
ctx.lineTo(x, y);
}
ctx.stroke();
} else {
// Append to line section
ctx.lineTo(x, y);
ctx.stroke();
}
ox = x;
oy = y;
sl++;
theta += thetaStep;
if(sl == section_length) sl = 0;
}
}
this.setColorPalette = function() {
//colorPalette = ColorUtil.blendArray(_color1, _color2, ((_steps / section_length) - 1), false)
//colorPalette = colorPalette.concat(ColorUtil.blendArray(_color2, _color1, ((_steps / section_length) - 1), false))
}
/**
* Get and set amplitude
*/
this.get_amplitude = function() {
return _amplitude;
}
this.set_amplitude = function(value) {
if(value !== _amplitude) _amplitude = value;
}
/**
* Get and set steps
*/
this.get_steps = function() {
return _steps;
}
this.set_steps = function(value) {
if(value !== _steps) _steps = value;
}
/**
* Set colour cycle steps
*/
this.get_colorCycle = function() {
return _colorCycle;
}
this.set_colorCycle = function(value) {
if(value !== _colorCycle) {
_colorCycle = value;
//setColorPalette();
}
}
/**
* Get and set R value - the Major ripple effect
*/
this.get_R = function() {
return _R;
}
this.set_R = function(value) {
if(value !== _R) _R = value;
}
/**
* Get and set r value - the minor ripple effect
*/
this.get_rr = function() {
return _rr;
}
this.set_rr = function(value) {
if(value !== _rr) _rr = value;
}
/**
* Get and set p - the radius type effect
*/
this.get_p = function() {
return _p;
}
this.set_p = function(value) {
if(value !== _p) _p = value;
}
/**
* Get and set m value - angle multiplier
*/
this.get_m = function() {
return _m;
}
this.set_m = function(value) {
if(value !== _m) _m = value;
}
/**
* Get and set opacity
*/
this.get_opacity = function() {
return _opacity;
}
this.set_opacity = function(value) {
if(value !== _opacity) _opacity = value;
}
/**
* Get and set thickness
*/
this.get_thickness = function() {
return _thickness;
}
this.set_thickness = function(value) {
if(value !== _thickness) _thickness = value;
}
/**
* Get and set colour
*/
this.get_color1 = function() {
return _color1;
}
this.set_color1 = function(value) {
if(value !== _color1) {
_color1 = value;
//setColorPalette();
}
}
this.get_color2 = function() {
return _color1;
}
this.set_color2 = function(value) {
if(value !== _color2) {
_color2 = value;
//setColorPalette();
}
}
return this;
}