-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdragonanim1.html
151 lines (133 loc) · 3.92 KB
/
dragonanim1.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- <meta name="viewport" content="initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/> -->
<title>Dragon Curve</title>
<style type="text/css">
body { background-color: #444; color: #CCC; margin: 0px; overflow: hidden; font-family: monospace;}
#info { top: 0px; width: 100%; color: #777; padding: 5px; text-align:center; }
#container { text-align:center; }
#container canvas { border:0px solid #777; }
div { text-align:center; }
</style>
</head>
<body>
<br/>
<div id="container"></div>
<div>
<span id="label"></span><br/>
<script type="text/javascript">
var canvas, canvas2, context, image, data, context2,dcurve;
var HEIGHT = 800;
var WIDTH = 1000;
var space = 5;
var iterations = 23;
function hslToRgb(h, s, l){
var r, g, b;
if (h > 1) { h -= Math.floor(h); }
if(s == 0){
r = g = b = l; // achromatic
}else{
function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return "rgb(" + Math.floor(r * 255) + "," + Math.floor(g * 255) + "," + Math.floor(b * 255) + ")";
}
function curve(iter) {
function revswap(foo) {
var rev = foo.slice(0);
for (var i = 0; i < rev.length; i++) {
rev[i] ^= 1;
}
rev.reverse();
return(rev);
}
var c = [];
for (var i = 0; i < iter; i++) {
c = c.concat([0],revswap(c));
}
return(c);
}
function init() {
dcurve = curve(iterations);
var container;
container = document.getElementById('container');
canvas = document.createElement("canvas");
canvas.width = WIDTH;
canvas.height = HEIGHT;
canvas.style.width = WIDTH + "px";
canvas.style.height = HEIGHT + "px";
container.appendChild(canvas);
context = canvas.getContext("2d");
context.fillStyle = "rgb(0, 0, 0)";
context.fillRect (0, 0, WIDTH, HEIGHT);
context.translate(canvas.width / 2, canvas.height / 2);
context.scale(1, 1);
var cdir = 1;
var cy = Math.floor(HEIGHT / 1.5)+0.5;
var cx = Math.floor(WIDTH / 3)+0.5;
context.lineWidth = 1;
cx = cy = 0;
var csz = 3;
var i = 0;
var timer;
var opow = 0;
function draw() {
var pow2 = 0;
if (i) { pow2 = Math.floor(Math.log(i)/Math.log(2)); }
document.getElementById('label').innerHTML = "Iteration #" + (pow2 + 1);
var len = 1;
if (pow2 - 5 > 1) { len = Math.pow(2,pow2 - 5); }
context.scale(.995, 0.995);
if (pow2 != opow) {
opow = pow2;
//context.scale(.85, 0.85);
}
for (var j=0; j <= len && i < dcurve.length; j++) {
context.beginPath();
context.moveTo(cx,cy);
//context.strokeStyle = hslToRgb((Math.floor(Math.log(i)/Math.log(2)) / 13 )+(i/dcurve.length), 1, 0.5);
context.strokeStyle = hslToRgb(pow2 / iterations, 1, 0.5);
switch (cdir % 4) {
case 0: cx += csz; break;
case 1: cy += csz; break;
case 2: cx -= csz; break;
case 3: cy -= csz; break;
}
if (! dcurve[i]) { cdir = (cdir + 1); } else { cdir = (cdir - 1); }
if (cdir < 0) { cdir += 4; }
var ox=cx; var oy=cy;
switch (cdir % 4) {
case 0: cx += csz; break;
case 1: cy += csz; break;
case 2: cx -= csz; break;
case 3: cy -= csz; break;
}
context.arcTo(ox,oy,cx,cy,csz);
context.stroke();
i++;
}
//if (i > dcurve.length) { clearInterval(timer); }
if (i <= dcurve.length) { setTimeout(draw,1000/30); }
}
// timer = setInterval(draw,10);
draw();
}
function loop() {
}
init();
</script>
</body>
</html>