-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample02.html
65 lines (53 loc) · 1.32 KB
/
example02.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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="scripts/Physical.base.js"></script>
<script type="text/javascript" src="scripts/Glimmer.core.js"></script>
</head>
<body>
<script type="text/javascript">
function drawPoint(ctx, p){
if(!p) return;
ctx.beginPath();
ctx.rect(p.x - 2.5, p.y - 2.5, 5, 5);
ctx.stroke();
}
function drawLines(ctx, ps){
if(!ps || ps.length < 2) return;
ctx.beginPath();
ctx.moveTo(ps[0].x, ps[0].y);
for(var i = 1, len = ps.length; i < len; i++){
ctx.lineTo(ps[i].x, ps[i].y);
}
ctx.stroke();
}
function drawSegment(ctx, seg){
if(!seg) return;
ctx.beginPath();
ctx.moveTo(seg.o.x, seg.o.y);
ctx.lineTo(seg.e.x, seg.e.y);
ctx.stroke();
}
var canvas = document.createElement('canvas');
canvas.style.cssText = 'border:1px solid #000';
var ctx = canvas.getContext('2d');
var reflector = Segment.create(60, 50, 200, 120);
var light = Segment.create(5, 140, 500, 10);
window.onload = function(){
document.body.appendChild(canvas);
drawSegment(ctx, reflector);
setInterval(function(){
var ls = reflector.reflect(light);
if(!!ls){
drawSegment(ctx, ls[0]);
drawSegment(ctx, ls[1]);
} else {
drawSegment(ctx, light);
}
light.rotate(Angle.degree(-7))
}, 50);
}
</script>
</body>
</html>