diff --git a/assets/menu icons/active-background-color.png b/assets/menu icons/active-background-color.png new file mode 100644 index 0000000..cec80b4 Binary files /dev/null and b/assets/menu icons/active-background-color.png differ diff --git a/assets/menu icons/inactive-background-color.png b/assets/menu icons/inactive-background-color.png new file mode 100644 index 0000000..602b1eb Binary files /dev/null and b/assets/menu icons/inactive-background-color.png differ diff --git a/assets/menu icons/not-active-brush.png b/assets/menu icons/inactive-brush.png similarity index 100% rename from assets/menu icons/not-active-brush.png rename to assets/menu icons/inactive-brush.png diff --git a/assets/menu icons/not-active-eraser.png b/assets/menu icons/inactive-eraser.png similarity index 100% rename from assets/menu icons/not-active-eraser.png rename to assets/menu icons/inactive-eraser.png diff --git a/dist/main.js b/dist/main.js index d94a22a..11b38c6 100644 --- a/dist/main.js +++ b/dist/main.js @@ -1,2 +1,2 @@ -!function(){var t={612:function(t){var e;self,e=()=>(()=>{"use strict";var t={d:(e,n)=>{for(var s in n)t.o(n,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:n[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.d(e,{default:()=>S});var n={};function s(t,e,n,s,o={}){e instanceof HTMLCollection||e instanceof NodeList?e=Array.from(e):Array.isArray(e)||(e=[e]),Array.isArray(n)||(n=[n]);for(const i of e)for(const e of n)i[t](e,s,{capture:!1,...o});return Array.prototype.slice.call(arguments,1)}t.r(n),t.d(n,{adjustableInputNumbers:()=>h,createElementFromString:()=>r,createFromTemplate:()=>a,eventPath:()=>c,off:()=>i,on:()=>o,resolveElement:()=>l});const o=s.bind(null,"addEventListener"),i=s.bind(null,"removeEventListener");function r(t){const e=document.createElement("div");return e.innerHTML=t.trim(),e.firstElementChild}function a(t){const e=(t,e)=>{const n=t.getAttribute(e);return t.removeAttribute(e),n},n=(t,s={})=>{const o=e(t,":obj"),i=e(t,":ref"),r=o?s[o]={}:s;i&&(s[i]=t);for(const s of Array.from(t.children)){const t=e(s,":arr"),o=n(s,t?{}:r);t&&(r[t]||(r[t]=[])).push(Object.keys(o).length?o:s)}return s};return n(r(t))}function c(t){let e=t.path||t.composedPath&&t.composedPath();if(e)return e;let n=t.target.parentElement;for(e=[t.target,n];n=n.parentElement;)e.push(n);return e.push(document,window),e}function l(t){return t instanceof Element?t:"string"==typeof t?t.split(/>>/g).reduce(((t,e,n,s)=>(t=t.querySelector(e),nt)){function n(n){const s=[.001,.01,.1][Number(n.shiftKey||2*n.ctrlKey)]*(n.deltaY<0?1:-1);let o=0,i=t.selectionStart;t.value=t.value.replace(/[\d.]+/g,((t,n)=>n<=i&&n+t.length>=i?(i=n,e(Number(t),s,o)):(o++,t))),t.focus(),t.setSelectionRange(i,i),n.preventDefault(),t.dispatchEvent(new Event("input"))}o(t,"focus",(()=>o(window,"wheel",n,{passive:!1}))),o(t,"blur",(()=>i(window,"wheel",n)))}const{min:d,max:p,floor:u,round:v}=Math;function m(t,e,n){e/=100,n/=100;const s=u(t=t/360*6),o=t-s,i=n*(1-e),r=n*(1-o*e),a=n*(1-(1-o)*e),c=s%6;return[255*[n,r,i,i,a,n][c],255*[a,n,n,r,i,i][c],255*[i,i,a,n,n,r][c]]}function g(t,e,n){const s=(2-(e/=100))*(n/=100)/2;return 0!==s&&(e=1===s?0:s<.5?e*n/(2*s):e*n/(2-2*s)),[t,100*e,100*s]}function b(t,e,n){const s=d(t/=255,e/=255,n/=255),o=p(t,e,n),i=o-s;let r,a;if(0===i)r=a=0;else{a=i/o;const s=((o-t)/6+i/2)/i,c=((o-e)/6+i/2)/i,l=((o-n)/6+i/2)/i;t===o?r=l-c:e===o?r=1/3+s-l:n===o&&(r=2/3+c-s),r<0?r+=1:r>1&&(r-=1)}return[360*r,100*a,100*o]}function f(t,e,n,s){return e/=100,n/=100,[...b(255*(1-d(1,(t/=100)*(1-(s/=100))+s)),255*(1-d(1,e*(1-s)+s)),255*(1-d(1,n*(1-s)+s)))]}function y(t,e,n){e/=100;const s=2*(e*=(n/=100)<.5?n:1-n)/(n+e)*100,o=100*(n+e);return[t,isNaN(s)?0:s,o]}function w(t){return b(...t.match(/.{2}/g).map((t=>parseInt(t,16))))}function _(t=0,e=0,n=0,s=1){const o=(t,e)=>(n=-1)=>e(~n?t.map((t=>Number(t.toFixed(n)))):t),i={h:t,s:e,v:n,a:s,toHSVA(){const t=[i.h,i.s,i.v,i.a];return t.toString=o(t,(t=>`hsva(${t[0]}, ${t[1]}%, ${t[2]}%, ${i.a})`)),t},toHSLA(){const t=[...g(i.h,i.s,i.v),i.a];return t.toString=o(t,(t=>`hsla(${t[0]}, ${t[1]}%, ${t[2]}%, ${i.a})`)),t},toRGBA(){const t=[...m(i.h,i.s,i.v),i.a];return t.toString=o(t,(t=>`rgba(${t[0]}, ${t[1]}, ${t[2]}, ${i.a})`)),t},toCMYK(){const t=function(t,e,n){const s=m(t,e,n),o=s[0]/255,i=s[1]/255,r=s[2]/255,a=d(1-o,1-i,1-r);return[100*(1===a?0:(1-o-a)/(1-a)),100*(1===a?0:(1-i-a)/(1-a)),100*(1===a?0:(1-r-a)/(1-a)),100*a]}(i.h,i.s,i.v);return t.toString=o(t,(t=>`cmyk(${t[0]}%, ${t[1]}%, ${t[2]}%, ${t[3]}%)`)),t},toHEXA(){const t=function(t,e,n){return m(t,e,n).map((t=>v(t).toString(16).padStart(2,"0")))}(i.h,i.s,i.v),e=i.a>=1?"":Number((255*i.a).toFixed(0)).toString(16).toUpperCase().padStart(2,"0");return e&&t.push(e),t.toString=()=>`#${t.join("").toUpperCase()}`,t},clone:()=>_(i.h,i.s,i.v,i.a)};return i}const C=t=>Math.max(Math.min(t,1),0);function H(t){const e={options:Object.assign({lock:null,onchange:()=>0,onstop:()=>0},t),_keyboard(t){const{options:n}=e,{type:s,key:o}=t;if(document.activeElement===n.wrapper){const{lock:n}=e.options,i="ArrowUp"===o,r="ArrowRight"===o,a="ArrowDown"===o,c="ArrowLeft"===o;if("keydown"===s&&(i||r||a||c)){let s=0,o=0;"v"===n?s=i||r?1:-1:"h"===n?s=i||r?-1:1:(o=i?-1:a?1:0,s=c?-1:r?1:0),e.update(C(e.cache.x+.01*s),C(e.cache.y+.01*o)),t.preventDefault()}else o.startsWith("Arrow")&&(e.options.onstop(),t.preventDefault())}},_tapstart(t){o(document,["mouseup","touchend","touchcancel"],e._tapstop),o(document,["mousemove","touchmove"],e._tapmove),t.cancelable&&t.preventDefault(),e._tapmove(t)},_tapmove(t){const{options:n,cache:s}=e,{lock:o,element:i,wrapper:r}=n,a=r.getBoundingClientRect();let c=0,l=0;if(t){const e=t&&t.touches&&t.touches[0];c=t?(e||t).clientX:0,l=t?(e||t).clientY:0,ca.left+a.width&&(c=a.left+a.width),la.top+a.height&&(l=a.top+a.height),c-=a.left,l-=a.top}else s&&(c=s.x*a.width,l=s.y*a.height);"h"!==o&&(i.style.left=`calc(${c/a.width*100}% - ${i.offsetWidth/2}px)`),"v"!==o&&(i.style.top=`calc(${l/a.height*100}% - ${i.offsetHeight/2}px)`),e.cache={x:c/a.width,y:l/a.height};const h=C(c/a.width),d=C(l/a.height);switch(o){case"v":return n.onchange(h);case"h":return n.onchange(d);default:return n.onchange(h,d)}},_tapstop(){e.options.onstop(),i(document,["mouseup","touchend","touchcancel"],e._tapstop),i(document,["mousemove","touchmove"],e._tapmove)},trigger(){e._tapmove()},update(t=0,n=0){const{left:s,top:o,width:i,height:r}=e.options.wrapper.getBoundingClientRect();"h"===e.options.lock&&(n=t),e._tapmove({clientX:s+i*t,clientY:o+r*n})},destroy(){const{options:t,_tapstart:n,_keyboard:s}=e;i(document,["keydown","keyup"],s),i([t.wrapper,t.element],"mousedown",n),i([t.wrapper,t.element],"touchstart",n,{passive:!1})}},{options:n,_tapstart:s,_keyboard:r}=e;return o([n.wrapper,n.element],"mousedown",s),o([n.wrapper,n.element],"touchstart",s,{passive:!1}),o(document,["keydown","keyup"],r),e}function k(t={}){t=Object.assign({onchange:()=>0,className:"",elements:[]},t);const e=o(t.elements,"click",(e=>{t.elements.forEach((n=>n.classList[e.target===n?"add":"remove"](t.className))),t.onchange(e),e.stopPropagation()}));return{destroy:()=>i(...e)}}const A={variantFlipOrder:{start:"sme",middle:"mse",end:"ems"},positionFlipOrder:{top:"tbrl",right:"rltb",bottom:"btrl",left:"lrbt"},position:"bottom",margin:8,padding:0};class S{static utils=n;static version="1.9.0";static I18N_DEFAULTS={"ui:dialog":"color picker dialog","btn:toggle":"toggle color picker dialog","btn:swatch":"color swatch","btn:last-color":"use previous color","btn:save":"Save","btn:cancel":"Cancel","btn:clear":"Clear","aria:btn:save":"save and close","aria:btn:cancel":"cancel and close","aria:btn:clear":"clear and close","aria:input":"color input field","aria:palette":"color selection area","aria:hue":"hue selection slider","aria:opacity":"selection slider"};static DEFAULT_OPTIONS={appClass:null,theme:"classic",useAsButton:!1,padding:8,disabled:!1,comparison:!0,closeOnScroll:!1,outputPrecision:0,lockOpacity:!1,autoReposition:!0,container:"body",components:{interaction:{}},i18n:{},swatches:null,inline:!1,sliders:null,default:"#42445a",defaultRepresentation:null,position:"bottom-middle",adjustableNumbers:!0,showAlways:!1,closeWithKey:"Escape"};_initializingActive=!0;_recalc=!0;_nanopop=null;_root=null;_color=_();_lastColor=_();_swatchColors=[];_setupAnimationFrame=null;_eventListener={init:[],save:[],hide:[],show:[],clear:[],change:[],changestop:[],cancel:[],swatchselect:[]};constructor(t){this.options=t=Object.assign({...S.DEFAULT_OPTIONS},t);const{swatches:e,components:n,theme:s,sliders:o,lockOpacity:i,padding:r}=t;["nano","monolith"].includes(s)&&!o&&(t.sliders="h"),n.interaction||(n.interaction={});const{preview:a,opacity:c,hue:l,palette:h}=n;n.opacity=!i&&c,n.palette=h||a||c||l,this._preBuild(),this._buildComponents(),this._bindEvents(),this._finalBuild(),e&&e.length&&e.forEach((t=>this.addSwatch(t)));const{button:d,app:p}=this._root;this._nanopop=((t,e,n)=>{const s="object"!=typeof t||t instanceof HTMLElement?{reference:t,popper:e,...n}:t;return{update(t=s){const{reference:e,popper:n}=Object.assign(s,t);if(!n||!e)throw new Error("Popper- or reference-element missing.");return((t,e,n)=>{const{container:s,arrow:o,margin:i,padding:r,position:a,variantFlipOrder:c,positionFlipOrder:l}={container:document.documentElement.getBoundingClientRect(),...A,...n},{left:h,top:d}=e.style;e.style.left="0",e.style.top="0";const p=t.getBoundingClientRect(),u=e.getBoundingClientRect(),v={t:p.top-u.height-i,b:p.bottom+i,r:p.right+i,l:p.left-u.width-i},m={vs:p.left,vm:p.left+p.width/2-u.width/2,ve:p.left+p.width-u.width,hs:p.top,hm:p.bottom-p.height/2-u.height/2,he:p.bottom-u.height},[g,b="middle"]=a.split("-"),f=l[g],y=c[b],{top:w,left:_,bottom:C,right:H}=s;for(const t of f){const n="t"===t||"b"===t;let s=v[t];const[i,a]=n?["top","left"]:["left","top"],[c,l]=n?[u.height,u.width]:[u.width,u.height],[h,d]=n?[C,H]:[H,C],[g,b]=n?[w,_]:[_,w];if(!(sh))for(const h of y){let v=m[(n?"v":"h")+h];if(!(vd)){if(v-=u[a],s-=u[i],e.style[a]=`${v}px`,e.style[i]=`${s}px`,o){const t=n?p.width/2:p.height/2,e=2*tnew S(t);_preBuild(){const{options:t}=this;for(const e of["el","container"])t[e]=l(t[e]);this._root=(t=>{const{components:e,useAsButton:n,inline:s,appClass:o,theme:i,lockOpacity:r}=t.options,c=t=>t?"":'style="display:none" hidden',l=e=>t._t(e),h=a(`\n
\n\n ${n?"":''}\n\n
\n
\n
\n \n
\n
\n\n
\n
\n
\n
\n\n
\n
\n
\n
\n\n
\n
\n
\n
\n
\n\n
\n\n
\n \n\n \n \n \n \n \n\n \n \n \n
\n
\n
\n `),d=h.interaction;return d.options.find((t=>!t.hidden&&!t.classList.add("active"))),d.type=()=>d.options.find((t=>t.classList.contains("active"))),h})(this),t.useAsButton&&(this._root.button=t.el),t.container.appendChild(this._root.root)}_finalBuild(){const t=this.options,e=this._root;if(t.container.removeChild(e.root),t.inline){const n=t.el.parentElement;t.el.nextSibling?n.insertBefore(e.app,t.el.nextSibling):n.appendChild(e.app)}else t.container.appendChild(e.app);t.useAsButton?t.inline&&t.el.remove():t.el.parentNode.replaceChild(e.root,t.el),t.disabled&&this.disable(),t.comparison||(e.button.style.transition="none",t.useAsButton||(e.preview.lastColor.style.transition="none")),this.hide()}_buildComponents(){const t=this,e=this.options.components,n=(t.options.sliders||"v").repeat(2),[s,o]=n.match(/^[vh]+$/g)?n:[],i=()=>this._color||(this._color=this._lastColor.clone()),r={palette:H({element:t._root.palette.picker,wrapper:t._root.palette.palette,onstop:()=>t._emit("changestop","slider",t),onchange(n,s){if(!e.palette)return;const o=i(),{_root:r,options:a}=t,{lastColor:c,currentColor:l}=r.preview;t._recalc&&(o.s=100*n,o.v=100-100*s,o.v<0&&(o.v=0),t._updateOutput("slider"));const h=o.toRGBA().toString(0);this.element.style.background=h,this.wrapper.style.background=`\n linear-gradient(to top, rgba(0, 0, 0, ${o.a}), transparent),\n linear-gradient(to left, hsla(${o.h}, 100%, 50%, ${o.a}), rgba(255, 255, 255, ${o.a}))\n `,a.comparison?a.useAsButton||t._lastColor||c.style.setProperty("--pcr-color",h):(r.button.style.setProperty("--pcr-color",h),r.button.classList.remove("clear"));const d=o.toHEXA().toString();for(const{el:e,color:n}of t._swatchColors)e.classList[d===n.toHEXA().toString()?"add":"remove"]("pcr-active");l.style.setProperty("--pcr-color",h)}}),hue:H({lock:"v"===o?"h":"v",element:t._root.hue.picker,wrapper:t._root.hue.slider,onstop:()=>t._emit("changestop","slider",t),onchange(n){if(!e.hue||!e.palette)return;const s=i();t._recalc&&(s.h=360*n),this.element.style.backgroundColor=`hsl(${s.h}, 100%, 50%)`,r.palette.trigger()}}),opacity:H({lock:"v"===s?"h":"v",element:t._root.opacity.picker,wrapper:t._root.opacity.slider,onstop:()=>t._emit("changestop","slider",t),onchange(n){if(!e.opacity||!e.palette)return;const s=i();t._recalc&&(s.a=Math.round(100*n)/100),this.element.style.background=`rgba(0, 0, 0, ${s.a})`,r.palette.trigger()}}),selectable:k({elements:t._root.interaction.options,className:"active",onchange(e){t._representation=e.target.getAttribute("data-type").toUpperCase(),t._recalc&&t._updateOutput("swatch")}})};this._components=r}_bindEvents(){const{_root:t,options:e}=this,n=[o(t.interaction.clear,"click",(()=>this._clearColor())),o([t.interaction.cancel,t.preview.lastColor],"click",(()=>{this.setHSVA(...(this._lastColor||this._color).toHSVA(),!0),this._emit("cancel")})),o(t.interaction.save,"click",(()=>{!this.applyColor()&&!e.showAlways&&this.hide()})),o(t.interaction.result,["keyup","input"],(t=>{this.setColor(t.target.value,!0)&&!this._initializingActive&&(this._emit("change",this._color,"input",this),this._emit("changestop","input",this)),t.stopImmediatePropagation()})),o(t.interaction.result,["focus","blur"],(t=>{this._recalc="blur"===t.type,this._recalc&&this._updateOutput(null)})),o([t.palette.palette,t.palette.picker,t.hue.slider,t.hue.picker,t.opacity.slider,t.opacity.picker],["mousedown","touchstart"],(()=>this._recalc=!0),{passive:!0})];if(!e.showAlways){const s=e.closeWithKey;n.push(o(t.button,"click",(()=>this.isOpen()?this.hide():this.show())),o(document,"keyup",(t=>this.isOpen()&&(t.key===s||t.code===s)&&this.hide())),o(document,["touchstart","mousedown"],(e=>{this.isOpen()&&!c(e).some((e=>e===t.app||e===t.button))&&this.hide()}),{capture:!0}))}if(e.adjustableNumbers){const e={rgba:[255,255,255,1],hsva:[360,100,100,1],hsla:[360,100,100,1],cmyk:[100,100,100,100]};h(t.interaction.result,((t,n,s)=>{const o=e[this.getColorRepresentation().toLowerCase()];if(o){const e=o[s],i=t+(e>=100?1e3*n:n);return i<=0?0:Number((i{s.isOpen()&&(e.closeOnScroll&&s.hide(),null===t?(t=setTimeout((()=>t=null),100),requestAnimationFrame((function e(){s._rePositioningPicker(),null!==t&&requestAnimationFrame(e)}))):(clearTimeout(t),t=setTimeout((()=>t=null),100)))}),{capture:!0}))}this._eventBindings=n}_rePositioningPicker(){const{options:t}=this;if(!t.inline&&!this._nanopop.update({container:document.body.getBoundingClientRect(),position:t.position})){const t=this._root.app,e=t.getBoundingClientRect();t.style.top=(window.innerHeight-e.height)/2+"px",t.style.left=(window.innerWidth-e.width)/2+"px"}}_updateOutput(t){const{_root:e,_color:n,options:s}=this;if(e.interaction.type()){const t=`to${e.interaction.type().getAttribute("data-type")}`;e.interaction.result.value="function"==typeof n[t]?n[t]().toString(s.outputPrecision):""}!this._initializingActive&&this._recalc&&this._emit("change",n,t,this)}_clearColor(t=!1){const{_root:e,options:n}=this;n.useAsButton||e.button.style.setProperty("--pcr-color","rgba(0, 0, 0, 0.15)"),e.button.classList.add("clear"),n.showAlways||this.hide(),this._lastColor=null,this._initializingActive||t||(this._emit("save",null),this._emit("clear"))}_parseLocalColor(t){const{values:e,type:n,a:s}=function(t){t=t.match(/^[a-zA-Z]+$/)?function(t){if("black"===t.toLowerCase())return"#000";const e=document.createElement("canvas").getContext("2d");return e.fillStyle=t,"#000"===e.fillStyle?null:e.fillStyle}(t):t;const e={cmyk:/^cmyk\D+([\d.]+)\D+([\d.]+)\D+([\d.]+)\D+([\d.]+)/i,rgba:/^rgba?\D+([\d.]+)(%?)\D+([\d.]+)(%?)\D+([\d.]+)(%?)\D*?(([\d.]+)(%?)|$)/i,hsla:/^hsla?\D+([\d.]+)\D+([\d.]+)\D+([\d.]+)\D*?(([\d.]+)(%?)|$)/i,hsva:/^hsva?\D+([\d.]+)\D+([\d.]+)\D+([\d.]+)\D*?(([\d.]+)(%?)|$)/i,hexa:/^#?(([\dA-Fa-f]{3,4})|([\dA-Fa-f]{6})|([\dA-Fa-f]{8}))$/i},n=t=>t.map((t=>/^(|\d+)\.\d+|\d+$/.test(t)?Number(t):void 0));let s;t:for(const o in e)if(s=e[o].exec(t))switch(o){case"cmyk":{const[,t,e,i,r]=n(s);if(t>100||e>100||i>100||r>100)break t;return{values:f(t,e,i,r),type:o}}case"rgba":{let[,t,,e,,i,,,r]=n(s);if(t="%"===s[2]?t/100*255:t,e="%"===s[4]?e/100*255:e,i="%"===s[6]?i/100*255:i,r="%"===s[9]?r/100:r,t>255||e>255||i>255||r<0||r>1)break t;return{values:[...b(t,e,i),r],a:r,type:o}}case"hexa":{let[,t]=s;4!==t.length&&3!==t.length||(t=t.split("").map((t=>t+t)).join(""));const e=t.substring(0,6);let n=t.substring(6);return n=n?parseInt(n,16)/255:void 0,{values:[...w(e),n],a:n,type:o}}case"hsla":{let[,t,e,i,,r]=n(s);if(r="%"===s[6]?r/100:r,t>360||e>100||i>100||r<0||r>1)break t;return{values:[...y(t,e,i),r],a:r,type:o}}case"hsva":{let[,t,e,i,,r]=n(s);if(r="%"===s[6]?r/100:r,t>360||e>100||i>100||r<0||r>1)break t;return{values:[t,e,i,r],a:r,type:o}}}return{values:null,type:null}}(t),{lockOpacity:o}=this.options,i=void 0!==s&&1!==s;return e&&3===e.length&&(e[3]=void 0),{values:!e||o&&i?null:e,type:n}}_t(t){return this.options.i18n[t]||S.I18N_DEFAULTS[t]}_emit(t,...e){this._eventListener[t].forEach((t=>t(...e,this)))}on(t,e){return this._eventListener[t].push(e),this}off(t,e){const n=this._eventListener[t]||[],s=n.indexOf(e);return~s&&n.splice(s,1),this}addSwatch(t){const{values:e}=this._parseLocalColor(t);if(e){const{_swatchColors:t,_root:n}=this,s=_(...e),i=r(`'}\n\n
\n
\n
\n \n
\n
\n\n
\n
\n
\n
\n\n
\n
\n
\n
\n\n
\n
\n
\n
\n
\n\n
\n\n
\n \n\n \n \n \n \n \n\n \n \n \n
\n
\n \n `),d=h.interaction;return d.options.find((t=>!t.hidden&&!t.classList.add("active"))),d.type=()=>d.options.find((t=>t.classList.contains("active"))),h})(this),t.useAsButton&&(this._root.button=t.el),t.container.appendChild(this._root.root)}_finalBuild(){const t=this.options,e=this._root;if(t.container.removeChild(e.root),t.inline){const n=t.el.parentElement;t.el.nextSibling?n.insertBefore(e.app,t.el.nextSibling):n.appendChild(e.app)}else t.container.appendChild(e.app);t.useAsButton?t.inline&&t.el.remove():t.el.parentNode.replaceChild(e.root,t.el),t.disabled&&this.disable(),t.comparison||(e.button.style.transition="none",t.useAsButton||(e.preview.lastColor.style.transition="none")),this.hide()}_buildComponents(){const t=this,e=this.options.components,n=(t.options.sliders||"v").repeat(2),[s,o]=n.match(/^[vh]+$/g)?n:[],r=()=>this._color||(this._color=this._lastColor.clone()),i={palette:H({element:t._root.palette.picker,wrapper:t._root.palette.palette,onstop:()=>t._emit("changestop","slider",t),onchange(n,s){if(!e.palette)return;const o=r(),{_root:i,options:a}=t,{lastColor:c,currentColor:l}=i.preview;t._recalc&&(o.s=100*n,o.v=100-100*s,o.v<0&&(o.v=0),t._updateOutput("slider"));const h=o.toRGBA().toString(0);this.element.style.background=h,this.wrapper.style.background=`\n linear-gradient(to top, rgba(0, 0, 0, ${o.a}), transparent),\n linear-gradient(to left, hsla(${o.h}, 100%, 50%, ${o.a}), rgba(255, 255, 255, ${o.a}))\n `,a.comparison?a.useAsButton||t._lastColor||c.style.setProperty("--pcr-color",h):(i.button.style.setProperty("--pcr-color",h),i.button.classList.remove("clear"));const d=o.toHEXA().toString();for(const{el:e,color:n}of t._swatchColors)e.classList[d===n.toHEXA().toString()?"add":"remove"]("pcr-active");l.style.setProperty("--pcr-color",h)}}),hue:H({lock:"v"===o?"h":"v",element:t._root.hue.picker,wrapper:t._root.hue.slider,onstop:()=>t._emit("changestop","slider",t),onchange(n){if(!e.hue||!e.palette)return;const s=r();t._recalc&&(s.h=360*n),this.element.style.backgroundColor=`hsl(${s.h}, 100%, 50%)`,i.palette.trigger()}}),opacity:H({lock:"v"===s?"h":"v",element:t._root.opacity.picker,wrapper:t._root.opacity.slider,onstop:()=>t._emit("changestop","slider",t),onchange(n){if(!e.opacity||!e.palette)return;const s=r();t._recalc&&(s.a=Math.round(100*n)/100),this.element.style.background=`rgba(0, 0, 0, ${s.a})`,i.palette.trigger()}}),selectable:k({elements:t._root.interaction.options,className:"active",onchange(e){t._representation=e.target.getAttribute("data-type").toUpperCase(),t._recalc&&t._updateOutput("swatch")}})};this._components=i}_bindEvents(){const{_root:t,options:e}=this,n=[o(t.interaction.clear,"click",(()=>this._clearColor())),o([t.interaction.cancel,t.preview.lastColor],"click",(()=>{this.setHSVA(...(this._lastColor||this._color).toHSVA(),!0),this._emit("cancel")})),o(t.interaction.save,"click",(()=>{!this.applyColor()&&!e.showAlways&&this.hide()})),o(t.interaction.result,["keyup","input"],(t=>{this.setColor(t.target.value,!0)&&!this._initializingActive&&(this._emit("change",this._color,"input",this),this._emit("changestop","input",this)),t.stopImmediatePropagation()})),o(t.interaction.result,["focus","blur"],(t=>{this._recalc="blur"===t.type,this._recalc&&this._updateOutput(null)})),o([t.palette.palette,t.palette.picker,t.hue.slider,t.hue.picker,t.opacity.slider,t.opacity.picker],["mousedown","touchstart"],(()=>this._recalc=!0),{passive:!0})];if(!e.showAlways){const s=e.closeWithKey;n.push(o(t.button,"click",(()=>this.isOpen()?this.hide():this.show())),o(document,"keyup",(t=>this.isOpen()&&(t.key===s||t.code===s)&&this.hide())),o(document,["touchstart","mousedown"],(e=>{this.isOpen()&&!c(e).some((e=>e===t.app||e===t.button))&&this.hide()}),{capture:!0}))}if(e.adjustableNumbers){const e={rgba:[255,255,255,1],hsva:[360,100,100,1],hsla:[360,100,100,1],cmyk:[100,100,100,100]};h(t.interaction.result,((t,n,s)=>{const o=e[this.getColorRepresentation().toLowerCase()];if(o){const e=o[s],r=t+(e>=100?1e3*n:n);return r<=0?0:Number((r{s.isOpen()&&(e.closeOnScroll&&s.hide(),null===t?(t=setTimeout((()=>t=null),100),requestAnimationFrame((function e(){s._rePositioningPicker(),null!==t&&requestAnimationFrame(e)}))):(clearTimeout(t),t=setTimeout((()=>t=null),100)))}),{capture:!0}))}this._eventBindings=n}_rePositioningPicker(){const{options:t}=this;if(!t.inline&&!this._nanopop.update({container:document.body.getBoundingClientRect(),position:t.position})){const t=this._root.app,e=t.getBoundingClientRect();t.style.top=(window.innerHeight-e.height)/2+"px",t.style.left=(window.innerWidth-e.width)/2+"px"}}_updateOutput(t){const{_root:e,_color:n,options:s}=this;if(e.interaction.type()){const t=`to${e.interaction.type().getAttribute("data-type")}`;e.interaction.result.value="function"==typeof n[t]?n[t]().toString(s.outputPrecision):""}!this._initializingActive&&this._recalc&&this._emit("change",n,t,this)}_clearColor(t=!1){const{_root:e,options:n}=this;n.useAsButton||e.button.style.setProperty("--pcr-color","rgba(0, 0, 0, 0.15)"),e.button.classList.add("clear"),n.showAlways||this.hide(),this._lastColor=null,this._initializingActive||t||(this._emit("save",null),this._emit("clear"))}_parseLocalColor(t){const{values:e,type:n,a:s}=function(t){t=t.match(/^[a-zA-Z]+$/)?function(t){if("black"===t.toLowerCase())return"#000";const e=document.createElement("canvas").getContext("2d");return e.fillStyle=t,"#000"===e.fillStyle?null:e.fillStyle}(t):t;const e={cmyk:/^cmyk\D+([\d.]+)\D+([\d.]+)\D+([\d.]+)\D+([\d.]+)/i,rgba:/^rgba?\D+([\d.]+)(%?)\D+([\d.]+)(%?)\D+([\d.]+)(%?)\D*?(([\d.]+)(%?)|$)/i,hsla:/^hsla?\D+([\d.]+)\D+([\d.]+)\D+([\d.]+)\D*?(([\d.]+)(%?)|$)/i,hsva:/^hsva?\D+([\d.]+)\D+([\d.]+)\D+([\d.]+)\D*?(([\d.]+)(%?)|$)/i,hexa:/^#?(([\dA-Fa-f]{3,4})|([\dA-Fa-f]{6})|([\dA-Fa-f]{8}))$/i},n=t=>t.map((t=>/^(|\d+)\.\d+|\d+$/.test(t)?Number(t):void 0));let s;t:for(const o in e)if(s=e[o].exec(t))switch(o){case"cmyk":{const[,t,e,r,i]=n(s);if(t>100||e>100||r>100||i>100)break t;return{values:f(t,e,r,i),type:o}}case"rgba":{let[,t,,e,,r,,,i]=n(s);if(t="%"===s[2]?t/100*255:t,e="%"===s[4]?e/100*255:e,r="%"===s[6]?r/100*255:r,i="%"===s[9]?i/100:i,t>255||e>255||r>255||i<0||i>1)break t;return{values:[...b(t,e,r),i],a:i,type:o}}case"hexa":{let[,t]=s;4!==t.length&&3!==t.length||(t=t.split("").map((t=>t+t)).join(""));const e=t.substring(0,6);let n=t.substring(6);return n=n?parseInt(n,16)/255:void 0,{values:[...w(e),n],a:n,type:o}}case"hsla":{let[,t,e,r,,i]=n(s);if(i="%"===s[6]?i/100:i,t>360||e>100||r>100||i<0||i>1)break t;return{values:[...y(t,e,r),i],a:i,type:o}}case"hsva":{let[,t,e,r,,i]=n(s);if(i="%"===s[6]?i/100:i,t>360||e>100||r>100||i<0||i>1)break t;return{values:[t,e,r,i],a:i,type:o}}}return{values:null,type:null}}(t),{lockOpacity:o}=this.options,r=void 0!==s&&1!==s;return e&&3===e.length&&(e[3]=void 0),{values:!e||o&&r?null:e,type:n}}_t(t){return this.options.i18n[t]||A.I18N_DEFAULTS[t]}_emit(t,...e){this._eventListener[t].forEach((t=>t(...e,this)))}on(t,e){return this._eventListener[t].push(e),this}off(t,e){const n=this._eventListener[t]||[],s=n.indexOf(e);return~s&&n.splice(s,1),this}addSwatch(t){const{values:e}=this._parseLocalColor(t);if(e){const{_swatchColors:t,_root:n}=this,s=_(...e),r=i(`'}\\n\\n
\\n
\\n
\\n \\n
\\n
\\n\\n
\\n
\\n
\\n
\\n\\n
\\n
\\n
\\n
\\n\\n
\\n
\\n
\\n
\\n
\\n\\n
\\n\\n
\\n \\n\\n \\n \\n \\n \\n \\n\\n \\n \\n \\n
\\n
\\n \\n `),u=p.interaction;return u.options.find((t=>!t.hidden&&!t.classList.add(\"active\"))),u.type=()=>u.options.find((t=>t.classList.contains(\"active\"))),p})(this),t.useAsButton&&(this._root.button=t.el),t.container.appendChild(this._root.root)}_finalBuild(){const t=this.options,e=this._root;if(t.container.removeChild(e.root),t.inline){const o=t.el.parentElement;t.el.nextSibling?o.insertBefore(e.app,t.el.nextSibling):o.appendChild(e.app)}else t.container.appendChild(e.app);t.useAsButton?t.inline&&t.el.remove():t.el.parentNode.replaceChild(e.root,t.el),t.disabled&&this.disable(),t.comparison||(e.button.style.transition=\"none\",t.useAsButton||(e.preview.lastColor.style.transition=\"none\")),this.hide()}_buildComponents(){const t=this,e=this.options.components,o=(t.options.sliders||\"v\").repeat(2),[n,i]=o.match(/^[vh]+$/g)?o:[],s=()=>this._color||(this._color=this._lastColor.clone()),r={palette:C({element:t._root.palette.picker,wrapper:t._root.palette.palette,onstop:()=>t._emit(\"changestop\",\"slider\",t),onchange(o,n){if(!e.palette)return;const i=s(),{_root:r,options:a}=t,{lastColor:l,currentColor:c}=r.preview;t._recalc&&(i.s=100*o,i.v=100-100*n,i.v<0&&(i.v=0),t._updateOutput(\"slider\"));const p=i.toRGBA().toString(0);this.element.style.background=p,this.wrapper.style.background=`\\n linear-gradient(to top, rgba(0, 0, 0, ${i.a}), transparent),\\n linear-gradient(to left, hsla(${i.h}, 100%, 50%, ${i.a}), rgba(255, 255, 255, ${i.a}))\\n `,a.comparison?a.useAsButton||t._lastColor||l.style.setProperty(\"--pcr-color\",p):(r.button.style.setProperty(\"--pcr-color\",p),r.button.classList.remove(\"clear\"));const u=i.toHEXA().toString();for(const{el:e,color:o}of t._swatchColors)e.classList[u===o.toHEXA().toString()?\"add\":\"remove\"](\"pcr-active\");c.style.setProperty(\"--pcr-color\",p)}}),hue:C({lock:\"v\"===i?\"h\":\"v\",element:t._root.hue.picker,wrapper:t._root.hue.slider,onstop:()=>t._emit(\"changestop\",\"slider\",t),onchange(o){if(!e.hue||!e.palette)return;const n=s();t._recalc&&(n.h=360*o),this.element.style.backgroundColor=`hsl(${n.h}, 100%, 50%)`,r.palette.trigger()}}),opacity:C({lock:\"v\"===n?\"h\":\"v\",element:t._root.opacity.picker,wrapper:t._root.opacity.slider,onstop:()=>t._emit(\"changestop\",\"slider\",t),onchange(o){if(!e.opacity||!e.palette)return;const n=s();t._recalc&&(n.a=Math.round(100*o)/100),this.element.style.background=`rgba(0, 0, 0, ${n.a})`,r.palette.trigger()}}),selectable:k({elements:t._root.interaction.options,className:\"active\",onchange(e){t._representation=e.target.getAttribute(\"data-type\").toUpperCase(),t._recalc&&t._updateOutput(\"swatch\")}})};this._components=r}_bindEvents(){const{_root:t,options:e}=this,o=[i(t.interaction.clear,\"click\",(()=>this._clearColor())),i([t.interaction.cancel,t.preview.lastColor],\"click\",(()=>{this.setHSVA(...(this._lastColor||this._color).toHSVA(),!0),this._emit(\"cancel\")})),i(t.interaction.save,\"click\",(()=>{!this.applyColor()&&!e.showAlways&&this.hide()})),i(t.interaction.result,[\"keyup\",\"input\"],(t=>{this.setColor(t.target.value,!0)&&!this._initializingActive&&(this._emit(\"change\",this._color,\"input\",this),this._emit(\"changestop\",\"input\",this)),t.stopImmediatePropagation()})),i(t.interaction.result,[\"focus\",\"blur\"],(t=>{this._recalc=\"blur\"===t.type,this._recalc&&this._updateOutput(null)})),i([t.palette.palette,t.palette.picker,t.hue.slider,t.hue.picker,t.opacity.slider,t.opacity.picker],[\"mousedown\",\"touchstart\"],(()=>this._recalc=!0),{passive:!0})];if(!e.showAlways){const n=e.closeWithKey;o.push(i(t.button,\"click\",(()=>this.isOpen()?this.hide():this.show())),i(document,\"keyup\",(t=>this.isOpen()&&(t.key===n||t.code===n)&&this.hide())),i(document,[\"touchstart\",\"mousedown\"],(e=>{this.isOpen()&&!l(e).some((e=>e===t.app||e===t.button))&&this.hide()}),{capture:!0}))}if(e.adjustableNumbers){const e={rgba:[255,255,255,1],hsva:[360,100,100,1],hsla:[360,100,100,1],cmyk:[100,100,100,100]};p(t.interaction.result,((t,o,n)=>{const i=e[this.getColorRepresentation().toLowerCase()];if(i){const e=i[n],s=t+(e>=100?1e3*o:o);return s<=0?0:Number((s{n.isOpen()&&(e.closeOnScroll&&n.hide(),null===t?(t=setTimeout((()=>t=null),100),requestAnimationFrame((function e(){n._rePositioningPicker(),null!==t&&requestAnimationFrame(e)}))):(clearTimeout(t),t=setTimeout((()=>t=null),100)))}),{capture:!0}))}this._eventBindings=o}_rePositioningPicker(){const{options:t}=this;if(!t.inline){if(!this._nanopop.update({container:document.body.getBoundingClientRect(),position:t.position})){const t=this._root.app,e=t.getBoundingClientRect();t.style.top=(window.innerHeight-e.height)/2+\"px\",t.style.left=(window.innerWidth-e.width)/2+\"px\"}}}_updateOutput(t){const{_root:e,_color:o,options:n}=this;if(e.interaction.type()){const t=`to${e.interaction.type().getAttribute(\"data-type\")}`;e.interaction.result.value=\"function\"==typeof o[t]?o[t]().toString(n.outputPrecision):\"\"}!this._initializingActive&&this._recalc&&this._emit(\"change\",o,t,this)}_clearColor(t=!1){const{_root:e,options:o}=this;o.useAsButton||e.button.style.setProperty(\"--pcr-color\",\"rgba(0, 0, 0, 0.15)\"),e.button.classList.add(\"clear\"),o.showAlways||this.hide(),this._lastColor=null,this._initializingActive||t||(this._emit(\"save\",null),this._emit(\"clear\"))}_parseLocalColor(t){const{values:e,type:o,a:n}=w(t),{lockOpacity:i}=this.options,s=void 0!==n&&1!==n;return e&&3===e.length&&(e[3]=void 0),{values:!e||i&&s?null:e,type:o}}_t(t){return this.options.i18n[t]||E.I18N_DEFAULTS[t]}_emit(t,...e){this._eventListener[t].forEach((t=>t(...e,this)))}on(t,e){return this._eventListener[t].push(e),this}off(t,e){const o=this._eventListener[t]||[],n=o.indexOf(e);return~n&&o.splice(n,1),this}addSwatch(t){const{values:e}=this._parseLocalColor(t);if(e){const{_swatchColors:t,_root:o}=this,n=A(...e),s=r(`'}\\n\\n
\\n
\\n
\\n \\n
\\n
\\n\\n
\\n
\\n
\\n
\\n\\n
\\n
\\n
\\n
\\n\\n
\\n
\\n
\\n
\\n
\\n\\n
\\n\\n
\\n \\n\\n \\n \\n \\n \\n \\n\\n \\n \\n \\n
\\n
\\n \\n `),u=p.interaction;return u.options.find((t=>!t.hidden&&!t.classList.add(\"active\"))),u.type=()=>u.options.find((t=>t.classList.contains(\"active\"))),p})(this),t.useAsButton&&(this._root.button=t.el),t.container.appendChild(this._root.root)}_finalBuild(){const t=this.options,e=this._root;if(t.container.removeChild(e.root),t.inline){const o=t.el.parentElement;t.el.nextSibling?o.insertBefore(e.app,t.el.nextSibling):o.appendChild(e.app)}else t.container.appendChild(e.app);t.useAsButton?t.inline&&t.el.remove():t.el.parentNode.replaceChild(e.root,t.el),t.disabled&&this.disable(),t.comparison||(e.button.style.transition=\"none\",t.useAsButton||(e.preview.lastColor.style.transition=\"none\")),this.hide()}_buildComponents(){const t=this,e=this.options.components,o=(t.options.sliders||\"v\").repeat(2),[n,i]=o.match(/^[vh]+$/g)?o:[],s=()=>this._color||(this._color=this._lastColor.clone()),r={palette:C({element:t._root.palette.picker,wrapper:t._root.palette.palette,onstop:()=>t._emit(\"changestop\",\"slider\",t),onchange(o,n){if(!e.palette)return;const i=s(),{_root:r,options:a}=t,{lastColor:l,currentColor:c}=r.preview;t._recalc&&(i.s=100*o,i.v=100-100*n,i.v<0&&(i.v=0),t._updateOutput(\"slider\"));const p=i.toRGBA().toString(0);this.element.style.background=p,this.wrapper.style.background=`\\n linear-gradient(to top, rgba(0, 0, 0, ${i.a}), transparent),\\n linear-gradient(to left, hsla(${i.h}, 100%, 50%, ${i.a}), rgba(255, 255, 255, ${i.a}))\\n `,a.comparison?a.useAsButton||t._lastColor||l.style.setProperty(\"--pcr-color\",p):(r.button.style.setProperty(\"--pcr-color\",p),r.button.classList.remove(\"clear\"));const u=i.toHEXA().toString();for(const{el:e,color:o}of t._swatchColors)e.classList[u===o.toHEXA().toString()?\"add\":\"remove\"](\"pcr-active\");c.style.setProperty(\"--pcr-color\",p)}}),hue:C({lock:\"v\"===i?\"h\":\"v\",element:t._root.hue.picker,wrapper:t._root.hue.slider,onstop:()=>t._emit(\"changestop\",\"slider\",t),onchange(o){if(!e.hue||!e.palette)return;const n=s();t._recalc&&(n.h=360*o),this.element.style.backgroundColor=`hsl(${n.h}, 100%, 50%)`,r.palette.trigger()}}),opacity:C({lock:\"v\"===n?\"h\":\"v\",element:t._root.opacity.picker,wrapper:t._root.opacity.slider,onstop:()=>t._emit(\"changestop\",\"slider\",t),onchange(o){if(!e.opacity||!e.palette)return;const n=s();t._recalc&&(n.a=Math.round(100*o)/100),this.element.style.background=`rgba(0, 0, 0, ${n.a})`,r.palette.trigger()}}),selectable:k({elements:t._root.interaction.options,className:\"active\",onchange(e){t._representation=e.target.getAttribute(\"data-type\").toUpperCase(),t._recalc&&t._updateOutput(\"swatch\")}})};this._components=r}_bindEvents(){const{_root:t,options:e}=this,o=[i(t.interaction.clear,\"click\",(()=>this._clearColor())),i([t.interaction.cancel,t.preview.lastColor],\"click\",(()=>{this.setHSVA(...(this._lastColor||this._color).toHSVA(),!0),this._emit(\"cancel\")})),i(t.interaction.save,\"click\",(()=>{!this.applyColor()&&!e.showAlways&&this.hide()})),i(t.interaction.result,[\"keyup\",\"input\"],(t=>{this.setColor(t.target.value,!0)&&!this._initializingActive&&(this._emit(\"change\",this._color,\"input\",this),this._emit(\"changestop\",\"input\",this)),t.stopImmediatePropagation()})),i(t.interaction.result,[\"focus\",\"blur\"],(t=>{this._recalc=\"blur\"===t.type,this._recalc&&this._updateOutput(null)})),i([t.palette.palette,t.palette.picker,t.hue.slider,t.hue.picker,t.opacity.slider,t.opacity.picker],[\"mousedown\",\"touchstart\"],(()=>this._recalc=!0),{passive:!0})];if(!e.showAlways){const n=e.closeWithKey;o.push(i(t.button,\"click\",(()=>this.isOpen()?this.hide():this.show())),i(document,\"keyup\",(t=>this.isOpen()&&(t.key===n||t.code===n)&&this.hide())),i(document,[\"touchstart\",\"mousedown\"],(e=>{this.isOpen()&&!l(e).some((e=>e===t.app||e===t.button))&&this.hide()}),{capture:!0}))}if(e.adjustableNumbers){const e={rgba:[255,255,255,1],hsva:[360,100,100,1],hsla:[360,100,100,1],cmyk:[100,100,100,100]};p(t.interaction.result,((t,o,n)=>{const i=e[this.getColorRepresentation().toLowerCase()];if(i){const e=i[n],s=t+(e>=100?1e3*o:o);return s<=0?0:Number((s{n.isOpen()&&(e.closeOnScroll&&n.hide(),null===t?(t=setTimeout((()=>t=null),100),requestAnimationFrame((function e(){n._rePositioningPicker(),null!==t&&requestAnimationFrame(e)}))):(clearTimeout(t),t=setTimeout((()=>t=null),100)))}),{capture:!0}))}this._eventBindings=o}_rePositioningPicker(){const{options:t}=this;if(!t.inline){if(!this._nanopop.update({container:document.body.getBoundingClientRect(),position:t.position})){const t=this._root.app,e=t.getBoundingClientRect();t.style.top=(window.innerHeight-e.height)/2+\"px\",t.style.left=(window.innerWidth-e.width)/2+\"px\"}}}_updateOutput(t){const{_root:e,_color:o,options:n}=this;if(e.interaction.type()){const t=`to${e.interaction.type().getAttribute(\"data-type\")}`;e.interaction.result.value=\"function\"==typeof o[t]?o[t]().toString(n.outputPrecision):\"\"}!this._initializingActive&&this._recalc&&this._emit(\"change\",o,t,this)}_clearColor(t=!1){const{_root:e,options:o}=this;o.useAsButton||e.button.style.setProperty(\"--pcr-color\",\"rgba(0, 0, 0, 0.15)\"),e.button.classList.add(\"clear\"),o.showAlways||this.hide(),this._lastColor=null,this._initializingActive||t||(this._emit(\"save\",null),this._emit(\"clear\"))}_parseLocalColor(t){const{values:e,type:o,a:n}=w(t),{lockOpacity:i}=this.options,s=void 0!==n&&1!==n;return e&&3===e.length&&(e[3]=void 0),{values:!e||i&&s?null:e,type:o}}_t(t){return this.options.i18n[t]||E.I18N_DEFAULTS[t]}_emit(t,...e){this._eventListener[t].forEach((t=>t(...e,this)))}on(t,e){return this._eventListener[t].push(e),this}off(t,e){const o=this._eventListener[t]||[],n=o.indexOf(e);return~n&&o.splice(n,1),this}addSwatch(t){const{values:e}=this._parseLocalColor(t);if(e){const{_swatchColors:t,_root:o}=this,n=A(...e),s=r(`
- + + - + diff --git a/src/scripts/UndoAndRedoHandler.js b/src/scripts/UndoAndRedoHandler.js index 565ba4f..59331ef 100644 --- a/src/scripts/UndoAndRedoHandler.js +++ b/src/scripts/UndoAndRedoHandler.js @@ -45,7 +45,7 @@ class UndoAndRedoHandler { this.canvasHandler.ctx.lineTo(point.x, point.y); // set brush styling based on point clr & brush size - if (path.isErase) point.color = this.canvasHandler.colorHandler.bgColorPicker.value; + if (path.isErase) point.color = this.canvasHandler.colorHandler.bgColor; this.canvasHandler.brushHandler.setBrushStyling(point.color, point.brushSize); this.canvasHandler.ctx.stroke(); diff --git a/src/scripts/colorHandler.js b/src/scripts/colorHandler.js index 93ca7d2..f6e16bd 100644 --- a/src/scripts/colorHandler.js +++ b/src/scripts/colorHandler.js @@ -7,7 +7,7 @@ class ColorHandler { this.setupColorHandling(); } - setupColorPicker () { + setupColorPicker() { this.brushColorPicker = Pickr.create({ el: "#color-picker", theme: "classic", @@ -23,11 +23,47 @@ class ColorHandler { setupColorHandling() { this.currentColor = "black"; + this.bgColor = "#FFFFFF"; // color change event for brush color this.brushColorPicker.on("change", (color) => { - this.currentColor = color.toHEXA().toString() + const selectedMode = this.getSelectedMode(); + if (selectedMode === "background-color-pickr") { + this.bgColor = color.toHEXA().toString(); + this.bgColorChange() + } else if (selectedMode === "brush") { + this.currentColor = color.toHEXA().toString(); + this.canvasHandler.brushHandler.setBrushStyling( + color.toHEXA().toString() + ); + } else if (selectedMode === "eraser") { + this.currentColor = color.toHEXA().toString(); + this.canvasHandler.brushHandler.setBrushStyling( + color.toHEXA().toString() + ); + } }); } + + bgColorChange() { + const drawnPaths = this.canvasHandler.undoAndRedoHandler.drawnPaths; + const redoStack = this.canvasHandler.undoAndRedoHandler.redoStack; + this.canvasHandler.eraseAndClearHandler.clear(); + this.canvasHandler.undoAndRedoHandler.drawnPaths = drawnPaths; + this.canvasHandler.undoAndRedoHandler.redoStack = redoStack; + drawnPaths.forEach((path) => + this.canvasHandler.undoAndRedoHandler.redrawPath(path) + ); + this.canvasHandler.canvas.style.backgroundColor = this.bgColor; + } + + getSelectedMode() { + const radioButtons = document.getElementsByName("tool"); + for (const radioButton of radioButtons) { + if (radioButton.checked) { + return radioButton.id; + } + } + } } export default ColorHandler; diff --git a/src/scripts/drawingHandler.js b/src/scripts/drawingHandler.js index b36a1c6..84af089 100644 --- a/src/scripts/drawingHandler.js +++ b/src/scripts/drawingHandler.js @@ -53,7 +53,7 @@ class DrawingHandler { // Set brush styling based on whether eraser or brush is checked const color = this.canvasHandler.eraseAndClearHandler.eraserCheckbox.checked - ? this.canvasHandler.colorHandler.bgColorPicker.value + ? this.canvasHandler.colorHandler.bgColor : this.canvasHandler.colorHandler.currentColor; this.canvasHandler.brushHandler.setBrushStyling( diff --git a/src/scripts/toolIcon.js b/src/scripts/toolIcon.js index e9714ac..ea98dfb 100644 --- a/src/scripts/toolIcon.js +++ b/src/scripts/toolIcon.js @@ -2,8 +2,12 @@ class ToolIcon { constructor() { this.brushRadio = document.getElementById("brush"); this.eraserRadio = document.getElementById("eraser"); + this.bgColorRadio = document.getElementById("background-color-pickr"); this.brushLabel = document.querySelector('label[for="brush"]'); this.eraserLabel = document.querySelector('label[for="eraser"]'); + this.bgColorLabel = document.querySelector( + 'label[for="background-color-pickr"]' + ); this.detectChange(); this.updateIcon(); } @@ -12,17 +16,22 @@ class ToolIcon { // update brush icon based on whether the brush is checked this.brushLabel.querySelector("img").src = this.brushRadio.checked ? "assets/tab-icon.png" - : "assets/menu icons/not-active-brush.png"; + : "assets/menu icons/inactive-brush.png"; // update eraser icon based on whether the eraser is checked this.eraserLabel.querySelector("img").src = this.eraserRadio.checked ? "assets/menu icons/active-eraser.png" - : "assets/menu icons/not-active-eraser.png"; + : "assets/menu icons/inactive-eraser.png"; + + // update bg color icon based on whether the bg color is checked + this.bgColorLabel.querySelector("img").src = this.bgColorRadio.checked + ? "assets/menu icons/active-background-color.png" + : "assets/menu icons/inactive-background-color.png"; } detectChange() { // update icon clr when tool is changed - [this.brushRadio, this.eraserRadio].forEach((radio) => { + [this.brushRadio, this.eraserRadio, this.bgColorRadio].forEach((radio) => { radio.addEventListener("change", () => this.updateIcon()); }); }