Update: this method is outdated again. The new solution is presented here Using Gulp with Browserify and Watchify - Update Nov 2015

Old method

In my previous 2014-08-06-using-watchify-with-gulp-for-fast-browserify-build.md, I have demonstrated how to use Browserify and Watchify in Gulp to automate the build process. The steps are to create a browserify bundle and return that bundle with a bunch of pipe inside a gulp task like this

var b = browserify({
  cache: {},
  packageCache: {},
  fullPaths: true
});
b = watchify(b);
b.add('./main.js');
return b.bundle()
    .pipe(uglify())
    .pipe(gulp.dest(dest));

We have to manually add the main.js file into Browserify so it will become ugly and complex when you have multiple bundles to build, not just one main.js file. It would be much better if we can do something like this, passing the source files as a glob as we usually do with Gulp

return gulp.src(source)
    .pipe(buildBrowserify)
    .pipe(uglify())
    .pipe(gulp.dest(dest));

In this post, I will illustrate how to create that buildBrowserify function.

Read more

Vagrant and Ansible

Recently, I have started a personal project built with Clojure, a website for managing family records and visualizing pedigree tree. The problem is that I need an automation tool for setting up the development environment and deploying my website to the server. And yes, Vagrant is one of the best solution out there. I have used Vagrant with Chef before and found that Chef is a bit complex and requires installation of Ruby and Chef on the server before you can deploying anything.

After looking at all of Vagrant’s Provisioning solution, I decided to give Ansible a try because it is simple and operate over SSH, which means I need to install nearly nothing on the server to use it (of course you still can Ansible on server and do a local Provision there, but it’s only one more command to type).

This blog post is the summary of my experience with Vagrant and Ansible, how I set up my development environment and how can I re-use the code for other types of project. The sample project can be found at https://github.com/tmtxt/clojure-web-skeleton. Before you come to the next part, take a look at the basic usage of Vagrant and Ansible.

Basic Structure

A project with Vagrant and Ansible will look like this

├── Vagrantfile
├── ansible
│   ├── group_vars
│   │   └── all
│   ├── main.yml
│   ├── roles
│   │   ├── apt
│   │   ├── emacs
│   │   ├── git
│   │   ├── nvm
│   │   ├── oraclejdk
│   │   ├── postgres
│   │   ├── virtualenv
│   │   └── zsh
│   └── templates
│       ├── db_config.clj
│       └── system_config.clj
├── project.clj
├── src
└── static
Read more

Cover

PostgreSQL official documentation is one good resource for researching PostgreSQL features. However, the documentation on its home page are exported to PDF format only, which make it hard to read on other devices since it has no text-reflowable feature. Luckily, Peter Eisentraut made a small commit that add epub target to the documentation build to export the epub file.

Here are the building instruction and the download links to the epub file that I have built before

Read more

Image Blend modes and HTML Canvas

Image Blend modes are the methods used to determine how 2 image layers are blended (mixed) to each other. As the digital images are stored in pixel, which are represented by numerical values, there are a large number of possible ways for blending based on the mathematical functions. With the help of Canvas API, now we can easily retrieve the images, export all the pixels on the image, apply blending effect, calculate to get the new blended pixels, export and display the new image on the web or save to server.

In this post, I will demonstrate some basic steps for applying Image Blend modes using HTML canvas API.

The Logic

Wikipedia already listed some popular blending methods here Blend Modes.

Assume that we have two image layers, top layer and bottom layer. For each loop, a is the value of a color channel in the underlying layer and b is that of the corresponding channel of the upper layer, we got the function for calculating the new pixel ƒ(a, b).

The steps for generating the new blended image from the two images are

  • First, retrieve those two images
  • Create two separate canvases with the same size and draw those two images into the corresponding canvas.
  • Get all the pixel data from the two canvases.
  • Loop through each pixel, apply the blending function to create a new pixel and store it inside an array
  • Create a new canvas with the same size, use the blended pixel data to draw the blended image on that new canvas
  • Export the canvas to image, file to save to server or to local computer.

Note: I mentioned that we need to create two canvases with the same size. However, that is just to make it easy for the demonstration purpose. You can still generate two canvases with different size, but you will need to change the calculation function a bit.

Read more

If you have not known what Conkeror is, you probably miss one of the greatest web browser in your life. Read this post for an introduction of how impressive Conkeror is Conkeror and How it changed the way I surf the web.

Overview

One of the most interesting feature that Conkeror brings from Emacs is the Browser Object. If you are coming from an Emacs background, you are not unfamiliar with universal-arguments, which can modify the function’s behaviour once it is called before activating the function. Browser Objects in Conkeror brings that idea from Emacs to the web environment but with better approach. Browser Objects in Conkeror can represent practically any type of data (e.g image, hyperlinks, frame,…).

Quote from Conkeror Wiki page

In the web environment, there are many data types which one wants to manipulate. Text, hyperlinks, images, form fields, to name just a few. Without some kind of abstraction layer to unify the interface to all these types of objects, it would be necessary to have a combinatorially large number of commands. For instance, you would need separate commands for follow-link, follow-image, and follow-frame. With browser objects, you have just one command, follow, which can operate on many types of data.

Read more

On my Mac, I always have 2 different partitions, one for installing the system and the other one for my home directory. Today, I’ve just re-installed the whole system and of course I have to specify the home path after the installation. However, after changing the home directory, many of my applications cannot work properly. I have checked and recognize that the $HOME variable in the shell now has a trailing slash (/Volumes/tmtxt/ instead of /Volumes/tmtxt).

That was so annoying and took me hours to investigate. Finally I found the problem. It comes from one of my carelessness when I change the home directory location.

Yes, I typed the path manually… How stupid I was. Why not just use the Choose button right next there :LOL:

Alt Text

Just change it to another folder, restart the computer and change back to the right home folder. Everything will be fixed automatically.

Read more

Most of us split Emacs horizontally by 2 windows so that we can easily edit and compare multi documents at the same time. However, the problem is that the two windows usually have the same size and sometimes difficult for you to work with files with long lines. Golden Ratio Mode is a small but useful package for Emacs that will automatically resize the focused window by golden ratio.

The window that has the main focus will have the perfect size for editing, while the ones that are not being actively edited will be re-sized to a smaller size that doesn’t get in the way, but at the same time will be readable enough to know it’s content.

You can install it using package.el. After finishing installation, just add this to your init.el file

(require 'golden-ratio)
(golden-ratio-mode 1)
Read more

Introduction

Emacs’s SQLi mode can be a better replacement for the default terminal database client (psql, sqlite3,…). I prefer SQLi mode because it supports syntax highlighting (and also Postgres keyword) and allows me to flexibly choose which region in the buffer to send to the shell to execute.

SQLi is integrated by default in Emacs. The following interpreters are supported

  • psql by PostgreSQL
  • mysql by MySQL
  • sqlite or sqlite3 for SQLite
  • solsql by Solid
  • SQL*Plus by Oracle
  • dbaccess by Informix
  • isql by SyBase
  • sql by Ingres
  • osql by MS SQL Server
  • isql by Interbase
  • db2 by DB2 (IBM)
  • inl by RELEX

Make sure that the client you need to use can be located inside the Emacs’s PATH. You can install exec-path-from-shell package using package.el for Emacs to auto import the PATH from your default shell.

If you are using Mac OS, I have written a post about PostgreSQL installation and configuration steps on Mac here: Install and Config PostgreSQL on Mac.

Read more

The Web Audio API provides a powerful and versatile system for controlling audio on the Web, allowing developers to choose audio sources, add effects to audio, create audio visualizations, apply spatial effects (such as panning) and much more.

Web Audio API - MDN

Record Audio and Export to WAV files with Recorder.js

Recorder.js is a library for recording and exporting the output of Web Audio API. It can be installed using Bower using the package name recorderjs (not recorder.js, it’s another library). After installation, load the bower_components/Recorderjs/recorder.js file to your html file to use.

Note: inside the Recorderjs, there is another file named recorderWorker.js. You will need to specify the path to that file later in Javascript code. You should run this under an http web server, not through file:///.

In the HTML page, I have 2 buttons for start and stop Recording

<button onclick="record()">Record</button>
<button onclick="stop()">Stop</button>

Next, in the Javascript code, you need to add this to the beginning of your script file to fix the browser compatibility.

Read more

Update: I made a new better solution for this. You can read it here Using Gulp with Browserify and Watchify - Updated

Using Watchify instead of gulp.watch

If you are using Browserify with Gulp, perhaps gulp.watch is not the best solution when you want to watch for file changes and rebuild them as changes happen. gulp.watch cannot recognize the dependencies of each Browserify bundle so every time you save your file, Gulp have to re-compile all your Browserify bundles. watchify is a solution for this. Watchify will only re-compile the bundle if its dependencies change.

Using Watchify is very similar to normal Browserify. Watchify object is very similar to a Browserify bundle so you can take the code from Browserify to Watchify with very little change in code.

To use Watchify, take the Browserify code (you can read more in this post Browserify - Bring Nodejs modules to browsers), wrap it inside a Watchify object, add an on update event handler for it and you are done.

var browserify = require('browserify');
var source = require("vinyl-source-stream");
var watchify = require('watchify');

gulp.task('browserify', function(){
  browserifyShare();
});

function browserifyShare(){
  // you need to pass these three config option to browserify
  var b = browserify({
    cache: {},
    packageCache: {},
    fullPaths: true
  });
  b = watchify(b);
  b.on('update', function(){
    bundleShare(b);
  });
  
  b.add('./main.js');
  bundleShare(b);
}

function bundleShare(b) {
  b.bundle()
    .pipe(source('main.js'))
    .pipe(gulp.dest('./dist'));
}
Read more