-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_welcome.html
391 lines (325 loc) · 12.7 KB
/
index_welcome.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/sb-1.4.1.min.js"></script>
<!--<script type="text/javascript" src="js/jquery-1.11.0.min.map"></script>-->
<script type="text/javascript">
// examples here: http://docs.spacebrew.cc/tutorials/2014/10/21/basics-spacebrew-javasript
var sb;
var app_name = "lens_table";
//localhost
// var sb_server = "sandbox.spacebrew.cc";
var sb_server = "192.168.1.96";
var sb_client_name = app_name;
var sb_description = "a table that shows images through lenses";
var ctx;
//48" x 30"
var screen_width = 1280;
var screen_height = 1024;
var table_width_in = 48;
var table_height_in = 30;
var screen_border_in = 2;
var lens1_x_in = 11;
var lens1_y_in = 20.75;
var lens1_x = 190;
var lens1_y = 560;
var lens1_d = 150;
var lens2_x = 0;
var lens2_y =0;
var lens2_d = 0;
var convert_in_to_px = function(in_x, in_y){
in_x = in_x - screen_border_in;
in_y = in_y - screen_border_in;
// px/screen = in/table
// px = in/table * screen
var out_px = (in_x / table_width_in) * screen_width;
var out_py = (in_y / table_height_in) * screen_height;
return {x: out_px, y: out_py};
}
$(document).ready(function(){
console.log("ready");
sb = new Spacebrew.Client(sb_server, sb_client_name, sb_description, {reconnect : true});
sb.onOpen = sbOnOpen;
sb.onClose = sbOnClose;
sb.onError = function(message){console.log("error: " + message);};
sb.onStringMessage = sbOnStringMessage;
sb.addSubscribe("setImageUrlAtLocationAndSize", "string");
sb.addSubscribe("setColorRectAtLocationAndSize", "string");
sb.addSubscribe("setTextAtLocationAndSize", "string");
sb.addPublish("sendSetMessage", "string");
sb.connect();
var canvas = document.getElementById('lenscanvas');
ctx = canvas.getContext('2d');
var lens1_conv = convert_in_to_px(lens1_x_in, lens1_y_in);
var lens1_x_px = lens1_conv.x;
var lens1_y_px = lens1_conv.y;
//setImageUrlAtLocationAndSize("images/Maker Hub logo bw 5 (3).jpg|640|512|1024|1024|0");
// setImageUrlAtLocationAndSize("images/catwater.gif|460|445|155|155|0");
// setImageUrlAtLocationAndSize("images/LaserCutter2.png|150|200|250|250|90");
// setColorRectAtLocationAndSize("red|150|100|50|50|0");
// setColorRectAtLocationAndSize("red|150|100|50|50|45");
setTextAtLocationAndSize("Maker Hub Open House Sept 7th 4-6pm|"+ (screen_width / 2) +"|"+ (screen_height / 2) +"|"+ (screen_width - 100) +"|"+ (screen_height - 100) +"|5");
$("#sendit").click(function(){
var value = $("#sendstring").val();
console.log("got value " + value);
sb.send("sendSetMessage", "string", value);
});
});
function setColorRectAtLocationAndSize(message){
// format:
// color|x|y|w|h|r
var split = message.split("|");
var color, x, y, w, h, r = false;
if(split.length >= 1){
color = split[0];
}
if(split.length >= 2){
x = parseFloat(split[1]);
}
if(split.length >= 3){
y = parseFloat(split[2]);
}
if(split.length >= 4){
w = parseFloat(split[3]);
}
if(split.length >= 5){
h = parseFloat(split[4]);
}
if(split.length >= 6){
r = parseFloat(split[5]);
}
console.log("setting " + color + ", " + x +", "+ y + ", " + w + ", " + h + ", " + r);
var sum = x+ y;
console.log("sum : " + sum);
if(!r){
r = 0;
}
// tx = x + (w / 2 );
// ty = y + (h / 2);
tx = x;
ty = y;
console.log("translating " + tx + " , " + ty);
ctx.translate(tx, ty);
ctx.rotate(r * Math.PI / 180);
ctx.fillStyle = color;
ctx.rect(0 - (w / 2) ,0 - ( h /2), w, h);
ctx.fill();
ctx.setTransform(1, 0, 0, 1, 0, 0);
}
function setImageUrlAtLocationAndSize(message){
// format:
// url|x|y|w|h|r
var split = message.split("|");
var url, x, y, w, h, r = false;
if(split.length >= 1){
url = split[0];
}
if(split.length >= 2){
x = parseFloat(split[1]);
}
if(split.length >= 3){
y = parseFloat(split[2]);
}
if(split.length >= 4){
w = parseFloat(split[3]);
}
if(split.length >= 5){
h = parseFloat(split[4]);
}
if(split.length >= 6){
r = parseFloat(split[5]);
}
console.log("tring to set");
var image = new Image();
image.onload = function(){
console.log("loaded " + image.width + " " + image.height);
console.log("setting " + url + ", " + x +", "+ y + ", " + w + ", " + h + ", " + r);
var sum = x+ y;
console.log("sum : " + sum);
if(!h){
h = image.height;
}
if(!w){
w = image.width;
}
if(!r){
r = 0;
}
// tx = x + (w / 2 );
// ty = y + (h / 2);
tx = x;
ty = y;
console.log("translating " + tx + " , " + ty);
ctx.translate(tx, ty);
ctx.rotate(r * Math.PI / 180);
ctx.drawImage(image, 0 - (w / 2) ,0 - ( h /2), w, h);
ctx.setTransform(1, 0, 0, 1, 0, 0);
};
image.src = url;
}
function setTextAtLocationAndSize(message){
// format:
// url|x|y|w|h|r
var split = message.split("|");
var text, x, y, w, h, r = false;
if(split.length >= 1){
text = split[0];
}
if(split.length >= 2){
x = parseFloat(split[1]);
}
if(split.length >= 3){
y = parseFloat(split[2]);
}
if(split.length >= 4){
w = parseFloat(split[3]);
}
if(split.length >= 5){
h = parseFloat(split[4]);
}
if(split.length >= 6){
r = parseFloat(split[5]);
}
ctx.fillStyle = 'black';
ctx.strokeStyle = 'black';
tx = x;
ty = y;
// tx = x + w/2;
// ty = y + h/2;
// console.log("translating " + tx + " , " + ty);
ctx.translate(tx, ty);
ctx.rotate(r * Math.PI / 180);
paint_centered_wrap(ctx, 0 - (w / 2) ,0 - ( h /2), w, h, text, 60, 1);
ctx.setTransform(1, 0, 0, 1, 0, 0);
}
function sbOnStringMessage(name, value){
console.log("got message: " + name + ", " + value);
switch (name) {
case "setImageUrlAtLocationAndSize":
setImageUrlAtLocationAndSize(value);
break;
case "setColorRectAtLocationAndSize":
setColorRectAtLocationAndSize(value);
break
case "setTextAtLocationAndSize":
setTextAtLocationAndSize(value);
break;
default:
console.log("unknown command " + name);
break;
}
}
function sbOnClose(){
console.log("sb_close");
}
function sbOnOpen(){
console.log("sb_open");
}
/*
filling in text in a given area:
ctx.font="30px Arial"; // 30px is the height
var txt="Hello World"
ctx.fillText("width:" + ctx.measureText(txt).width,10,50)
Look at this:
http://andreinc.net/2012/11/14/painting-and-wrapping-a-text-inside-a-rectangular-area-html5-canvas/
*/
paint_centered_wrap = function(ctx2d, x, y, w, h, text, fh, spl) {
// The painting properties
// Normally I would write this as an input parameter
var Paint = {
RECTANGLE_STROKE_STYLE : 'black',
RECTANGLE_LINE_WIDTH : 1,
VALUE_FONT : fh+'px Courier',
VALUE_FILL_STYLE : 'red'
}
/*
* @param ctx : The 2d context
* @param mw : The max width of the text accepted
* @param font : The font used to draw the text
* @param text : The text to be splitted into
*/
var split_lines = function(ctx, mw, font, text) {
// We give a little "padding"
// This should probably be an input param
// but for the sake of simplicity we will keep it
// this way
mw = mw - 10;
// We setup the text font to the context (if not already)
ctx2d.font = font;
// We split the text by words
var words = text.split(' ');
var new_line = words[0];
var lines = [];
for(var i = 1; i < words.length; ++i) {
if (ctx.measureText(new_line + " " + words[i]).width < mw) {
new_line += " " + words[i];
} else {
lines.push(new_line);
new_line = words[i];
}
ctx.measureText(new_line).widht > mw;
// do again, but smaller
}
lines.push(new_line);
// DEBUG
// for(var j = 0; j < lines.length; ++j) {
// console.log("line[" + j + "]=" + lines[j]);
// }
return lines;
}
// Obtains the context 2d of the canvas
// It may return null
if (ctx2d) {
// draw rectangular
ctx2d.strokeStyle=Paint.RECTANGLE_STROKE_STYLE;
ctx2d.lineWidth = Paint.RECTANGLE_LINE_WIDTH;
// ctx2d.strokeRect(x, y, w, h);
// Paint text
var lines = split_lines(ctx2d, w, Paint.VALUE_FONT, text);
// Block of text height
// try again with a smaller font.
for (var k = 0; k < lines.length; k++){
if(ctx.measureText(lines[k]).width > w-10){
paint_centered_wrap(ctx2d, x, y, w, h, text, fh-1, spl);
return;
}
}
var both = lines.length * (fh + spl);
if (both >= h) {
// We won't be able to wrap the text inside the area
// the area is too small. We should inform the user
// about this in a meaningful way
paint_centered_wrap(ctx2d, x, y, w, h, text, fh-1, spl);
return;
} else {
// We determine the y of the first line
// var ly = (h - both)/2 + y + spl*lines.length;
var ly = 0 - (both / 2) - (spl * lines.length);
var lx = 0;
for (var j = 0, ly; j < lines.length; ++j, ly+=fh+spl) {
// We continue to centralize the lines
lx = x+w/2-ctx2d.measureText(lines[j]).width/2;
// DEBUG
console.log("ctx2d.fillText('"+ lines[j] +"', "+ lx +", " + ly + ")");
ctx2d.fillText(lines[j], lx, ly + fh);
}
}
} else {
// Do something meaningful
}
}
</script>
</head>
<body>
<canvas id="lenscanvas" width="1080" height="960">
</canvas>
<!--
<input id="sendstring" type="text" size="80">
<span id="sendit">send</span><BR>
try images/Maker Hub logo bw 5 (3).jpg|0|0|250|250|45<br/>
try red|50|50|250|250|45<br/>
try HELLO WORLD|50|50|250|250|45<br/>
server at <a href="http://spacebrew.github.io/spacebrew/admin/admin.html?server=localhost" target="_new">http://spacebrew.github.io/spacebrew/admin/admin.html?server=localhost</a>
-->
</body>
</html>