-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlocky.coffee
61 lines (53 loc) · 1.97 KB
/
Blocky.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
window.Blocky = class Blocky
constructor: (text = null ,container=null, config={}) ->
if container
@container=window.document.getElementById(container)
if not @container
throw new Error "Invalid Container for QR Canvas"
if not text
throw new Error "No content for QR code"
#QR code default settings. Can be overridden via config array.
@cell_size = config.cell_size or 4
@black = config.black or "rgb(0,0,0)"
@white = config.white or "rgb(255,255,255)"
#type and error level control how much information we can encode.
@type_number = config.typenumber or 10
@error_level = config.error_level or 'M'
#in case they put something stupid
@error_level.charAt(0).toUpperCase in ['M', 'H', 'Q' ,'L'] or 'M'
#some fun preset colors! :D
if config.scheme in ['watermelon' , 'wedding' , 'arctic' , 'spicy']
switch config.scheme
when 'watermelon'
@black = "rgb(247,12,71)"; @white="rgb(168,247,130)"
when 'wedding'
@black ="rgb(250,171,0)"; @white="rgb(198,247,176)"
when 'arctic'
@black = "rgb(252,0,244)"; @white="rgb(214,255,252)"
when 'spicy'
@black = "rgb(255,0,0)"; @white="rgb(250,245,170)"
canvas = window.document.createElement 'canvas'
if not canvas
alert 'browser does not support Canvas. Sorry about that.'
return
@context = canvas.getContext '2d'
@das_code = new qrcode @type_number ,'L'
@das_code.addData text
@das_code.make()
canvas_size = (@das_code.getModuleCount() * @cell_size)
canvas.setAttribute 'width' , canvas_size
canvas.setAttribute 'height' , canvas_size
@container.appendChild canvas
#big main loops to build the QR code
limit= @das_code.getModuleCount()
if canvas.getContext
for r in [0...limit]
for c in [0...limit]
do (r,c) =>
if @das_code.isDark r,c
@context.fillStyle = @black
else
@context.fillStyle = @white
@context.fillRect c*@cell_size , r*@cell_size , @cell_size, @cell_size
else
console.log "no getContext.., cannot proceed."