7segment-driver
Version:
A driver library for 7-segment displays
2 lines (1 loc) • 4.89 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).SevenSegmentDriver={})}(this,function(e){"use strict";function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function r(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function n(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function o(e){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?n(Object(o),!0).forEach(function(t){r(e,t,o[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):n(Object(o)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))})}return e}function i(e,r){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i,a,c=[],l=!0,u=!1;try{if(i=(r=r.call(e)).next,0===t);else for(;!(l=(n=i.call(r)).done)&&(c.push(n.value),c.length!==t);l=!0);}catch(e){u=!0,o=e}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 a={long:30,short:15,angle:30,gap:10,cw:400,ch:300},c={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"]};e.createHexagonVertices=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=o(o({},a),t);!function(e){if(e&&!/^[\d:*]+$/.test(e))throw new Error("Target can only contain digits, : or *")}(e);var n=r.angle*Math.PI/180,l=[Math.abs(r.short*Math.cos(n)),Math.abs(r.short*Math.sin(n))],u=l[0],f=l[1],s=2*u+r.long,y=s+2*f+r.gap,d=2*u+r.long,h=f+(r.cw-y*e.length)/2,b=(r.ch-2*d)/2,g=i(function(e,t,r){return[[[{x:0,y:0},{x:e,y:t},{x:e+r,y:t},{x:2*e+r,y:0},{x:e+r,y:-t},{x:e,y:-t}]],[[{x:0,y:0},{x:e,y:t},{x:e,y:t},{x:2*e,y:0},{x:e,y:-t},{x:e,y:-t}]]]}(u,f,r.long),2),v=g[0],p=g[1],m=function(e,t,r){return{a:{x:0,y:0,rotate:0},b:{x:e,y:0,rotate:90},c:{x:e,y:t,rotate:90},d:{x:0,y:2*t,rotate:0},e:{x:0,y:t,rotate:90},f:{x:0,y:0,rotate:90},g:{x:0,y:t,rotate:0},h:{x:e/2-r,y:t/2,rotate:0},i:{x:e/2-r,y:t/2*3,rotate:0}}}(s,d,u);return{segments:e.split("").map(function(e,t){return{label:e,vertices:c["*"].map(function(r,n){var o=m[r],i=c[":"].includes(r);return{label:"".concat(t).concat(e,"-").concat(n).concat(r),startY:o.y+b,startX:o.x+h+y*t,vertices:i?p:v,rotate:(o.rotate||0)*Math.PI/180,isShow:(c[e]||c["*"]).includes(r)}})}})}},e.createResponsiveCanvas=function(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if("string"==typeof e){var n=document.querySelector(e);if(!n)throw new Error("Container element not found");t=n}else{if(!(e instanceof HTMLElement))throw new Error("Invalid container parameter");t=e}var o=document.createElement("canvas");o.style.display="block",o.style.width="100%",o.style.height="100%",o.width=t.clientWidth,o.height=t.clientHeight,t.appendChild(o);var i=function(e,t){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 e.apply(void 0,i)},t),c)return e.apply(void 0,i)}}(function(){var e=t.clientHeight,n=t.clientWidth,i=r.devicePixelRatio||window.devicePixelRatio||1;o.width=n*i,o.height=e*i,r.onResize&&r.onResize(o,n,e)},r.debounceTime||100),a=new ResizeObserver(i);return a.observe(t),{canvas:o,cleanup:function(){a.disconnect(),o.parentNode===t&&t.removeChild(o)}}}});