Autor wpisu: Śpiechu, dodany: 30.04.2012 11:44, tagi: javascript
It’s been a long time since I’ve written my last english-spoken text. It was about Webworkers API. The visitors number has been achieved, so it’s time for the next topic. Now I’m going to talk about Canvas and some CoffeeScript tricks. Again I’m asking You to forgive me my „language glitches”.
Since I don’t like to get to know about something new in an abstract way, we’ll going to write a simple EAN-13 barcode generator. You meet it at every store, it saves time and money for merchants. Price can be attached to barcode instead of price tags. Imagine changing a price in the same 100 products. But how to generate one? Wikipedia has a nice description. EAN-13 is composed of GS1 organization numbers, company numbers, item reference and check digit.
This time I’m going to cover some CoffeScript features I didn’t talk about 2 posts earlier: extending classes and static properties. I’ll write one class computing „0101…” EAN-13 code and another one capable of drawing the code on HTML canvas element. I even created a public GitHub repository, so feel free to steal some code. Working example can be found here.
I won’t explain You all code here, only the most important parts. For example how to compute control digit?
computeControlSum: -> sum = 0 # this one line of array comprehension substitutes four lines in pure JS sum += (if key % 2 then 3 else 1) * value for value, key in @eanArray controlSum = 10 - sum % 10 if controlSum == 10 then controlSum = 0 return controlSum
Piece of code above is translated into:
EAN13Generator.prototype.computeControlSum = function() { var controlSum, key, sum, value, _i, _len, _ref; sum = 0; _ref = this.eanArray; for (key = _i = 0, _len = _ref.length; _i < _len; key = ++_i) { value = _ref[key]; sum += (key % 2 ? 3 : 1) * value; } controlSum = 10 - sum % 10; if (controlSum === 10) { controlSum = 0; } return controlSum; };
As we can see we don’t need to worry about some temporary variables in CoffeScript.
Digits 2–7 in barcode (before the central „whiskers”) can be written as odd or even. Pattern depends on first barcode digit. It has been written as object literal:
class EAN13Generator # some code here # this is how we create class properties # we can call to such property like EAN13Generator.LEFT_SIDE_CODING @LEFT_SIDE_CODING: 0: ['odd', 'odd', 'odd', 'odd', 'odd', 'odd' ] 1: ['odd', 'odd', 'even', 'odd', 'even', 'even'] 2: ['odd', 'odd', 'even', 'even', 'odd', 'even'] 3: ['odd', 'odd', 'even', 'even', 'even', 'odd' ] 4: ['odd', 'even', 'odd', 'odd', 'even', 'even'] 5: ['odd', 'even', 'even', 'odd', 'odd', 'even'] 6: ['odd', 'even', 'even', 'even', 'odd', 'odd' ] 7: ['odd', 'even', 'odd', 'even', 'odd', 'even'] 8: ['odd', 'even', 'odd', 'even', 'even', 'odd' ] 9: ['odd', 'even', 'even', 'odd', 'even', 'odd' ]
The next interesing part is how to concatenate barcode string. Here’s how:
generateEANcode: -> # we're using class property to find out first 6 digits coding codingStyle = EAN13Generator.LEFT_SIDE_CODING[@eanArray[0]] eanCode = EAN13Generator.START_SENTINEL for i in [1..6] if codingStyle[i-1] == 'odd' eanCode += EAN13Generator.EAN_13_CODE_TABLE[@eanArray[i]].left.odd else eanCode += EAN13Generator.EAN_13_CODE_TABLE[@eanArray[i]].left.even eanCode += EAN13Generator.CENTRAL_SENTINEL eanCode += (EAN13Generator.EAN_13_CODE_TABLE[@eanArray[i]].right) for i in [7..12] eanCode += EAN13Generator.END_SENTINEL
Class extension is realized through extends keyword.
class EAN13CanvasDrawer extends EAN13Generator # @canvasId is shorthand for @canvasId = canvasId constructor: (eanString, @canvasId) -> # we're launching EAN13Generator constructor super(eanString)
I’m using jCanvaScript JS library to draw on canvas. Below is the whole drawBarcode() method: