UNPKG

hengine

Version:

A simple graphic engine for `canvasom`.

1 lines 5.66 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("canvasom")):"function"==typeof define&&define.amd?define(["exports","canvasom"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).HE={},t.COM)}(this,(function(t,e){"use strict";var i;t.Sizing=void 0,(i=t.Sizing||(t.Sizing={})).fill=function(t){var e=t.paddingLeft,i=t.paddingTop;return{width:t.containerWidth-e-t.paddingRight,height:t.containerHeight-i-t.paddingBottom,left:e,top:i,scale:1}},i.fixedWidth=function(t){var e=t.targetWidth,i=t.paddingLeft,n=t.paddingTop,d=t.containerWidth-i-t.paddingRight;return{width:d,height:t.containerHeight-n-t.paddingBottom,left:i,top:n,scale:d/e}},i.fixedHeight=function(t){var e=t.targetHeight,i=t.paddingLeft,n=t.paddingTop,d=t.containerWidth-i-t.paddingRight,a=t.containerHeight-n-t.paddingBottom;return{width:d,height:a,left:i,top:n,scale:a/e}},i.semifixed=function(t){var e=t.targetWidth,n=t.targetHeight,d=t.paddingLeft,a=t.paddingTop;return(t.containerWidth-d-t.paddingRight)/(t.containerHeight-a-t.paddingBottom)<e/n?i.fixedWidth(t):i.fixedHeight(t)},i.contain=function(t){var e=t.targetWidth,i=t.targetHeight,n=t.paddingLeft,d=t.paddingTop,a=t.containerWidth-n-t.paddingRight,o=t.containerHeight-d-t.paddingBottom;if(a/o<e/i){var r=a/e,h=i*r;return{width:a,height:h,left:n,top:(o-h)/2+d,scale:r}}var s=o/i,l=e*s;return{width:l,height:o,left:(a-l)/2+n,top:d,scale:s}},i.center=function(t){var e=t.targetWidth,i=t.targetHeight,n=t.paddingLeft,d=t.paddingTop;return{width:e,height:i,left:(t.containerWidth-n-t.paddingRight-e)/2+n,top:(t.containerHeight-d-t.paddingBottom-i)/2+d,scale:1}};var n=function(){function Resizer(e){var i,n,d,a,o,r,h,s,l,c;this.active=void 0,this.target=void 0,this.container=void 0,this.width=void 0,this.height=void 0,this.sizing=void 0,this.paddingTop=void 0,this.paddingRight=void 0,this.paddingBottom=void 0,this.paddingLeft=void 0,this.callback=void 0,this.update=void 0;var g=(null==e?void 0:e.target)||null;this.target=g,this.container=null!=(i=null==e?void 0:e.container)?i:g&&g.parentElement;var p=null!=(n=null==e?void 0:e.padding)?n:0;this.paddingTop=null!=(d=null==e?void 0:e.paddingTop)?d:p,this.paddingRight=null!=(a=null==e?void 0:e.paddingRight)?a:p,this.paddingBottom=null!=(o=null==e?void 0:e.paddingBottom)?o:p,this.paddingLeft=null!=(r=null==e?void 0:e.paddingLeft)?r:p,this.active=null==(h=null==e?void 0:e.active)||h,this.width=null!=(s=null==e?void 0:e.width)?s:0,this.height=null!=(l=null==e?void 0:e.height)?l:0,this.sizing=null!=(c=null==e?void 0:e.sizing)?c:t.Sizing.center,this.callback=(null==e?void 0:e.callback)||null,this.updateSync=this.updateSync.bind(this),this.onResize=this.onResize.bind(this),this.update=function(t,e){var i=null,n=function debounceWrapper(){for(var t=arguments.length,n=Array(t),d=0;d<t;d++)n[d]=arguments[d];null!==i&&clearTimeout(i),i=setTimeout.apply(void 0,[e,debounceWrapper.debounceTimeout].concat(n))};return n.debounceTimeout=100,n}(0,this.updateSync),!1!==(null==e?void 0:e.autoResize)&&(window.addEventListener("resize",this.onResize),window.addEventListener("orientationchange",this.onResize)),this.active&&this.update()}var e=Resizer.prototype;return e.updateSync=function updateSync(t){var e,i=this.target,n=this.container;if(this.active&&i&&n){var d=i.style,a=n.getBoundingClientRect(),o=this.sizing({containerWidth:a.width,containerHeight:a.height,paddingTop:this.paddingTop,paddingRight:this.paddingRight,paddingBottom:this.paddingBottom,paddingLeft:this.paddingLeft,targetWidth:this.width,targetHeight:this.height});d.width=o.width+"px",d.height=o.height+"px",d.marginLeft=o.left+"px",d.marginTop=o.top+"px",null!=(e=this.callback)&&e.call(this,o),null==t||t(o)}},e.onResize=function onResize(){this.update()},Resizer}();function _extends(){return _extends=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&(t[n]=i[n])}return t},_extends.apply(this,arguments)}function _inheritsLoose(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,_setPrototypeOf(t,e)}function _setPrototypeOf(t,e){return _setPrototypeOf=Object.setPrototypeOf||function _setPrototypeOf(t,e){return t.__proto__=e,t},_setPrototypeOf(t,e)}var d=function(t){function SceneNode(e){return t.call(this,_extends({stretch:1,penetrable:!0},e))||this}return _inheritsLoose(SceneNode,t),SceneNode}(e.CanvasNode),a=function(i){function CanvasEngine(e){var d;(d=i.call(this,e)||this).resizer=void 0,d.currentScene=null,d.onResize=d.onResize.bind(function _assertThisInitialized(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(d));var a={width:d.renderer.width,height:d.renderer.height,target:d.renderer.canvas,sizing:t.Sizing.contain,callback:d.onResize};return null!=e&&e.resizerOptions&&Object.assign(a,e.resizerOptions),d.resizer=new n(a),d.renderer.autoStyle=!1,d}_inheritsLoose(CanvasEngine,i);var d=CanvasEngine.prototype;return d.onResize=function onResize(t){var i=t.scale;this.renderer.resize(t.width/i,t.height/i),e.Schedule.updateAndRender(this)},d.enter=function enter(t){var i=this.currentScene;if(i){var n=new e.Event({name:"exit",stoppable:!0,cancelable:!0,data:{nextScene:t}});if(i.emit(n),n.canceled)return!1}if(t){var d=new e.Event({name:"enter",stoppable:!0,cancelable:!0,data:{currentScene:i}});if(t.emit(d),d.canceled)return!1;this.appendChild(t)}return i&&this.removeChild(i),this.currentScene=t,(i||t)&&e.Schedule.updateAndRender(this),!0},CanvasEngine}(e.CanvasRoot);t.CanvasEngine=a,t.Resizer=n,t.SceneNode=d,Object.defineProperty(t,"__esModule",{value:!0})}));