Skip to content

Instantly share code, notes, and snippets.

@dodecaphonic
Created December 4, 2012 12:18
Show Gist options
  • Save dodecaphonic/4203226 to your computer and use it in GitHub Desktop.
Save dodecaphonic/4203226 to your computer and use it in GitHub Desktop.
Drawing on canvas and getting an URI
class Smile.Utils.MarkImageDrawer
constructor: (@mark, @canvas) ->
@margin = 4
@backgroundColor = '#338e07'
@foregroundColor = '#fff'
@width = 40
@height = 60
# Public: Draws a Mark in the buffer.
#
# Returns a String with a base64-encoded data url.
draw: ->
@canvas.width = @width
@canvas.height = @height
ctx = @canvas.getContext('2d')
ctx.fillStyle = @backgroundColor
ctx.fillRect 0, 0, @width, @height
ctx.strokeStyle = ctx.fillStyle = @foregroundColor
ctx.lineWidth = 2
ctx.strokeRect 1, 1, @width - 2, @height - 2
ctx.strokeRect 5, 5, @width - 10, @height - 10
ctx.font = 'bold 12pt sans-serif'
numberOffset = switch @mark.km.toString().length
when 2 then 10
when 1 then 15
else 7
ctx.fillText @mark.sigla, 9, 25
ctx.fillText @mark.km, numberOffset, 45
@canvas.toDataURL()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment