-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandel.coffee
137 lines (107 loc) · 4.03 KB
/
mandel.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
# MandelPlot, Copyright (C) 2012 Chris Reuter
#
# MandelPlot is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
#
# MandelPlot is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MandelPlot; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# This is the main entry-point for MandelPlot.
# Global plot state
Plotter = null # This object handles the actual computation and plotting
Zoomer = null # This object lets the user select a new region to plot
Ctrl = null # This object constructs a bunch of controls
# The entry point. This gets called when the document is ready.
$(document).ready ->
initGlobals()
setupControls()
fetchUrlParams()
startRendering()
# Create the global objects which hold everything
initGlobals = ->
Plotter = new PlotState('#mandelplot_canvas')
Zoomer = new ZoomSelection('#mandelplot_selection')
Ctrl = new Quickform('mandelplot', 'mandelplot_ui')
# Create the controls that appear below the canvas
setupControls = ->
makeUI()
writeToUI()
updateCaption()
# Actually create the form UI
makeUI = ->
Ctrl.numEntry('iter', "Iterations per pixel")
Ctrl.numEntry('slice', "Pixels per Event:")
Ctrl.gridSpacer()
Ctrl.checkbox('cool', " Reverse colors")
Ctrl.checkbox('nohist', "Disable histogram coloring")
Ctrl.checkbox('nosmooth', "Disable smoothing")
Ctrl.gridSpacer()
Ctrl.button('rbtn', "Render the selected region", "Render", startRendering)
Ctrl.button('zbtn', "Zoom out by 50% and re-render", "Zoom Out",
zoomOutAndRender)
Ctrl.button('rsbtn', "Reset zoom and re-render", "Reset", resetPosAndRender)
# If the URL contains coordinates after the hash ('#'), parse them and
# set them as the starting point.
fetchUrlParams = ->
# Return unless there are parameters attached to the URL
hash = $(location).attr('hash')
return unless hash != ""
# Update Plotter from the URL params
Plotter.setFromLink(hash)
# And update the UI
writeToUI()
# Update the caption underneath the canvas to show the current
# coordinates and pixelsize
updateCaption = ->
$('#mandelplot_region').text(Plotter.desc())
baseUrl = $(location).attr('href').replace(/\#.*/, "")
#console.debug(baseUrl)
$('#mandelplot_link').attr('href', "#{baseUrl}##{Plotter.link()}")
# Overwrite the user's input with the actual values in Plotter
writeToUI = ->
Ctrl.val('iter', Plotter.iter)
Ctrl.val('slice', Plotter.slice)
Ctrl.val('cool', Plotter.reverseColor)
Ctrl.val('nohist', !Plotter.histColor)
Ctrl.val('nosmooth', !Plotter.smoothing)
# Set Plotter's parameters from the user's input IF the input is
# valid.
readFromUI = ->
iter = parseInt(Ctrl.val('iter'))
Plotter.iter = iter if iter >= 1
slice = parseInt(Ctrl.val('slice'))
Plotter.slice = slice if slice >= 1
Plotter.reverseColor = Ctrl.val('cool')
Plotter.histColor = !Ctrl.val('nohist')
Plotter.smoothing = !Ctrl.val('nosmooth')
# Start rendering the Mandelbrot set. If the user has selected a
# rectangle, first set Plotter to use it; otherwise, just use the
# current settings.
startRendering = ->
if Zoomer.hasSelection()
[x, y] = Zoomer.topleft
Plotter.setRenderRect(x, y, Zoomer.width)
Zoomer.clearSelection()
readFromUI()
writeToUI() # reset any invalid inputs
updateCaption()
#startTime = new Date()
Plotter.startRender() #-> console.debug("Elapsed: #{new Date() - startTime}"))
# Zoom out and start rendering.
zoomOutAndRender = ->
Zoomer.clearSelection()
Plotter.zoom(1.5)
startRendering()
# Reset the Plot position to the default and re-render
resetPosAndRender = () ->
Zoomer.clearSelection()
Plotter.reset()
startRendering()