-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpixasso.min.js
7 lines (7 loc) · 1.04 KB
/
pixasso.min.js
1
2
3
4
5
6
7
/*!
* pixasso - v1.0.0 - 2018-07-01
* https://github.com/howion/pixasso
*
* Copyright (c) 2018 howion
* Licensed under the MIT license */
var pixasso={colorPickMode:Object.freeze({FirstColor:0,JointColor:1}),determineColor:function(t,e){switch(e){case this.colorPickMode.FirstColor:return[t[0],t[1],t[2]];case this.colorPickMode.JointColor:var o,r=[t[0],t[1],t[2]];for(o=4;o<t.length;o+=4)r[0]=Math.floor((r[0]+t[o+0])/2),r[1]=Math.floor((r[1]+t[o+1])/2),r[2]=Math.floor((r[2]+t[o+2])/2);return r}},createPixelArt:function(t,e,o,r){var a=document.createElement("canvas"),i=t.naturalWidth,l=t.naturalHeight;a.width=i,a.height=l,a.getContext("2d").drawImage(t,0,0,i,l);var n=document.createElement("canvas");n.width=Math.floor(i/e)*o,n.height=Math.floor(l/e)*o;var c,h,d,f,s,g=n.getContext("2d"),m=a.getContext("2d");for(c=0,d=0;c<i;c+=e){for(h=0,f=0;h<l;h+=e)s=this.determineColor(m.getImageData(c,h,e,e).data,r),g.fillStyle="rgb("+s[0]+", "+s[1]+", "+s[2]+")",g.fillRect(d,f,o,o),f+=o;d+=o}var u=document.createElement("img");return u.src=n.toDataURL(),u}};