Wednesday, March 2, 2016

Creative SVG Letter Animations with Javascript

An experimental plugin for animating SVG letters using Segment. The idea is to animate the path strokes of an interesting display font in a creative way.


by via jQuery-Plugins.net RSS Feed

Creating a GraphQL Server with Node.js and MongoDB

Requesting data from the server on the client side is not a new concept. It allows an application to load data without having to refresh the page. This is most used in single page applications, which instead of getting a rendered page from the server, request only the data needed to render it on the client side.

The most common approach across the web in the last few years has been the REST architectural style. However, this approach brings some limitations for high data demand applications. In a RESTful system, we need to make multiple HTTP requests to grab all the data we want, which has a significant performance impact. What if there was a way to request multiple resources in a single HTTP request?

Introducing GraphQL, a query language which unifies the communication between client and server side. It allows the client side to describe exactly the data it needs, in a single request.

In this article, we'll create a Node.js/Express server with a GraphQL route which will handle all our queries and mutations. We'll then test this route by sending some POST requests and analyze the outcome using Postman.

You can find the full source code for this application here. I've also made a Postman collection that you can download here.

Setting up a GraphQL Endpoint on an Express Server

First thing to do is create our Node.js server using the Express framework. We'll also use MongoDB together with Mongoose for data persistency, and babel to use ES6. Since the code is transpiled to ES5 at runtime, there is no need for a build process. This is done in the index.js:

// index.js
require('babel/register');
require('./app');

In app.js we'll start our server, connect with a Mongo database and create a GraphQL route.

// app.js
import express from 'express';
import graphqlHTTP from 'express-graphql';
import mongoose from 'mongoose';

import schema from './graphql';

var app = express();

// GraphqQL server route
app.use('/graphql', graphqlHTTP(req => ({
  schema,
  pretty: true
})));

// Connect mongo database
mongoose.connect('mongodb://localhost/graphql');

// start server
var server = app.listen(8080, () => {
  console.log('Listening at port', server.address().port);
});

The most relative part of the code above, in this article context, is where we define our GraphQL route. We use express-graphql, an Express middleware developed by Facebook's GraphQL team. This will process the HTTP request through GraphQL and return the JSON response. For this to work we need to pass through in the options our GraphQL Schema which is discussed in the next section. We're also setting the option pretty to true. This makes the JSON responses pretty-printed, making them easier to read.

GraphQL schema

For GraphQL to understand our requests we need to define a schema. And a GraphQL schema is nothing else than a group of queries and mutations. You can think of queries as resources to retrieve from the database and mutations as any kind of update to your database. We'll create as an example a BlogPost and a Comment Mongoose model, and we will then create some queries and mutations for it.

Mongoose Models

Let's start by creating the mongoose models. Won't go into much detail here since mongoose isn't the focus of this article. You can find the two models in models/blog-post.js and models/comment.js.

GraphQL Types

Like with Mongoose, in GraphQL we need to define our data structure. The difference is that we define for each query and mutation what type of data can enter and what is sent in the response. If these types don't match, an error is thrown. Although it can seem redundant, since we've already defined a schema model in mongoose, it has great advantages, such as:

  • You control what is allowed in, which improves your system security
  • You control what is allowed out. This means you can define specific fields to never be allowed to be retrieved. For example: passwords or other sensitive data
  • It filters invalid requests so that no further processing is taken, which can improve the server's performance

You can find the source code for the GraphQL types in graphql/types/. Here's an example of one:

// graphql/types/blog-post.js
import {
  GraphQLObjectType,
  GraphQLNonNull,
  GraphQLString,
  GraphQLID
} from 'graphql';

export default new GraphQLObjectType({
  name: 'BlogPost',
  fields: {
    _id: {
      type: new GraphQLNonNull(GraphQLID)
    },
    title: {
      type: GraphQLString
    },
    description: {
      type: GraphQLString
    }
  }
});

Here, we're defining the blog post output GraphQL type, which we'll use further when creating the queries and mutations. Note how similar the structure is to the mongoose model BlogPost. It can seem duplication of work, but these are separated concerns. The mongoose model defines the data structure for the database, the GraphQL type defines a rule of what is accepted in a query or mutation to your server.

GraphQL Schema Creation

With the Mongoose models and the GraphQL types created we can now create our GraphQL schema.

