forked from ncase/sight-and-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft0.html
117 lines (100 loc) · 2.34 KB
/
draft0.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
116
117
<!doctype>
<html>
<head>
<style>
body{
margin:0;
background: #eee;
}
#canvas{
width:640px;
height:360px;
background: #fff;
}
</style>
</head>
<body>
<canvas id="canvas" width="640" height="360"></canvas>
</body>
</html>
<script>
// DRAWING
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
function draw(){
// Clear canvas
ctx.clearRect(0,0,canvas.width,canvas.height);
// Draw segments
ctx.strokeStyle = "#999";
for(var i=0;i<segments.length;i++){
var seg = segments[i];
ctx.beginPath();
ctx.moveTo(seg.a.x,seg.a.y);
ctx.lineTo(seg.b.x,seg.b.y);
ctx.stroke();
}
// Draw red dot
ctx.fillStyle = "#dd3838";
ctx.beginPath();
ctx.arc(Mouse.x, Mouse.y, 4, 0, 2*Math.PI, false);
ctx.fill();
}
// LINE SEGMENTS
var segments = [
// Border
{a:{x:0,y:0}, b:{x:640,y:0}},
{a:{x:640,y:0}, b:{x:640,y:360}},
{a:{x:640,y:360}, b:{x:0,y:360}},
{a:{x:0,y:360}, b:{x:0,y:0}},
// Polygon #1
{a:{x:100,y:150}, b:{x:120,y:50}},
{a:{x:120,y:50}, b:{x:200,y:80}},
{a:{x:200,y:80}, b:{x:140,y:210}},
{a:{x:140,y:210}, b:{x:100,y:150}},
// Polygon #2
{a:{x:100,y:200}, b:{x:120,y:250}},
{a:{x:120,y:250}, b:{x:60,y:300}},
{a:{x:60,y:300}, b:{x:100,y:200}},
// Polygon #3
{a:{x:200,y:260}, b:{x:220,y:150}},
{a:{x:220,y:150}, b:{x:300,y:200}},
{a:{x:300,y:200}, b:{x:350,y:320}},
{a:{x:350,y:320}, b:{x:200,y:260}},
// Polygon #4
{a:{x:340,y:60}, b:{x:360,y:40}},
{a:{x:360,y:40}, b:{x:370,y:70}},
{a:{x:370,y:70}, b:{x:340,y:60}},
// Polygon #5
{a:{x:450,y:190}, b:{x:560,y:170}},
{a:{x:560,y:170}, b:{x:540,y:270}},
{a:{x:540,y:270}, b:{x:430,y:290}},
{a:{x:430,y:290}, b:{x:450,y:190}},
// Polygon #6
{a:{x:400,y:95}, b:{x:580,y:50}},
{a:{x:580,y:50}, b:{x:480,y:150}},
{a:{x:480,y:150}, b:{x:400,y:95}}
];
// DRAW LOOP
window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame;
var updateCanvas = true;
function drawLoop(){
requestAnimationFrame(drawLoop);
if(updateCanvas){
draw();
updateCanvas = false;
}
}
window.onload = function(){
drawLoop();
};
// MOUSE
var Mouse = {
x: canvas.width/2,
y: canvas.height/2
};
canvas.onmousemove = function(event){
Mouse.x = event.clientX;
Mouse.y = event.clientY;
updateCanvas = true;
};
</script>