diff --git a/README.md b/README.md
index c5979e8..ae4203f 100644
--- a/README.md
+++ b/README.md
@@ -272,6 +272,61 @@ The ColorPicker function has recognized only two arguments which means that it b
and also fixes and positions indicators automatically.
+Pointer Events
+========
+If you require more widely support with mobile or touch device, you should add `touch-action="none"` to css like:
+```css
+
+ #picker { width: 200px; height: 200px; touch-action: none; }
+ #slider { width: 30px; height: 200px; touch-action: none; }
+
+
+ #picker-wrapper {
+ width: 200px;
+ height: 200px;
+ position: relative;
+ touch-action: none;
+ }
+ #slider-wrapper {
+ width: 30px;
+ height: 200px;
+ position: relative;
+ touch-action: none;
+ }
+```
+
+if you require [Pointer Events Polyfill](https://github.com/jquery/PEP) and don't want to use [**No hassle**](#no-hassle) method, according to [Polyfill limitations](https://github.com/jquery/PEP#polyfill-limitations), please add `touch-action="none"` attribute to element like:
+```html
+
+
+
+
+
+
+
+```
+Recommend to place in a big wrapper and set `touch-action="none"`, to prevent to fire default event like page scroll.
+```html
+
+```
+
+
+
License
========
diff --git a/colorpicker.js b/colorpicker.js
index e13892c..ff67ef8 100644
--- a/colorpicker.js
+++ b/colorpicker.js
@@ -13,11 +13,11 @@
var colorpickerHTMLSnippet = [
'',
''
@@ -35,8 +35,13 @@
if (evt.offsetX !== undefined && evt.offsetY !== undefined) {
return { x: evt.offsetX, y: evt.offsetY };
}
- // Firefox:
var wrapper = evt.target.parentNode.parentNode;
+ // PEP fix
+ if (typeof evt.offsetX === 'undefined') {
+ wrapper = wrapper.getBoundingClientRect();
+ return { x: evt.pageX - wrapper.x, y: evt.pageY - wrapper.y};
+ }
+ // Firefox:
return { x: evt.layerX - wrapper.offsetLeft, y: evt.layerY - wrapper.offsetTop };
}
@@ -187,6 +192,8 @@
return function(evt) {
evt = evt || window.event;
var mouse = mousePosition(evt);
+ mouse.y = mouse.y > slideElement.clientHeight ? slideElement.clientHeight : mouse.y;
+ mouse.y = mouse.y < 0 ? 0 : mouse.y;
ctx.h = mouse.y / slideElement.offsetHeight * 360 + hueOffset;
var pickerColor = hsv2rgb({ h: ctx.h, s: 1, v: 1 });
var c = hsv2rgb({ h: ctx.h, s: ctx.s, v: ctx.v });
@@ -206,6 +213,11 @@
width = pickerElement.offsetWidth,
height = pickerElement.offsetHeight;
+ mouse.x = mouse.x > pickerElement.clientWidth ? pickerElement.clientWidth : mouse.x;
+ mouse.x = mouse.x < 0 ? 0 : mouse.x;
+ mouse.y = mouse.y > pickerElement.clientHeight ? pickerElement.clientHeight : mouse.y;
+ mouse.y = mouse.y < 0 ? 0 : mouse.y;
+
ctx.s = mouse.x / width;
ctx.v = (height - mouse.y) / height;
var c = hsv2rgb(ctx);
@@ -320,16 +332,26 @@
var mousedown = false;
- addEventListener(element, 'mousedown', function(evt) { mousedown = true; });
- addEventListener(element, 'mouseup', function(evt) { mousedown = false; });
- addEventListener(element, 'mouseout', function(evt) { mousedown = false; });
- addEventListener(element, 'mousemove', function(evt) {
+ if (window.PointerEvent) {
+ addEventListener(element, 'pointerdown', function(evt) { mousedown = true; });
+ addEventListener(element, 'pointerup', function(evt) { mousedown = false; });
+ addEventListener(element, 'pointerout', function(evt) { mousedown = false; });
+ addEventListener(element, 'pointermove', function(evt) {
+ if (mousedown) {
+ listener(evt);
+ }
+ });
- if (mousedown) {
-
- listener(evt);
- }
- });
+ } else {
+ addEventListener(element, 'mousedown', function(evt) { mousedown = true; });
+ addEventListener(element, 'mouseup', function(evt) { mousedown = false; });
+ addEventListener(element, 'mouseout', function(evt) { mousedown = false; });
+ addEventListener(element, 'mousemove', function(evt) {
+ if (mousedown) {
+ listener(evt);
+ }
+ });
+ }
}
diff --git a/colorpicker.min.js b/colorpicker.min.js
index cce312e..ae7e3a5 100644
--- a/colorpicker.min.js
+++ b/colorpicker.min.js
@@ -1 +1 @@
-(function(s,t,u){var v=(s.SVGAngle||t.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure","1.1")?"SVG":"VML"),picker,slide,hueOffset=15,svgNS='http://www.w3.org/2000/svg';var w=['',''].join('');function mousePosition(a){if(s.event&&s.event.contentOverflow!==u){return{x:s.event.offsetX,y:s.event.offsetY}}if(a.offsetX!==u&&a.offsetY!==u){return{x:a.offsetX,y:a.offsetY}}var b=a.target.parentNode.parentNode;return{x:a.layerX-b.offsetLeft,y:a.layerY-b.offsetTop}}function $(a,b,c){a=t.createElementNS(svgNS,a);for(var d in b)a.setAttribute(d,b[d]);if(Object.prototype.toString.call(c)!='[object Array]')c=[c];var i=0,len=(c[0]&&c.length)||0;for(;i','','','',''].join('');picker=['','','','','','','','
'].join('');if(!t.namespaces['v'])t.namespaces.add('v','urn:schemas-microsoft-com:vml','#default#VML')}function hsv2rgb(a){var R,G,B,X,C;var h=(a.h%360)/60;C=a.v*a.s;X=C*(1-Math.abs(h%2-1));R=G=B=a.v-C;h=~~h;R+=[C,X,0,0,X,C][h];G+=[X,C,C,X,0,0][h];B+=[0,0,X,C,C,X][h];var r=Math.floor(R*255);var g=Math.floor(G*255);var b=Math.floor(B*255);return{r:r,g:g,b:b,hex:"#"+(16777216|b|(g<<8)|(r<<16)).toString(16).slice(1)}}function rgb2hsv(a){var r=a.r;var g=a.g;var b=a.b;if(a.r>1||a.g>1||a.b>1){r/=255;g/=255;b/=255}var H,S,V,C;V=Math.max(r,g,b);C=V-Math.min(r,g,b);H=(C==0?null:V==r?(g-b)/C+(g1||t.g>1||t.b>1)&&(e/=255,i/=255,o/=255);var s,n,r,l;return r=Math.max(e,i,o),l=r-Math.min(e,i,o),s=0==l?null:r==e?(i-o)/l+(is.clientHeight?s.clientHeight:a.y,a.y=a.y<0?0:a.y,e.h=a.y/s.offsetHeight*360+g;var c=n({h:e.h,s:1,v:1}),f=n({h:e.h,s:e.s,v:e.v});r.style.backgroundColor=c.hex,e.callback&&e.callback(f.hex,{h:e.h-g,s:e.s,v:e.v},{r:f.r,g:f.g,b:f.b},i,a)}}function a(e,i){return function(s){s=s||t.event;var r=o(s),l=i.offsetWidth,a=i.offsetHeight;r.x=r.x>i.clientWidth?i.clientWidth:r.x,r.x=r.x<0?0:r.x,r.y=r.y>i.clientHeight?i.clientHeight:r.y,r.y=r.y<0?0:r.y,e.s=r.x/l,e.v=(a-r.y)/a;var c=n(e);e.callback&&e.callback(c.hex,{h:e.h-g,s:e.s,v:e.v},{r:c.r,g:c.g,b:c.b},r)}}function c(t,e,i){if(!(this instanceof c))return new c(t,e,i);if(this.h=0,this.s=1,this.v=1,i)this.callback=i,this.pickerElement=e,this.slideElement=t;else{var o=t;o.innerHTML=m,this.slideElement=o.getElementsByClassName("slide")[0],this.pickerElement=o.getElementsByClassName("picker")[0];var s=o.getElementsByClassName("slide-indicator")[0],n=o.getElementsByClassName("picker-indicator")[0];c.fixIndicators(s,n),this.callback=function(t,i,o,r,l){c.positionIndicators(s,n,l,r),e(t,i,o)}}if("SVG"==u){var r=v.cloneNode(!0),p=d.cloneNode(!0),g=r.getElementsByTagName("linearGradient")[0],y=r.getElementsByTagName("rect")[0];g.id="gradient-hsv-"+x,y.setAttribute("fill","url(#"+g.id+")");var b=[p.getElementsByTagName("linearGradient")[0],p.getElementsByTagName("linearGradient")[1]],w=p.getElementsByTagName("rect");b[0].id="gradient-black-"+x,b[1].id="gradient-white-"+x,w[0].setAttribute("fill","url(#"+b[1].id+")"),w[1].setAttribute("fill","url(#"+b[0].id+")"),this.slideElement.appendChild(r),this.pickerElement.appendChild(p),x++}else this.slideElement.innerHTML=v,this.pickerElement.innerHTML=d;f(this.slideElement,"click",l(this,this.slideElement,this.pickerElement)),f(this.pickerElement,"click",a(this,this.pickerElement)),h(this,this.slideElement,l(this,this.slideElement,this.pickerElement)),h(this,this.pickerElement,a(this,this.pickerElement))}function f(t,e,i){t.attachEvent?t.attachEvent("on"+e,i):t.addEventListener&&t.addEventListener(e,i,!1)}function h(e,i,o){var s=!1;t.PointerEvent?(f(i,"pointerdown",function(t){s=!0}),f(i,"pointerup",function(t){s=!1}),f(i,"pointerout",function(t){s=!1}),f(i,"pointermove",function(t){s&&o(t)})):(f(i,"mousedown",function(t){s=!0}),f(i,"mouseup",function(t){s=!1}),f(i,"mouseout",function(t){s=!1}),f(i,"mousemove",function(t){s&&o(t)}))}function p(t,e,i,o){t.h=e.h%360,t.s=e.s,t.v=e.v;var s=n(t),r={y:t.h*t.slideElement.offsetHeight/360,x:0},l=t.pickerElement.offsetHeight,a={x:t.s*t.pickerElement.offsetWidth,y:l-t.v*l};return t.pickerElement.style.backgroundColor=n({h:t.h,s:1,v:1}).hex,t.callback&&t.callback(o||s.hex,{h:t.h,s:t.s,v:t.v},i||{r:s.r,g:s.g,b:s.b},a,r),t}var d,v,u=t.SVGAngle||e.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure","1.1")?"SVG":"VML",g=15,y="http://www.w3.org/2000/svg",m=['",'"].join("");"SVG"==u?(v=s("svg",{xmlns:"http://www.w3.org/2000/svg",version:"1.1",width:"100%",height:"100%"},[s("defs",{},s("linearGradient",{id:"gradient-hsv",x1:"0%",y1:"100%",x2:"0%",y2:"0%"},[s("stop",{offset:"0%","stop-color":"#FF0000","stop-opacity":"1"}),s("stop",{offset:"13%","stop-color":"#FF00FF","stop-opacity":"1"}),s("stop",{offset:"25%","stop-color":"#8000FF","stop-opacity":"1"}),s("stop",{offset:"38%","stop-color":"#0040FF","stop-opacity":"1"}),s("stop",{offset:"50%","stop-color":"#00FFFF","stop-opacity":"1"}),s("stop",{offset:"63%","stop-color":"#00FF40","stop-opacity":"1"}),s("stop",{offset:"75%","stop-color":"#0BED00","stop-opacity":"1"}),s("stop",{offset:"88%","stop-color":"#FFFF00","stop-opacity":"1"}),s("stop",{offset:"100%","stop-color":"#FF0000","stop-opacity":"1"})])),s("rect",{x:"0",y:"0",width:"100%",height:"100%",fill:"url(#gradient-hsv)"})]),d=s("svg",{xmlns:"http://www.w3.org/2000/svg",version:"1.1",width:"100%",height:"100%"},[s("defs",{},[s("linearGradient",{id:"gradient-black",x1:"0%",y1:"100%",x2:"0%",y2:"0%"},[s("stop",{offset:"0%","stop-color":"#000000","stop-opacity":"1"}),s("stop",{offset:"100%","stop-color":"#CC9A81","stop-opacity":"0"})]),s("linearGradient",{id:"gradient-white",x1:"0%",y1:"100%",x2:"100%",y2:"100%"},[s("stop",{offset:"0%","stop-color":"#FFFFFF","stop-opacity":"1"}),s("stop",{offset:"100%","stop-color":"#CC9A81","stop-opacity":"0"})])]),s("rect",{x:"0",y:"0",width:"100%",height:"100%",fill:"url(#gradient-white)"}),s("rect",{x:"0",y:"0",width:"100%",height:"100%",fill:"url(#gradient-black)"})])):"VML"==u&&(v=['','','',"","
"].join(""),d=['','','',"",'','',"","
"].join(""),e.namespaces.v||e.namespaces.add("v","urn:schemas-microsoft-com:vml","#default#VML"));var x=0;c.hsv2rgb=function(t){var e=n(t);return delete e.hex,e},c.hsv2hex=function(t){return n(t).hex},c.rgb2hsv=r,c.rgb2hex=function(t){return n(r(t)).hex},c.hex2hsv=function(t){return r(c.hex2rgb(t))},c.hex2rgb=function(t){return{r:parseInt(t.substr(1,2),16),g:parseInt(t.substr(3,2),16),b:parseInt(t.substr(5,2),16)}},c.prototype.setHsv=function(t){return p(this,t)},c.prototype.setRgb=function(t){return p(this,r(t),t)},c.prototype.setHex=function(t){return p(this,c.hex2hsv(t),i,t)},c.positionIndicators=function(t,e,i,o){i&&(t.style.top=i.y-t.offsetHeight/2+"px"),o&&(e.style.top=o.y-e.offsetHeight/2+"px",e.style.left=o.x-e.offsetWidth/2+"px")},c.fixIndicators=function(t,e){e.style.pointerEvents="none",t.style.pointerEvents="none"},t.ColorPicker=c}(window,window.document);
\ No newline at end of file
diff --git a/themes.css b/themes.css
index 7a49d13..c61ccfa 100644
--- a/themes.css
+++ b/themes.css
@@ -13,6 +13,7 @@
}
.picker,
.slide {
+ touch-action: none;
cursor: crosshair;
float: left;
}