Created
December 4, 2012 12:18
-
-
Save dodecaphonic/4203226 to your computer and use it in GitHub Desktop.
Drawing on canvas and getting an URI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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