From 5e54ba7aa27b95d1e0568693915e735ab8058ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= Date: Thu, 28 Jun 2012 19:49:45 +0000 Subject: Initial version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Asbjørn Sloth Tønnesen --- web/js/labitrack.d/10-template-standard.js | 252 +++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 web/js/labitrack.d/10-template-standard.js (limited to 'web/js/labitrack.d/10-template-standard.js') diff --git a/web/js/labitrack.d/10-template-standard.js b/web/js/labitrack.d/10-template-standard.js new file mode 100644 index 0000000..09a8fc1 --- /dev/null +++ b/web/js/labitrack.d/10-template-standard.js @@ -0,0 +1,252 @@ +λ.templates.register('standard', function(){ + + var qrcode = null; + var qrcode_url = null; + var left=1; + var right=2; + + function drawCircle(ctx) { + ctx.beginPath(); + ctx.arc(40, 0, 40, 0, Math.PI*2); + ctx.lineWidth = 8; + ctx.stroke(); + ctx.closePath(); + } + + function roundedRect(ctx, x, y, w, h, r) { + ctx.save(); + ctx.translate(x, y); + ctx.moveTo(0, r); + ctx.arc(r, r, r, Math.PI, Math.PI*1.5); + ctx.lineTo(w-r, 0); + ctx.arc(w-r, r, r, Math.PI*1.5, Math.PI*2); + ctx.lineTo(w, h-r); + ctx.arc(w-r, h-r, r, 0, Math.PI*0.5); + ctx.lineTo(r, h); + ctx.arc(r, h-r, r, Math.PI*0.5, Math.PI); + ctx.lineTo(0, r); + ctx.restore(); + } + + function measureCircle(ctx) { + return {width: 82, align: left}; + } + + function drawPerson(ctx, x, y) { + var m = this.measure(ctx); + ctx.save(); + ctx.translate(x, y); + + drawCircle(ctx); + + ctx.beginPath(); + ctx.arc(40, -25, 7.5, 0, Math.PI*2); + ctx.rect(32, -15, 16, 3); + ctx.moveTo(32, -12); + ctx.arc(32, -12, 2.5, Math.PI*1, Math.PI*1.5); + ctx.moveTo(48, -12); + ctx.arc(48, -12, 2.5, Math.PI*1.5, Math.PI*2); + ctx.rect(28, -12, 24, 20); + ctx.rect(32, 8, 16, 20); + ctx.fill(); + ctx.closePath(); + + ctx.restore(); + + return m.width; + } + + var stamplinewidth = 8; + + function drawManual(ctx, x, y) { + var m = this.measure(ctx); + ctx.save(); + ctx.translate(x, y); + + drawCircle(ctx); + + ctx.beginPath(); + ctx.moveTo(6, -25); + roundedRect(ctx, 20, -25, 30, 40, 5); + roundedRect(ctx, 30, -15, 30, 40, 5); + ctx.lineWidth = 4; + ctx.stroke(); + ctx.closePath(); + + ctx.restore(); + + return m.width; + } + + + function measureStamp(ctx, label) + { + ctx.font = '60px sans-serif'; + var tm = ctx.measureText(label); + return {width: tm.width + 40 + 8, align: right}; + } + + function drawStamp(ctx, label, x, y) + { + ctx.save(); + ctx.translate(x, y); + + ctx.font = '60px sans-serif'; + var tm = ctx.measureText(label); + + ctx.beginPath(); + var m = this.measure(ctx, label); + ctx.rect(stamplinewidth/2, -40, m.width - stamplinewidth, 80+(stamplinewidth/2), Math.PI*2); + ctx.lineWidth = stamplinewidth; + ctx.stroke(); + ctx.closePath(); + + ctx.fillText(label, (m.width - stamplinewidth - tm.width)/2, 20); + + ctx.restore(); + return m.width; + } + + var symbols = function(){ + var symbols = {}; + + function register(key, draw, measure) { + symbols[key] = {draw: draw, measure: measure}; + } + function get() { + return symbols; + } + return {get: get, register: register}; + }(); + + function registerStamp(key, label) { + symbols.register(key, + function(ctx, x, y){ + return drawStamp.apply(this, [ctx, label, x, y]); + }, + function(ctx){ + return measureStamp(ctx, label); + }); + } + + registerStamp('dnh', 'DNH'); + registerStamp('dohack', 'DO HACK'); + registerStamp('dne', 'DNE'); + symbols.register('person', drawPerson, measureCircle); + symbols.register('manual', drawManual, measureCircle); + + function drawQRcode(ctx, size, url) { + if (qrcode_url !== url) { + qrcode = null; + qrcode_url = url; + } + if (qrcode === null) { + // calculate the qrcode + qrcode = new QRCode(-1, QRErrorCorrectLevel.L); + qrcode.addData(url); + qrcode.make(); + } + + ctx.save(); + var scale = size / qrcode.getModuleCount(); + ctx.scale(scale, scale); + + // draw on the canvas + ctx.beginPath(); + for (var row = 0; row < qrcode.getModuleCount(); row++){ + for (var col=0; col < qrcode.getModuleCount(); col++){ + if (qrcode.isDark(row, col)) { + ctx.rect(col, row, 1, 1); + } + } + } + ctx.closePath(); + ctx.fill(); + ctx.restore(); + } + + function draw(data, preview) + { + var dw; + if (preview) { + dw = 560; + } else { + dw = 1083; + } + var w = 1083; + var ds = dw/w; + var h = 336; + var dh = Math.round(h * ds); + var ctx = labitrack.utils.new_context(dw, dh); + ctx.scale(ds, ds) + + ctx.shadowBlur = 0; + ctx.globalAlpha = 1; + + drawQRcode(ctx, h, 'http://o.labitat.dk/'+data.id); + + ctx.translate(h, 0); + var y = 50; + ctx.font = 'bold 40px sans-serif'; + ctx.fillText(data.name, 20, y); + y += 45; + ctx.font = '40px sans-serif'; + ctx.fillText('ID: '+data.id, 20, y); + y += 40; + var lines = data.desc.split('\n'); + for (var i=0; i