camera-2d-simple
Version:
2D camera for WebGL
2 lines (1 loc) • 2.99 kB
JavaScript
!function(t,a){"object"==typeof exports&&"undefined"!=typeof module?module.exports=a(require("gl-matrix")):"function"==typeof define&&define.amd?define(["gl-matrix"],a):(t="undefined"!=typeof globalThis?globalThis:t||self).createCamera2d=a(t.glMatrix)}(this,(function(t){"use strict";return function(a,r,n,e,i,o){void 0===a&&(a=[0,0]),void 0===r&&(r=1),void 0===n&&(n=0),void 0===e&&(e=[0,0]),void 0===i&&(i=[[0,1/0],[0,1/0]]),void 0===o&&(o=[[-1/0,1/0],[-1/0,1/0]]);var c=new Float32Array(16),s=new Float32Array(16),u=new Float32Array(16),l=t.mat4.create(),m=e.slice(0,2).concat([0],[1]),v=Array.isArray(i[0])?[].concat(i[0]):[].concat(i),f=Array.isArray(i[0])?[].concat(i[1]):[].concat(i),d=Array.isArray(o[0])?[].concat(o[0]):[].concat(o),g=Array.isArray(o[0])?[].concat(o[1]):[].concat(o),y=function(){return t.mat4.getScaling(c,l).slice(0,2)},A=function(){var t=y();return Math.min(t[0],t[1])},p=function(){var t=y();return Math.max(t[0],t[1])},h=function(a,r,n){void 0===a&&(a=[]);var e=a[0];void 0===e&&(e=0);var i=a[1];void 0===i&&(i=0),void 0===r&&(r=1),void 0===n&&(n=0),l=t.mat4.create(),x([-e,-i]),M(n),w(1/r)},x=function(a){void 0===a&&(a=[]);var r=a[0];void 0===r&&(r=0);var n=a[1];void 0===n&&(n=0),c[0]=r,c[1]=n,c[2]=0;var e=t.mat4.fromTranslation(s,c);t.mat4.multiply(l,e,l)},w=function(a,r){var n=Array.isArray(a),e=n?a[0]:a,i=n?a[1]:a;if(!(e<=0||i<=0||1===e&&1===i)){var o=y(),d=o[0]*e,g=o[1]*i;if(e=Math.max(v[0],Math.min(d,v[1]))/o[0],i=Math.max(f[0],Math.min(g,f[1]))/o[1],1!==e||1!==i){c[0]=e,c[1]=i,c[2]=1;var A=t.mat4.fromScaling(s,c),p=r?r.concat([0]):m,h=t.mat4.fromTranslation(c,p);t.mat4.multiply(l,h,t.mat4.multiply(l,A,t.mat4.multiply(l,t.mat4.invert(u,h),l)))}}},M=function(a){var r=t.mat4.create();t.mat4.fromRotation(r,a,[0,0,1]),t.mat4.multiply(l,r,l)},T=function(t){!t||t.length<16||(l=t)};return h(a,r,n),{get translation(){return t.mat4.getTranslation(c,l).slice(0,2)},get target(){return t.vec4.transformMat4(c,m,t.mat4.invert(u,l)).slice(0,2)},get scaling(){return y()},get minScaling(){return A()},get maxScaling(){return p()},get scaleBounds(){return[[].concat(v),[].concat(f)]},get translationBounds(){return[[].concat(d),[].concat(g)]},get distance(){return[1/(t=y())[0],1/t[1]];var t},get minDistance(){return 1/A()},get maxDistance(){return 1/p()},get rotation(){return Math.acos(l[0]/p())},get view(){return l},get viewCenter(){return m.slice(0,2)},lookAt:h,translate:x,pan:x,rotate:M,scale:w,zoom:w,reset:function(){h(a,r,n)},set:function(){for(var t=[],a=arguments.length;a--;)t[a]=arguments[a];return console.warn("`set()` is deprecated. Please use `setView()` instead."),T.apply(void 0,t)},setScaleBounds:function(t){var a=Array.isArray(t[0]);v[0]=a?t[0][0]:t[0],v[1]=a?t[0][1]:t[1],f[0]=a?t[1][0]:t[0],f[1]=a?t[1][1]:t[1]},setTranslationBounds:function(t){var a=Array.isArray(t[0]);d[0]=a?t[0][0]:t[0],d[1]=a?t[0][1]:t[1],g[0]=a?t[1][0]:t[0],g[1]=a?t[1][1]:t[1]},setView:T,setViewCenter:function(t){m=t.slice(0,2).concat([0],[1])}}}}));