Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
10 lines (9 sloc) 2.14 KB
/*
* AlphaPicker v0.1
* (c) 2014 uco
* http://utsutsunogare.com/
*
* Released under the MIT license
* http://opensource.org/licenses/mit-license.php
*/
(function(e,t){"use strict";function n(e){if(!(this instanceof n)){return new n(e)}else{var t=document.createElement("canvas");if(!t.getContext)throw new Error("HTML5 Canvas is not supported");this.image=e;this.style=getComputedStyle(e,"");this.context=t.getContext("2d");this.events={};this.init()}}function r(e,t,n,r){this.originalEvent=e;this.type=e.type;this.alphaPicker=t;this.alpha=n;this.offsetX=r.x;this.offsetY=r.y}function i(e){var t=e.target.getBoundingClientRect(),n={};n.x=parseInt(e.clientX-t.left,10);n.y=parseInt(e.clientY-t.top,10);return n}n.prototype={constructor:n.constructor,init:function(){this.width=parseInt(this.style.width,10);this.height=parseInt(this.style.height,10);this.context.canvas.width=this.width;this.context.canvas.height=this.height;this.context.drawImage(this.image,0,0,this.width,this.height);this.imageData=this.context.getImageData(0,0,this.width,this.height)},load:function(e){return this.addEventListener("load",e)},click:function(e){return this.addEventListener("click",e)},mousedown:function(e){return this.addEventListener("mousedown",e)},mouseup:function(e){return this.addEventListener("mouseup",e)},mousemove:function(e){return this.addEventListener("mousemove",e)},mouseout:function(e){return this.addEventListener("mouseout",e)},addEventListener:function(e,n,s){if(t===s)s=false;if(!this.events[e])this.events[e]={origin:[],listener:[]};if(this.hasEventListener(e,n)!==-1)return;var o=this,u=function(e){var t=i(e),s=o.imageData.data[(t.x+t.y*o.width)*4+3],u=new r(e,o,s,t);n.call(this,u)};this.events[e].origin.push(n);this.events[e].listener.push(u);this.image.addEventListener(e,u,s);return this},removeEventListener:function(e,n,r){if(t===r)r=false;var i=this.hasEventListener(e,n),s;if(i===-1){s=n}else{this.events[e].origin.splice(i,1);s=this.events[e].listener.splice(i,1)[0]}this.image.removeEventListener(e,s,r);return this},hasEventListener:function(e,t){return this.events[e].origin.indexOf(t)},reset:function(){this.init();return this}};e.AlphaPicker=n})(this);