-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypelighter.html
128 lines (103 loc) · 3.32 KB
/
typelighter.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
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
<head>
<meta name="twitter:card" content="summary" />
<meta name="twitter:creator" content="@TheOisinMoran" />
<meta property="og:url" content="https://oisinmoran.com/typelighter" />
<meta property="og:title" content="Typelighter" />
<meta property="og:description" content="Type lighter." />
<!-- <meta property="og:image" content="https://oisinmoran.com/typelighter.png" /> -->
<style type="text/css">
* { margin:0; padding:0; }
html, body { width:100%; height:100%; cursor:text;}
#trash {
position: absolute;
left: 10px;
top: 10px;
cursor: pointer;
opacity: 50%;
}
#trash:hover {
opacity: 100%;
}
</style>
</head>
<body>
<canvas id="myCanvas" style="display: block;">
Your browser does not support the HTML canvas tag :(
</canvas>
<img src="trash-solid.svg" width="20px" id="trash" onclick="clearCanvas()">
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var start = null
// var end = null
var keyCode = null
document.addEventListener('keydown', function(event) {
// console.log("DOWN");
if (start === null) {
start = event.timeStamp;
}
});
document.addEventListener('keypress', function(event) {
// console.log("P r e s s");
// ctx.font = "30px Courier";
keyCode = event.keyCode // keypress handles this more easily
// ctx.fillText(String.fromCharCode(event.keyCode), x, y + 7);
// moveCursor();
// localStorage.setItem("typelighter-main-canvas", c.toDataURL());
// console.log('PRESS', event.keyCode, String.fromCharCode(event.keyCode), x, y);
});
document.addEventListener('keyup', function(event) {
// console.log('UP', event.keyCode, String.fromCharCode(event.keyCode), x, y);
// console.log("up");
ctx.font = "30px Courier";
// console.log(event)
if (start) {
var time = event.timeStamp - start;
// console.log(String.fromCharCode(keyCode).toLowerCase(), time)
ctx.globalAlpha = Math.min(time/1000, 1)
var char = String.fromCharCode(keyCode)//event.keyCode)
if (time < 1000) {
char = char.toLowerCase()
} else {
char = char.toUpperCase()
}
ctx.fillText(char, x, y + 7);
moveCursor();
start = null
keyCode = null
}
});
var c = document.getElementById("myCanvas");
c.width = window.innerWidth;
c.height = window.innerHeight;
const maxWidth = Math.floor(c.width * 0.75);
const min_x = Math.floor(c.width * 0.25);
const min_y = 200;
var x = min_x;
var y = min_y;
var b = 0;
function moveCursor() {
b++;
x = min_x + (20 * b % (maxWidth - min_x));
y = min_y + (35 * Math.floor(20 * b / (maxWidth - min_x)));
}
// // Load data
// var dataURL = localStorage.getItem("typelighter-main-canvas");
// var img = new Image;
// var c = document.getElementById("myCanvas");
// var ctx = c.getContext("2d");
// img.src = dataURL;
// img.onload = function () {
// ctx.drawImage(img, 0, 0);
// };
function clearCanvas() {
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
localStorage.removeItem("typelighter-main-canvas");
}
</script>
</body>
</html>