-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathellipse.js-e
144 lines (125 loc) · 3.94 KB
/
ellipse.js-e
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
## Copyright (c) 2015, Empirical Modelling Group
## All rights reserved.
##
## See LICENSE.txt
${{
Ellipse = function(x, y, xradius, yradius, fillcolour, outlinecolour, drawingOptions) {
this.x = x;
this.y = y;
this.xradius = xradius;
this.yradius = yradius;
this.fillcolour = fillcolour;
this.outlinecolour = outlinecolour;
this.drawingOptions = drawingOptions;
this.name = edenUI.plugins.Canvas2D.initZoneFromDrawingOpts(drawingOptions, "Ellipse");
this.obsName = root.currentObservableName();
}
}}$;
/**
* Create an Ellipse
*
* @param x Centre X-coordinate
* @param y Centre Y-coordinate
* @param x_radius Ellipse x-axis radius from centre
* @param y_radius Ellipse y-axis radius from centre
* @param [fill_colour Fill colour for the ellipse on the canvas
* @param [outline_colour]] Outline colour of the ellipse on the canvas
* @param [drawing_options] A drawingOptions object
*
* #canvas #ellipse #drawingOptions
*/
func Ellipse { ${{
var x = arguments[0];
var y = arguments[1];
var xradius = arguments[2];
var yradius = arguments[3];
var fillcolour, outlinecolour, drawingOptions;
var lastArg = arguments[arguments.length - 1];
var processUpTo;
if (lastArg !== undefined && (lastArg instanceof Object) && !(lastArg instanceof EdenUI.plugins.Canvas2D.FillStyle)) {
drawingOptions = lastArg;
processUpTo = arguments.length - 2;
} else {
processUpTo = arguments.length - 1;
}
if (processUpTo >= 4) {
fillcolour = arguments[4];
if (processUpTo == 5) {
outlinecolour = arguments[5];
}
}
if (fillcolour === undefined && outlinecolour === undefined) {
outlinecolour = "black";
}
return new Ellipse(x, y, xradius, yradius, fillcolour, outlinecolour, drawingOptions);
}}$; };
${{
Ellipse.prototype.draw = function (context) {
if (this.xradius > 0 && this.yradius > 0) {
var scaleOutline;
if (this.outlinecolour === undefined || this.outlinecolour == "transparent") {
scaleOutline = false;
} else if (this.drawingOptions === undefined || !("scaleOutline" in this.drawingOptions)) {
scaleOutline = (this.fillcolour !== undefined && this.fillcolour != "transparent");
} else {
scaleOutline = this.drawingOptions.scaleOutline;
}
if (!scaleOutline) {
context.save();
}
var lineWidth = this.tracePath(context, scaleOutline);
if (this.fillcolour !== undefined) {
edenUI.plugins.Canvas2D.setFillStyle(context, this.fillcolour);
context.fill();
}
if (!scaleOutline) {
context.restore();
context.lineWidth = lineWidth;
}
if (this.outlinecolour !== undefined) {
context.strokeStyle = this.outlinecolour;
context.stroke();
}
}
};
Ellipse.prototype.tracePath = function (context, scaleOutline) {
var scaleFactor = this.yradius / this.xradius;
var lineWidth;
if (this.outlinecolour !== undefined && this.outlinecolour != "transparent") {
if (scaleOutline) {
lineWidth = context.lineWidth;
} else {
lineWidth = context.lineWidth / scaleFactor;
}
} else {
lineWidth = 0;
}
var adjustedXRadius;
//Not sure what to do when xradius is bigger than 1/2 line width but less than line width, but it isn't correct atm.
if (this.xradius <= context.lineWidth / 2) {
lineWidth = this.xradius;
context.lineWidth = lineWidth;
adjustedXRadius = this.xradius / 2;
} else {
adjustedXRadius = this.xradius - lineWidth / 2;
}
context.scale(1, scaleFactor);
context.beginPath();
context.arc(this.x, this.y / scaleFactor, adjustedXRadius, 0, 2 * Math.PI, false);
context.closePath();
return lineWidth;
}
Ellipse.prototype.isHit = function (context, scale, x, y) {
this.tracePath(context, false);
return context.isPointInPath(x,y);
}
Ellipse.prototype.toString = function() {
var s = "Ellipse(" + Eden.edenCodeForValues(this.x, this.y, this.xradius, this.yradius, this.fillcolour, this.outlinecolour);
if (this.drawingOptions !== undefined) {
s = s + ", " + Eden.edenCodeForValue(this.drawingOptions);
}
s = s + ")";
return s;
};
Ellipse.prototype.getEdenCode = Ellipse.prototype.toString;
}}$;