-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarks.html
180 lines (145 loc) · 5.1 KB
/
benchmarks.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
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
<!DOCTYPE html >
<!--
http://proceduralgraphics.blogspot.com
By Dave Lawrence on 24/4/2010
Optimisations:
draw boids to a canvas, then blit that across instead of using trig
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
html,body { width:100%; height:100%; margin:0; padding:0; overflow:hidden; }
</style>
<title></title>
<script type="text/javascript" src="util.js"></script>
<script type="text/javascript" src="drawing.js"></script>
<script type="text/javascript">
var width = window.innerWidth;
var height = window.innerHeight;
var fps = 10;
var ctx;
var sky;
var bird;
// write out canvas as we have to put width/height inline in the HTML, CSS will just scale pixels
document.write('<canvas id="canvas" width=' + width + ' height=' + height + '></canvas>');
function setup() {
ctx = document.getElementById("canvas").getContext("2d");
sky = newCanvas(width, height);
drawBackground(sky.getContext("2d"));
perlin_noise(sky);
var birdSize = 200;
var func = function(ctx) {
ctx.scale(200, 200);
drawBird(ctx, fill);
}
bird = shapeToCanvas(200, 200, rgba(0,0,0,0), "#000000", func);
//setInterval(draw, 1000 / fps);
//draw();
ctx.drawImage(sky, 0, 0);
var startTime = new Date();
var BIRDS = 100;
for (var i=0 ; i<BIRDS ; ++i) {
ctx.save();
ctx.translate(rand(width), rand(height));
ctx.rotate(rand(2*Math.PI));
ctx.drawImage(bird, 200, 200);
ctx.restore();
}
var timeTaken = new Date().getTime() - startTime.getTime();
var bitmapTime = timeTaken / BIRDS;
////////////////////////
ctx.drawImage(sky, 0, 0);
startTime = new Date();
ctx.fillStyle = "#FF0000";
for (var i=0 ; i<BIRDS ; ++i) {
ctx.save();
ctx.translate(rand(width), rand(height));
ctx.rotate(rand(2*Math.PI));
ctx.scale(200, 200);
drawBird(ctx, fill);
//ctx.drawImage(bird, 200, 200);
ctx.restore();
}
var timeTaken = new Date().getTime() - startTime.getTime();
var curveTime = timeTaken / BIRDS;
alert('bitmap time = ' + bitmapTime + ' curve time = ' + curveTime);
}
function drawBackground(ctx) {
ctx.fillStyle = "#0000FF";
ctx.fillRect(0,0,width,height);
}
function draw() {
ctx.drawImage(sky, 0, 0);
ctx.drawImage(bird, 500, 500);
drawMain();
}
function drawMain() {
//perlin_noise(document.getElementById("canvas"));
for (var i=0 ; i<1 ; ++i) {
ctx.save();
//ctx.translate(width/2, height/2);
ctx.translate(rand(width), rand(height));
ctx.scale(50, 50);
ctx.restore();
}
}
function drawBird(ctx, op) {
ctx.lineWidth = 0.005;
var bird = [
[.02, -0.3425, .015, -.31],
[.06, -.28, .05, -.2],
[.2, -.35, .5, .25],
[.2, -.15, .05, -.075],
[.045, .1, .045, .1],
[.06, .2, .14, .375],
[0.02, .2, 0, .125]
];
var drawBirdSide = function() {
ctx.moveTo(0, -.375);
for (var i=0 ; i<bird.length ; ++i) {
var curve = bird[i];
ctx.quadraticCurveTo(curve[0], curve[1], curve[2], curve[3]);
//ctx.quadraticCurveTo(bird[i][0], bird[i][1], bird[i][2], bird[i][3]);
}
}
ctx.beginPath();
drawBirdSide();
ctx.transform(-1, 0, 0, 1, 0, 0); // mirror
drawBirdSide();
op(ctx);
}
// Based on code by iron_wallaby posted at http://www.ozoneasylum.com/30982
// but I changed the noise to be white with random transparency for a cloud like effect
function perlin_noise (canvas) {
var canvas_ctx = canvas.getContext ("2d");
var saved_alpha = canvas_ctx.globalAlpha;
var offscreen = newCanvas(canvas.width, canvas.height);
var offscreen_ctx = offscreen.getContext ("2d");
var offscreen_id = offscreen_ctx.getImageData (0, 0,
offscreen.width,
offscreen.height);
var offscreen_pixels = offscreen_id.data;
for (var i = 0; i < offscreen_pixels.length; i += 4) {
offscreen_pixels[i ] = 255;
offscreen_pixels[i + 1] = 255;
offscreen_pixels[i + 2] = 255;
offscreen_pixels[i + 3] = irand(255);
}
offscreen_ctx.putImageData (offscreen_id, 0, 0);
/* Scale random iterations onto the canvas to generate Perlin noise. */
for (var size = 4; size <= offscreen.width; size *= 2) {
var x = irand(offscreen.width - size);
var y = irand(offscreen.height - size);
y = Math.max(y, 0);
var verticalSize = Math.min(size,offscreen.height);
canvas_ctx.globalAlpha = 4 / size;
canvas_ctx.drawImage (offscreen, x, y, size, verticalSize,
0, 0, canvas.width, canvas.height);
}
canvas_ctx.globalAlpha = saved_alpha;
}
</script>
</head>
<body onload="setup()">
</body>
</html>