1. Pie Charts, now with Gradient Filling

    Lots of people love pie. Some people love pie charts. I personally love pie charts so much that I wanted mine to use a gradient fill. Now yours can too, and here’s how:

    Drawing pie charts with canvas is relatively straightforward. Draw a complete arc of one color, and then an arc of another color to indicate the completed percent. Canvas even supports gradient fills, so we’re all set, right? Unfortunately, no. The linear and radial fills that canvas uses aren’t quite what we need. What we really need is a ‘polar gradient’. It’s a bit more work, but we can actually build one of these too. Using createImageData, get an array of pixels. Now, visualize this array as four quadrents with an origin in the center. Loop through every pixel and translate its cartesian coordinates to polar coordinates. Then, set the alpha channel of the pixel proportional to its theta value. Finally, insert this array of pixels into an offscreen canvas element with putImageData.

    So what good does this gradient do us? Well, it means we can draw an arc of one color in the main canvas, and then use drawImage to overlay the gradient on top. By default, canvas uses the source-over composite mode. With this set, we’d get the proper arc with drawImage, but there would be some ‘extra’ gradient pixels from the source canvas. So, we set globalCompositeOperation to source-atop, which means that now drawImage will only copy pixels from the source canvas where there are pixels in the target canvas. Perfect. Now we just need the background. We take advantage of globalCompositeOperation a second time, and set it to destination-over. This way, it draws behind the gradient that’s already in the main canvas.

    Here’s what the final pie chart looks like…

    pieChart example

    …and here’s the code to generate it…

    pieChart({
        fillPercent: 95,
        backgroundStrokeColor: "#CCC",
        foregroundStrokeColor: ["green", "blue"],
        animationRate: 5000,
        radius: 120,
        stroke: 12,
        container: document.getElementById('ff')
    });
    

    To learn more, check out the code here on github.

    - William Farrell

  2. MojoTech Team Wins Best Design

    image
    This past weekend Aaron Snyder, Aaron Rosenberg, Andrew Shedd and Ivan Manolov took home the win for best designed app at DownCity.js.

    They created an app called Meet. The app attempts to bridge the “friend of a friend gap.” It’s a location-aware, socially driven dating app that pairs matched couples, suggesting a spot for the two to meet for their date.

    DownCity.js is a twenty four hour hack competition where teams come in with a fresh idea and their laptops. Twenty-four hours later they are judged on what they have created.

    Be sure to follow @MojoTech for more updates!

  3. Introducing Backbone.RouterFilters

    Rails controller like filters for your Backbone Router

    Backbone is amazing and so small that you have no excuse not to look at the source code as you’re using it. The hardest things I’ve encounter with Backbone are reconciling it with my experience using the Rails MVC structure and reminding myself that a 1,500 line library (that’s with comments!) is sure not to have all the features I’ve become used to in Rails.

    Read more →

  4. Dynamic Image Loading For Adaptive Designs

    For our new website we use some large images.

    I wanted to optimize how we sent those images down the pipe to the users.

    Why push them a 700k image when they did not need it?

    Little did I know I would be going down a rabbit hole.

    Read more →

  5. Introducing Vire

    Vire.js is a jQuery plugin that allows you to trigger events during certain points in a Vimeo video. Here’s a demo with events firing at 1 and 3 seconds.

    -Aaron Snyder

  6. Google Analytics within jQuery Mobile

    On a mobile web app we are working on we wanted to track every page view on the inside of the jQuery Mobile framework.

    We ran into a bit of an issue doing that due to how pages are handled within jQuery Mobile. Page loads are not true hard reloads. Instead, they are dynamically inserted into the DOM. That’s a problem for the traditional Google Analytics tracking code.

    Read more →

  7. Introducing stickyMojo

    stickyMojo is a contained sticky sidebar plugin for jQuery. It is lightweight, fast, flexible and compatible with Firefox, Chrome, Safari, and IE8+. It will degrade gracefully in older versions of IE.

    After looking around for a contained fixed position sticky sidebar for our blog we found that there were few options that handled all of the intricacies of cross-browser support, window resizing, and smoothness.

    One of the solutions that many people have come up with is to have the sidebar be positioned absolutely, then animate the top property. Although this solution worked, it looked jankey compared to fixed position element.

    The problem with a simply “fixed” positioned element is that it doesn’t react well to a scroll or window resize. In many cases the sidebar will overlap elements such as the footer or content area. Generally this is not the intended effect.

    Luckily we’ve got stickyMojo.

    -Aaron Snyder

  8. JavaScript: Page Specific Modules

    On many Rails projects, the application.js file can quickly go from a simple manifest (if you’re using the asset pipeline) to a gigantic unmaintainable mess of objects, functions, and on DOM ready initializations. There are many components of your site that need more interactive front-end functionality, but adding and maintaining them soon amounts to hitting cmd + fand praying.

    On my first project at MojoTech we needed some front-end functionality that was unique to certain page components. Most of the components were present on a single page, though some could be on multiple pages. Since the bulk of each component’s functionality was not reusable, it seemed that there wouldn’t be much benefit in making a jQuery plugin. In order to keep the code clean and not pollute the public namespace, we created an encapsulated module in its own js file that contained all the setup, event listeners, and function handlers for each component. This module was then initiated with the parent element the code was designed for on DOM ready. All these files would get included by the asset pipeline so there was no need to add the js include tags for each one.

    Read more →

  9. Incremental Async Recursive Javascript Functions

    Often I find myself needing to query multiple web API’s while iterating over a collection and then use that data to query another web service— fun stuff eh?

    So in other languages I would simply wait until I received the message and then move on to getting the next message— blocking any other operations from happening in my code.

    But with Javascript we have closures! And so the fun begins.

    But is this ever practical? Of course it is!

    Read more →

  10. Custom Selects without JS

    So for a project I was working on I needed to implement a custom button to trigger a select list.

    Instead of using a JS plugin a decided to roll a very simple solution that yielded a highly useful bit of code.

    The idea behind it is that we hide the select tag by setting the opacity to 0. Due to the way select options are rendered they still show when their parent select has been tripped

    Read more →