-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpatternizer.min.js
15 lines (14 loc) · 1.61 KB
/
patternizer.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* patternizer.js
* v1.1.0
* To see what this is capable of, see the UI at patternizer.com
*
* Developed by Matthew Lein
* matthewlein.com
*
* Released under the MIT license.
* Please leave this license and author info intact.
*
* Copyright 2017
*/
var patternizer={supportsCanvas:function(){var t=document.createElement("canvas");return!(!t.getContext||!t.getContext("2d"))},isArray:function(t){return t&&!t.propertyIsEnumerable("length")&&"object"==typeof t&&"number"==typeof t.length},DEGREES_RADIANS:function(t){return t*Math.PI/180},HEX_RGBA:function(t,e){return"rgba("+parseInt(t.substring(1,3),16)+","+parseInt(t.substring(3,5),16)+","+parseInt(t.substring(5,7),16)+","+e+")"},normalizeStripes:function(t){return t=t.map(function(t){return t.mode&&(t.plaid=t.plaid||"plaid"===t.mode,delete t.mode),t})},stripe:function(t,e){var r=patternizer.normalizeStripes(e.stripes),n=e.bg,a=t.getContext("2d"),i=t.width,o=t.height,p=Math.sqrt(i*i+o*o);a.fillStyle=n,a.fillRect(0,0,i,o);for(var l=r.length-1;l>=0;l--){var s=r[l],u=s.opacity/100||.5,f=patternizer.HEX_RGBA(s.color,u),c=s.plaid||!1,d=s.rotation||0,g=s.width,E=s.offset||0,h=g+(s.gap||g),S=(2*p+E)/h;a.rotate(patternizer.DEGREES_RADIANS(d));for(var m=0;m<S;m++){if(patternizer.isArray(f)){for(var v=h*m+E,z=h*m+E+g,R=a.createLinearGradient(v,0,z,0),y=1/(f.length-1),A=0;A<f.length;A++)R.addColorStop(y*A,f[A]);a.fillStyle=R}else a.fillStyle=f;a.fillRect(h*m-p-E,-p,g,2*p),c&&a.fillRect(-p,h*m-p-E,2*p,g)}a.rotate(-patternizer.DEGREES_RADIANS(d))}}};patternizer.supportsCanvas&&(HTMLCanvasElement.prototype.patternizer=function(t){return patternizer.stripe(this,t),this});