UNPKG

7segment-driver

Version:

A driver library for 7-segment displays

2 lines (1 loc) 4.66 kB
function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function e(t,e,r){return(e=function(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:e+""}(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function r(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)}return r}function n(t){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?r(Object(o),!0).forEach(function(r){e(t,r,o[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):r(Object(o)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))})}return t}function o(e,r){return function(t){if(Array.isArray(t))return t}(e)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,c=[],l=!0,u=!1;try{if(i=(r=r.call(t)).next,0===e);else for(;!(l=(n=i.call(r)).done)&&(c.push(n.value),c.length!==e);l=!0);}catch(t){u=!0,o=t}finally{try{if(!l&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(u)throw o}}return c}}(e,r)||function(e,r){if(e){if("string"==typeof e)return t(e,r);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(e,r):void 0}}(e,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}var i={long:30,short:15,angle:30,gap:10,cw:400,ch:300},a={0:["a","b","c","d","e","f"],1:["b","c"],2:["a","b","g","e","d"],3:["a","b","g","c","d"],4:["f","b","g","c"],5:["a","f","g","c","d"],6:["a","f","g","c","d","e"],7:["a","b","c"],8:["a","b","c","d","e","f","g"],9:["a","b","c","d","f","g"],":":["h","i"],"*":["a","b","c","d","e","f","g","h","i"]};function c(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=n(n({},i),e);!function(t){if(t&&!/^[\d:*]+$/.test(t))throw new Error("Target can only contain digits, : or *")}(t);var c=r.angle*Math.PI/180,l=[Math.abs(r.short*Math.cos(c)),Math.abs(r.short*Math.sin(c))],u=l[0],f=l[1],y=2*u+r.long,s=y+2*f+r.gap,h=2*u+r.long,d=f+(r.cw-s*t.length)/2,b=(r.ch-2*h)/2,g=function(t,e,r){return[[[{x:0,y:0},{x:t,y:e},{x:t+r,y:e},{x:2*t+r,y:0},{x:t+r,y:-e},{x:t,y:-e}]],[[{x:0,y:0},{x:t,y:e},{x:t,y:e},{x:2*t,y:0},{x:t,y:-e},{x:t,y:-e}]]]}(u,f,r.long),v=o(g,2),p=v[0],m=v[1],w=function(t,e,r){return{a:{x:0,y:0,rotate:0},b:{x:t,y:0,rotate:90},c:{x:t,y:e,rotate:90},d:{x:0,y:2*e,rotate:0},e:{x:0,y:e,rotate:90},f:{x:0,y:0,rotate:90},g:{x:0,y:e,rotate:0},h:{x:t/2-r,y:e/2,rotate:0},i:{x:t/2-r,y:e/2*3,rotate:0}}}(y,h,u);return{segments:t.split("").map(function(t,e){return{label:t,vertices:a["*"].map(function(r,n){var o=w[r],i=a[":"].includes(r);return{label:"".concat(e).concat(t,"-").concat(n).concat(r),startY:o.y+b,startX:o.x+d+s*e,vertices:i?m:p,rotate:(o.rotate||0)*Math.PI/180,isShow:(a[t]||a["*"]).includes(r)}})}})}}function l(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("string"==typeof t){var n=document.querySelector(t);if(!n)throw new Error("Container element not found");e=n}else{if(!(t instanceof HTMLElement))throw new Error("Invalid container parameter");e=t}var o=document.createElement("canvas");o.style.display="block",o.style.width="100%",o.style.height="100%",o.width=e.clientWidth,o.height=e.clientHeight,e.appendChild(o);var i=function(t,e){var r=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=null;return function(){for(var o=arguments.length,i=new Array(o),a=0;a<o;a++)i[a]=arguments[a];var c=r&&!n;if(n&&clearTimeout(n),n=setTimeout(function(){if(n=null,!r)return t.apply(void 0,i)},e),c)return t.apply(void 0,i)}}(function(){var t=e.clientHeight,n=e.clientWidth,i=r.devicePixelRatio||window.devicePixelRatio||1;o.width=n*i,o.height=t*i,r.onResize&&r.onResize(o,n,t)},r.debounceTime||100),a=new ResizeObserver(i);return a.observe(e),{canvas:o,cleanup:function(){a.disconnect(),o.parentNode===e&&e.removeChild(o)}}}export{c as createHexagonVertices,l as createResponsiveCanvas};