Wednesday, July 12, 2017

SVG Tip: Create a Bold Vector Halftone Graphic in Under 2 Minutes

This is a section from a 500 year old woodcut from perhaps the 'Andy Warhol of woodcut artists' - Albrecht Dürer. Woodcut is an old printing technique where the image is hand engraved into a wooden block by specialist blockcutters. Prints are then taken from the block.

[caption id="attachment_157164" align="alignright" width="187"]Dürer Dürer (Surprisingly, not the
guy from Nickleback.)[/caption]

Woodcuts are always bold and stark in nature because – unlike most painting techniques – there are no mid-tone colors or blends. The paper is either clean white or drenched ink black – there are no gray areas.

However as you can see, Dürer is able to mimic mid-tone grays by building up a fine rendering of linework. In his case, he uses parallel fill lines, but it's possible to get the same effect with cross-hatching, waves, ribbons, arcs, dots and other patterns.

Although it's a very old technique, we still see it everywhere today – from tattoos to beer labels to bank notes. Most of us have admired the beautiful, wavy ribbon linework used on paper money, sometimes called 'guilloché'.

Halftone examples

It also turns out to be perfectly suited to the mathematical precision of SVG – though hand rendering thousands of precision lines by hand may be impractical for many of us. There are professional options available if you're super-serious – Excentro is cool.

But if that's overkill, let's look at a simpler, more affordable solution.

How do I make a Halftone?

HalftonePro is an online service that converts photos into 'halftones' - what the printing industry calls these graphics.

Using this app is relatively simple but the results take some tuning.

  1. IMAGE: Upload your photo. If this is your first time, jump to the 'Preset' menu at the bottom, as it will give you a feel for the app faster.

  2. SHAPE: The 'Shape' menu lets you change the shape that makes up the image – circles, squares, lines, etc.

  3. PATTERN: The 'Pattern' menu is where things get complex. You can choose the grid that your shape adhere to. While I'm not going to bore you by explaining all the sliders here, the 'Vertical elements' slider is probably the most important. This sets the level of detail in your conversion. A setting of 25 is chunky – a setting of 150 is finely detailed. TIP: Don't set Vertical elements higher than about 150 as you'll be creating a HUGE SVG file and may even crash your browser.

  4. COLORS: Pick your palette.

  5. RESIZE: You can guess.

  6. PRESETS: The dozen or so presets provided give you a good sense of what this app can do. You can also save your own custom presets here.

Bowie - Before changes

High contrast images are likely to work best. I uploaded this classic black and white Bowie shot as a test and used the default 'Wave' preset with a 'Vertical elements' setting of 150.

Continue reading %SVG Tip: Create a Bold Vector Halftone Graphic in Under 2 Minutes%


by Alex Walker via SitePoint

#298: Is It Safe to Start using CSS Grid Layout?

Frontend Focus
Issue 298 — July 12, 2017
Kalpesh Singh uses Flexbox features to build a ‘mega’ drop-down navigation menu as are particularly common on e-commerce sites recently.
SitePoint

..with no dependencies. Demo here.
Jared Reich

Some thoughts on old browsers and how to make good decisions about the technology choices that you have.
Rachel Andrew

Learn how to build data-intensive, cross-platform web apps leveraging over 115 Sencha components and React.
Sencha, Inc   Sponsor

Tim Berners-Lee, inventor of the Web and director of the W3C, is supporting the EME spec (commonly thought of as ‘DRM for the Web’) to gain W3C Recommendation status.
Zak Rogoff

A helpful refresher on collapsed margins: it’s what occurs when two block-level elements with meeting vertical margins combine.
Ire Aderinokun

Using @supports, you can apply styles conditionally based on support for certain features. Not a new article but now almost universally supported in browsers.
Jen Simmons

Jobs

Can't find the right job? Want companies to apply to you? Try Hired.com.

In Brief

The Options for Programmatically Documenting CSS tutorial
“documentation should be kept as close to the code as possible.”
Kaloyan Kosev

A Beginner’s Guide to Making Progressive Web Apps tutorial
Yuvesh Tulsiani

Building Pattern Libraries With Shadow DOM in Markdown tutorial
Heydon Pickering

The CSS 3 Speech Module and iOS tutorial
VoiceOver on iOS supports 4 settings on the CSS speak-as property.
Eric Bailey

Designing The Perfect Date and Time Picker tutorial
Vitaly Friedman

It’s easy to version control your database alongside your application 
Connect your database to your version control system with SQL Source Control and keep track of every change.
Redgate  Sponsor

Browser Security in 2017 video
A good summary of the current state of security oriented browser technologies.
Johann Hofmann / Mozilla

How to Label Shadow DOM video
Rob Dodson shares a quick accessibility tip for labelling content inside of a shadow root.
Google Chrome Developers

