|
|
@ -8,21 +8,22 @@ class AsciiIo.Renderer.Canvas extends AsciiIo.Renderer.Base
|
|
|
|
initialize: (options) ->
|
|
|
|
initialize: (options) ->
|
|
|
|
super(options)
|
|
|
|
super(options)
|
|
|
|
@ctx = @el.getContext('2d')
|
|
|
|
@ctx = @el.getContext('2d')
|
|
|
|
@cellWidth = 7
|
|
|
|
|
|
|
|
@cellHeight = 14
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
afterInsertedToDom: ->
|
|
|
|
afterInsertedToDom: ->
|
|
|
|
width = @cols * @cellWidth
|
|
|
|
sample = $('<span class="font-sample">M</span>')
|
|
|
|
height = @lines * @cellHeight
|
|
|
|
@$el.parent().append(sample)
|
|
|
|
@$el.attr('width', width)
|
|
|
|
@cellWidth = sample.width()
|
|
|
|
@$el.attr('height', height)
|
|
|
|
@cellHeight = sample.height()
|
|
|
|
|
|
|
|
sample.remove()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@$el.attr('width', @cols * @cellWidth)
|
|
|
|
|
|
|
|
@$el.attr('height', @lines * @cellHeight)
|
|
|
|
|
|
|
|
|
|
|
|
@setFont()
|
|
|
|
@setFont()
|
|
|
|
|
|
|
|
|
|
|
|
setFont: ->
|
|
|
|
setFont: ->
|
|
|
|
size = @$el.css('font-size')
|
|
|
|
size = @$el.css('font-size')
|
|
|
|
# console.log size
|
|
|
|
|
|
|
|
family = @$el.css('font-family')
|
|
|
|
family = @$el.css('font-family')
|
|
|
|
# console.log family
|
|
|
|
|
|
|
|
@font = "#{size} #{family}"
|
|
|
|
@font = "#{size} #{family}"
|
|
|
|
@ctx.font = @font
|
|
|
|
@ctx.font = @font
|
|
|
|
@prevFont = @font
|
|
|
|
@prevFont = @font
|
|
|
@ -32,18 +33,32 @@ class AsciiIo.Renderer.Canvas extends AsciiIo.Renderer.Base
|
|
|
|
left = 0
|
|
|
|
left = 0
|
|
|
|
width = @cols * @cellWidth
|
|
|
|
width = @cols * @cellWidth
|
|
|
|
top = n * @cellHeight
|
|
|
|
top = n * @cellHeight
|
|
|
|
|
|
|
|
cursorText = undefined
|
|
|
|
|
|
|
|
rendered = 0
|
|
|
|
|
|
|
|
|
|
|
|
for fragment in fragments
|
|
|
|
for fragment in fragments
|
|
|
|
[text, brush] = fragment
|
|
|
|
[text, brush] = fragment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if cursorX isnt undefined and rendered <= cursorX < rendered + text.length
|
|
|
|
|
|
|
|
cursorText = text[cursorX - rendered]
|
|
|
|
|
|
|
|
|
|
|
|
@setBackgroundAttributes(brush)
|
|
|
|
@setBackgroundAttributes(brush)
|
|
|
|
@ctx.fillRect left, top, text.length * @cellWidth, @cellHeight
|
|
|
|
@ctx.fillRect left, top, text.length * @cellWidth, @cellHeight
|
|
|
|
|
|
|
|
|
|
|
|
# if char != ' '
|
|
|
|
unless text.match(/^\s*$/)
|
|
|
|
@setTextAttributes(brush)
|
|
|
|
@setTextAttributes(brush)
|
|
|
|
@ctx.fillText text, left, top + @cellHeight
|
|
|
|
@ctx.fillText text, left, top + @cellHeight
|
|
|
|
|
|
|
|
|
|
|
|
left += text.length * @cellWidth
|
|
|
|
left += text.length * @cellWidth
|
|
|
|
|
|
|
|
rendered += text.length
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if cursorX
|
|
|
|
|
|
|
|
x = cursorX * @cellWidth
|
|
|
|
|
|
|
|
@ctx.fillStyle = AsciiIo.colors[7]
|
|
|
|
|
|
|
|
@ctx.fillRect x, top, @cellWidth, @cellHeight
|
|
|
|
|
|
|
|
if cursorText
|
|
|
|
|
|
|
|
@ctx.fillStyle = AsciiIo.colors[0]
|
|
|
|
|
|
|
|
@ctx.fillText cursorText, x, top + @cellHeight
|
|
|
|
|
|
|
|
|
|
|
|
setBackgroundAttributes: (brush) ->
|
|
|
|
setBackgroundAttributes: (brush) ->
|
|
|
|
@ctx.fillStyle = AsciiIo.colors[brush.bgColor()]
|
|
|
|
@ctx.fillStyle = AsciiIo.colors[brush.bgColor()]
|
|
|
|