// graphql/index.js
import {
  GraphQLObjectType,
  GraphQLSchema
} from 'graphql';

import mutations from './mutations';
import queries from './queries';

export default new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: queries
  }),
  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: mutations
  })
});

Here we export a GraphQLSchema where we define two properties: query and mutation. A GraphQLObjectType is one of the many GraphQL types. With this one in particular you can specify:

  • name - which should be unique and identifies the object;
  • fields - property which accepts an object than in this case will be our queries and mutations.

We're importing queries and mutations from another location, this is only for structural purposes. The source code is structured in a way that enables our project to scale well if we want to add more models, queries, mutations, etc.

The queries and mutations variables that we’re passing through to fields are plain JavaScript objects. The keys are the mutation or query names. The values are plain JavaScript objects with a configuration that tells GraphQL what to do with them. Let’s take the following GraphQL query as an example:

Continue reading %Creating a GraphQL Server with Node.js and MongoDB%


by Bruno Mota via SitePoint

HAUS

HAUS is a branding and marketing company based in Los Angeles with a focus on the intersection between design and technology.
by via Awwwards - Sites of the day

An Introduction to the CSS Grid Layout Module

As web applications become more and more complex, we need a more natural way to do advanced layouts easily without hacky solutions that use floats and other less burdensome techniques. An exciting new solution for creating layouts comes with the CSS Grid Layout Module.

In this introductory tutorial, I’ll introduce you to this relatively new CSS feature, I’ll discuss the current browser support, and I’ll show you using some examples how the CSS Grid Layout Module works.

[author_more]

What is the CSS Grid Layout Module?

The core idea behind the Grid Layout is to divide a web page into columns and rows, along with the ability to position and size the building block elements based on the rows and columns we have created in terms of size, position, and layer.

The grid also gives us a flexible way to change the position of elements with only CSS without any change to the HTML. This can be used with media queries to alter the layout at different breakpoints.

Browser Support

Before we can dive more into Grid Layout, it’s good to know the status of browser support, and how we can enable this feature in current browsers.

Browser support for CSS Grid Layout Module

Internet Explorer

The first proposal of Grid Layout was developed by Microsoft, and IE10 shipped with an -ms prefixed implementation. If you take a look at support on Can I Use, you’ll see that both IE11 and Edge also support Grid Layout.

Chrome and Opera

To enable Grid Layout in browsers that use the Blink rendering engine, like Chrome or Opera, navigate to chrome://flags (or opera://flags, but either works in Opera) using the browser’s address bar and look for the Enable experimental Web Platform features flag. After you enable it, you will be asked to relaunch the browser.

Enabling Grid Layout in Chrome

It’s recommended to use either Chrome or Opera with the examples in this article, or with your own experimentations, as the Blink engine has the most updated implementationsof Grid Layout.

Firefox

Navigate to about:config, search for the layout.css.grid.enabled flag, then double click or press enter to toggle it to true.

Enabling Grid Layout in Firefox

Grid Layout Polyfill

A polyfill is also available to provide a working implementation of the Grid Module for current browsers.

A Grid Layout Example

Let’s start with an example to see the power of Grid Layout, and then I’ll explain some new concepts in more detail.

Imagine you want to create a Twitter app with four full height columns layout (Tweets, Replies, Search, and Messages), something abstracted and similar to the screenshot below.

4 Column Layout

Here is our HTML:

[code language="html"]
<div class="app-layout">
<div class="tweets">Tweets</div>
<div class="replies">Replies</div>
<div class="search">Search</div>
<div class="messages">Messages</div>
</div>
[/code]

Then we will apply some CSS to the .app-layout container element:

[code language="css"]
.app-layout {
display: grid; /* 1 */
grid-template-columns: 1fr 1fr 1fr 1fr; /* 2 */
grid-template-rows: 100vh; /* 3 */
}
[/code]

View a demo here

Here is the explanation of what we’ve done in the previous CSS:

  1. Set the display property to grid.
  2. Divide the container element into four columns, each column is 1fr (one fraction) of the free space within the grid container.
  3. Create one row and set the height to be 100vh (full viewport height).

As you can see, the Grid Layout Module adds a new value to the display property which is grid. The grid value is responsible for setting the .app-layout element to be a grid container, which also establishes a new grid formatting context for its contents. This property is required to start using Grid Layout.

