-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas_cycle_test.html
106 lines (88 loc) · 2.17 KB
/
canvas_cycle_test.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
<!doctype html>
<html lang="en">
<head>
</head>
<body>
<canvas id="thecanvas" width="500" height="500" ></canvas>
<script>
var fps = 5;
//var size = 500;
//var startTime = (new Date()).getTime();
var canvas = document.getElementById("thecanvas");
var ctx = canvas.getContext("2d");
var size = canvas.width;
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0,size,size);
//var num = 0;
//var palette = ["#000000", "#FF0000", "#FF0000", "#FF0000"];
var palette = ["#000000", "#00FFFF", "#008000", "#FF5733"];
//var image = [];
var image = new Array(size * size);
image.fill(0);
for (var i = size / 3.0; i < size / 2.0; i++) {
for (var j = 0; j < size; j++) {
var sin = Math.sin(10 * j/ (size));
var y = Math.abs(sin);
var index = twoToOne(Math.floor(i), Math.floor(j));
//console.log(y);
if (y > (i * 2 / (size))) {
image[index] = 1;
} else {
//image[index] = 0;
}
//image[index] = 1;
}
}
for (var i = size / 2.0; i < 2.0 * size / 3.0; i++) {
for (var j = 0; j < size; j++) {
var index = twoToOne(Math.floor(i), Math.floor(j));
image[index] = 2;
}
}
//console.log(image);
/*
for (var i = 0; i < size / 4.0; i++) {
image.push(0);
}
for (var i = size / 4.0; i < size / 2.0; i++) {
image.push(1);
}
for (var i = size / 2.0; i < size * 3.0 / 4.0; i++) {
image.push(2);
}
for (var i = size * 3.0 / 4.0; i < size; i++) {
image.push(3);
}
*/
paint();
function twoToOne(x, y) {
return x + size * y;
}
function paint() {
var temp = Object.assign([], palette);
for (var i = 1; i < palette.length; i++) {
if (temp[i - 1] != "#000000") {
palette[i] = temp[i - 1];
}
}
palette[1] = temp[temp.length - 1];
for (var i = 0; i < size; i++) {
for (var j = 0; j < size; j++) {
ctx.fillStyle = palette[image[twoToOne(i, j)]];
ctx.fillRect(i, j, 1, 1);
}
}
/*
if (num % 2 === 0) {
ctx.fillStyle = palette[0];
} else {
ctx.fillStyle = palette[1];
}
ctx.fillRect(50, 50, 1, 1);
num++;
*/
setTimeout( function() { paint(); }, 1000 / fps );
}
</script>
</body>
</html>