-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle.js-e
128 lines (112 loc) · 3.36 KB
/
circle.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
## Copyright (c) 2013, Empirical Modelling Group
## All rights reserved.
##
## See LICENSE.txt
${{
Circle = function(x, y, radius, fillcolour, outlinecolour, drawingOptions) {
this.x = x;
this.y = y;
this.radius = radius;
if (fillcolour === undefined && outlinecolour === undefined) {
this.fillcolour = undefined;
this.outlinecolour = "black";
} else {
this.fillcolour = fillcolour;
this.outlinecolour = outlinecolour;
}
this.drawingOptions = drawingOptions;
this.name = edenUI.plugins.Canvas2D.initZoneFromDrawingOpts(drawingOptions, "Circle");
this.obsName = root.currentObservableName();
}
}}$;
#! Creates a circle. See [drawing options](@external CanvasExamples > drawingoptions) for more styling options.
#
# ##Usage Example
# `Circle(x, y, radius, "purple");`
# `Circle(x, y, radius, "green", "red");`
#
# @param x x position of center of the circle
# @param y y position of center of the circle
# @param radius radius of the circle
# @param [fillColour]
# @param [outlineColour] defaults to black
# @param [drawingOptions] object of drawingOptions
#
# #canvas #circle #shape #draw #library
func Circle {
${{
var x = arguments[0];
var y = arguments[1];
var radius = arguments[2];
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 >= 3) {
fillcolour = arguments[3];
if (processUpTo == 4) {
outlinecolour = arguments[4];
}
}
if (fillcolour === undefined && outlinecolour === undefined) {
outlinecolour = "black";
}
return new Circle(x, y, radius, fillcolour, outlinecolour, drawingOptions);
}}$; };
${{
Circle.prototype.draw = function (context, scale) {
if (this.radius > 0) {
this.tracePath(context, scale);
if (this.fillcolour !== undefined) {
edenUI.plugins.Canvas2D.setFillStyle(context, this.fillcolour);
context.fill();
}
if (this.outlinecolour !== undefined) {
context.strokeStyle = this.outlinecolour;
context.stroke();
}
}
};
Circle.prototype.tracePath = function (context, scale) {
var halfLineWidth;
if (this.outlinecolour !== undefined) {
halfLineWidth = context.lineWidth / 2;
} else {
halfLineWidth = 0;
}
var adjustedRadius;
if (this.radius <= halfLineWidth) {
context.lineWidth = this.radius;
adjustedRadius = this.radius / 2;
} else {
adjustedRadius = this.radius - halfLineWidth;
}
context.beginPath();
context.arc(this.x, this.y, adjustedRadius, 0, 2 * Math.PI, false);
context.closePath();
}
Circle.prototype.isHit = function (context, scale, x, y) {
this.tracePath(context, scale);
return context.isPointInPath(x,y);
}
Circle.prototype.toString = function(p) {
var s = "Circle(" + Eden.edenCodeForValuesP(p, this.x, this.y, this.radius, this.fillcolour, this.outlinecolour);
if (this.drawingOptions !== undefined) {
s = s + ", " + Eden.edenCodeForValue(this.drawingOptions);
}
s = s + ")";
return s;
};
Circle.prototype.getEdenCode = Circle.prototype.toString;
Circle.prototype.imageMapArea = function () {
return "shape=\"circle\" coords=\"" + this.x + "," + this.y + "," + this.radius + "\"";
}
Circle.prototype.centre = function () {
return new Point(this.x, this.y);
}
}}$;