-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_continuous_canvas.js
83 lines (66 loc) · 1.96 KB
/
simple_continuous_canvas.js
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
var ContinuousVisualization = function(height, width, context) {
var height = height;
var width = width;
var context = context;
this.draw = function(objects) {
for (var i in objects) {
var p = objects[i];
if (p.Shape == "rect")
this.drawRectange(p.x, p.y, p.w, p.h, p.Color, p.Filled);
if (p.Shape == "circle")
this.drawCircle(p.x, p.y, p.r, p.Color, p.Filled);
};
};
this.drawCircle = function(x, y, radius, color, fill) {
var cx = x * width;
var cy = y * height;
var r = radius;
context.beginPath();
context.arc(cx, cy, r, 0, Math.PI * 2, false);
context.closePath();
context.strokeStyle = color;
context.stroke();
if (fill) {
context.fillStyle = color;
context.fill();
}
};
this.drawRectange = function(x, y, w, h, color, fill) {
context.beginPath();
var dx = w * width;
var dy = h * height;
// Keep the drawing centered:
var x0 = (x*width) - 0.5*dx;
var y0 = (y*height) - 0.5*dy;
context.strokeStyle = color;
context.fillStyle = color;
if (fill)
context.fillRect(x0, y0, dx, dy);
else
context.strokeRect(x0, y0, dx, dy);
};
this.resetCanvas = function() {
context.clearRect(0, 0, height, width);
context.beginPath();
};
};
var Simple_Continuous_Module = function(canvas_width, canvas_height) {
// Create the element
// ------------------
// Create the tag:
var canvas_tag = "<canvas width='" + canvas_width + "' height='" + canvas_height + "' ";
canvas_tag += "style='border:1px dotted'></canvas>";
// Append it to body:
var canvas = $(canvas_tag)[0];
$("body").append(canvas);
// Create the context and the drawing controller:
var context = canvas.getContext("2d");
var canvasDraw = new ContinuousVisualization(canvas_width, canvas_height, context);
this.render = function(data) {
canvasDraw.resetCanvas();
canvasDraw.draw(data);
};
this.reset = function() {
canvasDraw.resetCanvas();
};
};