Coder Social home page Coder Social logo

iros / patternfills Goto Github PK

View Code? Open in Web Editor NEW
577.0 11.0 72.0 528 KB

A collection of svg patterns and build scripts that allow utilizing them in svg, css and d3.

Home Page: http://iros.github.io/patternfills/

License: MIT License

JavaScript 5.24% CSS 7.31% HTML 87.45%

patternfills's Introduction

PatternFills

See the live site here: http://iros.github.io/patternfills/

This is a collection of svg-based pattern fills that can be used both as SVG patterns defs and CSS background image urls. There is also an example of how one might use pattern fills with d3.js.

A lot of these pattens started from: http://philbit.com/svgpatterns/ which is an amazing project.

Project structure:

All the patterns live under src/patterns. Each file is a single svg file containing the pattern itself and nothing else.

To view any of the patterns, one must build the sample files. There are three of them that are created:

  • public/sample_svg.html - a raw sample of using the pattens within SVG.
  • public/sample_css.html - a sample of using the patterns after they are converted to css image urls. See the accompanying pattern.css.
  • public/sample_d3.html - a sample of d3 code that generates the same exact output that sample_svg.html does, but using d3.js.

Using the patterns

Website

You can use the patterns as indicated via the website:

  • By downloading the pattern.css file
  • Copying individual CSS / SVG snippets into your source

NPM

You can install patternfills via npm like so:

npm install -g patternfills

It comes with an executable that will allow you to produce the CSS classes or the SVG classes and save them to an appropriate file. This might come in handy when you want to integrate patternfills into your build process.

To produce either the css, svg or a set of svg files you can run:

patternfills --format=svg|css|files

You can provide a destination like so

patternfills --format=svg|css|files --dest=your/path

Note that if you are outputing files you need to provide a folder. Otherwise you need to provide a single file name.

And even point to a different pattern set like this:

patternfills --format=svg|css|files --source=your/patterns

How do patterns work?

In this project, we are using patterns in two different ways:

  1. Using actual patterns for svg
  2. Using base64 encoded strings for css-based fills.

