-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathdrawstuff.js
More file actions
218 lines (192 loc) · 8.81 KB
/
Copy pathdrawstuff.js
File metadata and controls
218 lines (192 loc) · 8.81 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* classes */
// Color constructor
class Color {
constructor(r,g,b,a) {
try {
if ((typeof(r) !== "number") || (typeof(g) !== "number") || (typeof(b) !== "number") || (typeof(a) !== "number"))
throw "color component not a number";
else if ((r<0) || (g<0) || (b<0) || (a<0))
throw "color component less than 0";
else if ((r>255) || (g>255) || (b>255) || (a>255))
throw "color component bigger than 255";
else {
this.r = r; this.g = g; this.b = b; this.a = a;
}
} // end try
catch (e) {
console.log(e);
}
} // end Color constructor
// Color change method
change(r,g,b,a) {
try {
if ((typeof(r) !== "number") || (typeof(g) !== "number") || (typeof(b) !== "number") || (typeof(a) !== "number"))
throw "color component not a number";
else if ((r<0) || (g<0) || (b<0) || (a<0))
throw "color component less than 0";
else if ((r>255) || (g>255) || (b>255) || (a>255))
throw "color component bigger than 255";
else {
this.r = r; this.g = g; this.b = b; this.a = a;
}
} // end throw
catch (e) {
console.log(e);
}
} // end Color change method
} // end color class
/* utility functions */
// draw a pixel at x,y using color
function drawPixel(imagedata,x,y,color) {
try {
if ((typeof(x) !== "number") || (typeof(y) !== "number"))
throw "drawpixel location not a number";
else if ((x<0) || (y<0) || (x>=imagedata.width) || (y>=imagedata.height))
throw "drawpixel location outside of image";
else if (color instanceof Color) {
var pixelindex = (y*imagedata.width + x) * 4;
imagedata.data[pixelindex] = color.r;
imagedata.data[pixelindex+1] = color.g;
imagedata.data[pixelindex+2] = color.b;
imagedata.data[pixelindex+3] = color.a;
} else
throw "drawpixel color is not a Color";
} // end try
catch(e) {
console.log(e);
}
} // end drawPixel
// draw random pixels
function drawRandPixels(context) {
var c = new Color(0,0,0,0); // the color at the pixel: black
var w = context.canvas.width;
var h = context.canvas.height;
var imagedata = context.createImageData(w,h);
const PIXEL_DENSITY = 0.01;
var numPixels = (w*h)*PIXEL_DENSITY;
// Loop over 1% of the pixels in the image
for (var x=0; x<numPixels; x++) {
c.change(Math.random()*255,Math.random()*255,
Math.random()*255,255); // rand color
drawPixel(imagedata,
Math.floor(Math.random()*w),
Math.floor(Math.random()*h),
c);
} // end for x
context.putImageData(imagedata, 0, 0);
} // end draw random pixels
// get the input ellipsoids from the standard class URL
function getInputEllipsoids() {
const INPUT_ELLIPSOIDS_URL =
"https://ncsucgclass.github.io/prog1/ellipsoids.json";
// load the ellipsoids file
var httpReq = new XMLHttpRequest(); // a new http request
httpReq.open("GET",INPUT_ELLIPSOIDS_URL,false); // init the request
httpReq.send(null); // send the request
var startTime = Date.now();
while ((httpReq.status !== 200) && (httpReq.readyState !== XMLHttpRequest.DONE)) {
if ((Date.now()-startTime) > 3000)
break;
} // until its loaded or we time out after three seconds
if ((httpReq.status !== 200) || (httpReq.readyState !== XMLHttpRequest.DONE)) {
console.log*("Unable to open input ellipses file!");
return String.null;
} else
return JSON.parse(httpReq.response);
} // end get input ellipsoids
// put random points in the ellipsoids from the class github
function drawRandPixelsInInputEllipsoids(context) {
var inputEllipsoids = getInputEllipsoids();
var w = context.canvas.width;
var h = context.canvas.height;
var imagedata = context.createImageData(w,h);
const PIXEL_DENSITY = 0.1;
var numCanvasPixels = (w*h)*PIXEL_DENSITY;
if (inputEllipsoids != String.null) {
var x = 0; var y = 0; // pixel coord init
var cx = 0; var cy = 0; // init center x and y coord
var ellipsoidXRadius = 0; // init ellipsoid x radius
var ellipsoidYRadius = 0; // init ellipsoid y radius
var numEllipsoidPixels = 0; // init num pixels in ellipsoid
var c = new Color(0,0,0,0); // init the ellipsoid color
var n = inputEllipsoids.length; // the number of input ellipsoids
//console.log("number of ellipses: " + n);
// Loop over the ellipsoids, draw rand pixels in each
for (var e=0; e<n; e++) {
cx = w*inputEllipsoids[e].x; // ellipsoid center x
cy = h*inputEllipsoids[e].y; // ellipsoid center y
ellipsoidXRadius = Math.round(w*inputEllipsoids[e].a); // x radius
ellipsoidYRadius = Math.round(h*inputEllipsoids[e].b); // y radius
numEllipsoidPixels = ellipsoidXRadius*ellipsoidYRadius*Math.PI; // projected ellipsoid area
numEllipsoidPixels *= PIXEL_DENSITY; // percentage of ellipsoid area to render to pixels
numEllipsoidPixels = Math.round(numEllipsoidPixels);
//console.log("ellipsoid x radius: "+ellipsoidXRadius);
//console.log("ellipsoid y radius: "+ellipsoidYRadius);
//console.log("num ellipsoid pixels: "+numEllipsoidPixels);
c.change(
inputEllipsoids[e].diffuse[0]*255,
inputEllipsoids[e].diffuse[1]*255,
inputEllipsoids[e].diffuse[2]*255,
255); // ellipsoid diffuse color
for (var p=0; p<numEllipsoidPixels; p++) {
do {
x = Math.random()*2 - 1; // in unit square
y = Math.random()*2 - 1; // in unit square
} while (Math.sqrt(x*x + y*y) > 1) // a circle is also an ellipse
drawPixel(imagedata,
cx+Math.round(x*ellipsoidXRadius),
cy+Math.round(y*ellipsoidYRadius),c);
//console.log("color: ("+c.r+","+c.g+","+c.b+")");
//console.log("x: "+Math.round(w*inputEllipsoids[e].x));
//console.log("y: "+Math.round(h*inputEllipsoids[e].y));
} // end for pixels in ellipsoid
} // end for ellipsoids
context.putImageData(imagedata, 0, 0);
} // end if ellipsoids found
} // end draw rand pixels in input ellipsoids
// draw 2d projections read from the JSON file at class github
function drawInputEllipsoidsUsingArcs(context) {
var inputEllipsoids = getInputEllipsoids();
if (inputEllipsoids != String.null) {
var c = new Color(0,0,0,0); // the color at the pixel: black
var w = context.canvas.width;
var h = context.canvas.height;
var n = inputEllipsoids.length;
//console.log("number of ellipsoids: " + n);
// Loop over the ellipsoids, draw each in 2d
for (var e=0; e<n; e++) {
context.fillStyle =
"rgb(" + Math.floor(inputEllipsoids[e].diffuse[0]*255)
+","+ Math.floor(inputEllipsoids[e].diffuse[1]*255)
+","+ Math.floor(inputEllipsoids[e].diffuse[2]*255) +")"; // diffuse color
context.save(); // remember previous (non-) scale
context.scale(1, inputEllipsoids[e].b/inputEllipsoids[e].a); // scale by ellipsoid ratio
context.beginPath();
context.arc(
Math.round(w*inputEllipsoids[e].x),
Math.round(h*inputEllipsoids[e].y),
Math.round(w*inputEllipsoids[e].a),
0,2*Math.PI);
context.restore(); // undo scale before fill so stroke width unscaled
context.fill();
//console.log(context.fillStyle);
//console.log("x: "+Math.round(w*inputEllipsoids[e].x));
//console.log("y: "+Math.round(h*inputEllipsoids[e].y));
//console.log("a: "+Math.round(w*inputEllipsoids[e].a));
//console.log("b: "+Math.round(h*inputEllipsoids[e].b));
} // end for ellipsoids
} // end if ellipsoids found
} // end draw input ellipsoids
/* main -- here is where execution begins after window load */
function main() {
// Get the canvas and context
var canvas = document.getElementById("viewport");
var context = canvas.getContext("2d");
// Create the image
//drawRandPixels(context);
// shows how to draw pixels
drawRandPixelsInInputEllipsoids(context);
// shows how to draw pixels and read input file
//drawInputEllipsoidsUsingArcs(context);
// shows how to read input file, but not how to draw pixels
}