Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aimacode
GitHub Repository: aimacode/aima-python
Path: blob/master/js/canvas.js
621 views
1
/*
2
JavaScript functions that are executed by running the corresponding methods of a Canvas object
3
Donot use these functions by making a js file. Instead use the python Canvas class.
4
See canvas.py for help on how to use the Canvas class to draw on the HTML Canvas
5
*/
6
7
8
//Manages the output of code executed in IPython kernel
9
function output_callback(out, block){
10
console.log(out);
11
//Handle error in python
12
if(out.msg_type == "error"){
13
console.log("Error in python script!");
14
console.log(out.content);
15
return ;
16
}
17
script = out.content.data['text/html'];
18
script = script.substr(8, script.length - 17);
19
eval(script)
20
}
21
22
//Handles mouse click by calling mouse_click of Canvas object with the co-ordinates as arguments
23
function click_callback(element, event, varname){
24
var rect = element.getBoundingClientRect();
25
var x = event.clientX - rect.left;
26
var y = event.clientY - rect.top;
27
var kernel = IPython.notebook.kernel;
28
var exec_str = varname + ".mouse_click(" + String(x) + ", " + String(y) + ")";
29
console.log(exec_str);
30
kernel.execute(exec_str,{'iopub': {'output': output_callback}}, {silent: false});
31
}
32
33
function rgbToHex(r,g,b){
34
var hexValue=(r<<16) + (g<<8) + (b<<0);
35
var hexString=hexValue.toString(16);
36
hexString ='#' + Array(7-hexString.length).join('0') + hexString; //Add 0 padding
37
return hexString;
38
}
39
40
function toRad(x){
41
return x*Math.PI/180;
42
}
43
44
//Canvas class to store variables
45
function Canvas(id){
46
this.canvas = document.getElementById(id);
47
this.ctx = this.canvas.getContext("2d");
48
this.WIDTH = this.canvas.width;
49
this.HEIGHT = this.canvas.height;
50
this.MOUSE = {x:0,y:0};
51
}
52
53
//Sets the fill color with which shapes are filled
54
Canvas.prototype.fill = function(r, g, b){
55
this.ctx.fillStyle = rgbToHex(r,g,b);
56
}
57
58
//Set the stroke color
59
Canvas.prototype.stroke = function(r, g, b){
60
this.ctx.strokeStyle = rgbToHex(r,g,b);
61
}
62
63
//Set width of the lines/strokes
64
Canvas.prototype.strokeWidth = function(w){
65
this.ctx.lineWidth = w;
66
}
67
68
//Draw a rectangle with top left at (x,y) with 'w' width and 'h' height
69
Canvas.prototype.rect = function(x, y, w, h){
70
this.ctx.fillRect(x,y,w,h);
71
}
72
73
//Draw a line with (x1, y1) and (x2, y2) as end points
74
Canvas.prototype.line = function(x1, y1, x2, y2){
75
this.ctx.beginPath();
76
this.ctx.moveTo(x1, y1);
77
this.ctx.lineTo(x2, y2);
78
this.ctx.stroke();
79
}
80
81
//Draw an arc with (x, y) as centre, 'r' as radius from angles start to stop
82
Canvas.prototype.arc = function(x, y, r, start, stop){
83
this.ctx.beginPath();
84
this.ctx.arc(x, y, r, toRad(start), toRad(stop));
85
this.ctx.stroke();
86
}
87
88
//Clear the HTML canvas
89
Canvas.prototype.clear = function(){
90
this.ctx.clearRect(0, 0, this.WIDTH, this.HEIGHT);
91
}
92
93
//Change font, size and style
94
Canvas.prototype.font = function(font_str){
95
this.ctx.font = font_str;
96
}
97
98
//Draws "filled" text on the canvas
99
Canvas.prototype.fill_text = function(text, x, y){
100
this.ctx.fillText(text, x, y);
101
}
102
103
//Write text on the canvas
104
Canvas.prototype.stroke_text = function(text, x, y){
105
this.ctx.strokeText(text, x, y);
106
}
107
108
109
//Test if the canvas functions are working
110
Canvas.prototype.test_run = function(){
111
var dbg = false;
112
if(dbg)
113
alert("1");
114
this.clear();
115
if(dbg)
116
alert("2");
117
this.fill(0, 200, 0);
118
if(dbg)
119
alert("3");
120
this.rect(this.MOUSE.x, this.MOUSE.y, 100, 200);
121
if(dbg)
122
alert("4");
123
this.stroke(0, 0, 50);
124
if(dbg)
125
alert("5");
126
this.line(0, 0, 100, 100);
127
if(dbg)
128
alert("6");
129
this.stroke(200, 200, 200);
130
if(dbg)
131
alert("7");
132
this.arc(200, 100, 50, 0, 360);
133
if(dbg)
134
alert("8");
135
}
136
137