2 el: "canvas#header-canvas"
6 @context = if cvs and cvs.getContext then cvs.getContext('2d') else null
11 constructor: (attr={}) ->
18 Canvas.ctx.clearRect(0, 0, @width, @height)
30 x.draw() for x in @objects
37 for x in @objects when x.isAnimating? and x.isAnimating()
40 if animating then @animate() else @afterAnimation()
44 x.afterAnimation(@) for x in @objects when x.afterAnimation?
47 constructor: (text, attr={}) ->
56 Canvas.ctx.font = @font
57 Canvas.ctx.measureText(@text).width
59 draw: (offsetX=0, offsetY=0) ->
60 pos = screenPosition @, offsetX, offsetY
61 Canvas.ctx.font = @font
62 Canvas.ctx.fillStyle = @color
63 Canvas.ctx.fillText @text, pos.x, pos.y
66 constructor: (attr={}) ->
67 @x0 = attr.x0 or attr.x or 0
68 @y0 = attr.y0 or attr.y or 0
69 @x1 = attr.x1 or attr.x or 0
70 @y1 = attr.y1 or attr.y or 0
71 @color = attr.color or '#fff'
73 draw: (offsetX=0, offsetY=0) ->
74 pos = screenPosition @, offsetX, offsetY
77 c.strokeStyle = @color
78 c.moveTo pos.x0, pos.y0
79 c.lineTo pos.x1, pos.y1
83 class AbstractRectangle
84 constructor: (attr={}) ->
91 draw: (offsetX=0, offsetY=0) ->
92 pos = screenPosition @, offsetX, offsetY
93 Canvas.ctx.beginPath()
94 Canvas.ctx.rect(pos.x0, pos.y0, pos.x1 - pos.x0, pos.y1 - pos.y0)
96 class GradientRectangle extends AbstractRectangle
97 constructor: (attr={}) ->
99 @colors ?= ['#fff', '#fff']
100 @direction ?= 'horizontal'
102 draw: (offsetX=0, offsetY=0) ->
104 p = screenPosition(@, offsetX, offsetY)
106 if @direction is 'vertical'
107 g = c.createLinearGradient(p.x0, p.y0, p.x0, p.y1)
108 else if @direction is 'horizontal'
109 g = c.createLinearGradient(p.x0, p.y0, p.x1, p.y0)
113 d += 1 / (@colors.length - 1)
115 super(offsetX, offsetY)
119 constructor: (attr={}) ->
125 @performance = attr.performance or 0
130 draw: (offsetX=0, offsetY=0) ->
131 width = (@width * 1.0) / @max * @performance * @frame / @frames
137 c.shadowColor = "rgba(0, 0, 0, 0.4)"
138 pos = screenPosition @, offsetX, offsetY
139 g = c.createLinearGradient(pos.x, pos.y, pos.x, pos.y + @height)
140 g.addColorStop 0.0, "#fc8a27"
141 g.addColorStop 0.1, "#fcb229"
142 g.addColorStop 0.4, "#fc8a29"
143 g.addColorStop 1.0, "#e97122"
145 c.rect(pos.x, pos.y, width, @height)
152 afterAnimation: (screen) ->
153 rate = 100.0 * @performance / @max
154 val = rate.toFixed(2)
155 val = parseInt(val) if parseInt(val) * 1.0 is Number(val)
156 label = new Label "#{val}%",
158 width = 1.0 * @width / @max * @performance
159 x = (width - label.getWidth()) / 2
160 x = width + 10 if x < 5
162 label.y = @height / 2 + 4 + @y
165 merge = (dist, src) ->
166 dist[k] = v for k, v of src when src.hasOwnProperty(k)
168 screenPosition = (obj, offsetX, offsetY) ->
173 for m in screenPosition.fields when obj.hasOwnProperty(m)
174 pos[m] = obj[m] + offset[m[0]]
176 screenPosition.fields = ['x', 'y', 'x0', 'y0', 'x1', 'y1']
185 update: (callback) ->
186 exec = (type, func) ->
189 url: '/cgi-bin/num-of-kanjis.cgi'
192 success: (data, dataType) ->
193 KanjiInfo[type] = data
195 error: (request, textStatus, errorThrown) ->
199 exec 'mincho', callback
201 makeGraphPage = (screen, pageName) ->
204 titleLabel = new Label window.dictionary.title,
208 fontLabel = new Label window.dictionary.fonts[pageName],
209 x: 30 + titleLabel.getWidth()
212 bottomLine = new Line y: 53, x0: 0, x1: 680
214 screen.add titleLabel
216 screen.add bottomLine
220 graphArea = new GradientRectangle
226 'rgba(0, 0, 0, 0.25)'
233 for _, x in new Array(5)
238 color: "rgb(178, 255, 178)"
250 for field, n in fields
251 screen.add new Label window.dictionary[field],
256 screen.add new GraphBar
260 max: KanjiInfo.max[field]
261 performance: KanjiInfo[pageName][field]
268 color: "rgb(255, 255, 255)"
273 Canvas.generateContext()
274 return unless Canvas.ctx
278 scale[m] = parseInt(Canvas.$el.css(m)) for m in ['width', 'height']
282 screen = new Screen screenScale
285 drawPage = (pageIndex) ->
286 pageName = drawPage.mapping[pageIndex]
287 makeGraphPage(screen, pageName).draw().animate() if pageName
293 $container = $('#docs-header .buttons-container .inner-container')
295 for name, n in drawPage.mapping
296 b = document.createElement('div')
297 $(b).addClass('button').text(n + 1).click ->
298 return if screen.locking or $(@).hasClass('pressed')
300 index = parseInt($(@).text() - 1)
301 name = drawPage.mapping[parseInt($(@).text() - 1)]
302 makeGraphPage(screen, name).draw().animate() if name
303 buttons.forEach (x) -> $(x).removeClass('pressed')
304 $(@).addClass('pressed')
309 $(buttons[0]).addClass('pressed')