-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCanvas-Parser.coffee
358 lines (279 loc) · 9.2 KB
/
Canvas-Parser.coffee
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
class Canvas_Geometry
class Line extends Canvas_Geometry
Line::draw = (obj)->
for i in obj
color = if (i.color is null || i.color is undefined )
"#FF0000"
else
i.color
width = if (i.width is null || i.width is undefined )
1
else
i.width
lib.context.beginPath()
lib.context.moveTo(i.startX, i.startY)
lib.context.lineTo(i.endX, i.endY)
lib.context.strokeStyle = color
lib.context.lineWidth = width
lib.context.stroke()
lib.context.closePath()
lib.context.strokeStyle = "#000000"
lib.context.lineWidth = 1
#"Area" : [{
# "points": [[10,30], [40,150], [70,100]],
# "color" : "black",
# "fillColor": "yellow",
# "margin": 5
#}]
class Area extends Canvas_Geometry
Area::draw = (obj)->
for i in obj
color = if (i.color is null || i.color is undefined )
"#FF0000"
else
i.color
fillColor = if (i.fillColor is null || i.fillColor is undefined )
"#ffffff"
else
i.fillColor
lib.context.beginPath()
lib.context.lineCap = "round"
lib.context.moveTo(i.points[0][0], i.height-i.margin)
for k in i.points
lib.context.lineTo(k[0],k[1])
lib.context.lineTo(i.points[i.points.length-1][0],i.height-i.margin)
lib.context.fillStyle = fillColor
lib.context.fill()
lib.context.strokeStyle = color
lib.context.stroke()
lib.context.closePath()
lib.context.strokeStyle = "#000000"
lib.context.lineWidth = 1
lib.context.fillStyle = "#000000"
class Background extends Canvas_Geometry
Background::draw = (value)->
url = "http"
if value.indexOf(url) > -1
lib.canvas.style.backgroundImage = 'url('+value+')'
lib.canvas.style.backgroundSize = '100% 100%'
else
lib.canvas.style.backgroundColor = value
class Diamond extends Canvas_Geometry
Diamond::draw = (obj)->
for i in obj
color = if (i.color is null || i.color is undefined )
"#FF0000"
else
i.color
cursorx = 4
cursory = 4
lib.context.beginPath()
lib.context.moveTo(i.x, i.y-cursory)
lib.context.lineTo(i.x+cursorx, i.y)
lib.context.lineTo(i.x, i.y+cursory)
lib.context.lineTo(i.x - cursorx, i.y)
lib.context.lineTo(i.x, i.y-cursory)
lib.context.fillStyle = color
lib.context.fill()
lib.context.strokeStyle = color
lib.context.stroke()
lib.context.closePath()
lib.context.strokeStyle = "#000000"
lib.context.lineWidth = 1
lib.context.fillStyle = "#000000"
class Point extends Canvas_Geometry
Point::draw = (obj) ->
for i in obj
color = if (i.color is null || i.color is undefined )
"#000000"
else
i.color
fillColor = if (i.fillColor is null || i.fillColor is undefined )
"#ffffff"
else
i.fillColor
linewidth = if (i.linewidth is null || i.linewidth is undefined )
1
else
i.linewidth
radius = if (i.radius is null || i.radius is undefined )
7
else
i.radius
anticlockwise = if (i.anticlockwise is null || i.anticlockwise is undefined )
false
else
i.anticlockwise
startAngle = if (i.startAngle is null || i.startAngle is undefined )
0
else
i.startAngle
endAngle = if (i.endAngle is null || i.endAngle is undefined )
360
else
i.endAngle
lib.context.beginPath()
lib.context.arc(i.x, i.y, radius, (Math.PI / 180) * startAngle, (Math.PI / 180) * endAngle, anticlockwise)
lib.context.fillStyle = fillColor
lib.context.fill()
lib.context.lineWidth = linewidth
lib.context.strokeStyle = color
lib.context.stroke()
lib.context.strokeStyle = "#000000"
lib.context.lineWidth = 1
lib.context.fillStyle = "#000000"
class Picture extends Canvas_Geometry
Picture::draw = (obj) ->
for i in obj
Picture.Process(i)
Picture::Process = (obj) ->
image = new Image()
image.src = obj.src
image.onload = ()->
imageWidth = image.width
imageHeight = image.height
lib.context.save()
lib.context.translate(obj.x - (imageHeight/2), obj.y - (imageWidth/2))
lib.context.drawImage(image, 0, 0)
lib.context.restore()
class Text extends Canvas_Geometry
Text::draw = (obj)->
for i in obj
lib.context.font = "15px Arial";
lib.context.fillText(i.text, i.x, i.y)
class Axes extends Canvas_Geometry
#{type : "x" , orientation: "top", values : [27,54,81,108,135] , min: 5, max: 140, margin: 5, height: 240, width: 180, grid: true}
Axes::draw = (obj) ->
for i in obj
grid = if (i.grid is null || i.grid is undefined )
false
else
i.grid
if i.type == "x"
orientation = if (i.orientation is null || i.orientation is undefined )
"bottom"
else
i.orientation
else
orientation = if (i.orientation is null || i.orientation is undefined )
"left"
else
i.orientation
lib.context.beginPath()
if i.type == "x"
lib.context.strokeStyle = "#000000"
if i.orientation == "top"
lib.context.moveTo(i.margin, i.margin)
lib.context.lineTo(i.width - i.margin, i.margin)
else
lib.context.moveTo(i.margin, i.height - i.margin)
lib.context.lineTo(i.width - i.margin, i.height - i.margin)
lib.context.stroke()
lib.context.strokeStyle = "rgba(128, 128, 255, 0.5)"
# j = 0
# while j <= i.values.length
# currentY = i.margin + j / i.values.length * (i.height - (2 * i.margin))
# if i.grid == true
# lib.context.moveTo i.width - i.margin, currentY
# lib.context.lineTo i.margin, currentY
# lib.context.fillText(Math.ceil(i.min + ((i.max - i.min) / i.values.length) * (i.values.length - j)), 10, currentY + 4);
# j++
j = 0
while j <= i.values.length
currentX = i.margin + j / i.values.length * (i.width - (2 * i.margin))
if i.grid == true
lib.context.moveTo currentX, i.margin
lib.context.lineTo currentX, (i.height - i.margin)
if i.orientation == "top"
lib.context.fillText((i.min + ((i.max - i.min) / i.values.length) * (j)).toFixed(2), currentX - 3,
i.margin / 2)
else
lib.context.fillText((i.min + ((i.max - i.min) / i.values.length) * (j)).toFixed(2), currentX - 3,
(i.height - i.margin) + i.margin / 2)
j++
lib.context.stroke()
lib.context.strokeStyle = "#000000"
if i.type == "y"
if i.orientation == "right"
lib.context.moveTo(i.width - i.margin, i.margin)
lib.context.lineTo(i.width - i.margin, i.height - i.margin)
else
lib.context.moveTo(i.margin, i.margin)
lib.context.lineTo(i.margin, i.height - i.margin)
lib.context.stroke()
lib.context.strokeStyle = "rgba(128, 128, 255, 0.5)"
# j = 0
# while j <= i.values.length
# currentX = i.margin + j / i.values.length * (i.width - (2 * i.margin))
# if i.grid == true
# lib.context.moveTo currentX, i.margin
# lib.context.lineTo currentX, (i.height - i.margin)
# lib.context.fillText(Math.ceil(i.min + ((i.max - i.min) / i.values.length) * (j)), currentX - 3,
# (i.height - i.margin) + i.margin / 2)
# j++
j = 0
while j <= i.values.length
currentY = i.margin + j / i.values.length * (i.height - (2 * i.margin))
if i.grid == true
lib.context.moveTo i.width - i.margin, currentY
lib.context.lineTo i.margin, currentY
if i.orientation == "right"
lib.context.fillText((i.min + ((i.max - i.min) / i.values.length) * (i.values.length - j)).toFixed(2), 10 + i.width - i.margin , currentY + 4);
else
lib.context.fillText((i.min + ((i.max - i.min) / i.values.length) * (i.values.length - j)).toFixed(2), 10, currentY + 4);
j++
lib.context.stroke()
lib.context.strokeStyle = "#000000"
class Tooltip
constructor: (@obj) ->
Tooltip::draw = (value) ->
#
# div = d3.select("body").append("div")
# .attr("class", "tooltip")
# .style("opacity", 1e-6)
#
#
# svglayer = d3.select("svg")
# .on("mouseover", Tooltip.mouseover)
# .on("mousemove", Tooltip.mousemove)
# .on("mouseout", Tooltip.mouseout)
Tooltip::mouseover = ()->
# d3.select("body").append("div")
# .attr("class", "tooltip")
# .style("opacity", 1e-6).transition()
# .duration(500)
# .style("opacity", 1)
Tooltip::mousemove = () ->
# d3.select("body").append("svg:image")
# .attr("xlink:href", "http://www.e-pint.com/epint.jpg")
# .attr("width", 150)
# .attr("height", 200);
Tooltip::mouseout = () ->
# d3.select("body").append("div")
# .attr("class", "tooltip")
# .style("opacity", 1e-6).transition()
# .duration(500)
# .style("opacity", 1e-6)
Tooltip::getMousePos = (canvas, evt) ->
# rect = lib.canvas.getBoundingClientRect()
# cords =
# x: evt.clientX - rect.left
# y: evt.clientY - rect.top
# return cords
Canvas_Parse = (obj)->
lib.canvas = document.getElementById(obj.id)
lib.context = lib.canvas.getContext("2d")
lib.Line = new Line()
lib.Point = new Point()
lib.Picture = new Picture()
lib.Axes = new Axes()
lib.Diamond = new Diamond()
lib.Background = new Background()
lib.Tooltip = new Tooltip(obj)
lib.Area = new Area()
lib.Text = new Text()
for key,value of obj
if key == "id"
continue
else
lib[key]["draw"](value)