SVG images are typically smaller than bitmap images and remain sharp on high-dpi screens. Unlike CSS3 gradients, SVG images are supported on IE9. (source: http://philbit.com/svgpatterns/#whysvg)

SVG patterns

To define a pattern that can then be used in svg, one must add it to the defs section of an svg container:

<svg height="8" width="8" xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <pattern id="crosshatch" patternUnits="userSpaceOnUse" width="8" height="8">
      <image xlink:href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='8' height='8'><path fill='#fff' d='M0 0h8v8h-8z'/><path d='M0 0l8 8zm8 0l-8 8z' stroke-width='.5' stroke='#aaa'/></svg>" x="0" y="0" width="8" height="8">
      </image>
    </pattern>
  </defs>
</svg>

Important parts to note about this section are:

  1. The pattern id string

To use the pattern to color in some svg element, like a rect, simply set the style attribute fill to the string: url(#patternName) #fallbackcolor. For example:

<svg height="150" width="150" style="float:left">
  <rect style="fill: url(#crosshatch) #fff;" x="0" y="0" height="150" width="150">
  </rec>
</svg>

CSS patterns

Css patterns utilize image fills. For exmple, the following is a definition of one of our patterns, converted to base64 string and turned into a css class definition:

.crosshatch {
  background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='8' height='8'><path fill='#fff' d='M0 0h8v8h-8z'/><path d='M0 0l8 8zm8 0l-8 8z' stroke-width='.5' stroke='#aaa'/></svg>");
  background-repeat: repeat;
}

Then simply use the class name:

<div style="width: 150px; height: 150px; float:left" class="crosshatch">
</div>

Modifying/Adding patterns

First, set up your development environemnt, if you haven't already:

yarn install

Then open two terminal windows, both at the root of this repository. From one, run:

grunt dev - this will watch for any changes under the src directory and rebuild the sample files, while running a dev server.

From the other run:

grunt connect - this will fire up a server on localhost running on port 8000. Navigate to that url to one of the sample files you wish to see (http://localhost:8000/sample_svg.html, http://localhost:8000/sample_d3.html or http://localhost:8000/sample_css.html).

The project is setup with live reloading, so that any time you modify any of the pattern files you will see your page automatically reload.

You can build the website any time by just calling grunt render or simply grunt.

The files will be built and put into the public folder. If you don't intend to customize the patterns or add new ones, you can simply look at the generated code and use it for your own needs.

Submitting patterns

Ideally, this pattern library will grow and contain many new patterns! Please submit yours in a pull request and I will be more than happy to merge them if they work appropriatly.

Currently some of our patterns are:

patterns

But you should check them all out at http://iros.github.io/patternfills/

patternfills's People

Contributors

darby3 avatar gnarf avatar iros avatar jugglinmike avatar thell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

patternfills's Issues

How to convert patterns to use path?

The README shows the CSS pattern below. How do we convert patterns to use path as shown here?

.crosshatch {
  background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='8' height='8'><path fill='#fff' d='M0 0h8v8h-8z'/><path d='M0 0l8 8zm8 0l-8 8z' stroke-width='.5' stroke='#aaa'/></svg>");
  background-repeat: repeat;
}

Color formatting doesn't seem to be working? (Powershell, windows)

Hi there. I installed this via npm and everything seemed to work great. It blasts the changes to stdout just like it should. However it seems that the color flags don't seem to work.
I have tried:

patternfills -b 'aa0000' -f svg
pattternfills -b aa0000 -f svg
patternfills -b aa0000 -f css
patternfills -b 'aa0000' -f css

to no avail. Is there an alternate way of editing the colors of these svgs? This project would be extremely useful to me if I could. What am I doing wrong?

(To clarify, it doesn't yell at me, but the outputs I have gotten have been purely black squares or no changes at all)

Remove all base64 encoded svg+xml

Base64 takes more characters than plain text. XML can be transferred in plain ascii, so you can do this:

.crosshatch {
  background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='8' height='8'><rect width='8' height='8' fill='#fff'/><path d='M0 0L8 8ZM8 0L0 8Z' stroke-width='0.5' stroke='#aaa'/></svg>");
}
/* compare in scrollbar distance to: */
.crosshatch {
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnIHdpZHRoPSc4JyBoZWlnaHQ9JzgnPgogIDxyZWN0IHdpZHRoPSc4JyBoZWlnaHQ9JzgnIGZpbGw9JyNmZmYnLz4KICA8cGF0aCBkPSdNMCAwTDggOFpNOCAwTDAgOFonIHN0cm9rZS13aWR0aD0nMC41JyBzdHJva2U9JyNhYWEnLz4KPC9zdmc+Cg==");
}

http://jsbin.com/hepo/1/edit?css,output
Also, gzip will love the plain text version.

The build step would need to:

  • remove all "unnecessary white space" between the tags in the source,
  • replace all /\s+/ with a single space (in case any newlines survive the former clean up).
  • normalize all of the quotes in the svg to either single or double then use the opposite outside to quote the url, adding a \ to a quote if it must.

Hoping I might find some time to play with this (not at 1:30am), so I'm leaving myself this note :)

Also, will need to replace /\s+/ with a space, and remove all .

Color formatting from command line not working

Not sure if I'm doing something wrong but the documentation on custom colors from the command line wasn't super clear to me.

I looked into render.js and it looks like the CLI is looking for arguments 2 and 3 to generate custom colors.

patternfills -b "#a9a9a9" "#d8d8d8" -f svg

Screen Shot 2020-02-14 at 5 00 43 PM

so it looks like it's picking up my second color but not my third. Is this just not possible with the svg format?

base64 encoding and inkscape svg

Interesting project! I found patternfills after doing a bunch of searching for the wikipedia chart module svg patterns for use with vega.

There is a problem though... svg rendering and then using inkscape to convert to eps and pdf fails! The svg patterns provided via the patternfills svg page do not work. After removing the 'image' tag and the base64 encoded data and replacing with the much shorter, modifiable, easier to read plain text the conversions worked. The base64 worked fine in every basic viewer and browser I checked with while debugging, but failed in all of the editors I tried for converting.

Thinking I could install the patternfills binary and generate the patterns locally I followed the directions on the front page and get a d3 error. Before trying to debug the binary issue...

Can the patterns be generated without base64 encoding? Can the public facing svg page display the actual pattern defs instead of unreadable base64?

source flag requires source to be in module?

thell@f38e22ecfb83:~/Projects/SomeKnitrTests$ patternfills -f svg -s ~/SomeKnitrTests/patterns -d patternfills.svg

fs.js:659
  return binding.readdir(pathModule._makeLong(path));
                 ^
Error: ENOENT, no such file or directory '/usr/lib/node_modules/patternfills/src/home/thell/SomeKnitrTests/patterns'

Note the path tested...

In pattern_builder.js all the path instructions are using joins against __dirname. What do you think about testing for, and setting, root prior to calling pattern_builder and using it without modification within pattern_builder?

How do I use this programmatically?

Can I use these programatically?

Yep! If you'd like to use Pattern Fills in your application. You can install patternfills via npm like so:

npm install --save patternfills

And then what?

import "patternfills" fails with "Could not find module in path: 'patternfills' relative to '/src/index.js'" (sandbox), and runkit has the following to say:

patternfills lists no main file and has no index.js, so it can't be directly required. If this is a mistake, please let us know. It may however contain internal files that you can require manually

Attempting to import anything from a path under patternfills fails with the same error as trying to import patternfills itself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.