From c4bdbe2e0cfa191d3da527770853d277d000c6ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Werner=20Vestera=CC=8As?= Date: Fri, 26 Dec 2014 01:27:42 +0100 Subject: [PATCH] -Moved parameters from font to text -Added comments to examples --- examples/berries.js | 6 +++-- examples/circles.js | 9 ++++--- examples/clock.js | 57 +++++++++++++++++++++++------------------- examples/lines.js | 5 ++-- examples/rectangles.js | 8 +++--- examples/text.js | 18 +++++++------ package.json | 2 +- src/framebuffer.cc | 20 +++++++++------ src/framebuffer.h | 4 +-- 9 files changed, 73 insertions(+), 56 deletions(-) diff --git a/examples/berries.js b/examples/berries.js index ee65525..6a21559 100644 --- a/examples/berries.js +++ b/examples/berries.js @@ -1,12 +1,14 @@ var pitft = require("../pitft"); -var fb = pitft("/dev/fb1"); +var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode + +// Clear the screen buffer fb.clear(); var xMax = fb.size().width; var yMax = fb.size().height; -fb.image(x, y, "raspberry-pi.png"); +fb.image(x, y, "raspberry-pi.png"); // Draw the image from the file "raspberry-pi.png" at position x, y for (var n=0; n<1000; n++) { var x = Math.random() * (xMax + 32) - 16; diff --git a/examples/circles.js b/examples/circles.js index 8afc9ad..b525437 100644 --- a/examples/circles.js +++ b/examples/circles.js @@ -1,6 +1,8 @@ var pitft = require("../pitft"); -var fb = pitft("/dev/fb1"); +var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode + +// Clear the screen buffer fb.clear(); var xMax = fb.size().width; @@ -13,12 +15,13 @@ for (var n=0; n<250; n++) { y = parseInt(Math.random() * yMax, 10); radius = parseInt(Math.random() * yMax / 2, 10); + // Create a random color r = Math.random(); g = Math.random(); b = Math.random(); fb.color(r, g, b); - fb.circle(x, y, radius, false, 1); + fb.circle(x, y, radius, false, 1); // Draw an outlined circle with a 1 pixel wide border } fb.clear(); @@ -35,5 +38,5 @@ for (var n=0; n<250; n++) { b = Math.random(); fb.color(r, g, b); - fb.circle(x, y, radius, true); + fb.circle(x, y, radius, true); // Draw a filled circle } \ No newline at end of file diff --git a/examples/clock.js b/examples/clock.js index 22bc99d..64ce58f 100644 --- a/examples/clock.js +++ b/examples/clock.js @@ -1,49 +1,51 @@ var pitft = require("../pitft"); -var fb = pitft("/dev/fb1", true); +var fb = pitft("/dev/fb1", true); // Returns a framebuffer in double buffering mode + +// Clear the back buffer fb.clear(); var xMax = fb.size().width; var yMax = fb.size().height; -var rad = 180/Math.PI; + +var radius = yMax/2 - 10; + +var RA = 180/Math.PI; var hand = function(_fb, x, y, angle, length, width) { - var x0 = xMax/2 + Math.sin(angle/rad); - var y0 = yMax/2 - Math.cos(angle/rad); + var x0 = xMax/2 + Math.sin(angle/RA); + var y0 = yMax/2 - Math.cos(angle/RA); - var x1 = xMax/2 + Math.sin(angle/rad) * length; - var y1 = yMax/2 - Math.cos(angle/rad) * length; + var x1 = xMax/2 + Math.sin(angle/RA) * length; + var y1 = yMax/2 - Math.cos(angle/RA) * length; fb.line(x0, y0, x1, y1, width); } var drawDial = function() { fb.color(1, 1, 1); - fb.circle(xMax/2, yMax/2, 100); + fb.circle(xMax/2, yMax/2, radius); fb.color(0, 0, 0); for (var a = 0; a < 360; a += 6) { var x0, y0; - var x0 = xMax/2 + Math.sin(a/rad) * 95; - var y0 = yMax/2 + Math.cos(a/rad) * 95; + var x0 = xMax/2 + Math.sin(a/RA) * (radius * 0.95); + var y0 = yMax/2 + Math.cos(a/RA) * (radius * 0.95); if (a % 30 == 0) { - x1 = xMax/2 + Math.sin(a/rad) * 85; - y1 = yMax/2 + Math.cos(a/rad) * 85; - fb.line(x0, y0, x1, y1, 5); + x1 = xMax/2 + Math.sin(a/RA) * (radius * 0.85); + y1 = yMax/2 + Math.cos(a/RA) * (radius * 0.85); + fb.line(x0, y0, x1, y1, radius * 0.05); } else { - x1 = xMax/2 + Math.sin(a/rad) * 90; - y1 = yMax/2 + Math.cos(a/rad) * 90; - fb.line(x0, y0, x1, y1, 1); + x1 = xMax/2 + Math.sin(a/RA) * (radius * 0.90); + y1 = yMax/2 + Math.cos(a/RA) * (radius * 0.90); + fb.line(x0, y0, x1, y1, radius * 0.01); } } } -var updateHands = function() { - fb.color(1, 1, 1); - fb.circle(xMax/2, yMax/2, 85); - +var update = function() { var now = new Date(), midnight = new Date( now.getFullYear(), @@ -52,19 +54,22 @@ var updateHands = function() { 0,0,0), hours = (now.getTime() - midnight.getTime())/1000/60/60, minutes = (hours * 60) % 60; - seconds = parseInt((minutes * 60) % 60); + seconds = parseInt((minutes * 60) % 60); + fb.color(1, 1, 1); + fb.circle(xMax/2, yMax/2, radius * 0.85); + fb.image(xMax/2 - 16, yMax/2 + radius * 0.50 - 16, "raspberry-pi-icon.png"); fb.color(1, 0, 0); - hand(fb, 0, 0, hours/12 * 360, 60, 5); - hand(fb, 0, 0, minutes/60 * 360, 80, 5); + hand(fb, 0, 0, hours/12 * 360, radius * 0.6, radius * 0.05); + hand(fb, 0, 0, minutes/60 * 360, radius * 0.8, radius * 0.05); fb.color(0, 0, 0); - hand(fb, 0, 0, seconds/60 * 360, 80, 1.5); + hand(fb, 0, 0, seconds/60 * 360, radius * 0.8, radius * 0.015); fb.color(1, 0, 0); - fb.circle(xMax/2, yMax/2, 7.5); - fb.blit(); + fb.circle(xMax/2, yMax/2, radius * 0.075); + fb.blit(); // Transfer the back buffer to the screen buffer }; drawDial(); setInterval(function() { - updateHands(); + update(); }, 100); \ No newline at end of file diff --git a/examples/lines.js b/examples/lines.js index 04903bd..060b731 100644 --- a/examples/lines.js +++ b/examples/lines.js @@ -1,7 +1,8 @@ var pitft = require("../pitft"); -var fb = pitft("/dev/fb1"); +var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode +// Clear the screen buffer fb.clear(); var xMax = fb.size().width; @@ -21,5 +22,5 @@ for (var n=0; n<500; n++) { b = Math.random(); fb.color(r, g, b); - fb.line(x0, y0, x1, y1, 1, r, g, b); + fb.line(x0, y0, x1, y1, 1, r, g, b); // Draw a line from (x0, y0) to (x1, y1) with a width of one pixel, and the color (r, g, b) } diff --git a/examples/rectangles.js b/examples/rectangles.js index 0790a53..2921405 100644 --- a/examples/rectangles.js +++ b/examples/rectangles.js @@ -1,6 +1,8 @@ var pitft = require("../pitft"); -var fb = pitft("/dev/fb1"); +var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode + +// Clear the screen buffer fb.clear(); var xMax = fb.size().width; @@ -24,7 +26,7 @@ for (var n=0; n<500; n++) { b = Math.random(); fb.color(r, g, b); - fb.rect(x, y, w, h, false, 1); + fb.rect(x, y, w, h, false, 1); // Draw an outlined rectangle with a 1 pixel wide border } fb.clear(); @@ -47,5 +49,5 @@ for (var n=0; n<500; n++) { b = Math.random(); fb.color(r, g, b); - fb.rect(x, y, w, h, true); + fb.rect(x, y, w, h, true); // Draw a filled rectangle } \ No newline at end of file diff --git a/examples/text.js b/examples/text.js index af56741..aea3517 100644 --- a/examples/text.js +++ b/examples/text.js @@ -1,24 +1,26 @@ var pitft = require("../pitft"); -var fb = pitft("/dev/fb1"); +var fb = pitft("/dev/fb1"); // Returns a framebuffer in direct mode. See the clock.js example for double buffering mode + +// Clear the screen buffer fb.clear(); var xMax = fb.size().width; var yMax = fb.size().height; -fb.color(1, 1, 1); +fb.color(1, 1, 1); // Set the color to white for (var a=0; a<=90; a+=15) { - fb.font("fantasy", 12, false, a); - fb.text(20, 20, "Rotated text"); + fb.font("fantasy", 12); // Use the "fantasy" font with size 12 + fb.text(20, 20, "Rotated text", false, a); // Draw the text non-centered, rotated _a_ degrees } for (var a=0; a<=180; a+=15) { - fb.font("fantasy", 24, true, a); - fb.text(xMax/2, yMax/2, "Rotated text"); + fb.font("fantasy", 24, true); // Use the "fantasy" font with size 24, and font weight bold + fb.text(xMax/2, yMax/2, "Rotated text", true, a); // Draw the text centered, rotated _a_ degrees } for (var a=180; a<=270; a+=15) { - fb.font("fantasy", 12, false, a); - fb.text(xMax-20, yMax-20, "Rotated text"); + fb.font("fantasy", 12); // Use the "fantasy" font with size 12 + fb.text(xMax-20, yMax-20, "Rotated text", false, a); // Draw the text non-centered, rotated _a_ degrees } diff --git a/package.json b/package.json index d045dc5..0080b71 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "pitft", "description": "A NodeJS module that gives you access to the PiTFT framebuffer", - "version": "0.0.8", + "version": "0.0.9", "main": "pitft.js", "author": "Werner Vesteraas ", "license": { diff --git a/src/framebuffer.cc b/src/framebuffer.cc index 0050db0..ce5993f 100644 --- a/src/framebuffer.cc +++ b/src/framebuffer.cc @@ -227,8 +227,7 @@ NAN_METHOD(FrameBuffer::Font) { obj->fontName = _fontName.c_str(); obj->fontSize = args[1]->IsUndefined() ? 12 : args[1]->NumberValue(); - obj->textCentered = args[2]->IsUndefined() ? false : args[2]->BooleanValue(); - obj->textRotation = args[3]->IsUndefined() ? 0 : args[3]->NumberValue(); + obj->fontBold = args[2]->IsUndefined() ? false : args[2]->BooleanValue(); NanReturnUndefined(); } @@ -242,25 +241,30 @@ NAN_METHOD(FrameBuffer::Text) { v8::String::Utf8Value text(args[2]->ToString()); std::string _text = std::string(*text); + bool textCentered = args[3]->IsUndefined() ? false : args[3]->BooleanValue(); + double textRotation = args[4]->IsUndefined() ? 0 : args[4]->NumberValue(); + FrameBuffer *obj = ObjectWrap::Unwrap(args.Holder()); cairo_t *cr = getDrawingContext(obj); cairo_set_source_rgb(cr, obj->r, obj->g, obj->b); - cairo_select_font_face(cr, obj->fontName, - CAIRO_FONT_SLANT_NORMAL, - CAIRO_FONT_WEIGHT_NORMAL); + if (obj->fontBold) { + cairo_select_font_face(cr, obj->fontName, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD); + } else { + cairo_select_font_face(cr, obj->fontName, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); + } cairo_set_font_size(cr, obj->fontSize); cairo_translate(cr, x, y); - if (obj->textRotation != 0) { - cairo_rotate(cr, obj->textRotation / (180.0 / 3.141592654)); + if (textRotation != 0) { + cairo_rotate(cr, textRotation / (180.0 / 3.141592654)); } - if (obj->textCentered) { + if (textCentered) { cairo_text_extents_t extents; cairo_text_extents(cr, _text.c_str(), &extents); diff --git a/src/framebuffer.h b/src/framebuffer.h index 91e46fb..e407ea6 100644 --- a/src/framebuffer.h +++ b/src/framebuffer.h @@ -57,9 +57,7 @@ class FrameBuffer : public node::ObjectWrap { const char *fontName; double fontSize; - - double textRotation; - bool textCentered; + bool fontBold; bool drawToBuffer; };