Browser Module Loading: Can We Stop Bundling Yet? opinion
Includes performance tests comparing bundled and unbundled.
Sérgio Gomes

Life Is About to Get Harder for Sites Not Using HTTPS opinion
Increased numbers of warnings in browsers will make users think twice.
Troy Hunt

CSS vs. JavaScript: Trust vs. Control opinion
Thoughts on what using CSS means and how programmers may not use it to its strengths.
Christian Heilmann

URLs are UI opinion
“You change fonts, move CSS around ad infinitum, and agonize over single pixels. You should also care about your URLs.”
Scott Hanselman

Inline/Live Validation Is Problematic opinion
Simple = Human

$20 Free on a new Linode account 
Linux cloud hosting starting at 1GB of RAM for $5/mo. Use promo code HTML520 and get $20 credit.
Linode  Sponsor

csvtotable: CLI Utility to Convert CSV Files to Sortable HTML Tables tools
Vivek R

6 Material Design CSS Frameworks Compared code
SitePoint

fractures: A Baseline Atomic CSS Toolkit code
Provides a set of non-blocking, atomic, utility classes for quick prototyping.
Krisztian Puska

pinhole-js: A 3D Wireframe Drawing Library for HTML Canvas code
Josh Baker

Tonik: A WordPress Starter Theme Using Modern Web Practices code
An open-source theme which leans heavily on ES6, Sass, webpack, and modern PHP features.
Tonik

AC-D3: Library for Building Audiovisual Charts in D3 code
Bring the power of embedded videos to D3 visualizations.
Michals, Worrel and Nunns


by via Frontend Focus

Halide

Halide

Gorgeous intro device load transition in this One Pager for premium iPhone camera, Halide. Also love how the colorful screenshots really glow against the dark-schemed web page.

by Rob Hope via One Page Love

Plupload – JavaScript API for Building File Uploaders

Plupload is JavaScript API for building file uploaders. It supports multiple file selection, file filtering, chunked upload, client side image downsizing and when necessary can fallback to alternative runtimes, like Flash and Silverlight.


by via jQuery-Plugins.net RSS Feed

Maud

Maud

Minimal One Page portfolio with a simple, centered showreel of work and an info overlay for digital agency, Maud.

by Rob Hope via One Page Love

Working With the File System in Elixir

Working with the file system in Elixir does not really differ from doing so using other popular programming languages. There are three modules to solve this task: IO, File, and Path. They provide functions to open, create, modify, read and destroy files, expand paths, etc. There are, however, some interesting gotchas that you should be aware of.

In this article we will talk about working with the file system in Elixir while taking a look at some code examples.

The Path Module

The Path module, as the name suggests, is used to work with file system paths. The functions of this module always return UTF-8 encoded strings.

For instance, you can expand a path and then generate an absolute path easily:

Note, by the way, that in Windows, backslashes are replaced with forward slashes automatically. The resulting path can be passed to the functions of the File module, for example:

Here we are constructing a full path to the file and then writing some contents to it.

All in all, working with the Path module is simple, and most of its functions do not interact with the file system. We will see some use cases for this module later in the article.

IO and File Modules

IO, as the name implies, is the module to work with input and output. For example, it provides such functions as puts and inspect. IO has a concept of devices, which can be either process identifiers (PID) or atoms. For instance, there are :stdio and :stderr generic devices (which are actually shortcuts). Devices in Elixir maintain their position, so subsequent read or write operations start from the place where the device was previously accessed.

The File module, in turn, allows us to access files as IO devices. Files are opened in binary mode by default; however, you might pass :utf8 as an option. Also when a filename is specified as a character list ('some_name.txt'), it is always treated as UTF-8.

Now let's see some examples of using the modules mentioned above.

Opening and Reading Files With IO

The most common task is, of course, opening and reading files. To open a file, a function called open/2 can be used. It accepts a path to the file and an optional list of modes. For example, let's try to open a file for reading and writing:

You may then read this file using the read/2 function from the IO module as well:

Here we are reading the file line by line. Note the :eof atom that means "end of file".

You can also pass :all instead of :line to read the whole file at once:

In this case, :eof won't be returned—instead, we get an empty string. Why? Well, because, as we said earlier, devices maintain their position, and we start reading from the previously accessed place.

There is also an open/3 function, which accepts a function as the third argument. After the passed function has finished its work, the file is closed automatically:

Reading Files With File Module

In the previous section I've shown how to use IO.read in order to read files, but it appears that the File module actually has a function with the same name:

This function returns a tuple containing the result of the operation and a binary data object. In this example it contains "test", which is the contents of the file.

If the operation was unsuccessful, then the tuple will contain an :error atom and the error's reason:

