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
|
VCanvas_vml = createClass(VCanvas_base, {
init: function (width, height, target) {
var groupel;
VCanvas_vml._super.init.call(this, width, height, target);
if (target[0]) {
target = target[0];
}
$.data(target, '_jqs_vcanvas', this);
this.canvas = document.createElement('span');
$(this.canvas).css({ display: 'inline-block', position: 'relative', overflow: 'hidden', width: width, height: height, margin: '0px', padding: '0px', verticalAlign: 'top'});
this._insert(this.canvas, target);
this._calculatePixelDims(width, height, this.canvas);
this.canvas.width = this.pixelWidth;
this.canvas.height = this.pixelHeight;
groupel = '<v:group coordorigin="0 0" coordsize="' + this.pixelWidth + ' ' + this.pixelHeight + '"' +
' style="position:absolute;top:0;left:0;width:' + this.pixelWidth + 'px;height=' + this.pixelHeight + 'px;"></v:group>';
this.canvas.insertAdjacentHTML('beforeEnd', groupel);
this.group = $(this.canvas).children()[0];
this.rendered = false;
this.prerender = '';
},
_drawShape: function (shapeid, path, lineColor, fillColor, lineWidth) {
var vpath = [],
initial, stroke, fill, closed, vel, plen, i;
for (i = 0, plen = path.length; i < plen; i++) {
vpath[i] = '' + (path[i][0]) + ',' + (path[i][1]);
}
initial = vpath.splice(0, 1);
lineWidth = lineWidth === undefined ? 1 : lineWidth;
stroke = lineColor === undefined ? ' stroked="false" ' : ' strokeWeight="' + lineWidth + 'px" strokeColor="' + lineColor + '" ';
fill = fillColor === undefined ? ' filled="false"' : ' fillColor="' + fillColor + '" filled="true" ';
closed = vpath[0] === vpath[vpath.length - 1] ? 'x ' : '';
vel = '<v:shape coordorigin="0 0" coordsize="' + this.pixelWidth + ' ' + this.pixelHeight + '" ' +
' id="jqsshape' + shapeid + '" ' +
stroke +
fill +
' style="position:absolute;left:0px;top:0px;height:' + this.pixelHeight + 'px;width:' + this.pixelWidth + 'px;padding:0px;margin:0px;" ' +
' path="m ' + initial + ' l ' + vpath.join(', ') + ' ' + closed + 'e">' +
' </v:shape>';
return vel;
},
_drawCircle: function (shapeid, x, y, radius, lineColor, fillColor, lineWidth) {
var stroke, fill, vel;
x -= radius;
y -= radius;
stroke = lineColor === undefined ? ' stroked="false" ' : ' strokeWeight="' + lineWidth + 'px" strokeColor="' + lineColor + '" ';
fill = fillColor === undefined ? ' filled="false"' : ' fillColor="' + fillColor + '" filled="true" ';
vel = '<v:oval ' +
' id="jqsshape' + shapeid + '" ' +
stroke +
fill +
' style="position:absolute;top:' + y + 'px; left:' + x + 'px; width:' + (radius * 2) + 'px; height:' + (radius * 2) + 'px"></v:oval>';
return vel;
},
_drawPieSlice: function (shapeid, x, y, radius, startAngle, endAngle, lineColor, fillColor) {
var vpath, startx, starty, endx, endy, stroke, fill, vel;
if (startAngle === endAngle) {
return ''; // VML seems to have problem when start angle equals end angle.
}
if ((endAngle - startAngle) === (2 * Math.PI)) {
startAngle = 0.0; // VML seems to have a problem when drawing a full circle that doesn't start 0
endAngle = (2 * Math.PI);
}
startx = x + Math.round(Math.cos(startAngle) * radius);
starty = y + Math.round(Math.sin(startAngle) * radius);
endx = x + Math.round(Math.cos(endAngle) * radius);
endy = y + Math.round(Math.sin(endAngle) * radius);
if (startx === endx && starty === endy) {
if ((endAngle - startAngle) < Math.PI) {
// Prevent very small slices from being mistaken as a whole pie
return '';
}
// essentially going to be the entire circle, so ignore startAngle
startx = endx = x + radius;
starty = endy = y;
}
if (startx === endx && starty === endy && (endAngle - startAngle) < Math.PI) {
return '';
}
vpath = [x - radius, y - radius, x + radius, y + radius, startx, starty, endx, endy];
stroke = lineColor === undefined ? ' stroked="false" ' : ' strokeWeight="1px" strokeColor="' + lineColor + '" ';
fill = fillColor === undefined ? ' filled="false"' : ' fillColor="' + fillColor + '" filled="true" ';
vel = '<v:shape coordorigin="0 0" coordsize="' + this.pixelWidth + ' ' + this.pixelHeight + '" ' +
' id="jqsshape' + shapeid + '" ' +
stroke +
fill +
' style="position:absolute;left:0px;top:0px;height:' + this.pixelHeight + 'px;width:' + this.pixelWidth + 'px;padding:0px;margin:0px;" ' +
' path="m ' + x + ',' + y + ' wa ' + vpath.join(', ') + ' x e">' +
' </v:shape>';
return vel;
},
_drawRect: function (shapeid, x, y, width, height, lineColor, fillColor) {
return this._drawShape(shapeid, [[x, y], [x, y + height], [x + width, y + height], [x + width, y], [x, y]], lineColor, fillColor);
},
reset: function () {
this.group.innerHTML = '';
},
appendShape: function (shape) {
var vel = this['_draw' + shape.type].apply(this, shape.args);
if (this.rendered) {
this.group.insertAdjacentHTML('beforeEnd', vel);
} else {
this.prerender += vel;
}
this.lastShapeId = shape.id;
return shape.id;
},
replaceWithShape: function (shapeid, shape) {
var existing = $('#jqsshape' + shapeid),
vel = this['_draw' + shape.type].apply(this, shape.args);
existing[0].outerHTML = vel;
},
replaceWithShapes: function (shapeids, shapes) {
// replace the first shapeid with all the new shapes then toast the remaining old shapes
var existing = $('#jqsshape' + shapeids[0]),
replace = '',
slen = shapes.length,
i;
for (i = 0; i < slen; i++) {
replace += this['_draw' + shapes[i].type].apply(this, shapes[i].args);
}
existing[0].outerHTML = replace;
for (i = 1; i < shapeids.length; i++) {
$('#jqsshape' + shapeids[i]).remove();
}
},
insertAfterShape: function (shapeid, shape) {
var existing = $('#jqsshape' + shapeid),
vel = this['_draw' + shape.type].apply(this, shape.args);
existing[0].insertAdjacentHTML('afterEnd', vel);
},
removeShapeId: function (shapeid) {
var existing = $('#jqsshape' + shapeid);
this.group.removeChild(existing[0]);
},
getShapeAt: function (el, x, y) {
var shapeid = el.id.substr(8);
return shapeid;
},
render: function () {
if (!this.rendered) {
// batch the intial render into a single repaint
this.group.innerHTML = this.prerender;
this.rendered = true;
}
}
});
|