-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shape.js
90 lines (76 loc) · 2.27 KB
/
Shape.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
84
85
86
87
88
89
90
(function(root) {
root.Shape = Shape;
function Shape() {
this.points = [];
this.color = 'red';
this.border_color = 'black';
this.border_width = 2;
this.is_closed = false;
};
_.extend(Shape.prototype, Backbone.Events, {
'addPoint': function addPoint(point) {
if (point.isEqual(this.firstPoint()))
this.is_closed = true;
this.points.push(point);
this.trigger('change');
},
'hasPoints': function hasPoints() {
return this.points.length;
},
'firstPoint': function firstPoint() {
return this.points[0];
},
'removeLastPoint': function removeLastPoint() {
this.points.pop();
if (this.is_closed)
this.is_closed = false;
this.trigger('change');
return this;
},
'applyColor': function applyColor(color) {
var dark_color = 'Dark' + color;
// for some reason, Salmon & Grey are darker than DarkSalmon & DarkGrey
if (color == 'Grey' || color == 'Salmon' || color == 'OliveGreen') {
var orig_color = color;
color = dark_color;
dark_color = orig_color;
}
if (color == 'Brown') {
color = 'Sienna';
dark_color = 'SaddleBrown';
}
if (color == 'Black')
dark_color = 'Black';
this.color = color;
this.border_color = dark_color;
this.trigger('change');
return this;
},
'scale': function scale(scale) {
this.points.forEach(function(pt) {
pt.scale(scale);
});
},
'toJSON': function toJSON() {
return {
'points': this.points,
'color': this.color,
'border_color': this.border_color,
'border_width': this.border_width,
'is_closed': this.is_closed
};
}
});
Shape.deserialize = function deserialize(raw_shape) {
var shape = new Shape();
shape.color = raw_shape.color;
shape.border_color = raw_shape.border_color;
shape.border_width = raw_shape.border_width;
shape.is_closed = raw_shape.is_closed;
raw_shape.points.forEach(function(raw_point) {
var point = Point.deserialize(raw_point);
shape.addPoint(point);
});
return shape;
};
})(this);