forked from littledivy/drawille
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sine.ts
66 lines (54 loc) · 1.11 KB
/
sine.ts
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
import Canvas from "../canvas.ts";
let c = new Canvas();
let ctx = c.getContext("2d");
let w = c.width;
let h = c.height / 2;
let f = 2;
function calcSineY(x: number) {
return h - h * Math.sin(x * 2 * Math.PI * (f / w));
}
function drawSine(x: number) {
ctx.clear();
ctx.clearRect(0, 0, w, h * 2);
ctx.beginPath();
ctx.moveTo(0, h);
ctx.lineTo(w, h);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, h);
for (var i = 0; i < x; i++) {
let y = calcSineY(x);
ctx.moveTo(i, y);
ctx.lineTo(x, y);
}
ctx.stroke();
ctx.beginPath();
for (var i = 0; i < x; i++) {
let y = calcSineY(x);
ctx.moveTo(x, h);
ctx.lineTo(x, y);
}
ctx.stroke();
ctx.beginPath();
for (var i = 0; i < x; i++) {
if (i / 3 == Math.round(i / 3)) {
let y = calcSineY(i);
ctx.moveTo(i, h);
ctx.lineTo(i, y);
}
}
ctx.stroke();
ctx.beginPath();
for (var i = 0; i < x; i++) {
let y = calcSineY(i);
ctx.lineTo(i, y);
}
ctx.stroke();
console.log(ctx.toString());
}
let x = 0;
let interval = setInterval(function () {
drawSine(x);
x++;
if (x > w) x = 0;
}, 20);