The grid-template-columns property specifies the width of each grid column within the Grid, and in our case it divides the .app-layout container to four columns; each one is 1fr (25%) of the available space.

The grid-template-rows specifies the height of each grid row, and in our example we only created one row at 100vh.

a layout with two columns and two rows would look like this:

4 columns, 2 rows

And we would use the following CSS:

[code language="css"]
.app-layout {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 50vh 50vh;
}
[/code]

View a demo here

We can also achieve the above example only on small screens by wrapping the code inside a media query. This opens up a great opportunity for us to customize the layout differently in different viewports. For example, we can create the previous layout only on viewports under 1024px as follows:

[code language="css"]
@media screen and (max-width: 1024px) {
.app-layout {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 50vh 50vh;
}
}
[/code]

Continue reading %An Introduction to the CSS Grid Layout Module%


by Ahmad Ajmi via SitePoint

This Week's HTML5 and Browser Technology News (Issue 229)


Read this e-mail on the Web
HTML 5 Weekly
Issue 229 — March 2, 2016
SitePoint
Practical explanations and examples. Includes a look at contextual highlighting with ‘mark’, quotations with ‘q’, and more.


W3C
The styling has been updated and the spec is now easier to read than ever (although it certainly isn’t bedtime reading).


Sarah Drasner
The CSS Scroll Snap Points spec promises to help us lock an element into the viewport on scroll without JavaScript. Support varies wildly between browsers so far though.


Opbeat  Sponsored
Instant performance insights, built for AngularJS developers. Pinpoint performance issues with a breakdown that makes sense. Sign up for early access.

Opbeat

Mozilla Hacks
Mozilla’s VR team is working hard to support the creation and display or VR content in the browser. Casey Yee looks at what’s happening.


David Attard
A look at Google’s AMP (Accelerated Mobile Pages) project, why it’s worth exploring, and a quick look at how you can convert an existing Web design to using its principles.


Smashing Magazine
A look at a new Web standard aimed at improving performance and providing more granular control over loading to Web developers.


Mozilla Hacks
Mozilla wants the DevTools in Firefox to be as hackable as the Web sites you’re working on. Reload lets you make changes to the tools themselves in real time with automatic reloading.


In brief

Curated by Peter Cooper and published by Cooper Press.
Want to post a job? E-mail us or use our self-serve system.

Unsubscribe : Change email address : Read this issue on the Web

Published by Cooper Press Ltd. Office 30, Lincoln Way, Louth, LN11 0LS, UK


by via HTML5 Weekly

Glide: Easy Dynamic on-Demand Image Resizing

Glide kayaks image

Glide is an image processing library built on top of Intervention. Its purpose is to facilitate on-demand image processing. That’s a fancy way of saying it creates images as they’re requested if they don’t exist.

For example, you might have a large, high quality portrait for your user profile. But when opening on a mobile device, downloading a 10 MB image is not only wasteful, it is slow. Instead, you could add a media query into your CSS, like so:

@media (min-width: 400px) {
        .profilePic {
                background-image: url('/images/myprofile.jpg');        
        }
}

@media (max-width: 400px) {
    .profilePic {
                background-image: url('/images/myprofile-320.jpg');                    
        }
}

This would make all devices under 400px of width load the smaller background image, thus downloading faster. But manually resizing every image you might need in your app is tedious, time consuming, and error prone. That’s where Glide comes in.

Glide can be configured to respond to missing image requests (such as the non-existant myprofile-320.jpg from the example above) by creating them from a predetermined source. In a nutshell, if the requested image doesn’t exist, but its source does, the requested image gets created from it. What’s more, the image can be saved into a cache, so that future requests don’t invoke the library and waste precious CPU resources on re-rendering.

Let’s configure it.

If you’d like to follow along, feel free to use our no-framework application and Homestead Improved for quickly setting up an environment and sample application to test this in.

Bootstrapping

Step one is installing Glide:

composer require league/glide

Then, we need to configure the Glide server. In the NOFW project above, this happens in app/config.php where all the services are configured for future injection via PHP-DI.

Continue reading %Glide: Easy Dynamic on-Demand Image Resizing%


by Bruno Skvorc via SitePoint

How to Create a News Reader With React Native: Web Page Component