JSDM

HTML

 
1
<head>
2
  <meta charset="UTF-8">
3
  <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
4
5
  <title>sin_cos</title>
6
7
  <link rel="stylesheet" type="text/css" href="css/main.css">
8
</head>
9
<body>
10
  <canvas id="drawing_canvas"></canvas>
11
12
  <script src="//cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
13
  <script src="js/main.js"></script>
14
</body>

CSS

xxxxxxxxxx
22
 
1
html, body {
2
    width: 100%;
3
    height: 100%;
4
    overflow: hidden;
5
}
6
7
body {
8
    background-color: #222;
9
    margin: 0;
10
}
11
12
#drawing_canvas {
13
    background-color: #aaa;
14
    position: absolute;
15
    margin: auto;
16
    width: 1024px;
17
    height: 480px;
18
    top: 0;
19
    bottom: 0;
20
    left: 0;
21
    right: 0;
22
}
? ?
? ?
必须是有效的URL
+ 添加另一个资源

JS

xxxxxxxxxx
174
 
1
const TWO_PI = Math.PI * 2;
2
const HALF_PI = Math.PI * 0.5;
3
4
var viewWidth = 1024;
5
var viewHeight = 480;
6
var drawingCanvas, ctx;
7
var speed = 60;
8
var timeStep = (1/speed);
9
var time = 0;
10
var radius = 100;
11
var points = [];
12
13
initDrawingCanvas();
14
initGUI();
15
16
function initGUI() {
17
    var gui = new dat.GUI();
18
19
    gui.add(window, "radius", 20, 100);
20
    gui.add(window, "speed", 1, 120).onChange(function() {
21
        timeStep = (1 / (121 - speed));
22
    });
23
}
24
25
function initDrawingCanvas() {
26
    drawingCanvas = document.getElementById("drawing_canvas");
27
    drawingCanvas.width = viewWidth;
28
    drawingCanvas.height = viewHeight;
29
    ctx = drawingCanvas.getContext('2d');
30
}
31
32
function draw() {
33
    // update points
34
    var x = -Math.sin(time) * radius + 120;
35
    var y = Math.cos(time) * radius + 120;
36
37
    points.unshift({x:x, y:y});
38
    if (points.length > 4096) points.pop();
39
40
    ctx.lineWidth = 2;
41
    ctx.lineCap = "round";
42
43
    clear();
44
    drawBackground();
45
    drawProjectionLines(x, y);
46
47
    // draw line from circle center to [x, y]
48
    ctx.strokeStyle = "#fff";
49
    drawLine(120, 120, x, y);
50
51
    // draw little circles at intersections
52
    ctx.fillStyle = "#fff";
53
    drawCircleFill(x, y, 5);
54
55
    ctx.fillStyle = "#f0f";
56
    drawCircleFill(240, y, 5);
57
58
    ctx.fillStyle = "#0ff";
59
    drawCircleFill(x, 240, 5);
60
    drawCircleFill(240, 480 - x, 5);
61
62
    // draw the waves
63
    drawSinWave();
64
    drawCosWave();
65
66
    // next frame
67
    time += timeStep;
68
    requestAnimationFrame(draw);
69
}
70
71
function clear() {
72
    ctx.fillStyle = '#000';
73
    ctx.fillRect(0, 0, viewWidth, viewHeight);
74
}
75
76
function drawBackground() {
77
    var dividerCount = Math.ceil((viewWidth - 240) / (radius * Math.PI)) * 2,
78
        dividerX;
79
80
    // top graph
81
    ctx.strokeStyle = '#333';
82
    drawLine(120, 120 - radius, viewWidth, 120 - radius);
83
    drawLine(0, 120, viewWidth, 120);
84
    drawLine(120, 120 + radius, viewWidth, 120 + radius);
85
    // bottom graph
86
    drawLine(240, 360 - radius, viewWidth, 360 - radius);
87
    drawLine(240, 360, viewWidth, 360);
88
    drawLine(240, 360 + radius, viewWidth, 360 + radius);
89
    drawLine(120 - radius, 120, 120 - radius, 240);
90
    drawLine(120, 0, 120, 240);
91
    drawLine(120 + radius, 120, 120 + radius, 240);
92
    // arcs
93
    drawArc(240, 240, 120 - radius, HALF_PI, Math.PI);
94
    drawArc(240, 240, 120, HALF_PI, Math.PI);
95
    drawArc(240, 240, 120 + radius, HALF_PI, Math.PI);
96
    // vertical dividers
97
    for (var i = 0; i < dividerCount; i++) {
98
        dividerX = 240 + radius * HALF_PI * i;
99
        dividerX += radius * (time % HALF_PI);
100
101
        drawLine(dividerX, 0, dividerX, 480);
102
    }
103
    // circle and axis
104
    ctx.strokeStyle = '#777';
105
    drawLine(240, 0, 240, viewHeight);
106
    drawLine(0, 240, viewWidth, 240);
107
    drawCircleStroke(120, 120, radius);
108
}
109
110
function drawProjectionLines(x, y) {
111
    // draw lines connecting the circle and waves
112
    ctx.setLineDash([5]);
113
114
    ctx.strokeStyle = '#f0f';
115
    drawLine(x, y, 240, y);
116
117
    ctx.strokeStyle = '#0ff';
118
    ctx.beginPath();
119
    ctx.moveTo(x, y);
120
    ctx.lineTo(x, 240);
121
    ctx.arcTo(x, 480 - x, 240, 480 - x, 240 - x);
122
    ctx.stroke();
123
124
    ctx.setLineDash([]);
125
}
126
127
function drawSinWave() {
128
    ctx.strokeStyle = "#f0f";
129
    ctx.beginPath();
130
131
    for (var i = 0; i < points.length; i++) {
132
        ctx.lineTo(i * timeStep * (radius) + 240, points[i].y);
133
    }
134
135
    ctx.stroke();
136
}
137
138
function drawCosWave() {
139
    ctx.strokeStyle = "#0ff";
140
    ctx.beginPath();
141
142
    for (var j = 0; j < points.length; j++) {
143
        ctx.lineTo(240 + j * timeStep * radius, -points[j].x + 480);
144
    }
145
146
    ctx.stroke();
147
}
148
149
function drawLine(x1, y1, x2, y2) {
150
    ctx.beginPath();
151
    ctx.moveTo(x1, y1);
152
    ctx.lineTo(x2, y2);
153
    ctx.stroke();
154
}
155
156
function drawCircleFill(x, y, r) {
157
    ctx.beginPath();
158
    ctx.arc(x, y, r, 0, TWO_PI);
159
    ctx.fill();
160
}
161
162
function drawCircleStroke(x, y, r) {
163
    ctx.beginPath();
164
    ctx.arc(x, y, r, 0, TWO_PI);
165
    ctx.stroke();
166
}
167
168
function drawArc(x, y, r, s, e) {
169
    ctx.beginPath();
170
    ctx.arc(x, y, r, s, e);
171
    ctx.stroke();
172
}
173
174
requestAnimationFrame(draw);
必须是有效的URL
+ 添加另一个资源
Close

文件管理 点击文件查看URL

图片

  1. 暂无文件

CSS

  1. 暂无文件

JavaScript

  1. 暂无文件

其他

  1. 暂无文件
拖动文件到上面的区域或者:
加载中 ..................