···
9
9
<link rel="stylesheet" type="text/css" href="../css/home.css?r=<?=rand(0,999)?>">
10
10
</head>
11
11
12
12
-
<body style="background-color:#272730">
12
12
+
<body class="eight" style="background-color:#272730">
13
13
<section class="main">
14
14
<canvas style="background-color:#303039"></canvas>
15
15
</section>
16
16
-
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> -->
17
17
-
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.js"></script> -->
18
18
-
<script src="main.js"></script>
16
16
+
<script src="main.js?r=<?=rand(0,999)?>"></script>
19
17
</body>
20
18
</html>
···
7
7
<!--Roboto--> <link href="https://fonts.googleapis.com/css?family=Roboto:400,500" rel="stylesheet">
8
8
<!--Favicon: http:/#F986DF/realfavicongenerator.net -->
9
9
<link rel="stylesheet" type="text/css" href="../css/home.css?r=<?=rand(0,999)?>">
10
10
-
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.css">
11
10
</head>
12
11
13
13
-
<body style="background-color:#272730">
12
12
+
<body class="eight" style="background-color:#272730">
14
13
<section class="main">
15
14
<canvas style="background-color:#303039"></canvas>
16
15
</section>
17
17
-
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> -->
18
18
-
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquerymobile/1.4.5/jquery.mobile.min.js"></script> -->
19
16
<script src="main.js?r=<?=rand(0,999)?>"></script>
20
17
</body>
21
18
</html>
···
19
19
canvas.height = cw/2;
20
20
}
21
21
}
22
22
-
23
23
-
// update canvas size
24
22
resize();
25
25
-
// window.addEventListener("resize", function() {
26
26
-
// resize();
27
27
-
// });
28
23
29
29
-
var mousedown, mx, my, oldmx, oldmy, leftMargin, topMargin;
30
30
-
window.addEventListener("mousedown", function() {
31
31
-
mousedown = true;
24
24
+
// get position of the touch/mouse relative to canvas
25
25
+
function getTouchPos(e) {
26
26
+
return {
27
27
+
x: e.touches[0].clientX - canvas.getBoundingClientRect().left,
28
28
+
y: e.touches[0].clientY - canvas.getBoundingClientRect().top
29
29
+
}
30
30
+
}
31
31
+
function getMousePos(e) {
32
32
+
return {
33
33
+
x: e.clientX - canvas.getBoundingClientRect().left,
34
34
+
y: e.clientY - canvas.getBoundingClientRect().top
35
35
+
}
36
36
+
}
37
37
+
38
38
+
var pos = {}, posLast = {}, drawing;
39
39
+
// on touch down
40
40
+
canvas.addEventListener("touchstart", function(e) {
41
41
+
var touch = e.touches[0];
42
42
+
pos = getTouchPos(e);
43
43
+
startDraw();
44
44
+
}, false);
45
45
+
// on touch up
46
46
+
window.addEventListener("touchend", function (e) {
47
47
+
endDraw();
48
48
+
}, false);
49
49
+
// on touch move
50
50
+
window.addEventListener("touchmove", function(e) {
51
51
+
if (drawing) {
52
52
+
posLast = pos;
53
53
+
pos = getTouchPos(e);
54
54
+
draw();
55
55
+
}
56
56
+
}, false);
57
57
+
58
58
+
// on mouse down
59
59
+
canvas.addEventListener("mousedown", function(e) {
60
60
+
pos = getMousePos(e);
32
61
startDraw();
33
62
});
34
34
-
window.addEventListener("mouseup", function() {
35
35
-
mousedown = false;
36
36
-
});
37
37
-
window.addEventListener("click", function() {
38
38
-
leftMargin = (window.innerWidth - canvas.width)/2;
39
39
-
topMargin = (window.innerHeight - canvas.height)/2;
40
40
-
mx = window.event.clientX - leftMargin;
41
41
-
my = window.event.clientY - topMargin;
42
42
-
c.moveTo(mx, my);
43
43
-
c.arc(mx, my, 0.5, Math.PI*2, 0);
44
44
-
c.stroke();
63
63
+
window.addEventListener("mouseup", function(e) {
64
64
+
endDraw();
45
65
});
46
46
-
47
66
window.addEventListener("mousemove", function(e) {
48
48
-
if (mousedown) {
49
49
-
oldmx = mx;
50
50
-
oldmy = my;
51
51
-
leftMargin = (window.innerWidth - canvas.width)/2;
52
52
-
topMargin = (window.innerHeight - canvas.height)/2;
53
53
-
mx = window.event.clientX - leftMargin;
54
54
-
my = window.event.clientY - topMargin;
55
55
-
// if mousedown and inside canvas
67
67
+
if (drawing) {
68
68
+
posLast = pos;
69
69
+
pos = getMousePos(e);
56
70
draw();
57
71
}
58
72
});
59
73
74
74
+
60
75
function startDraw() {
61
61
-
c.beginPath();
76
76
+
console.log("start");
62
77
c.lineWidth = 1;
63
78
c.strokeStyle = "#ffffff";
79
79
+
drawing = true;
64
80
}
65
65
-
81
81
+
function endDraw() {
82
82
+
drawing = false;
83
83
+
}
66
84
function draw() {
67
67
-
c.lineTo(mx, my);
85
85
+
c.beginPath();
86
86
+
c.moveTo(posLast.x, posLast.y);
87
87
+
c.lineTo(pos.x, pos.y);
68
88
c.stroke();
69
89
}
90
90
+
91
91
+
92
92
+
93
93
+
94
94
+
function old() {
95
95
+
"use strict";
96
96
+
// init canvas
97
97
+
var canvas = document.querySelector("canvas");
98
98
+
var c = canvas.getContext("2d");
99
99
+
100
100
+
var cw, ch;
101
101
+
function resize() {
102
102
+
cw = window.innerWidth-50;
103
103
+
ch = window.innerHeight-50;
104
104
+
// if cw is odd, make aspect ratio correct (2:1)
105
105
+
if (cw%2 == 1) cw--;
106
106
+
107
107
+
// set canvas w/h
108
108
+
if (ch < cw/2) {
109
109
+
canvas.width = ch*2;
110
110
+
canvas.height = ch;
111
111
+
} else {
112
112
+
canvas.width = cw;
113
113
+
canvas.height = cw/2;
114
114
+
}
115
115
+
}
116
116
+
117
117
+
// update canvas size
118
118
+
resize();
119
119
+
// window.addEventListener("resize", function() {
120
120
+
// resize();
121
121
+
// });
122
122
+
123
123
+
var mousedown, mx, my, oldmx, oldmy, leftMargin, topMargin;
124
124
+
window.addEventListener("mousedown", function() {
125
125
+
mousedown = true;
126
126
+
startDraw();
127
127
+
});
128
128
+
window.addEventListener("mouseup", function() {
129
129
+
mousedown = false;
130
130
+
});
131
131
+
window.addEventListener("click", function() {
132
132
+
leftMargin = (window.innerWidth - canvas.width)/2;
133
133
+
topMargin = (window.innerHeight - canvas.height)/2;
134
134
+
mx = window.event.clientX - leftMargin;
135
135
+
my = window.event.clientY - topMargin;
136
136
+
c.moveTo(mx, my);
137
137
+
c.arc(mx, my, 0.5, Math.PI*2, 0);
138
138
+
c.stroke();
139
139
+
});
140
140
+
141
141
+
window.addEventListener("mousemove", function(e) {
142
142
+
if (mousedown) {
143
143
+
oldmx = mx;
144
144
+
oldmy = my;
145
145
+
leftMargin = (window.innerWidth - canvas.width)/2;
146
146
+
topMargin = (window.innerHeight - canvas.height)/2;
147
147
+
mx = window.event.clientX - leftMargin;
148
148
+
my = window.event.clientY - topMargin;
149
149
+
// if mousedown and inside canvas
150
150
+
draw();
151
151
+
}
152
152
+
});
153
153
+
154
154
+
function startDraw() {
155
155
+
c.beginPath();
156
156
+
c.lineWidth = 1;
157
157
+
c.strokeStyle = "#ffffff";
158
158
+
}
159
159
+
160
160
+
function draw() {
161
161
+
c.lineTo(mx, my);
162
162
+
c.stroke();
163
163
+
}
164
164
+
}
···
1
1
-
<!DOCTYPE html>
2
2
-
<html>
3
3
-
<head>
4
4
-
<meta charset="utf-8">
5
5
-
<meta name="theme-color" content="#db5945">
6
6
-
<title>Canvas</title>
7
7
-
<!--Roboto--> <link href="https://fonts.googleapis.com/css?family=Roboto:400,500" rel="stylesheet">
8
8
-
<!--Favicon: http:/#F986DF/realfavicongenerator.net -->
9
9
-
<link rel="stylesheet" type="text/css" href="../css/home.css?r=<?=rand(0,999)?>">
10
10
-
</head>
11
11
-
12
12
-
<body class="nine" style="background-color:#2B2330">
13
13
-
<section class="main">
14
14
-
<canvas style="background-color:#303039"></canvas>
15
15
-
</section>
16
16
-
<script src="main.js?r=<?=rand(0,999)?>"></script>
17
17
-
</body>
18
18
-
</html>
···
1
1
-
<!DOCTYPE html>
2
2
-
<html>
3
3
-
<head>
4
4
-
<meta charset="utf-8">
5
5
-
<meta name="theme-color" content="#db5945">
6
6
-
<title>Canvas</title>
7
7
-
<!--Roboto--> <link href="https://fonts.googleapis.com/css?family=Roboto:400,500" rel="stylesheet">
8
8
-
<!--Favicon: http:/#F986DF/realfavicongenerator.net -->
9
9
-
<link rel="stylesheet" type="text/css" href="../css/home.css?r=<?=rand(0,999)?>">
10
10
-
</head>
11
11
-
12
12
-
<body class="nine" style="background-color:#2B2330">
13
13
-
<section class="main">
14
14
-
<canvas style="background-color:#303039"></canvas>
15
15
-
</section>
16
16
-
<script src="main.js?r=<?=rand(0,999)?>"></script>
17
17
-
</body>
18
18
-
</html>
···
1
1
-
"use strict";
2
2
-
// init canvas
3
3
-
var canvas = document.querySelector("canvas");
4
4
-
var c = canvas.getContext("2d");
5
5
-
6
6
-
var cw, ch;
7
7
-
function resize() {
8
8
-
cw = window.innerWidth-50;
9
9
-
ch = window.innerHeight-50;
10
10
-
// if cw is odd, make aspect ratio correct (2:1)
11
11
-
if (cw%2 == 1) cw--;
12
12
-
13
13
-
// set canvas w/h
14
14
-
if (ch < cw/2) {
15
15
-
canvas.width = ch*2;
16
16
-
canvas.height = ch;
17
17
-
} else {
18
18
-
canvas.width = cw;
19
19
-
canvas.height = cw/2;
20
20
-
}
21
21
-
}
22
22
-
resize();
23
23
-
24
24
-
// get position of the touch/mouse relative to canvas
25
25
-
function getTouchPos(e) {
26
26
-
return {
27
27
-
x: e.touches[0].clientX - canvas.getBoundingClientRect().left,
28
28
-
y: e.touches[0].clientY - canvas.getBoundingClientRect().top
29
29
-
}
30
30
-
}
31
31
-
function getMousePos(e) {
32
32
-
return {
33
33
-
x: e.clientX - canvas.getBoundingClientRect().left,
34
34
-
y: e.clientY - canvas.getBoundingClientRect().top
35
35
-
}
36
36
-
}
37
37
-
38
38
-
var pos = {}, posLast = {}, drawing;
39
39
-
// on touch down
40
40
-
canvas.addEventListener("touchstart", function(e) {
41
41
-
var touch = e.touches[0];
42
42
-
pos = getTouchPos(e);
43
43
-
startDraw();
44
44
-
}, false);
45
45
-
// on touch up
46
46
-
window.addEventListener("touchend", function (e) {
47
47
-
endDraw();
48
48
-
}, false);
49
49
-
// on touch move
50
50
-
window.addEventListener("touchmove", function(e) {
51
51
-
if (drawing) {
52
52
-
posLast = pos;
53
53
-
pos = getTouchPos(e);
54
54
-
draw();
55
55
-
}
56
56
-
}, false);
57
57
-
58
58
-
// on mouse down
59
59
-
canvas.addEventListener("mousedown", function(e) {
60
60
-
pos = getMousePos(e);
61
61
-
startDraw();
62
62
-
});
63
63
-
window.addEventListener("mouseup", function(e) {
64
64
-
endDraw();
65
65
-
});
66
66
-
window.addEventListener("mousemove", function(e) {
67
67
-
if (drawing) {
68
68
-
posLast = pos;
69
69
-
pos = getMousePos(e);
70
70
-
draw();
71
71
-
}
72
72
-
});
73
73
-
74
74
-
75
75
-
function startDraw() {
76
76
-
console.log("start");
77
77
-
c.lineWidth = 1;
78
78
-
c.strokeStyle = "#ffffff";
79
79
-
drawing = true;
80
80
-
}
81
81
-
function endDraw() {
82
82
-
drawing = false;
83
83
-
}
84
84
-
function draw() {
85
85
-
c.beginPath();
86
86
-
c.moveTo(posLast.x, posLast.y);
87
87
-
c.lineTo(pos.x, pos.y);
88
88
-
c.stroke();
89
89
-
}
90
90
-
91
91
-
92
92
-
93
93
-
94
94
-
function old() {
95
95
-
"use strict";
96
96
-
// init canvas
97
97
-
var canvas = document.querySelector("canvas");
98
98
-
var c = canvas.getContext("2d");
99
99
-
100
100
-
var cw, ch;
101
101
-
function resize() {
102
102
-
cw = window.innerWidth-50;
103
103
-
ch = window.innerHeight-50;
104
104
-
// if cw is odd, make aspect ratio correct (2:1)
105
105
-
if (cw%2 == 1) cw--;
106
106
-
107
107
-
// set canvas w/h
108
108
-
if (ch < cw/2) {
109
109
-
canvas.width = ch*2;
110
110
-
canvas.height = ch;
111
111
-
} else {
112
112
-
canvas.width = cw;
113
113
-
canvas.height = cw/2;
114
114
-
}
115
115
-
}
116
116
-
117
117
-
// update canvas size
118
118
-
resize();
119
119
-
// window.addEventListener("resize", function() {
120
120
-
// resize();
121
121
-
// });
122
122
-
123
123
-
var mousedown, mx, my, oldmx, oldmy, leftMargin, topMargin;
124
124
-
window.addEventListener("mousedown", function() {
125
125
-
mousedown = true;
126
126
-
startDraw();
127
127
-
});
128
128
-
window.addEventListener("mouseup", function() {
129
129
-
mousedown = false;
130
130
-
});
131
131
-
window.addEventListener("click", function() {
132
132
-
leftMargin = (window.innerWidth - canvas.width)/2;
133
133
-
topMargin = (window.innerHeight - canvas.height)/2;
134
134
-
mx = window.event.clientX - leftMargin;
135
135
-
my = window.event.clientY - topMargin;
136
136
-
c.moveTo(mx, my);
137
137
-
c.arc(mx, my, 0.5, Math.PI*2, 0);
138
138
-
c.stroke();
139
139
-
});
140
140
-
141
141
-
window.addEventListener("mousemove", function(e) {
142
142
-
if (mousedown) {
143
143
-
oldmx = mx;
144
144
-
oldmy = my;
145
145
-
leftMargin = (window.innerWidth - canvas.width)/2;
146
146
-
topMargin = (window.innerHeight - canvas.height)/2;
147
147
-
mx = window.event.clientX - leftMargin;
148
148
-
my = window.event.clientY - topMargin;
149
149
-
// if mousedown and inside canvas
150
150
-
draw();
151
151
-
}
152
152
-
});
153
153
-
154
154
-
function startDraw() {
155
155
-
c.beginPath();
156
156
-
c.lineWidth = 1;
157
157
-
c.strokeStyle = "#ffffff";
158
158
-
}
159
159
-
160
160
-
function draw() {
161
161
-
c.lineTo(mx, my);
162
162
-
c.stroke();
163
163
-
}
164
164
-
}
···
6
6
7
7
section canvas { position: relative; transform: translate(-50%, -50%); top: 50%; left: 50%; display: block; }
8
8
9
9
-
body.nine section canvas { touch-action: none; }
9
9
+
body.eight section canvas { touch-action: none; }
···
18
18
// width: 300px
19
19
// height: 150px
20
20
display: block
21
21
-
body.nine section canvas
21
21
+
body.eight section canvas
22
22
touch-action: none
···
10
10
</head>
11
11
12
12
<body>
13
13
-
<a href="9">9. Touch drawing board</a>
14
14
-
<a href="8">8. Drawing board</a>
13
13
+
<a href="8">8. Drawing board, supports touch & mouse</a>
15
14
<a href="7">7. Three fancy loading animations</a>
16
15
<a href="5">5. and now they bounce off of each other</a>
17
16
<a href="4">4. many boucning circles with random colors, radiuses and window resize support</a>