Greatly improved cancas renderer

This commit is contained in:
Marcin Kulik 2012-03-17 20:33:01 +01:00
parent 850944cece
commit 38a68daa0f
3 changed files with 32 additions and 19 deletions

View File

@ -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()]

View File

@ -44,7 +44,6 @@ class AsciiIo.Renderer.Pre extends AsciiIo.Renderer.Base
renderLine: (n, fragments, cursorX) -> renderLine: (n, fragments, cursorX) ->
html = [] html = []
rendered = 0 rendered = 0
for fragment in fragments for fragment in fragments

View File

@ -33,19 +33,19 @@ $color5: #EDC951;
// margin: 0px 0px 30px 20px; // margin: 0px 0px 30px 20px;
position: relative; position: relative;
.terminal, .font-sample {
font-family: 'Bitstream Vera Sans Mono', 'Courier New', monospace;
font-size: 12px;
line-height: inherit;
}
.terminal { .terminal {
overflow: hidden; overflow: hidden;
padding: 2px; padding: 2px;
margin: 0px; margin: 0px;
display: block; display: block;
font-size: 12px;
// font-family: Monaco,"Courier New","DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;
font-family: monospace;
// font-family: 'Droid Sans Mono', Monospace;
white-space: pre; white-space: pre;
background-color: black; background-color: black;
// line-height: 1.2em;
line-height: inherit;
color: #ccc; color: #ccc;
cursor: pointer; cursor: pointer;
@ -74,7 +74,6 @@ $color5: #EDC951;
} }
.line { .line {
font-size: 12px;
/* background-color: black;*/ /* background-color: black;*/
/* padding: 0;*/ /* padding: 0;*/
/* margin: 0;*/ /* margin: 0;*/