Here, :enoent means that the file does not exist. There are some other reasons like :eacces (has no permissions).

The returned tuple can be used in pattern matching to handle different outcomes:

In this example, we either print out the file's contents or display an error reason.

Another function to read files is called read!/1. If you have come from the Ruby world, you've probably guessed what it does. Basically, this function opens a file and returns its contents in the form of a string (not tuple!):

However, if something goes wrong and the file cannot be read, an error is raised instead:

So, to be on the safe side, you can, for example, employ the exists?/1 function to check whether a file actually exists: 

Great, now we know how to read files. However, there is much more we can do, so let's proceed to the next section!

Writing to Files

To write something to a file, use the write/3 function. It accepts a path to a file, the contents, and an optional list of modes. If the file does not exist, it will be created automatically. If, however, it does exist, all its contents will be overwritten by default. To prevent this from happening, set the :append mode:

In this case, the contents will be appended to the file and :ok will be returned as a result. If something goes wrong, you'll get a tuple {:error, reason}, just like with the read function.

Also, there is a write! function that does pretty much the same, but raises an exception if the contents cannot be written. For example, we can write an Elixir program that creates a Ruby program that, in turn, prints "hello!":

Streaming Files

The files can indeed be pretty large, and when using the read function you load all the contents into the memory. The good news is that files can be streamed quite easily:

In this example, we open a file, stream it line by line, and inspect each line. The result will look like this:

Note that the new line symbols are not removed automatically, so you may want to get rid of them using the String.replace/4 function.

It is a bit tedious to stream a file line by line as shown in the previous example. Instead, you can rely on the stream!/3 function, which accepts a path to the file and two optional arguments: a list of modes and a value explaining how a file should be read (the default value is :line):

In this piece of code we are streaming a file while removing newline characters and then printing out each line. File.stream! is slower than File.read, but we don't need to wait until all lines are available—we can start processing the contents right away. This is especially useful when you need to read a file from a remote location.

Let's take a look at a slightly more complex example. I'd like to stream a file with my Elixir script, remove newline characters, and display each line with a line number next to it:

Stream.with_index/2 accepts an enumerable and returns a collection of tuples, where each tuple contains a value and its index. Next, we just iterate over this collection and print out the line number and the line itself. As a result, you'll see the same code with line numbers:

Moving and Removing Files

Now let's also briefly cover how to manipulate files—specifically, move and remove them. The functions we're interested in are rename/2 and rm/1. I won't bore you by describing all the arguments they accept as you can read the documentation yourself, and there is absolutely nothing complex about them. Instead, let's take a look at some examples.

First, I'd like to code a function that takes all files from the current directory based on a condition and then moves them to another directory. The function should be called like this:

So, here I want to grab all .txt files and move them to the texts directory. How can we solve this task? Well, firstly, let's define a module and a private function to prepare a destination directory:

mkdir!, as you've already guessed, tries to create a directory and returns an error if this operation fails.

Next, we need to grab all the files from the current directory. This can be done using the ls! function, which returns a list of file names:

Lastly, we need to filter the resulting list based on the provided function and rename each file, which effectively means moving it to another directory. Here is the final version of the program:

Now let's see the rm in action by coding a similar function that is going to remove all files based on a condition. The function will be called in the following way:

Here is the corresponding solution:

rm!/1 will raise an error if the file cannot be removed. As always, it has an rm/1 counterpart that will return a tuple with the error's reason if something goes wrong.

You may note that the remove_if and transfer_to functions are very similar. So why don't we remove code duplication as an exercise? I'll add yet another private function that takes all the files, filters them based on the provided condition, and then applies an operation to them:

Now simply utilize this function:

Third-Party Solutions

Elixir's community is growing, and fancy new libraries solving various tasks are emerging. The Awesome Elixir GitHub repo lists some popular solutions, and of course there is a section with libraries for working with files and directories. There are implementations for file uploading, monitoring, filename sanitization, and more.

For example, there is an interesting solution called Librex for converting your documents with the help of LibreOffice. To see it in action, you can create a new project:

Then add a new dependency to the mix.exs file:

After that, run:

Next, you can include the library and perform conversions:

In order for this to work, the LibreOffice executable (soffice.exe) must be present in the PATH. Otherwise, you'll need to provide a path to this file as a third argument:

Conclusion

That's all for today! In this article, we've seen the IO, File and Path modules in action and discussed some useful functions like open, read, write, and others. 

There are lots of other functions available for use, so be sure to browse Elixir's documentation. Also, there is an introductory tutorial on the official website of the language that can come in handy as well.

I hope you enjoyed this article and now feel a bit more confident about working with the file system in Elixir. Thank you for staying with me, and until next time!


by Ilya Bodrov via Envato Tuts+ Code

How to Add CAPTCHAs to Android Apps