-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathleaflet.boatmarker.js
179 lines (141 loc) · 3.95 KB
/
leaflet.boatmarker.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* LEAFLET.BOATMARKER
* v1.1.0
* Thomas Brüggemann
*/
/* BOAT ICON */
L.BoatIcon = L.Icon.extend({
// OPTIONS
options: {
iconSize: new L.Point(150, 150),
className: "leaflet-boat-icon",
course: 0,
speed: 0,
color: "#8ED6FF",
labelAnchor: [23, 0],
wind: false,
windDirection: 0,
windSpeed: 0,
idleCircle: false
},
// PROPERTIES
x: 66,
y: 85,
x_fac: 0.18,
y_fac: 0.18,
ctx: null,
lastHeading: 0,
lastWindDirection: 0,
// CREATE ICON
// setup the icon and start drawing
createIcon: function () {
var e = document.createElement("canvas");
this._setIconStyles(e, "icon");
var s = this.options.iconSize;
e.width = s.x;
e.height = s.y;
this.lastHeading = 0; // reset in case the marker is removed and added again
this.ctx = e.getContext("2d");
this.draw(this.ctx, s.x, s.y);
return e;
},
// DRAW
// renders the boat icon onto the canvas element
draw: function(ctx, w, h) {
if(!ctx) return;
var x = this.x;
var y = this.y;
var x_fac = this.x_fac;
var y_fac = this.y_fac;
ctx.clearRect(0, 0, w, h);
ctx.translate(w/2, h/2);
ctx.rotate(this.options.course*Math.PI/180);
ctx.translate(-w/2, -h/2);
//ctx.fillRect(0,0,w,h);
ctx.beginPath();
// draw idle boat shape
if(this.options.idleCircle === true && this.options.speed === 0) {
ctx.arc(x+(50*x_fac), y-(50*y_fac), 50*x_fac, 0, 2 * Math.PI);
}
// draw boat shape in motion
else {
ctx.moveTo(x, y);
ctx.bezierCurveTo(x, y+(80*y_fac), x+(100*x_fac), y+(80*y_fac), x+(100*x_fac), y);
ctx.quadraticCurveTo(x+(100*x_fac), y-(100*y_fac), x+(50*x_fac), y-(200*y_fac));
ctx.quadraticCurveTo(x, y-(100*y_fac), x, y);
}
ctx.fillStyle = this.options.color;
ctx.fill();
ctx.stroke();
ctx.closePath();
// draw wind
if(this.options.wind == true) {
ctx.translate(w/2, h/2);
ctx.rotate(this.options.windDirection*Math.PI/180);
ctx.translate(-w/2, -h/2);
ctx.beginPath();
ctx.moveTo(w/2, y-45);
ctx.lineTo(w/2, y-70);
var center = w/2;
var spd = 5 * Math.round(this.options.windSpeed / 5);
var tenLines = Math.floor(spd / 10);
var fiveLine = ((spd % 10) > 0);
var carriage = 70;
for(var i = 0; i < tenLines; i++) {
ctx.moveTo(center, y - carriage);
ctx.lineTo(center + 8, y - carriage - 8);
carriage -= 5;
}
if(fiveLine) {
if(tenLines == 0) carriage -= 5;
ctx.moveTo(center, y - carriage);
ctx.lineTo(center + 5, y - carriage - 5);
}
ctx.stroke();
}
},
setHeadingWind: function(heading, windSpeed, windDirection) {
this.options.wind = true;
this.options.course = (heading % 360) - this.lastHeading;
this.lastHeading = heading % 360;
this.options.windDirection = (windDirection % 360) - (heading % 360);
this.lastHeading += this.options.windDirection;
this.options.windSpeed = windSpeed;
var s = this.options.iconSize;
this.draw(this.ctx, s.x, s.y);
},
// SET HEADING
// sets the boat heading and
// update the boat icon accordingly
setHeading: function(heading) {
this.options.course = (heading % 360) - this.lastHeading;
this.lastHeading = heading % 360;
var s = this.options.iconSize;
this.draw(this.ctx, s.x, s.y);
},
// SET SPEED
// sets the boat speed value and
// update the boat icon accordingly
setSpeed: function(speed) {
this.options.speed = speed;
var s = this.options.iconSize;
this.draw(this.ctx, s.x, s.y);
}
});
L.BoatMarker = L.Marker.extend({
setHeadingWind: function(heading, windSpeed, windDirection) {
this.options.icon.setHeadingWind(heading, windSpeed, windDirection);
},
setHeading: function(heading) {
this.options.icon.setHeading(heading);
},
setSpeed: function(speed) {
this.options.icon.setSpeed(speed);
}
});
L.boatMarker = function(pos, options) {
var c = ("color" in options) ? options.color : "#f1c40f";
var i = ("idleCircle" in options) ? options.idleCircle : false;
options.icon = new L.BoatIcon({ color: c, idleCircle: i});
return new L.BoatMarker(pos, options);
};