-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagemap.js
307 lines (269 loc) · 10.1 KB
/
imagemap.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* This uses an imageId and an imagemap id.
* It will render a canvas over the imagemap.
* When something is clicked the underlying imagemap is used to render a figure with fillColors over the image.
* The imageareas are augmented with a "toggled" attribute.
*
* @param imageIdL Id of the image.
* @param mapIdL Id of the map.
* @param updateFunctionL Your updatefunction, can be null.
* @param fillColor a color, preferably with transparency used to highlight an image.
* @returns {{}}
* @constructor
*/
function ImageMapAugChecks(imageIdL, mapIdL, updateFunctionL, fillColor) {
const toggleAttribute = "toggled";
/**
*
* @param ids array of ids to toggle.
*/
function populateAreas(ids) {
var map = document.getElementById(this.mapId);
if (map === null) {
throw "Found no map elements with id " + this.mapId;
}
if (ids.constructor !== Array) {
throw "ids isn't an array " + ids;
}
var childNodes = Array.from(map.childNodes);
var codeAttribute = this.codeAttribute;
childNodes.forEach(function (child) {
if (child.nodeType !== Node.TEXT_NODE && child.nodeType !== Node.COMMENT_NODE) {
if (contains.call(ids, child.getAttribute(codeAttribute))) {
child.setAttribute(toggleAttribute, 1);
} else {
child.setAttribute(toggleAttribute, 0);
}
}
});
}
function getDictionary() {
var dictionary = {};
var map = document.getElementById(this.mapId);
if (map === null) {
throw "Found no map elements with id " + this.mapId;
}
var childNodes = Array.from(map.childNodes);
var codeAttribute = this.codeAttribute;
childNodes.forEach(function (child) {
if (child.nodeType !== Node.TEXT_NODE && child.nodeType !== Node.COMMENT_NODE) {
var codeAttributeValue = child.getAttribute(codeAttribute);
if (!child.hasAttribute(toggleAttribute) || child.getAttribute(toggleAttribute) == 0) {
dictionary[codeAttributeValue] = 0;
} else {
dictionary[codeAttributeValue] = 1;
}
}
});
return dictionary;
}
function getPos(el) {
// yay readability
for (var lx=0, ly=0;
el != null;
lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
return {x: lx,y: ly};
}
/**
* Highlight the selected areas over the image.
*/
function renderMap() {
function createDrawingCanvas() {
canvas = document.createElement("canvas");
canvas.id = this.imageId + "canvas";
console.log("Created a canvas with id: " + canvas.id);
canvas.style.position = "absolute";
var checkTime = 100; //100 ms interval
var check = setInterval(function() {
if (image.offsetWidth === 0)
{
canvas.style.left = "-1px";
canvas.style.top = "-1px";
canvas.width = "0px";
canvas.height = "0px";
} else
{
var position = getPos(image);
if (canvas.style.left !== position.x+"px" || canvas.style.top !== position.y+"px")
{
canvas.style.left = position.x + "px";
canvas.style.top = position.y + "px";
canvas.width = image.width;
canvas.height = image.height;
renderMap.call(canvas.imageMapAugChecks);
}
}
}, checkTime);
canvas.zIndex = 2000;
canvas.imageMapAugChecks = this;
}
var imageId = this.imageId;
var mapId = this.mapId;
var image = document.getElementById(imageId);
if (image === null) throw "Image with id " + imageId + " not found.";
var canvas = document.getElementById(imageId + "canvas");
if (canvas === null) {
createDrawingCanvas.call(this);
canvas.onclick = clickOnImage;
document.body.appendChild(canvas);
}
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
var map = document.getElementById(this.mapId);
if (map === null) {
throw "Found no map elements with id " + mapId;
}
var childNodes = Array.from(map.childNodes);
var fillColor = this.fillColor;
childNodes.forEach(function (child) {
if (child.nodeType !== Node.TEXT_NODE && child.nodeType !== Node.COMMENT_NODE) {
if (child.getAttribute(toggleAttribute) == 1) {
draw(ctx, child.getAttribute("coords").split(","), fillColor);
}
}
});
}
/**
* Toggle an area.
* @param area
* @param imageMapAugChecks
*/
function toggle(area, imageMapAugChecks) {
var newValue = 1;
if (area.hasAttribute(toggleAttribute)) {
var val = area.getAttribute(toggleAttribute);
if (val == 1) newValue = 0;
}
area.setAttribute(toggleAttribute, newValue);
console.info("Toggled " + area + " to " + newValue);
imageMapAugChecks.renderMap();
if (imageMapAugChecks.updateFunction != null) {
imageMapAugChecks.updateFunction(imageMapAugChecks.getDictionary());
}
}
/**
* We must find the area map that is behind the canvas. The trick is to hide the canvas quickly.
* Find the element at x, y and render the map again.
* @param event A click event.
*/
function clickOnImage(event) {
// find the element at x,y
// toggle it.
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE");
var pageXcorrection = 0;
var pageYcorrection = 0;
if (msie !== -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./))
{
var pageXcorrection = window.pageXOffset;
var pageYcorrection = window.pageYOffset;
}
var canvas = document.elementFromPoint(event.x - pageXcorrection, event.y - pageYcorrection);
var mapId = canvas.imageMapAugChecks.mapId;
var canvasPos = getPos(canvas);
var area = findAreaByMapId(mapId, event.pageX - canvasPos.x, event.pageY - canvasPos.y, canvas);
if (area != null)
{
toggle(area, canvas.imageMapAugChecks);
}
}
function findAreaByMapId(mapId, x, y, canvas)
{
var map = document.getElementById(mapId);
var childNodes = Array.from(map.childNodes);
var area = null;
var tempcanvas = document.createElement("canvas");
tempcanvas.width = canvas.width;
tempcanvas.height = canvas.height;
var ctx = tempcanvas.getContext("2d");
childNodes.forEach(function (child) {
if (child.nodeType !== Node.TEXT_NODE && child.nodeType !== Node.COMMENT_NODE && child.getAttribute("coords") != null) {
ctx.clearRect(0, 0, tempcanvas.width, tempcanvas.height);
draw(ctx, child.getAttribute("coords").split(","), "rgb(255,212,0)");
var p = ctx.getImageData(x, y, 1, 1).data;
if (p[0] !== 0)
area = child;
}
});
return area;
}
/**
* Draw the image according to the coordinates.
* @param ctx Canvas drawing context.
* @param coords Coords as from imageMap.
* @param fillColor color to fill the image up with.
*/
function draw(ctx, coords, fillColor) {
function drawPoly(ctx, coords) {
ctx.beginPath();
ctx.moveTo(coords[0], coords[1]);
for (var index = 2; index < coords.length; index += 2) {
ctx.lineTo(coords[index], coords[index + 1]);
}
ctx.closePath();
ctx.fill();
}
/**
*
* @param ctx
* @param coords x, y, radius
*/
function drawCircle(ctx, coords) {
ctx.beginPath();
ctx.arc(coords[0], coords[1], coords[2], 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
}
/**
*
* @param ctx Context
* @param coords int[]
*/
function drawRectangle(ctx, coords) {
ctx.fillRect(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]);
}
ctx.fillStyle = fillColor;
if (coords.length < 3) {
throw "Unexpectedly low coordinates: " + coords.length + " " + coords;
}
if (coords.length === 4) {
drawRectangle(ctx, coords);
}
else if (coords.length === 3) {
drawCircle(ctx, coords);
} else {
drawPoly(ctx, coords);
}
}
var contains = function (needle) {
// Per spec, the way to identify NaN is that it is not equal to itself
var findNaN = needle !== needle;
var indexOf;
if (!findNaN && typeof Array.prototype.indexOf === 'function') {
indexOf = Array.prototype.indexOf;
} else {
indexOf = function (needle) {
var i = -1, index = -1;
for (i = 0; i < this.length; i++) {
var item = this[i];
if ((findNaN && item !== item) || item === needle) {
index = i;
break;
}
}
return index;
};
}
return indexOf.call(this, needle) > -1;
};
var obj = {};
obj.codeAttribute = "code";
obj.imageId = imageIdL;
obj.mapId = mapIdL;
obj.updateFunction = updateFunctionL;
obj.renderMap = renderMap;
obj.populateAreas = populateAreas;
obj.getDictionary = getDictionary;
obj.fillColor = fillColor;
return obj;
}