charting_ui 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42a8038a79d2cf2b8ad5025a28bbd181e7a37f05
4
+ data.tar.gz: 419ef3f40e265926ab109ab633ed71aaf41dc577
5
+ SHA512:
6
+ metadata.gz: eb10c05540c28820afc7a840d3b52b72bc8a9ee320d419f069ee656bf0be255577cbec268bf58f4ce654010da9430096d88b727df1a0adaf5991d35f26fb1cc6
7
+ data.tar.gz: 19d390669cdf17ac0ebb996b227ee1fdcc67b424b14d2928fe131ad1984ae8aa4a3c294dc6f62e153d1e3d9c4d424b732eba737328a8223abad963092feb3c05
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at leo.benkel@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,25 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in charting_ui.gemspec
4
+ gemspec
5
+
6
+ # TODO: move it into the gemfile
7
+
8
+ # Use jquery as the JavaScript library
9
+ gem 'jquery-rails', '> 3.0.0'
10
+
11
+ # To use a debugger
12
+ gem 'byebug', group: [:development, :test]
13
+
14
+ # Know when its a mobile device
15
+ gem "browser", '2.1.0' # https://github.com/fnando/browser
16
+
17
+ # Use SCSS for stylesheets
18
+ gem 'sass-rails', '>= 3.2'
19
+
20
+ # Use CoffeeScript for .coffee assets and views
21
+ gem 'coffee-rails', '~> 4.1.0'
22
+ gem 'coffee-script-source', '1.8.0', platforms: [:mingw, :mswin, :x64_mingw]
23
+
24
+ # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
25
+ gem 'spring', group: [:development, :test, :staging]
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Leo Benkel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # ChartingUi
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/charting_ui`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'charting_ui'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install charting_ui
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/charting_ui. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,525 @@
1
+ #= require ./charting_point
2
+
3
+ class window.ChartEngine_Core
4
+
5
+ ############# VARIABLES #################
6
+ Super_Verbose = false
7
+ first_time_dimension: true
8
+
9
+ canvas_element: null
10
+ ctx: null
11
+ ctx_interactive: null
12
+ canvas_element_interactive: null
13
+ info_box: null
14
+ source: null
15
+ min_x: null
16
+ max_x: null
17
+ min_y: null
18
+ max_y: null
19
+
20
+ indent_x: null
21
+ indent_y: null
22
+
23
+ axe_x_title: null
24
+ axe_y_title: null
25
+
26
+ offset_left: -1
27
+ offset_right: -1
28
+ offset_top: -1
29
+ offset_bottom: -1
30
+
31
+ canvas_width: null
32
+ canvas_height: null
33
+
34
+ default_font_size: -1
35
+ default_font: null
36
+
37
+ default_line_color: "#aaaaff"
38
+ default_cross_color: "magenta"
39
+ default_color: "black"
40
+ default_background_color: "white"
41
+
42
+ old_width: -1
43
+ old_height: -1
44
+
45
+ info_box_padding: 5
46
+
47
+ previous_drawn_region: [0, 0, 0, 0]
48
+
49
+ ready_to_run: false
50
+
51
+ colors: []
52
+
53
+ color_resource: [[240,163,255],[0,117,220],[153,63,0],[76,0,92],[25,25,25],[0,92,49],[43,206,72],[255,204,153],[128,128,128],[148,255,181],[143,124,0],[157,204,0],[194,0,136],[0,51,128],[255,164,5],[255,168,187],[66,102,0],[255,0,16],[94,241,242],[0,153,143],[224,255,102],[116,10,255],[153,0,0],[255,255,128],[255,255,0],[255,80,5]]
54
+
55
+ #########################################
56
+ ############### SETUP ###################
57
+ constructor: (block) ->
58
+ console.log "---------STARTING NEW CHART----------"
59
+ self = this
60
+ _dom_element_fix = $(block).find('.charting_ui_chart_fix')
61
+ _dom_element_interactive = $(block).find('.charting_ui_chart_interactive')
62
+ _dom_info_box = $(block).find('.charting_ui_info_block')
63
+ _data = $(block).find('.charting_ui_data')
64
+
65
+
66
+ @canvas_element = $(_dom_element_fix)
67
+ @ctx = @canvas_element.get(0).getContext("2d")
68
+
69
+ @canvas_element_interactive = $(_dom_element_interactive)
70
+ @ctx_interactive = @canvas_element_interactive.get(0).getContext("2d")
71
+
72
+ @info_box = $(_dom_info_box)
73
+
74
+ @data = $(_data).data().data
75
+
76
+ #@colors = [@default_line_color, "green", "red" ]
77
+ $.each(@color_resource, (i, value) ->
78
+ self.colors.push("rgb(" + value[0] + ", " + value[1] + ", " + value[2] + ")")
79
+ )
80
+
81
+ @axe_x_title = @data.axes.x.title
82
+ @axe_y_title = @data.axes.y.title
83
+
84
+ @offset_left = @data.offsets.left
85
+ @offset_right = @data.offsets.right
86
+ @offset_top = @data.offsets.top
87
+ @offset_bottom = @data.offsets.bottom
88
+
89
+ @default_font_size = @data.font.size
90
+ @default_font = @data.font.family
91
+
92
+ @info_box.empty().append($(@get_info_box_content()))
93
+
94
+ @ready_to_run = @load_source()
95
+
96
+ if @ready_to_run
97
+ @canvas_resize()
98
+ @linkMouseInteraction()
99
+ console.log "Chart CORE initialized!"
100
+ else
101
+ @info_box.empty()
102
+ $(_data).text("...Failed to load the chart...")
103
+ $(_data).show()
104
+ console.log "Failed to load chart"
105
+
106
+ load_source: () ->
107
+ console.log("'load_source' need to be implemented.")
108
+ throw new Error("'load_source' need to be implemented.")
109
+
110
+ get_info_box_content: () ->
111
+ console.log("'get_info_box_content' need to be implemented.")
112
+ throw new Error("'get_info_box_content' need to be implemented.")
113
+
114
+ linkMouseInteraction: ->
115
+ self = this
116
+ @canvas_element_interactive.mouseup (e) ->
117
+ self.handlemouseup(e)
118
+
119
+ @canvas_element_interactive.mousedown (e) ->
120
+ self.handlemousedown(e)
121
+
122
+ @canvas_element_interactive.mousemove (e) ->
123
+ self.handlemousemove(e)
124
+
125
+ @canvas_element_interactive.mouseout (e) ->
126
+ self.handlemouseout(e)
127
+
128
+ @canvas_element_interactive.get(0).addEventListener("touchstart", @handlemousedown, false)
129
+ @canvas_element_interactive.get(0).addEventListener("touchend", @handlemouseup, false)
130
+ @canvas_element_interactive.get(0).addEventListener("touchcancel", @handlemouseup, false)
131
+ @canvas_element_interactive.get(0).addEventListener("touchmove", @handlemousemove, false)
132
+
133
+ return true
134
+
135
+ #########################################
136
+ ############ DRAW GLOBAL ################
137
+ draw_axes: () ->
138
+ @draw_axe_x()
139
+ @draw_axe_x_indent(@getIndentation())
140
+ @draw_axe_y()
141
+ @draw_axe_y_indent()
142
+ @draw_axes_titles()
143
+
144
+ redraw: () ->
145
+ @clear()
146
+ @before_redraw()
147
+ @draw_chart()
148
+ @draw_axes()
149
+
150
+ clear: () ->
151
+ @ctx.clearRect(0, 0, @canvas_width, @canvas_height)
152
+ @ctx_interactive.clearRect(0, 0, @canvas_width, @canvas_height)
153
+
154
+ draw_chart: () ->
155
+ console.log("'draw_chart' need to be implemented.")
156
+ throw new Error("'draw_chart' need to be implemented.")
157
+
158
+ getIndentation: () ->
159
+ return 10
160
+
161
+ before_redraw: () ->
162
+
163
+ #########################################
164
+ ############# DRAW AXES #################
165
+ draw_axe_x: () ->
166
+ @draw_line(
167
+ @convert_unit_to_pixel(new Chart_Point(@min_x, @min_y)),
168
+ @convert_unit_to_pixel(new Chart_Point(@max_x, @min_y))
169
+ )
170
+
171
+ draw_axe_x_indent: (indentation) ->
172
+ delta = (@max_x - @min_x)
173
+ indentation = Math.min(indentation, delta)
174
+ @indent_x = delta / indentation * 1.0
175
+ for i in [0..indentation]
176
+ n = i * @indent_x + @min_x
177
+ unit_y = @convert_pixel_to_unit_y(@canvas_height - @offset_bottom + 5)
178
+
179
+ @draw_line(
180
+ @convert_unit_to_pixel(new Chart_Point(n, @min_y)),
181
+ @convert_unit_to_pixel(new Chart_Point(n, unit_y))
182
+ )
183
+
184
+ x_pixel = @get_axe_text_x_pixel_position(n)
185
+ y_pixel = @canvas_height - @offset_bottom + 5 + @default_font_size
186
+ location = new Chart_Point(x_pixel, y_pixel)
187
+
188
+ @draw_text(@get_axe_text_x_text(n), location)
189
+
190
+ get_axe_text_x_pixel_position: (n) ->
191
+ return @convert_unit_to_pixel_x(n)
192
+
193
+ get_axe_text_x_text: (n) ->
194
+ return n
195
+
196
+ draw_axe_y_indent: (indentation = 10) ->
197
+ delta = (@max_y - @min_y)
198
+ indentation = Math.min(indentation, delta)
199
+ @indent_y = delta / indentation * 1.0
200
+ for i in [0..indentation]
201
+ n = i * @indent_y + @min_y
202
+ unit_x = @convert_pixel_to_unit_x(@offset_left - 5)
203
+
204
+ @draw_line(
205
+ @convert_unit_to_pixel(new Chart_Point(@min_x, n)),
206
+ @convert_unit_to_pixel(new Chart_Point(unit_x, n))
207
+ )
208
+
209
+ y_pixel = (@convert_unit_to_pixel_y(n) + (@default_font_size * 0.4))
210
+ x_pixel = @offset_left - 7
211
+ location = new Chart_Point(x_pixel, y_pixel)
212
+
213
+ @draw_text(parseInt(n), location, "right")
214
+
215
+ draw_axes_titles: () ->
216
+ axe_title_font_size = 15
217
+
218
+ # x
219
+ self = this
220
+
221
+ align = "left"
222
+ total_width_text = @get_text_width(@axe_x_title, align, axe_title_font_size)
223
+ extra_width = 0
224
+ $.each(@axe_x_title.split(","),
225
+ (i, title)->
226
+ extra_width += self.draw_text(title, new Chart_Point(self.canvas_width / 2 - total_width_text / 2 + extra_width, self.canvas_height - axe_title_font_size * 0.5 ), align, axe_title_font_size, self.colors[i])
227
+ )
228
+
229
+ # y
230
+ @draw_text(@axe_y_title, new Chart_Point(@offset_left, @offset_top - axe_title_font_size * 0.9 ), "center", axe_title_font_size)
231
+
232
+ draw_axe_y: () ->
233
+ @draw_line(
234
+ @convert_unit_to_pixel(new Chart_Point(@min_x, @min_y)),
235
+ @convert_unit_to_pixel(new Chart_Point(@min_x, @max_y))
236
+ )
237
+
238
+ #########################################
239
+ ############ DRAW BASICS ################
240
+ draw_lines: (points, color = @default_color) ->
241
+ previous_point = points[0]
242
+ for i in [1..points.length - 1]
243
+ @draw_line(previous_point, points[i], color)
244
+ previous_point = points[i]
245
+ return
246
+
247
+ draw_line: (point1, point2, color = @default_color, thickness = 2) ->
248
+ @draw_line_generic(@ctx, point1, point2, color, thickness)
249
+
250
+ draw_line_interactive: (point1, point2, color = @default_color, thickness = 2) ->
251
+ @draw_line_generic(@ctx_interactive, point1, point2, color, thickness)
252
+
253
+ draw_line_generic: (ctx, point1, point2, color = @default_color, thickness = 2) ->
254
+ ctx.beginPath()
255
+ ctx.lineWidth = thickness
256
+ ctx.fillStyle = color
257
+ ctx.strokeStyle = color
258
+ ctx.moveTo(point1.x, point1.y)
259
+ ctx.lineTo(point2.x, point2.y)
260
+ ctx.stroke()
261
+
262
+ if Super_Verbose
263
+ console.log("Draw line from " + point1.to_s() + " to " + point2.to_s())
264
+
265
+ draw_text: (text, point, align = "center", font_size = @default_font_size, color = @default_color, font = @default_font) ->
266
+ return @draw_text_generic(@ctx, text, point, align, font_size, color, font)
267
+
268
+ draw_text_generic: (context, text, point, align = "center", font_size = @default_font_size, color = @default_color, font = @default_font) ->
269
+ if text == null
270
+ return
271
+ context.fillStyle = color
272
+ context.textAlign = align
273
+ context.font = font_size + "px " + font
274
+ context.fillText(text, point.x, point.y)
275
+ if Super_Verbose
276
+ console.log "Drawing text: '" + text + "' at " + point.to_s()
277
+ return context.measureText(text).width
278
+
279
+ get_text_width: (text, align = "center", font_size = @default_font_size, font = @default_font) ->
280
+ @ctx.textAlign = align
281
+ @ctx.font = font_size + "px " + font
282
+ return @ctx.measureText(text).width
283
+
284
+ draw_circle: (context, point, radius, fillcolor = @default_cross_color, tickness = 2, bordercolor = @default_color) ->
285
+ context.beginPath()
286
+ context.arc(point.x, point.y, radius, 0, 2 * Math.PI, false)
287
+ context.fillStyle = fillcolor
288
+ context.fill()
289
+ context.lineWidth = tickness
290
+ context.strokeStyle = bordercolor
291
+ context.stroke()
292
+
293
+ return [point.x - radius, point.y - radius, point.x + radius, point.y + radius]
294
+
295
+ draw_rectangle: (point1, point2, fillcolor = @default_line_color, bordercolor = @default_background_color, thickness = 2) ->
296
+ return @draw_rectangle_generic(@ctx, point1, point2, fillcolor, bordercolor, thickness)
297
+
298
+ draw_rectangle_interactive: (point1, point2, fillcolor = @default_line_color, bordercolor = @default_background_color, thickness = 2) ->
299
+ return @draw_rectangle_generic(@ctx_interactive, point1, point2, fillcolor, bordercolor, thickness)
300
+
301
+ draw_rectangle_generic: (context, point1, point2, fillcolor = @default_line_color, bordercolor = @default_background_color, thickness = 2) ->
302
+ context.beginPath()
303
+ context.rect(Math.min(point1.x, point2.x), Math.min(point1.y, point2.y),
304
+ Math.abs(point2.x - point1.x), Math.abs(point1.y - point2.y)
305
+ )
306
+ context.fillStyle = fillcolor
307
+ context.fill()
308
+ if thickness > 0
309
+ context.lineWidth = thickness
310
+ context.strokeStyle = bordercolor
311
+ context.stroke()
312
+
313
+ if Super_Verbose
314
+ console.log("Draw rectangle from: " + point1.to_s() + ", to: " + point2.to_s())
315
+
316
+ return [
317
+ Math.min(point1.x, point2.x) - thickness,
318
+ Math.min(point1.y, point2.y) - thickness,
319
+ Math.max(point1.x, point2.x) + thickness,
320
+ Math.max(point1.y, point2.y) + thickness
321
+ ]
322
+
323
+ #########################################
324
+ ######### CONVERT COORDINATE ############
325
+ convert_unit_to_pixel_x: (x) ->
326
+ delta_x = @max_x - @min_x
327
+ ratio_x = (@canvas_width - @offset_left - @offset_right) / delta_x * 1.0
328
+ new_x = ((x - @min_x) * ratio_x) + @offset_left
329
+ return new_x
330
+
331
+ convert_pixel_to_unit_x: (x) ->
332
+ delta_x = @max_x - @min_x
333
+ ratio_x = delta_x / (@canvas_width - @offset_left - @offset_right) * 1.0
334
+ new_x = ((x - @offset_left ) * ratio_x) + @min_x
335
+ return new_x
336
+
337
+ convert_unit_to_pixel_y: (y) ->
338
+ delta_y = @max_y - @min_y
339
+ ratio_y = (@canvas_height - @offset_bottom - @offset_top) / delta_y * 1.0
340
+ new_y = @canvas_height - ((y - @min_y) * ratio_y) - @offset_bottom
341
+ return new_y
342
+
343
+ convert_pixel_to_unit_y: (y) ->
344
+ delta_y = @max_y - @min_y
345
+ ratio_y = delta_y / (@canvas_height - @offset_bottom - @offset_top) * 1.0
346
+ new_y = ((@canvas_height - y - @offset_bottom) * ratio_y) + @min_y
347
+ return new_y
348
+
349
+ convert_pixel_to_unit: (point) ->
350
+ return new Chart_Point(@convert_pixel_to_unit_x(point.x), @convert_pixel_to_unit_y(point.y))
351
+
352
+ convert_unit_to_pixel: (point) ->
353
+ return new Chart_Point(@convert_unit_to_pixel_x(point.x), @convert_unit_to_pixel_y(point.y))
354
+
355
+ test_convert_unit_pixel_unit: (x, y)->
356
+ return @convert_pixel_to_unit(@convert_unit_to_pixel(new Chart_Point(x, y)))
357
+
358
+ test_convert_unit_pixel_unit_x: (x)->
359
+ return @convert_pixel_to_unit_x(@convert_unit_to_pixel_x(x))
360
+
361
+ test_convert_unit_pixel_unit_y: (y)->
362
+ return @convert_pixel_to_unit_y(@convert_unit_to_pixel_y(y))
363
+
364
+ round_up: (value, decimal = 2)->
365
+ result = Math.round(value * Math.pow(10, decimal)) / Math.pow(10, decimal)
366
+ pieces = (result + "").split(".")
367
+ decimal_part = pieces[1] || ""
368
+ while decimal_part.length < decimal
369
+ decimal_part += "0"
370
+
371
+ return pieces[0] + "." + decimal_part
372
+
373
+ #########################################
374
+ ############## RESIZE ###################
375
+ canvas_resize: () ->
376
+ if !@ready_to_run
377
+ return false
378
+
379
+ width = @getWindowWidth()
380
+ height = width / 1.618
381
+ height = Math.min(height, @getWindowHeight() / (1.618 * 2) )
382
+
383
+ if width == @old_width && height == @old_height
384
+ return
385
+
386
+ if Super_Verbose || @first_time_dimension
387
+ console.log 'w: ' + width
388
+ console.log 'h: ' + height
389
+ @first_time_dimension = false
390
+
391
+ $(@canvas_element).attr('width', width)
392
+ $(@canvas_element_interactive).attr('width', width)
393
+ @canvas_width = width
394
+
395
+ $(@canvas_element).attr('height', height)
396
+ $(@canvas_element_interactive).attr('height', height)
397
+ @canvas_height = height
398
+
399
+ $(@canvas_element).parent().css('width', width)
400
+ $(@canvas_element).parent().css('height', height)
401
+
402
+ @redraw()
403
+ @old_width = width
404
+ @old_height = height
405
+ return true
406
+
407
+ getWindowWidth: () ->
408
+ return $(@canvas_element).parent().parent().innerWidth()
409
+
410
+ getWindowHeight: () ->
411
+ return $(window).innerHeight()
412
+ #########################################
413
+ ######### UPDATE INTERACTIVE ############
414
+ updateInteractiveCanvas: (mouse_coordinate) ->
415
+ @cleanUpInteractive()
416
+ if(mouse_coordinate.x <= @offset_left || mouse_coordinate.x >= @canvas_width - @offset_right || mouse_coordinate.y <= @offset_top || mouse_coordinate.y >= @canvas_height - @offset_bottom)
417
+ if !@info_box_always_on()
418
+ @info_box.hide()
419
+ else
420
+ @info_box.show()
421
+ @updateInfoBox(mouse_coordinate)
422
+ return
423
+
424
+
425
+ if @update_user_interaction(mouse_coordinate)
426
+ @info_box.show()
427
+ @updateInfoBox(mouse_coordinate)
428
+ else
429
+ @cleanUpInteractive()
430
+ @info_box.hide()
431
+
432
+ info_box_always_on: () ->
433
+ return false
434
+
435
+ update_user_interaction: (mouse_coordinate) ->
436
+ console.log("'update_user_interaction' need to be implemented.")
437
+ throw new Error("'update_user_interaction' need to be implemented.")
438
+
439
+ updateInfoBox: (mouse_coordinate) ->
440
+ @update_info_box_content(mouse_coordinate)
441
+ @updateInfoBoxPosition(mouse_coordinate)
442
+
443
+ updateInfoBoxPosition: (mouse_coordinate) ->
444
+ if @canvas_width - mouse_coordinate.x - @offset_right <= @info_box.outerWidth()
445
+ @info_box.css('left', (mouse_coordinate.x - @info_box.outerWidth() - @info_box_padding) + 'px')
446
+ else
447
+ @info_box.css('left', (mouse_coordinate.x + @info_box_padding) + 'px')
448
+
449
+ if @canvas_height - mouse_coordinate.y - @offset_bottom <= @info_box.outerHeight()
450
+ @info_box.css('top', (mouse_coordinate.y - @info_box.outerHeight() - @info_box_padding) + 'px')
451
+ else
452
+ @info_box.css('top', (mouse_coordinate.y + @info_box_padding) + 'px')
453
+
454
+ @update_info_box_content(mouse_coordinate)
455
+
456
+ update_info_box_content: (mouse_coordinate) ->
457
+ console.log("'update_info_box_content' need to be implemented.")
458
+ throw new Error("'update_info_box_content' need to be implemented.")
459
+
460
+ cleanUpInteractive: () ->
461
+ if @previous_drawn_region.length == 0
462
+ return
463
+
464
+ self = this
465
+ $.each(@previous_drawn_region, (i, elem) ->
466
+ self.ctx_interactive.clearRect(
467
+ Math.min(self.canvas_width, Math.max(0, elem[0])),
468
+ Math.min(self.canvas_height, Math.max(0, elem[1])),
469
+ Math.min(self.canvas_width, Math.max(0, elem[2])),
470
+ Math.min(self.canvas_height, Math.max(0, elem[3]))
471
+ )
472
+ )
473
+
474
+ @previous_drawn_region = []
475
+
476
+ #########################################
477
+ ############# USER INPUT ################
478
+ extractCoordinate: (e) ->
479
+ e.preventDefault()
480
+ if e.touches?
481
+ if e.touches.length == 0
482
+ return null
483
+ x = e.touches[0].pageX - e.touches[0].target.offsetParent.offsetLeft
484
+ y = e.touches[0].pageY - e.touches[0].target.offsetParent.offsetTop
485
+ else
486
+ x = e.offsetX
487
+ y = e.offsetY
488
+
489
+ return {x: x, y: y}
490
+
491
+ handlemousemove: (e) ->
492
+ coordinate = @extractCoordinate(e)
493
+ x = coordinate.x
494
+ y = coordinate.y
495
+
496
+ @updateInteractiveCanvas(coordinate)
497
+ return false
498
+
499
+ handlemouseup: (e) ->
500
+ coordinate = @extractCoordinate(e)
501
+ if(coordinate == null)
502
+ x = last_x
503
+ y = last_y
504
+ else
505
+ x = coordinate.x
506
+ y = coordinate.y
507
+
508
+ console.log "UP: " + x + ", " + y
509
+
510
+ return false
511
+
512
+ handlemousedown: (e) ->
513
+ coordinate = @extractCoordinate(e)
514
+ x = coordinate.x
515
+ y = coordinate.y
516
+
517
+ console.log "DOWN: " + x + ", " + y
518
+
519
+ return false
520
+
521
+ handlemouseout: (e) ->
522
+ @cleanUpInteractive()
523
+ @info_box.hide()
524
+
525
+ #########################################