-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsdraw.html
115 lines (107 loc) · 3.17 KB
/
jsdraw.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
<html>
<head>
<title>JS Draw!</title>
</head>
<body>
<!-- TODO center the canvas does not work yet -->
<!-- <canvas id="myCanvas" width="1000" height="750" style="background-color: grey; margin-left=auto; margin-right=auto; display=block;"/> -->
<div>
<canvas id="myCanvas" width="1200" height="500" style="background-color: #eee;"></canvas>
</div>
<a id="download" download="my_image.jpg" href="" style="display: none;">Download to my_image.jpg</a>
<script type="text/javascript" src="util.js"></script>
<script type="text/javascript" src="pytha.js"></script>
<script>
function draw_rect_demo(ctx) {
ctx.fillStyle = "#00ff00";
ctx.fillRect(0, 0, 20, 40);
}
function draw_circle_demo(canvas, ctx) {
reset_canvas(canvas, ctx);
num_circle = rand_int(5, 11);
color_list = ["#ff0000", "#00ff00", "#0000ff"];
for (var circ_id = 0; circ_id < num_circle; ++circ_id) {
x = rand_int(0, canvas.width)
y = rand_int(0, canvas.height)
radius = rand_int(20, 50)
color = color_list[rand_int(0, color_list.length)];
ctx.fillStyle = color;
ctx.beginPath()
ctx.arc(x, y, radius, 0, 3.14 * 2);
ctx.fill()
}
setTimeout(function() { draw_circle_demo(canvas, ctx);}, 1000);
}
// y = yscale * sin(x / xscale)
function draw_sin(canvas, ctx, shift=0, ang=0) {
reset_canvas(canvas, ctx);
orig_x = canvas.width / 2
orig_y = canvas.height / 2
xscale = 20
yscale = -20
for (var x = 0; x < canvas.width - 10; ++x) {
var y = yscale * Math.sin((x + shift) / xscale);
ctx.strokeStyle = "#ff0000"
rot1 = rotate_vec(x, y, ang);
rot2 = rotate_vec(x, -y, ang);
draw_pixel(ctx, rot1[0] + orig_x, rot1[1] + orig_y)
ctx.strokeStyle = "#00ff00"
draw_pixel(ctx, rot2[0] + orig_x, rot2[1] + orig_y)
}
setTimeout(function() { draw_sin(canvas, ctx, shift + 1, ang + 3.14 / 1000); }, 10);
}
// r = a*(1 - sin(theta))
function draw_heart(canvas, ctx, clr=128, inc=4) {
ctx.fillStyle = to_rgbstr(clr, 0, 0);
tot_step = 3600
step_size = (2 * 3.1415927) / tot_step
a = 100
cx = canvas.width / 2
cy = canvas.height / 3
ctx.beginPath()
for (var step = 0; step < tot_step; ++step) {
theta = step_size * step;
r = a * (1 - Math.sin(theta))
x = r * Math.cos(theta)
y = r * Math.sin(theta)
if (step == 0) {
ctx.moveTo(cx + x, cy - y)
} else {
ctx.lineTo(cx + x, cy - y)
}
}
ctx.fill()
setTimeout(function() {
new_clr = clr + inc
if (clr < 128 || clr > 255) {
inc = -inc;
new_clr = clr + inc;
}
draw_heart(canvas, ctx, new_clr, inc);
}, 20);
}
var canvas = document.getElementById("myCanvas");
console.log(`canvas width ${canvas.width} height ${canvas.height}`);
var ctx = canvas.getContext("2d");
var cmd = http_get_params()['cmd']
var animate = http_get_params()['animate']
if (!cmd) {
cmd = "circle"; // default
}
console.log(`cmd is ${cmd}`);
if (cmd == "sin") {
draw_sin(canvas, ctx)
} else if (cmd == "rect_demo") {
draw_rect_demo(ctx);
} else if (cmd == "pytha") {
draw_pythagoras_tree(canvas, ctx, !!animate);
} else if (cmd == "circle") {
draw_circle_demo(canvas, ctx);
} else if (cmd == "heart") {
draw_heart(canvas, ctx)
} else {
alert(`Invalid command ${cmd}`);
}
</script>
</body>
</html>