-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathDraw_paper.html
More file actions
218 lines (193 loc) · 6.12 KB
/
Draw_paper.html
File metadata and controls
218 lines (193 loc) · 6.12 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>E-Paper Matrix Live Drawing</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
max-width: 600px;
padding: 20px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#canvas {
width: 100%;
height: auto;
image-rendering: -moz-crisp-edges;
image-rendering: pixelated;
border: 1px solid #ddd;
}
.info {
font-size: 14px;
color: #666;
margin-bottom: 20px;
}
.info1 {
font-size: 18px;
color: #666;
}
.info2 {
font-size: 11px;
color: #666;
}
.info3 {
font-size: 16px;
color: #666;
}
.info a {
color: #007bff;
text-decoration: none;
}
#links a {
color: #007bff;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<h1>E-Paper Matrix Live Drawing</h1>
<div class="info">
<p>The project video can be found <a href="https://youtu.be/QrgLoryouCg">here</a>.</p>
<p>E-Paper Drawing: Click in the canvas to enable or disable a pixel!</p>
<p>Changed pixels will show after 1 second without further changes + YouTube delay.</p>
<p>The time will be shown again after 1 minute with no changes.</p>
</div>
<div class="info1">
<p>Live Stream of your drawing: <a href="https://www.youtube.com/watch?v=qt7HHOFgaF0">youtube.com/qt7HHOFgaF0</a></p>
</div>
<canvas id="canvas" width="16" height="16"></canvas>
<!-- <div id="links">
<input type="button" value="All White" onclick="allWhite();">
<input type="button" value="All Black" onclick="allBlack();">
</div> -->
<div class="info2">
<p>This code is based on <a href="https://github.com/TheNitek/draw">TheNitek's work</a>.</p>
</div>
<div class="info3">
by <a href="https://twitter.com/atc1441">@ATC1441</a> - <a href="https://ATCnetz.de">https://ATCnetz.de</a>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type="text/javascript"></script>
<script>
const draw_topic = 'paper/fill';
const sync_topic = 'paper/sync';
const connect_topic = 'paper/connect';
client = new Paho.MQTT.Client('wss://broker.emqx.io:8084/mqtt', Math.random().toString(36).substring(20));
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});
function onConnect() {
console.log("onConnect");
client.subscribe(draw_topic);
client.subscribe(sync_topic);
message = new Paho.MQTT.Message("1");
message.destinationName = connect_topic;
client.send(message);
}
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
console.log("Reconnecting");
client.connect({onSuccess:onConnect});
}
function onMessageArrived(message) {
console.log("onMessageArrived: "+toHexString(message.payloadBytes));
if(message.payloadBytes.length == 3) {
processPixel(message.payloadBytes);
} else if(message.payloadBytes.length == 16*16) {
console.log("got sync: " + message.payloadBytes.length);
processMatrix(message.payloadBytes);
} else {
console.log("invalid msg length: " + message.payloadBytes.length);
}
}
function processPixel(payload) {
let x = payload[0];
let y = payload[1];
let c = payload[2];
if(c)
context.fillStyle = '#ffffff';
else
context.fillStyle = '#000000';
context.fillRect(x, y, 1, 1);
}
function processMatrix(payload) {
for(x = 0; x < 16; x++) {
for(y = 0; y < 16; y++) {
let i = (x + (y*16));
let c = payload[i];
if(c)
context.fillStyle = '#ffffff';
else
context.fillStyle = '#000000';
context.fillRect(x, y, 1, 1);
}
}
}
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
var pixel_width = context.canvas.clientWidth / canvas.width;
var pixel_height = context.canvas.clientHeight / canvas.height;
var rect = canvas.getBoundingClientRect();
context.fillStyle = "black";
context.fillRect(0, 0, canvas.width, canvas.height);
canvas.addEventListener("click", function(e) {
var x = Math.floor((e.clientX - rect.left) / pixel_width);
var y = Math.floor((e.clientY - rect.top) / pixel_height);
const data = context.getImageData(x, y, 1, 1).data;
if(data[0] == 0x00){
context.fillStyle = "#FFFFFF";
var new_color = 1;
} else {
context.fillStyle = "#000000";
var new_color = 0;
}
context.fillRect(x, y, 1, 1);
let payload = new Uint8Array(3);
payload[0] = x;
payload[1] = y;
payload[2] = new_color;
message = new Paho.MQTT.Message(payload);
message.destinationName = draw_topic;
client.send(message);
}, true);
function allWhite() {
let payload = new Uint8Array(16*16);
for(var i = 0; i < payload.length; i++) {
payload[i] = 1;
}
message = new Paho.MQTT.Message(payload);
message.destinationName = draw_topic;
client.send(message);
}
function allBlack() {
let payload = new Uint8Array(16*16);
for(var i = 0; i < payload.length; i++) {
payload[i] = 0;
}
message = new Paho.MQTT.Message(payload);
message.destinationName = draw_topic;
client.send(message);
}
</script>
</body>
</html>