forked from google/blockly-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
542 lines (514 loc) · 12.9 KB
/
index.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Blockly Demo: Graph</title>
<script src="https://www.google.com/jsapi"></script>
<script src="./node_modules/blockly/blockly_compressed.js"></script>
<script src="./node_modules/blockly/blocks_compressed.js"></script>
<script src="./node_modules/blockly/javascript_compressed.js"></script>
<script src="./node_modules/blockly/msg/en.js"></script>
<style>
body {
background-color: #fff;
font-family: sans-serif;
}
h1 {
font-weight: normal;
font-size: 140%;
}
#funcText {
margin-top: 1em;
margin-left: 1.5em;
font-family: sans-serif;
}
#funcText>img {
height: 3px;
width: 15px;
vertical-align: middle;
margin-right: .5em;
}
#y1 {
background-color: #36c;
}
</style>
</head>
<body>
<p>This is a demo of giving instant feedback as blocks are changed.</p>
<p>→ More info on <a href="https://developers.google.com/blockly/guides/configure/web/code-generators#generating_code">Realtime generation</a>…</p>
<table>
<tr>
<td>
<div id="visualization" style="width: 400px"></div>
</td>
<td>
<div id="blocklyDiv" style="height: 400px"></div>
</td>
</tr>
</table>
<div id="funcText">
<img id="y1" src="../../media/1x1.gif">
...
</div>
<xml xmlns="https://developers.google.com/blockly/xml" id="startBlocks" style="display: none">
<block type="graph_set_y" deletable="false" x="100" y="100">
<value name="VALUE">
<block type="math_arithmetic">
<field name="OP">POWER</field>
<value name="A">
<block type="graph_get_x"></block>
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
<value name="B">
<block type="math_number">
<field name="NUM">2</field>
</block>
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
</value>
</block>
</xml>
<script>
// Load the Google Chart Tools Visualization API and the chart package.
if (typeof google == 'object') {
google.load('visualization', '1', {packages: ['corechart']});
} else {
alert('Unable to load Google\'s chart API.\n' +
'Are you connected to the Internet?');
}
// Define the toolbox
const toolbox = {
"kind": "categoryToolbox",
"contents": [
{
"kind": "category",
"name": "Math",
"colour": "%{BKY_MATH_HUE}",
"contents": [
{
"kind": "block",
"type": "math_number",
"fields": {
"NUM": 123
}
},
{
"kind": "block",
"type": "math_arithmetic",
"inputs": {
"A": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1
}
}
},
"B": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1
}
}
}
}
},
{
"kind": "block",
"type": "math_single",
"inputs": {
"NUM": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 9
}
}
}
}
},
{
"kind": "block",
"type": "math_trig",
"inputs": {
"NUM": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 45
}
}
}
}
},
{
"kind": "block",
"type": "math_constant"
},
{
"kind": "block",
"type": "math_number_property",
"inputs": {
"NUMBER_TO_CHECK": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 0
}
}
}
}
},
{
"kind": "block",
"type": "math_round",
"inputs": {
"NUM": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 3.1
}
}
}
}
},
{
"kind": "block",
"type": "math_modulo",
"inputs": {
"DIVIDEND": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 64
}
}
},
"DIVISOR": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 10
}
}
}
}
},
{
"kind": "block",
"type": "math_constrain",
"inputs": {
"VALUE": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 50
}
}
},
"LOW": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1
}
}
},
"HIGH": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 100
}
}
}
}
},
{
"kind": "block",
"type": "math_random_int",
"inputs": {
"FROM": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1
}
}
},
"TO": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 100
}
}
}
}
},
{
"kind": "block",
"type": "math_random_float"
},
{
"kind": "block",
"type": "math_atan2",
"inputs": {
"X": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1
}
}
},
"Y": {
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1
}
}
}
}
},
]
},
{
"kind": "category",
"name": "Variables",
"colour": "%{BKY_VARIABLES_HUE}",
"contents": [
{
"kind": "block",
"type": "graph_get_x",
}
]
},
{
"kind": "category",
"name": "Logic",
"colour": "%{BKY_LOGIC_HUE}",
"contents": [
{
"kind": "block",
"type": "logic_compare"
},
{
"kind": "block",
"type": "logic_operation"
},
{
"kind": "block",
"type": "logic_negate"
},
{
"kind": "block",
"type": "logic_boolean"
},
{
"kind": "block",
"type": "logic_ternary"
}
]
}
]
};
const startBlocks = {
"blocks": {
"blocks": [{
"kind": "block",
"type": "graph_set_y",
"x": 100,
"y": 100,
"inputs": {
"VALUE": {
"block": {
"type": "math_arithmetic",
"fields": {
"OP": "POWER"
},
"inputs": {
"A": {
"block":{
"type": "graph_get_x",
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1
}
}
}
},
"B": {
"block": {
"type": "math_number",
"fields": {
"NUM": 2
},
"shadow": {
"type": "math_number",
"fields": {
"NUM": 1
}
}
}
}
}
}
}
}
}]
}
};
// Define the custom blocks and their JS generators.
Blockly.defineBlocksWithJsonArray([{
"type": "graph_get_x",
"message0": "x",
"output": "Number",
"colour": Blockly.Msg['VARIABLES_HUE'],
"tooltip": Blockly.Msg['VARIABLES_GET_TOOLTIP'],
"helpUrl": Blockly.Msg['VARIABLES_GET_HELPURL']
}]);
Blockly.JavaScript['graph_get_x'] = function(block) {
// x variable getter.
return ['x', Blockly.JavaScript.ORDER_ATOMIC];
};
Blockly.defineBlocksWithJsonArray([{
"type": "graph_set_y",
"message0": "y = %1",
"args0": [
{
"type": "input_value",
"name": "VALUE",
"check": "Number"
}
],
"colour": Blockly.Msg['VARIABLES_HUE'],
"tooltip": Blockly.Msg['VARIABLES_SET_TOOLTIP'],
"helpUrl": Blockly.Msg['VARIABLES_SET_HELPURL']
}]);
Blockly.JavaScript['graph_set_y'] = function(block) {
// block.setDeletable cannot be set from json
block.setDeletable(false);
// y variable setter.
var argument0 = Blockly.JavaScript.valueToCode(block, 'VALUE',
Blockly.JavaScript.ORDER_ASSIGNMENT) || '';
return 'y = ' + argument0 + ';';
};
/**
* Create a namespace for the application.
*/
var Graph = {};
/**
* Main Blockly workspace.
* @type {Blockly.WorkspaceSvg}
*/
Graph.workspace = null;
/**
* Cached copy of the function string.
* @type {?string}
* @private
*/
Graph.oldFormula_ = null;
/**
* Drawing options for the Chart API.
* @type {!Object}
* @private
*/
Graph.options_ = {
//curveType: 'function',
width: 400, height: 400,
chartArea: {left: '10%', width: '85%', height: '85%'}
};
/**
* Visualize the graph of y = f(x) using Google Chart Tools.
* For more documentation on Google Chart Tools, see this linechart example:
* https://developers.google.com/chart/interactive/docs/gallery/linechart
*/
Graph.drawVisualization = function() {
var formula = Blockly.JavaScript.workspaceToCode(Graph.workspace);
if (formula === Graph.oldFormula_) {
// No change in the formula, don't recompute.
return;
}
Graph.oldFormula_ = formula;
// Create and populate the data table.
var data = google.visualization.arrayToDataTable(Graph.plot(formula));
// Create and draw the visualization, passing in the data and options.
new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, Graph.options_);
// Create the "y = ..." label. Find the relevant part of the code.
formula = formula.substring(formula.indexOf('y = '));
formula = formula.substring(0, formula.indexOf(';'));
var funcText = document.getElementById('funcText');
funcText.replaceChild(document.createTextNode(formula), funcText.lastChild);
};
/**
* Plot points on the function y = f(x).
* @param {string} code JavaScript code.
* @return {!Array.<!Array>} 2D Array of points on the graph.
*/
Graph.plot = function(code) {
// Initialize a table with two column headings.
var table = [];
var y;
// TODO: Improve range and scale of graph.
for (var x = -10; x <= 10; x = Math.round((x + 0.1) * 10) / 10) {
try {
eval(code);
} catch (e) {
y = NaN;
}
if (!isNaN(y)) {
// Prevent y from being displayed inconsistently, some in decimals, some
// in scientific notation, often when y has accumulated rounding errors.
y = Math.round(y * Math.pow(10, 14)) / Math.pow(10, 14);
table.push([x, y]);
}
}
// Add column heading to table.
if (table.length) {
table.unshift(['x', 'y']);
} else {
// If the table is empty, add a [0, 0] row to prevent graph error.
table.unshift(['x', 'y'], [0, 0]);
}
return table;
};
/**
* Force Blockly to resize into the available width.
*/
Graph.resize = function() {
var width = Math.max(window.innerWidth - 440, 250);
document.getElementById('blocklyDiv').style.width = width + 'px';
Blockly.svgResize(Graph.workspace);
};
/**
* Initialize Blockly and the graph. Called on page load.
*/
Graph.init = function() {
Graph.workspace = Blockly.inject('blocklyDiv',
{collapse: false,
disable: false,
media: './node_modules/blockly/media/',
toolbox: toolbox});
Blockly.serialization.workspaces.load(startBlocks, Graph.workspace);
Graph.workspace.clearUndo();
// When Blockly changes, update the graph.
Graph.workspace.addChangeListener(Graph.drawVisualization);
Graph.workspace.addChangeListener(Blockly.Events.disableOrphans);
Graph.resize();
};
window.addEventListener('load', Graph.init);
window.addEventListener('resize', Graph.resize);
</script>
</body>
</html>