• Stars
    star
    3,769
  • Rank 11,674 (Top 0.3 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This JavaScript library produces complementary gradients generated from the top 2 dominant colours in supplied images.

Grade

Demo

Check it out

Install

Download this repo and grab the grade.js file from the /docs/dist folder.

Or install from npm: npm install grade-js

Use the CDN link:

https://unpkg.com/grade-js/docs/dist/grade.js

Usage

Recommended HTML structure:

<!--the gradients will be applied to these outer divs, as background-images-->
<div class="gradient-wrap">
    <img src="./samples/finding-dory.jpg" alt="" />
</div>
<div class="gradient-wrap">
    <img src="./samples/good-dinosaur.jpg" alt="" />
</div>

If you have the grade.js in your project, you can include it with a script tag and initialise it like so:

<script src="path/to/grade.js"></script>
<script type="text/javascript">
    window.addEventListener('load', function(){
        /*
            A NodeList of all your image containers (Or a single Node).
            The library will locate an <img /> within each
            container to create the gradient from.
         */
        Grade(document.querySelectorAll('.gradient-wrap'))
    })
</script>

If you pass in a 3rd parameter and it's a function, the HTML element(s) you passed in as the 1st parameter will not be manipulated, but an array will be returned to you, for you to do as you please with, ie.

Grade(document.querySelectorAll('.gradient-wrap'), null, function(gradientData){
    // sample contents of `gradientData` can be inspected here https://jsonblob.com/57c4601ee4b0dc55a4f180f1
})

If you've installed from npm, you can use the library like so:

import Grade from 'grade-js'
// initialise as above

The module this imports will be using ES2015 syntax, so it will need to be transpiled by a build tool, like Babel, and if you are importing the module in this fashion (and using npm), I imagine you're already using a bundling tool, like Webpack or Browserify!

Running locally

If you want to run this locally, just to test it, you need to serve index.html via a webserver, not just by opening it in a browser, else the browser will throw a security error. I would recommend either live-server (requires Node.js installed on your machine) or if you have Python installed, just run python -m SimpleHTTPServer inside the project root. If you're on Windows, I believe WAMP/Apache is the best way to go.

Remote images

This plugin utilises the <canvas> element and the ImageData object, and due to cross-site security limitations, the script will fail if one tries to extract the colors from an image not hosted on the current domain, unless the image allows for Cross Origin Resource Sharing.

Enabling CORS on S3

To enable CORS for images hosted on S3 buckets, follow the Amazon guide here; adding the following to the bucket's CORS configuration:

<CORSRule>
 <AllowedOrigin>*</AllowedOrigin>
 <AllowedMethod>GET</AllowedMethod>
</CORSRule>

For all images, you can optionally also include a cross-origin attribute in your image.

<img src="/image.jpg" cross-origin="anonymous"/>

License

MIT License

Copyright (c) 2016 Ben Howdle

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

svgeezy

Allows for automatic Image fallbacks for browsers that can't handle SVGs
JavaScript
875
star
2

touche

Effortlessly re-map click events to touch events on touchscreen UIs
JavaScript
605
star
3

Adaptive-Backgrounds

A JavaScript plugin for extracting the dominant color from images and applying the color to their parent.
CSS
245
star
4

deSVG

Remove inline SVG bloat from your HTML document
JavaScript
159
star
5

isomorphic-js-app

A JS/Node app that uses Backbone.js/React.js/Browserify to make it Isomorphic
JavaScript
22
star
6

filtergram

For People Who Want To See Some Instagram Content But Aren't Too Keen On The Instagram Experience Itself.
JavaScript
22
star
7

cortado

A sans-jQueryโ„ข View & Event library for JavaScript web applications
JavaScript
20
star
8

typo

Typography CSS library
16
star
9

Chatt

A tiny chat app written in Meteor for my 12Devs article.
JavaScript
15
star
10

ihi

Easy location sharing
CSS
14
star
11

speculo

Colour palette visualiser
JavaScript
14
star
12

Seo.js

SEO checklist for your website, not for production
JavaScript
13
star
13

fwd

A dead simple JavaScript plugin to help manage page-specific JavaScript code in the browser.
JavaScript
13
star
14

musings

Musings - advice & inspiration from people in the web industry
CSS
11
star
15

palette

HTML
10
star
16

readmeapp-site

JavaScript
9
star
17

varr

A basic variable replacement preprocessor for CSS files
JavaScript
7
star
18

readmeapp-api

JavaScript
7
star
19

thrift

GitHub repo/idea thrift store for the open web.
JavaScript
6
star
20

jekyll-search-demo

A Jekyll project demoing a dynamic search page
CSS
5
star
21

facebookInfo

A small class designed to take the pain away from working with the Facebook API.
PHP
5
star
22

React-Redux-Conversational

JavaScript
5
star
23

js-idioms

CSS
5
star
24

gustavo

A form validation library for Backbone Views.
JavaScript
5
star
25

everyday-stoic

JavaScript
4
star
26

disconnect-today

Twitter digest emails without the distraction
JavaScript
4
star
27

placevaughnandfranco

Vaughn & Franco place it image service website
JavaScript
4
star
28

ubiq

Easily share tracks, artists or albums across different music services
JavaScript
3
star
29

BB-Express

Boilerplate app for Backbone & Express
JavaScript
3
star
30

top-spot

Top Spot looks through your Spotify playlists, finds the most frequently occurring tracks, and creates you a playlist from them.
JavaScript
2
star
31

beattheclock

A beat-the-clock game designed to help people learn JavaScript
JavaScript
2
star
32

albums

JavaScript
2
star
33

moodboard

JavaScript
2
star
34

referrral-app

Backbone.js front-end for Referrral
JavaScript
2
star
35

halcyon

JavaScript
2
star
36

aviewtoakill

A boilerplate Backbone.js app for pushState and Dan Harper's ViewManager
JavaScript
2
star
37

cmyk

JavaScript
1
star
38

Costarred

CSS
1
star
39

venetian

JavaScript
1
star
40

Class

Simple, elegant inheritance for your JavaScript
CSS
1
star
41

atme

JavaScript
1
star
42

miniload

PHP
1
star
43

Foto

A HTML/CSS Template for kickstarting a project
PHP
1
star
44

shade

JavaScript
1
star
45

secretFilmProject

JavaScript
1
star
46

ihi-ios

iOS app as a companion for iHi
Objective-C
1
star