Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aimacode
GitHub Repository: aimacode/aima-python
Path: blob/master/js/gridworld.js
621 views
1
var latest_output_area ="NONE"; // Jquery object for the DOM element of output area which was used most recently
2
3
function handle_output(out, block){
4
var output = out.content.data["text/html"];
5
latest_output_area.html(output);
6
}
7
8
function handle_click(canvas,coord) {
9
console.log(canvas,coord);
10
latest_output_area = $(canvas).parents('.output_subarea');
11
$(canvas).parents('.output_subarea')
12
var world_object_name = canvas.dataset.world_name;
13
var command = world_object_name + ".handle_click(" + JSON.stringify(coord) + ")";
14
console.log("Executing Command: " + command);
15
var kernel = IPython.notebook.kernel;
16
var callbacks = { 'iopub' : {'output' : handle_output}};
17
kernel.execute(command,callbacks);
18
};
19
20
21
function generateGridWorld(state,size,elements)
22
{
23
// Declaring array to store image object
24
var $imgArray = new Object(), hasImg=false;
25
// Loading images LOOP
26
$.each(elements, function(i, val) {
27
// filtering for type img
28
if(val["type"]=="img") {
29
// setting image load
30
hasImg = true;
31
$imgArray[i] = $('<img />').attr({height:size,width:size,src:val["source"]}).data({name:i,loaded:false}).load(function(){
32
// Check for all image loaded
33
var execute=true;
34
$(this).data("loaded",true);
35
$.each($imgArray, function(i, val) {
36
if(!$(this).data("loaded")) {
37
execute=false;
38
// exit on unloaded image
39
return false;
40
}
41
});
42
if (execute) {
43
// Converting loaded image to canvas covering block size.
44
$.each($imgArray, function(i, val) {
45
$imgArray[i] = $('<canvas />').attr({width:size,height:size}).get(0);
46
$imgArray[i].getContext('2d').drawImage(val.get(0),0,0,size,size);
47
});
48
// initialize the world
49
initializeWorld();
50
}
51
});
52
}
53
});
54
55
if(!hasImg) {
56
initializeWorld();
57
}
58
59
function initializeWorld(){
60
var $parentDiv = $('div.map-grid-world');
61
// remove object reference
62
$('div.map-grid-world').removeClass('map-grid-world');
63
// get some info about the canvas
64
var row = state.length;
65
var column = state[0].length;
66
var canvas = $parentDiv.find('canvas').get(0);
67
var ctx = canvas.getContext('2d');
68
canvas.width = size * column;
69
canvas.height = size * row;
70
71
//Initialize previous positions
72
for(var i=0;i<row;++i) {
73
for (var j = 0; j < column;++j) {
74
if(elements[state[i][j]["val"]]["type"]=="color") {
75
if( state[i][j]["val"] == "default") {
76
blockCreate('black',elements[state[i][j]["val"]]["source"], i, j);
77
}
78
else {
79
blockCreate('transparent',elements[state[i][j]["val"]]["source"], i, j);
80
}
81
}
82
else if(elements[state[i][j]["val"]]["type"]==="img") {
83
pattern = ctx.createPattern($imgArray[state[i][j]["val"]], "repeat");
84
blockCreate('transparent',pattern, i, j);
85
}
86
}
87
}
88
89
// function to create a block of given parameters
90
function blockCreate(borderColor,fillColor,x,y) {
91
ctx.fillStyle = fillColor;
92
ctx.fillRect(y * size, x * size, size, size);
93
ctx.lineWidth=2;
94
ctx.strokeStyle = borderColor;
95
ctx.strokeRect((y * size)+1, (x * size)+1, size-2, size-2);
96
}
97
98
// click event, using jQuery for cross-browser convenience
99
$(canvas).click(function(e) {
100
// calculate grid square numbers
101
var gy = ~~ (e.offsetX / size);
102
var gx = ~~ (e.offsetY/ size);
103
//handle click send data
104
var coord = new Array();
105
coord.push(gx,gy)
106
handle_click(canvas,coord)
107
});
108
109
// Display tooltip and change on mousemove and mouseout
110
var nameElement = $parentDiv.find('span');
111
$(canvas).mousemove(function(e) {
112
//calculate grid square numbers
113
var gy = ~~ (e.offsetX / size);
114
var gx = ~~ (e.offsetY/ size);
115
// updating tooltip
116
if( gx>=0 && gx<row && gy>=0 && gy<column ) {
117
nameElement.html(state[gx][gy]["tooltip"]);
118
}
119
}).mouseout(function(){
120
nameElement.html("");
121
});
122
};
123
};
124
125
// function call
126
generateGridWorld(gridArray,size,elements);
127