glre 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/events.ts CHANGED
@@ -23,7 +23,7 @@ export const glEvent = (self: GL) =>
23
23
  window.removeEventListener('mousemove', e.on('mousemove'))
24
24
  },
25
25
  // user mousemove event
26
- mousemove(_e, { clientX, clientY }, ...args) {
26
+ mousemove(_e, { clientX, clientY }) {
27
27
  const [w, h] = self.size
28
28
  self.mouse[0] = (clientX - w / 2) / (w / 2)
29
29
  self.mouse[1] = -(clientY - h / 2) / (h / 2)
package/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("refr"),n=require("reev");function t(e,n,t){var r=e.createShader(t);if(e.shaderSource(r,n),e.compileShader(r),e.getShaderParameter(r,e.COMPILE_STATUS))return r;console.warn(e.getShaderInfoLog(r))}var r=function(e){return n.event({mount:function(n){var r=e.el=document.getElementById(e.id),o=e.gl=null==r?void 0:r.getContext("webgl"),i=t(o,e.frag,null==o?void 0:o.FRAGMENT_SHADER),a=t(o,e.vert,null==o?void 0:o.VERTEX_SHADER);e.pg=function(e,n,t){var r=e.createProgram();return e.attachShader(r,n),e.attachShader(r,t),e.linkProgram(r),e.getProgramParameter(r,e.LINK_STATUS)?(e.useProgram(r),r):(console.log(e.getProgramInfoLog(r)),null)}(o,a,i),window.addEventListener("resize",n.on("resize")),window.addEventListener("mousemove",n.on("mousemove")),n("resize"),e.frame()},clean:function(e){window.removeEventListener("resize",e.on("resize")),window.removeEventListener("mousemove",e.on("mousemove"))},mousemove:function(n,t){var r=t.clientX,o=t.clientY,i=e.size,a=i[0],u=i[1];e.mouse[0]=(r-a/2)/(a/2),e.mouse[1]=-(o-u/2)/(u/2),e.setUniform("mouse",e.mouse)},resize:function(n,t,r,o){void 0===r&&(r=0),void 0===o&&(o=0),e.size[0]=e.el.width=r||window.innerWidth,e.size[1]=e.el.height=o||window.innerHeight,e.setUniform("resolution",e.size)},load:function(n,t){e.setFrame((function(){var n=t.path[0],r=e.activeUnit(n.alt),o=e.location(n.alt),i=function(e,n){var t=e.createTexture();return e.bindTexture(e.TEXTURE_2D,t),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n),e.generateMipmap(e.TEXTURE_2D),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),t}(e.gl,n);e.setFrame((function(){return e.gl.uniform1i(o,r),e.gl.activeTexture(e.gl["TEXTURE"+r]),e.gl.bindTexture(e.gl.TEXTURE_2D,i),!0}))}))}})},o=[-1,-1,1,-1,-1,1,-1,1,1,-1,1,1],i=function(t){var i=function e(n){return void 0===n&&(n=""),"function"==typeof n&&e.frame.mount(n),"string"!=typeof n||e.frag||(e.frag=n),"string"==typeof n&&e.frag&&(e.vert=n),e};i.id="myCanvas",i.frag="\n precision highp float;\n uniform vec2 resolution;\n void main() {\n gl_FragColor = vec4(fract(gl_FragCoord.xy / resolution), 0, 1);\n }\n",i.vert="\n attribute vec4 a_position;\n void main() {\n gl_Position = a_position;\n }\n",i.size=[0,0],i.mouse=[0,0],i.count=6;var a=i.event=r(i),u=i.frame=e.frame();return i.lastActiveUnit=0,i.activeUnit=n.nested((function(){return i.lastActiveUnit++})),i.stride=n.nested((function(e,n,t){var r=t?Math.max.apply(Math,t)+1:i.count;return n.length/r<<0})),i.uniformType=n.nested((function(e,n,t){return void 0===t&&(t=!1),"number"==typeof n?"uniform1f":t?"uniformMatrix"+(Math.sqrt(n.length)<<0)+"fv":"uniform"+n.length+"fv"})),i.location=n.nested((function(e,n){var t,r;return void 0===n&&(n=!1),n?null==(t=i.gl)?void 0:t.getAttribLocation(i.pg,e):null==(r=i.gl)?void 0:r.getUniformLocation(i.pg,e)})),i.setDpr=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void a.apply(void 0,["resize"].concat(n))||i},i.setSize=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void a.apply(void 0,["resize"].concat(n))||i},i.setFrame=function(e){return void u.mount(e)||i},i.setMount=function(e){return void a.mount({mount:e})||i},i.setClean=function(e){return void a.mount({clean:e})||i},i.setConfig=n.durable((function(e,n){i[e]=n}),i),i.setUniform=n.durable((function(e,n,t){void 0===t&&(t=!1);var r=i.uniformType(e,n,t);i.setFrame(t?function(){return i.gl[r](i.location(e),!1,n)}:function(){return i.gl[r](i.location(e),n)})}),i),i.setAttribute=n.durable((function(e,n,t){i.setFrame((function(){var r=i.gl,o=i.stride(e,n,t),a=i.location(e,!0);r.bindBuffer(r.ARRAY_BUFFER,function(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ARRAY_BUFFER,t),e.bufferData(e.ARRAY_BUFFER,new Float32Array(n),e.STATIC_DRAW),e.bindBuffer(e.ARRAY_BUFFER,null),t}}(r,n)),r.enableVertexAttribArray(a),r.vertexAttribPointer(a,o,r.FLOAT,!1,0,0),t&&r.bindBuffer(r.ELEMENT_ARRAY_BUFFER,function(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,t),e.bufferData(e.ELEMENT_ARRAY_BUFFER,new Int16Array(n),e.STATIC_DRAW),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null),t}}(r,t))}))}),i),i.setTexture=n.durable((function(e,n){var t=new Image;t.addEventListener("load",a.on("load"),!1),Object.assign(t,{src:n,alt:e,crossOrigin:"anonymous"})}),i),i.mount=function(){return a("mount")},i.clean=function(){return a("clean")},i.clear=function(e){return i.gl.clear(i.gl[e||"COLOR_BUFFER_BIT"])},i.viewport=function(e){var n;return(n=i.gl).viewport.apply(n,[0,0].concat(e||i.size))},i.drawArrays=function(e){return void 0===e&&(e="TRIANGLES"),i.gl.drawArrays(i.gl[e],0,i.count)},i.drawElements=function(e,n){return void 0===e&&(e="TRIANGLES"),void 0===n&&(n="UNSIGNED_SHORT"),i.gl.drawElements(i.gl[e],i.count,i.gl[n],0)},"string"==typeof t&&(t={id:t}),"object"==typeof t&&i.setConfig(t),6===i.count&&i.setAttribute({a_position:o}),i};i.default=null,exports.default=i,exports.gl=i;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("refr"),n=require("reev");function r(e,n){for(var r=e[0],t=0,o=n.length;t<o;t+=1)r+=n[t]+e[t+1];return r}function t(e){return Array.isArray(e)&&"string"==typeof e[0]}function o(e,n,r){var t=e.createShader(r);if(e.shaderSource(t,n),e.compileShader(t),e.getShaderParameter(t,e.COMPILE_STATUS))return t;console.warn(e.getShaderInfoLog(t))}function i(e,n){if(n){var r=e.createBuffer();return e.bindBuffer(e.ARRAY_BUFFER,r),e.bufferData(e.ARRAY_BUFFER,new Float32Array(n),e.STATIC_DRAW),e.bindBuffer(e.ARRAY_BUFFER,null),r}}function a(e,n){if(n){var r=e.createBuffer();return e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,r),e.bufferData(e.ELEMENT_ARRAY_BUFFER,new Int16Array(n),e.STATIC_DRAW),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null),r}}var u=function(e){return n.event({mount:function(n){var r=e.el=document.getElementById(e.id),t=e.gl=null==r?void 0:r.getContext("webgl"),i=o(t,e.frag,null==t?void 0:t.FRAGMENT_SHADER),a=o(t,e.vert,null==t?void 0:t.VERTEX_SHADER);e.pg=function(e,n,r){var t=e.createProgram();return e.attachShader(t,n),e.attachShader(t,r),e.linkProgram(t),e.getProgramParameter(t,e.LINK_STATUS)?(e.useProgram(t),t):(console.log(e.getProgramInfoLog(t)),null)}(t,a,i),window.addEventListener("resize",n.on("resize")),window.addEventListener("mousemove",n.on("mousemove")),n("resize"),e.frame()},clean:function(e){window.removeEventListener("resize",e.on("resize")),window.removeEventListener("mousemove",e.on("mousemove"))},mousemove:function(n,r){var t=r.clientX,o=r.clientY,i=e.size,a=i[0],u=i[1];e.mouse[0]=(t-a/2)/(a/2),e.mouse[1]=-(o-u/2)/(u/2),e.setUniform("mouse",e.mouse)},resize:function(n,r,t,o){void 0===t&&(t=0),void 0===o&&(o=0),e.size[0]=e.el.width=t||window.innerWidth,e.size[1]=e.el.height=o||window.innerHeight,e.setUniform("resolution",e.size)},load:function(n,r){e.setFrame((function(){var n=r.path[0],t=e.activeUnit(n.alt),o=e.location(n.alt),i=function(e,n){var r=e.createTexture();return e.bindTexture(e.TEXTURE_2D,r),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n),e.generateMipmap(e.TEXTURE_2D),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),r}(e.gl,n);e.setFrame((function(){return e.gl.uniform1i(o,t),e.gl.activeTexture(e.gl["TEXTURE"+t]),e.gl.bindTexture(e.gl.TEXTURE_2D,i),!0}))}))}})},f=[-1,-1,1,-1,-1,1,-1,1,1,-1,1,1],l="\n attribute vec4 a_position;\n void main() {\n gl_Position = a_position;\n }\n",c="\n precision highp float;\n uniform vec2 resolution;\n void main() {\n gl_FragColor = vec4(fract(gl_FragCoord.xy / resolution), 0, 1);\n }\n",s=function(o){var s=function e(n){for(var o=arguments.length,i=new Array(o>1?o-1:0),a=1;a<o;a++)i[a-1]=arguments[a];return t(n)&&(n=r(n,i)),"string"==typeof n&&(e.frag=n),"function"==typeof n&&e.frame.mount(n),e};s.id="myCanvas",s.frag=c,s.vert=l,s.size=[0,0],s.mouse=[0,0],s.count=6;var d=s.event=u(s),v=s.frame=e.frame();s.lastActiveUnit=0,s.activeUnit=n.nested((function(){return s.lastActiveUnit++})),s.stride=n.nested((function(e,n,r){var t=r?Math.max.apply(Math,r)+1:s.count;return n.length/t<<0})),s.uniformType=n.nested((function(e,n,r){return void 0===r&&(r=!1),"number"==typeof n?"uniform1f":r?"uniformMatrix"+(Math.sqrt(n.length)<<0)+"fv":"uniform"+n.length+"fv"})),s.location=n.nested((function(e,n){var r,t;return void 0===n&&(n=!1),n?null==(r=s.gl)?void 0:r.getAttribLocation(s.pg,e):null==(t=s.gl)?void 0:t.getUniformLocation(s.pg,e)})),s.setDpr=function(){for(var e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];return void d.apply(void 0,["resize",{}].concat(n))||s},s.setSize=function(){for(var e=arguments.length,n=new Array(e),r=0;r<e;r++)n[r]=arguments[r];return void d.apply(void 0,["resize",{}].concat(n))||s},s.setFrame=function(e){return void v.mount(e)||s},s.setMount=function(e){return void d.mount({mount:e})||s},s.setClean=function(e){return void d.mount({clean:e})||s},s.setConfig=n.durable((function(e,n){s[e]=n}),s),s.setUniform=n.durable((function(e,n,r){void 0===r&&(r=!1);var t=s.uniformType(e,n,r);r?s.setFrame((function(){return s.gl[t](s.location(e),!1,n)})):s.setFrame((function(){return s.gl[t](s.location(e),n)}))}),s),s.setAttribute=n.durable((function(e,n,r){s.setFrame((function(){var t=s.gl,o=s.stride(e,n,r),u=s.location(e,!0);t.bindBuffer(t.ARRAY_BUFFER,i(t,n)),t.enableVertexAttribArray(u),t.vertexAttribPointer(u,o,t.FLOAT,!1,0,0),r&&t.bindBuffer(t.ELEMENT_ARRAY_BUFFER,a(t,r))}))}),s),s.setTexture=n.durable((function(e,n){var r=new Image;r.addEventListener("load",d.on("load"),!1),Object.assign(r,{src:n,alt:e,crossOrigin:"anonymous"})}),s),s.mount=function(){return d("mount")},s.clean=function(){return d("clean")},s.clear=function(e){return s.gl.clear(s.gl[e||"COLOR_BUFFER_BIT"])},s.viewport=function(e){var n;return(n=s.gl).viewport.apply(n,[0,0].concat(e||s.size))},s.drawArrays=function(e){return void 0===e&&(e="TRIANGLES"),s.gl.drawArrays(s.gl[e],0,s.count)},s.drawElements=function(e,n){return void 0===e&&(e="TRIANGLES"),void 0===n&&(n="UNSIGNED_SHORT"),s.gl.drawElements(s.gl[e],s.count,s.gl[n],0)};for(var E=arguments.length,g=new Array(E>1?E-1:0),m=1;m<E;m++)g[m-1]=arguments[m];return t(o)&&(o=r(o,g)),"string"==typeof o&&(s.frag=o),"object"==typeof o&&s.setConfig(o),6===s.count&&s.setAttribute({a_position:f}),s};s.default=null,exports.default=s,exports.gl=s;
package/index.develop.js CHANGED
@@ -1 +1 @@
1
- !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("refr"),require("reev")):"function"==typeof define&&define.amd?define(["exports","refr","reev"],n):n(((e="undefined"!=typeof globalThis?globalThis:e||self).index=e.index||{},e.index.ts={}),e.refr,e.reev)}(this,(function(e,n,t){"use strict";function r(e,n,t){var r=e.createShader(t);if(e.shaderSource(r,n),e.compileShader(r),e.getShaderParameter(r,e.COMPILE_STATUS))return r;console.warn(e.getShaderInfoLog(r))}var o=function(e){return t.event({mount:function(n){var t=e.el=document.getElementById(e.id),o=e.gl=null==t?void 0:t.getContext("webgl"),i=r(o,e.frag,null==o?void 0:o.FRAGMENT_SHADER),a=r(o,e.vert,null==o?void 0:o.VERTEX_SHADER);e.pg=function(e,n,t){var r=e.createProgram();return e.attachShader(r,n),e.attachShader(r,t),e.linkProgram(r),e.getProgramParameter(r,e.LINK_STATUS)?(e.useProgram(r),r):(console.log(e.getProgramInfoLog(r)),null)}(o,a,i),window.addEventListener("resize",n.on("resize")),window.addEventListener("mousemove",n.on("mousemove")),n("resize"),e.frame()},clean:function(e){window.removeEventListener("resize",e.on("resize")),window.removeEventListener("mousemove",e.on("mousemove"))},mousemove:function(n,t){var r=t.clientX,o=t.clientY,i=e.size,a=i[0],u=i[1];e.mouse[0]=(r-a/2)/(a/2),e.mouse[1]=-(o-u/2)/(u/2),e.setUniform("mouse",e.mouse)},resize:function(n,t,r,o){void 0===r&&(r=0),void 0===o&&(o=0),e.size[0]=e.el.width=r||window.innerWidth,e.size[1]=e.el.height=o||window.innerHeight,e.setUniform("resolution",e.size)},load:function(n,t){e.setFrame((function(){var n=t.path[0],r=e.activeUnit(n.alt),o=e.location(n.alt),i=function(e,n){var t=e.createTexture();return e.bindTexture(e.TEXTURE_2D,t),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n),e.generateMipmap(e.TEXTURE_2D),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),t}(e.gl,n);e.setFrame((function(){return e.gl.uniform1i(o,r),e.gl.activeTexture(e.gl["TEXTURE"+r]),e.gl.bindTexture(e.gl.TEXTURE_2D,i),!0}))}))}})},i=[-1,-1,1,-1,-1,1,-1,1,1,-1,1,1],a=function(e){var r=function e(n){return void 0===n&&(n=""),"function"==typeof n&&e.frame.mount(n),"string"!=typeof n||e.frag||(e.frag=n),"string"==typeof n&&e.frag&&(e.vert=n),e};r.id="myCanvas",r.frag="\n precision highp float;\n uniform vec2 resolution;\n void main() {\n gl_FragColor = vec4(fract(gl_FragCoord.xy / resolution), 0, 1);\n }\n",r.vert="\n attribute vec4 a_position;\n void main() {\n gl_Position = a_position;\n }\n",r.size=[0,0],r.mouse=[0,0],r.count=6;var a=r.event=o(r),u=r.frame=n.frame();return r.lastActiveUnit=0,r.activeUnit=t.nested((function(){return r.lastActiveUnit++})),r.stride=t.nested((function(e,n,t){var o=t?Math.max.apply(Math,t)+1:r.count;return n.length/o<<0})),r.uniformType=t.nested((function(e,n,t){return void 0===t&&(t=!1),"number"==typeof n?"uniform1f":t?"uniformMatrix"+(Math.sqrt(n.length)<<0)+"fv":"uniform"+n.length+"fv"})),r.location=t.nested((function(e,n){var t,o;return void 0===n&&(n=!1),n?null==(t=r.gl)?void 0:t.getAttribLocation(r.pg,e):null==(o=r.gl)?void 0:o.getUniformLocation(r.pg,e)})),r.setDpr=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void a.apply(void 0,["resize"].concat(n))||r},r.setSize=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void a.apply(void 0,["resize"].concat(n))||r},r.setFrame=function(e){return void u.mount(e)||r},r.setMount=function(e){return void a.mount({mount:e})||r},r.setClean=function(e){return void a.mount({clean:e})||r},r.setConfig=t.durable((function(e,n){r[e]=n}),r),r.setUniform=t.durable((function(e,n,t){void 0===t&&(t=!1);var o=r.uniformType(e,n,t);r.setFrame(t?function(){return r.gl[o](r.location(e),!1,n)}:function(){return r.gl[o](r.location(e),n)})}),r),r.setAttribute=t.durable((function(e,n,t){r.setFrame((function(){var o=r.gl,i=r.stride(e,n,t),a=r.location(e,!0);o.bindBuffer(o.ARRAY_BUFFER,function(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ARRAY_BUFFER,t),e.bufferData(e.ARRAY_BUFFER,new Float32Array(n),e.STATIC_DRAW),e.bindBuffer(e.ARRAY_BUFFER,null),t}}(o,n)),o.enableVertexAttribArray(a),o.vertexAttribPointer(a,i,o.FLOAT,!1,0,0),t&&o.bindBuffer(o.ELEMENT_ARRAY_BUFFER,function(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,t),e.bufferData(e.ELEMENT_ARRAY_BUFFER,new Int16Array(n),e.STATIC_DRAW),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null),t}}(o,t))}))}),r),r.setTexture=t.durable((function(e,n){var t=new Image;t.addEventListener("load",a.on("load"),!1),Object.assign(t,{src:n,alt:e,crossOrigin:"anonymous"})}),r),r.mount=function(){return a("mount")},r.clean=function(){return a("clean")},r.clear=function(e){return r.gl.clear(r.gl[e||"COLOR_BUFFER_BIT"])},r.viewport=function(e){var n;return(n=r.gl).viewport.apply(n,[0,0].concat(e||r.size))},r.drawArrays=function(e){return void 0===e&&(e="TRIANGLES"),r.gl.drawArrays(r.gl[e],0,r.count)},r.drawElements=function(e,n){return void 0===e&&(e="TRIANGLES"),void 0===n&&(n="UNSIGNED_SHORT"),r.gl.drawElements(r.gl[e],r.count,r.gl[n],0)},"string"==typeof e&&(e={id:e}),"object"==typeof e&&r.setConfig(e),6===r.count&&r.setAttribute({a_position:i}),r};a.default=null,e.default=a,e.gl=a,Object.defineProperty(e,"__esModule",{value:!0})}));
1
+ !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("refr"),require("reev")):"function"==typeof define&&define.amd?define(["exports","refr","reev"],n):n(((e="undefined"!=typeof globalThis?globalThis:e||self).index=e.index||{},e.index.ts={}),e.refr,e.reev)}(this,(function(e,n,t){"use strict";function r(e,n){for(var t=e[0],r=0,o=n.length;r<o;r+=1)t+=n[r]+e[r+1];return t}function o(e){return Array.isArray(e)&&"string"==typeof e[0]}function i(e,n,t){var r=e.createShader(t);if(e.shaderSource(r,n),e.compileShader(r),e.getShaderParameter(r,e.COMPILE_STATUS))return r;console.warn(e.getShaderInfoLog(r))}function a(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ARRAY_BUFFER,t),e.bufferData(e.ARRAY_BUFFER,new Float32Array(n),e.STATIC_DRAW),e.bindBuffer(e.ARRAY_BUFFER,null),t}}function u(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,t),e.bufferData(e.ELEMENT_ARRAY_BUFFER,new Int16Array(n),e.STATIC_DRAW),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null),t}}var f=function(e){return t.event({mount:function(n){var t=e.el=document.getElementById(e.id),r=e.gl=null==t?void 0:t.getContext("webgl"),o=i(r,e.frag,null==r?void 0:r.FRAGMENT_SHADER),a=i(r,e.vert,null==r?void 0:r.VERTEX_SHADER);e.pg=function(e,n,t){var r=e.createProgram();return e.attachShader(r,n),e.attachShader(r,t),e.linkProgram(r),e.getProgramParameter(r,e.LINK_STATUS)?(e.useProgram(r),r):(console.log(e.getProgramInfoLog(r)),null)}(r,a,o),window.addEventListener("resize",n.on("resize")),window.addEventListener("mousemove",n.on("mousemove")),n("resize"),e.frame()},clean:function(e){window.removeEventListener("resize",e.on("resize")),window.removeEventListener("mousemove",e.on("mousemove"))},mousemove:function(n,t){var r=t.clientX,o=t.clientY,i=e.size,a=i[0],u=i[1];e.mouse[0]=(r-a/2)/(a/2),e.mouse[1]=-(o-u/2)/(u/2),e.setUniform("mouse",e.mouse)},resize:function(n,t,r,o){void 0===r&&(r=0),void 0===o&&(o=0),e.size[0]=e.el.width=r||window.innerWidth,e.size[1]=e.el.height=o||window.innerHeight,e.setUniform("resolution",e.size)},load:function(n,t){e.setFrame((function(){var n=t.path[0],r=e.activeUnit(n.alt),o=e.location(n.alt),i=function(e,n){var t=e.createTexture();return e.bindTexture(e.TEXTURE_2D,t),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n),e.generateMipmap(e.TEXTURE_2D),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),t}(e.gl,n);e.setFrame((function(){return e.gl.uniform1i(o,r),e.gl.activeTexture(e.gl["TEXTURE"+r]),e.gl.bindTexture(e.gl.TEXTURE_2D,i),!0}))}))}})},l=[-1,-1,1,-1,-1,1,-1,1,1,-1,1,1],c="\n attribute vec4 a_position;\n void main() {\n gl_Position = a_position;\n }\n",s="\n precision highp float;\n uniform vec2 resolution;\n void main() {\n gl_FragColor = vec4(fract(gl_FragCoord.xy / resolution), 0, 1);\n }\n",d=function(e){var i=function e(n){for(var t=arguments.length,i=new Array(t>1?t-1:0),a=1;a<t;a++)i[a-1]=arguments[a];return o(n)&&(n=r(n,i)),"string"==typeof n&&(e.frag=n),"function"==typeof n&&e.frame.mount(n),e};i.id="myCanvas",i.frag=s,i.vert=c,i.size=[0,0],i.mouse=[0,0],i.count=6;var d=i.event=f(i),v=i.frame=n.frame();i.lastActiveUnit=0,i.activeUnit=t.nested((function(){return i.lastActiveUnit++})),i.stride=t.nested((function(e,n,t){var r=t?Math.max.apply(Math,t)+1:i.count;return n.length/r<<0})),i.uniformType=t.nested((function(e,n,t){return void 0===t&&(t=!1),"number"==typeof n?"uniform1f":t?"uniformMatrix"+(Math.sqrt(n.length)<<0)+"fv":"uniform"+n.length+"fv"})),i.location=t.nested((function(e,n){var t,r;return void 0===n&&(n=!1),n?null==(t=i.gl)?void 0:t.getAttribLocation(i.pg,e):null==(r=i.gl)?void 0:r.getUniformLocation(i.pg,e)})),i.setDpr=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void d.apply(void 0,["resize",{}].concat(n))||i},i.setSize=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void d.apply(void 0,["resize",{}].concat(n))||i},i.setFrame=function(e){return void v.mount(e)||i},i.setMount=function(e){return void d.mount({mount:e})||i},i.setClean=function(e){return void d.mount({clean:e})||i},i.setConfig=t.durable((function(e,n){i[e]=n}),i),i.setUniform=t.durable((function(e,n,t){void 0===t&&(t=!1);var r=i.uniformType(e,n,t);t?i.setFrame((function(){return i.gl[r](i.location(e),!1,n)})):i.setFrame((function(){return i.gl[r](i.location(e),n)}))}),i),i.setAttribute=t.durable((function(e,n,t){i.setFrame((function(){var r=i.gl,o=i.stride(e,n,t),f=i.location(e,!0);r.bindBuffer(r.ARRAY_BUFFER,a(r,n)),r.enableVertexAttribArray(f),r.vertexAttribPointer(f,o,r.FLOAT,!1,0,0),t&&r.bindBuffer(r.ELEMENT_ARRAY_BUFFER,u(r,t))}))}),i),i.setTexture=t.durable((function(e,n){var t=new Image;t.addEventListener("load",d.on("load"),!1),Object.assign(t,{src:n,alt:e,crossOrigin:"anonymous"})}),i),i.mount=function(){return d("mount")},i.clean=function(){return d("clean")},i.clear=function(e){return i.gl.clear(i.gl[e||"COLOR_BUFFER_BIT"])},i.viewport=function(e){var n;return(n=i.gl).viewport.apply(n,[0,0].concat(e||i.size))},i.drawArrays=function(e){return void 0===e&&(e="TRIANGLES"),i.gl.drawArrays(i.gl[e],0,i.count)},i.drawElements=function(e,n){return void 0===e&&(e="TRIANGLES"),void 0===n&&(n="UNSIGNED_SHORT"),i.gl.drawElements(i.gl[e],i.count,i.gl[n],0)};for(var E=arguments.length,g=new Array(E>1?E-1:0),m=1;m<E;m++)g[m-1]=arguments[m];return o(e)&&(e=r(e,g)),"string"==typeof e&&(i.frag=e),"object"==typeof e&&i.setConfig(e),6===i.count&&i.setAttribute({a_position:l}),i};d.default=null,e.default=d,e.gl=d,Object.defineProperty(e,"__esModule",{value:!0})}));
package/index.js CHANGED
@@ -1 +1 @@
1
- import{frame as e}from"refr";import{event as n,nested as t,durable as r}from"reev";function o(e,n,t){var r=e.createShader(t);if(e.shaderSource(r,n),e.compileShader(r),e.getShaderParameter(r,e.COMPILE_STATUS))return r;console.warn(e.getShaderInfoLog(r))}var i=function(e){return n({mount:function(n){var t=e.el=document.getElementById(e.id),r=e.gl=null==t?void 0:t.getContext("webgl"),i=o(r,e.frag,null==r?void 0:r.FRAGMENT_SHADER),a=o(r,e.vert,null==r?void 0:r.VERTEX_SHADER);e.pg=function(e,n,t){var r=e.createProgram();return e.attachShader(r,n),e.attachShader(r,t),e.linkProgram(r),e.getProgramParameter(r,e.LINK_STATUS)?(e.useProgram(r),r):(console.log(e.getProgramInfoLog(r)),null)}(r,a,i),window.addEventListener("resize",n.on("resize")),window.addEventListener("mousemove",n.on("mousemove")),n("resize"),e.frame()},clean:function(e){window.removeEventListener("resize",e.on("resize")),window.removeEventListener("mousemove",e.on("mousemove"))},mousemove:function(n,t){var r=t.clientX,o=t.clientY,i=e.size,a=i[0],u=i[1];e.mouse[0]=(r-a/2)/(a/2),e.mouse[1]=-(o-u/2)/(u/2),e.setUniform("mouse",e.mouse)},resize:function(n,t,r,o){void 0===r&&(r=0),void 0===o&&(o=0),e.size[0]=e.el.width=r||window.innerWidth,e.size[1]=e.el.height=o||window.innerHeight,e.setUniform("resolution",e.size)},load:function(n,t){e.setFrame((function(){var n=t.path[0],r=e.activeUnit(n.alt),o=e.location(n.alt),i=function(e,n){var t=e.createTexture();return e.bindTexture(e.TEXTURE_2D,t),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n),e.generateMipmap(e.TEXTURE_2D),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),t}(e.gl,n);e.setFrame((function(){return e.gl.uniform1i(o,r),e.gl.activeTexture(e.gl["TEXTURE"+r]),e.gl.bindTexture(e.gl.TEXTURE_2D,i),!0}))}))}})},a=[-1,-1,1,-1,-1,1,-1,1,1,-1,1,1],u=function(n){var o=function e(n){return void 0===n&&(n=""),"function"==typeof n&&e.frame.mount(n),"string"!=typeof n||e.frag||(e.frag=n),"string"==typeof n&&e.frag&&(e.vert=n),e};o.id="myCanvas",o.frag="\n precision highp float;\n uniform vec2 resolution;\n void main() {\n gl_FragColor = vec4(fract(gl_FragCoord.xy / resolution), 0, 1);\n }\n",o.vert="\n attribute vec4 a_position;\n void main() {\n gl_Position = a_position;\n }\n",o.size=[0,0],o.mouse=[0,0],o.count=6;var u=o.event=i(o),f=o.frame=e();return o.lastActiveUnit=0,o.activeUnit=t((function(){return o.lastActiveUnit++})),o.stride=t((function(e,n,t){var r=t?Math.max.apply(Math,t)+1:o.count;return n.length/r<<0})),o.uniformType=t((function(e,n,t){return void 0===t&&(t=!1),"number"==typeof n?"uniform1f":t?"uniformMatrix"+(Math.sqrt(n.length)<<0)+"fv":"uniform"+n.length+"fv"})),o.location=t((function(e,n){var t,r;return void 0===n&&(n=!1),n?null==(t=o.gl)?void 0:t.getAttribLocation(o.pg,e):null==(r=o.gl)?void 0:r.getUniformLocation(o.pg,e)})),o.setDpr=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void u.apply(void 0,["resize"].concat(n))||o},o.setSize=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void u.apply(void 0,["resize"].concat(n))||o},o.setFrame=function(e){return void f.mount(e)||o},o.setMount=function(e){return void u.mount({mount:e})||o},o.setClean=function(e){return void u.mount({clean:e})||o},o.setConfig=r((function(e,n){o[e]=n}),o),o.setUniform=r((function(e,n,t){void 0===t&&(t=!1);var r=o.uniformType(e,n,t);o.setFrame(t?function(){return o.gl[r](o.location(e),!1,n)}:function(){return o.gl[r](o.location(e),n)})}),o),o.setAttribute=r((function(e,n,t){o.setFrame((function(){var r=o.gl,i=o.stride(e,n,t),a=o.location(e,!0);r.bindBuffer(r.ARRAY_BUFFER,function(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ARRAY_BUFFER,t),e.bufferData(e.ARRAY_BUFFER,new Float32Array(n),e.STATIC_DRAW),e.bindBuffer(e.ARRAY_BUFFER,null),t}}(r,n)),r.enableVertexAttribArray(a),r.vertexAttribPointer(a,i,r.FLOAT,!1,0,0),t&&r.bindBuffer(r.ELEMENT_ARRAY_BUFFER,function(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,t),e.bufferData(e.ELEMENT_ARRAY_BUFFER,new Int16Array(n),e.STATIC_DRAW),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null),t}}(r,t))}))}),o),o.setTexture=r((function(e,n){var t=new Image;t.addEventListener("load",u.on("load"),!1),Object.assign(t,{src:n,alt:e,crossOrigin:"anonymous"})}),o),o.mount=function(){return u("mount")},o.clean=function(){return u("clean")},o.clear=function(e){return o.gl.clear(o.gl[e||"COLOR_BUFFER_BIT"])},o.viewport=function(e){var n;return(n=o.gl).viewport.apply(n,[0,0].concat(e||o.size))},o.drawArrays=function(e){return void 0===e&&(e="TRIANGLES"),o.gl.drawArrays(o.gl[e],0,o.count)},o.drawElements=function(e,n){return void 0===e&&(e="TRIANGLES"),void 0===n&&(n="UNSIGNED_SHORT"),o.gl.drawElements(o.gl[e],o.count,o.gl[n],0)},"string"==typeof n&&(n={id:n}),"object"==typeof n&&o.setConfig(n),6===o.count&&o.setAttribute({a_position:a}),o};u.default=null;export{u as default,u as gl};
1
+ import{frame as e}from"refr";import{event as n,nested as t,durable as r}from"reev";function o(e,n){for(var t=e[0],r=0,o=n.length;r<o;r+=1)t+=n[r]+e[r+1];return t}function i(e){return Array.isArray(e)&&"string"==typeof e[0]}function a(e,n,t){var r=e.createShader(t);if(e.shaderSource(r,n),e.compileShader(r),e.getShaderParameter(r,e.COMPILE_STATUS))return r;console.warn(e.getShaderInfoLog(r))}function u(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ARRAY_BUFFER,t),e.bufferData(e.ARRAY_BUFFER,new Float32Array(n),e.STATIC_DRAW),e.bindBuffer(e.ARRAY_BUFFER,null),t}}function f(e,n){if(n){var t=e.createBuffer();return e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,t),e.bufferData(e.ELEMENT_ARRAY_BUFFER,new Int16Array(n),e.STATIC_DRAW),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null),t}}var l=function(e){return n({mount:function(n){var t=e.el=document.getElementById(e.id),r=e.gl=null==t?void 0:t.getContext("webgl"),o=a(r,e.frag,null==r?void 0:r.FRAGMENT_SHADER),i=a(r,e.vert,null==r?void 0:r.VERTEX_SHADER);e.pg=function(e,n,t){var r=e.createProgram();return e.attachShader(r,n),e.attachShader(r,t),e.linkProgram(r),e.getProgramParameter(r,e.LINK_STATUS)?(e.useProgram(r),r):(console.log(e.getProgramInfoLog(r)),null)}(r,i,o),window.addEventListener("resize",n.on("resize")),window.addEventListener("mousemove",n.on("mousemove")),n("resize"),e.frame()},clean:function(e){window.removeEventListener("resize",e.on("resize")),window.removeEventListener("mousemove",e.on("mousemove"))},mousemove:function(n,t){var r=t.clientX,o=t.clientY,i=e.size,a=i[0],u=i[1];e.mouse[0]=(r-a/2)/(a/2),e.mouse[1]=-(o-u/2)/(u/2),e.setUniform("mouse",e.mouse)},resize:function(n,t,r,o){void 0===r&&(r=0),void 0===o&&(o=0),e.size[0]=e.el.width=r||window.innerWidth,e.size[1]=e.el.height=o||window.innerHeight,e.setUniform("resolution",e.size)},load:function(n,t){e.setFrame((function(){var n=t.path[0],r=e.activeUnit(n.alt),o=e.location(n.alt),i=function(e,n){var t=e.createTexture();return e.bindTexture(e.TEXTURE_2D,t),e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n),e.generateMipmap(e.TEXTURE_2D),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_MAG_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.bindTexture(e.TEXTURE_2D,null),t}(e.gl,n);e.setFrame((function(){return e.gl.uniform1i(o,r),e.gl.activeTexture(e.gl["TEXTURE"+r]),e.gl.bindTexture(e.gl.TEXTURE_2D,i),!0}))}))}})},c=[-1,-1,1,-1,-1,1,-1,1,1,-1,1,1],s="\n attribute vec4 a_position;\n void main() {\n gl_Position = a_position;\n }\n",E="\n precision highp float;\n uniform vec2 resolution;\n void main() {\n gl_FragColor = vec4(fract(gl_FragCoord.xy / resolution), 0, 1);\n }\n",m=function(n){var a=function e(n){for(var t=arguments.length,r=new Array(t>1?t-1:0),a=1;a<t;a++)r[a-1]=arguments[a];return i(n)&&(n=o(n,r)),"string"==typeof n&&(e.frag=n),"function"==typeof n&&e.frame.mount(n),e};a.id="myCanvas",a.frag=E,a.vert=s,a.size=[0,0],a.mouse=[0,0],a.count=6;var m=a.event=l(a),v=a.frame=e();a.lastActiveUnit=0,a.activeUnit=t((function(){return a.lastActiveUnit++})),a.stride=t((function(e,n,t){var r=t?Math.max.apply(Math,t)+1:a.count;return n.length/r<<0})),a.uniformType=t((function(e,n,t){return void 0===t&&(t=!1),"number"==typeof n?"uniform1f":t?"uniformMatrix"+(Math.sqrt(n.length)<<0)+"fv":"uniform"+n.length+"fv"})),a.location=t((function(e,n){var t,r;return void 0===n&&(n=!1),n?null==(t=a.gl)?void 0:t.getAttribLocation(a.pg,e):null==(r=a.gl)?void 0:r.getUniformLocation(a.pg,e)})),a.setDpr=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void m.apply(void 0,["resize",{}].concat(n))||a},a.setSize=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return void m.apply(void 0,["resize",{}].concat(n))||a},a.setFrame=function(e){return void v.mount(e)||a},a.setMount=function(e){return void m.mount({mount:e})||a},a.setClean=function(e){return void m.mount({clean:e})||a},a.setConfig=r((function(e,n){a[e]=n}),a),a.setUniform=r((function(e,n,t){void 0===t&&(t=!1);var r=a.uniformType(e,n,t);t?a.setFrame((function(){return a.gl[r](a.location(e),!1,n)})):a.setFrame((function(){return a.gl[r](a.location(e),n)}))}),a),a.setAttribute=r((function(e,n,t){a.setFrame((function(){var r=a.gl,o=a.stride(e,n,t),i=a.location(e,!0);r.bindBuffer(r.ARRAY_BUFFER,u(r,n)),r.enableVertexAttribArray(i),r.vertexAttribPointer(i,o,r.FLOAT,!1,0,0),t&&r.bindBuffer(r.ELEMENT_ARRAY_BUFFER,f(r,t))}))}),a),a.setTexture=r((function(e,n){var t=new Image;t.addEventListener("load",m.on("load"),!1),Object.assign(t,{src:n,alt:e,crossOrigin:"anonymous"})}),a),a.mount=function(){return m("mount")},a.clean=function(){return m("clean")},a.clear=function(e){return a.gl.clear(a.gl[e||"COLOR_BUFFER_BIT"])},a.viewport=function(e){var n;return(n=a.gl).viewport.apply(n,[0,0].concat(e||a.size))},a.drawArrays=function(e){return void 0===e&&(e="TRIANGLES"),a.gl.drawArrays(a.gl[e],0,a.count)},a.drawElements=function(e,n){return void 0===e&&(e="TRIANGLES"),void 0===n&&(n="UNSIGNED_SHORT"),a.gl.drawElements(a.gl[e],a.count,a.gl[n],0)};for(var g=arguments.length,d=new Array(g>1?g-1:0),T=1;T<g;T++)d[T-1]=arguments[T];return i(n)&&(n=o(n,d)),"string"==typeof n&&(a.frag=n),"object"==typeof n&&a.setConfig(n),6===a.count&&a.setAttribute({a_position:c}),a};m.default=null;export{m as default,m as gl};
package/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { frame } from 'refr'
2
2
  import { glEvent } from './events'
3
3
  import { durable, nested } from 'reev'
4
- import { createVbo, createIbo } from './utils'
5
- import type { GL, GLConfig } from './types'
4
+ import { createVbo, createIbo, interleave, isTemplateLiteral } from './utils'
5
+ import type { GL, GLInitArg } from './types'
6
6
 
7
7
  const a_position = [-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1];
8
8
 
@@ -21,16 +21,16 @@ const _defaultFragmentShader = `
21
21
  }
22
22
  `
23
23
 
24
- export const gl = (config?: string | Partial<GLConfig>) => {
25
- const self = ((arg = '') => {
26
- if (typeof arg === 'function') self.frame.mount(arg)
27
- if (typeof arg === 'string' && !self.frag) self.frag = arg
28
- if (typeof arg === 'string' && self.frag) self.vert = arg
29
- return self
24
+ export const gl = (initArg?: GLInitArg, ...initArgs: any[]) => {
25
+ const self = ((arg: any, ...args: any[]) => {
26
+ if (isTemplateLiteral(arg)) arg = interleave(arg, args)
27
+ if (typeof arg === 'string') self.frag = arg
28
+ if (typeof arg === 'function') self.frame.mount(arg)
29
+ return self
30
30
  }) as GL
31
31
 
32
32
  // default state
33
- self.id = 'myCanvas'
33
+ self.id = 'myCanvas' // @TODO feat: create hashid
34
34
  self.frag = _defaultFragmentShader;
35
35
  self.vert = _defaultVertexShader;
36
36
  self.size = [0, 0]
@@ -58,8 +58,8 @@ export const gl = (config?: string | Partial<GLConfig>) => {
58
58
  })
59
59
 
60
60
  // setter
61
- self.setDpr = (...args) => void ev('resize', ...args) || self
62
- self.setSize = (...args) => void ev('resize', ...args) || self
61
+ self.setDpr = (...args) => void ev('resize', {}, ...args) || self
62
+ self.setSize = (...args) => void ev('resize', {}, ...args) || self
63
63
  self.setFrame = (frame) => void fr.mount(frame) || self
64
64
  self.setMount = (mount) => void ev.mount({ mount }) || self
65
65
  self.setClean = (clean) => void ev.mount({ clean }) || self
@@ -105,8 +105,9 @@ export const gl = (config?: string | Partial<GLConfig>) => {
105
105
  self.gl.drawElements(self.gl[mode], self.count, self.gl[type], 0)
106
106
 
107
107
  // init config
108
- if (typeof config === "string") config = { id: config }
109
- if (typeof config === "object") self.setConfig(config)
108
+ if (isTemplateLiteral(initArg)) initArg = interleave(initArg, initArgs)
109
+ if (typeof initArg === "string") self.frag = initArg
110
+ if (typeof initArg === "object") self.setConfig(initArg)
110
111
  if (self.count === 6) self.setAttribute({ a_position })
111
112
 
112
113
  return self;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "glre",
3
3
  "author": "tseijp",
4
4
  "license": "MIT",
5
- "version": "0.4.0",
5
+ "version": "0.6.0",
6
6
  "module": "index.js",
7
7
  "types": "index.ts",
8
8
  "main": "index.cjs.js",
@@ -24,6 +24,7 @@
24
24
  "reactjs",
25
25
  "reactive",
26
26
  "solid",
27
+ "solidjs",
27
28
  "typescript"
28
29
  ],
29
30
  "files": [
package/types.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { GL } from './types';
2
1
  /**
3
2
  * ref: https://stackoverflow.com/questions/52489261/typescript-can-i-define-an-n-length-tuple-type
4
3
  */
@@ -14,7 +13,9 @@ export type Attributes = Record<string, Attribute>
14
13
  export type Uniforms = Record<string, Uniform>
15
14
 
16
15
  export interface GL {
16
+ (fun: Fun): GL
17
17
  (shader: string): GL
18
+ (strings: TemplateStringsArray, ...args: any[]): GL
18
19
  /**
19
20
  * initial value
20
21
  */
@@ -72,11 +73,16 @@ export interface GL {
72
73
  mount(): void
73
74
  clean(): void
74
75
  clear(key?: GLClearMode): void
75
- viewport(size: [number, number]): void
76
+ viewport(size?: [number, number]): void
76
77
  drawArrays(key?: GLDrawMode): void
77
78
  drawElements(key?: GLDrawMode): void
78
79
  }
79
80
 
81
+ export type GLInitArg =
82
+ | string
83
+ | Partial<GLConfig>
84
+ | TemplateStringsArray
85
+
80
86
  export interface GLConfig {
81
87
  id: string
82
88
  frag: string
package/utils.ts CHANGED
@@ -1,3 +1,23 @@
1
+ /**
2
+ * utils
3
+ */
4
+ export function interleave(
5
+ strings: readonly string[],
6
+ args: any[]
7
+ ) {
8
+ let result = strings[0]
9
+ for (let i = 0, len = args.length; i < len; i += 1)
10
+ result += args[i] + strings[i + 1]
11
+ return result
12
+ }
13
+
14
+ export function isTemplateLiteral(strings: unknown): strings is string[] {
15
+ return Array.isArray(strings) && typeof strings[0] === "string"
16
+ }
17
+
18
+ /**
19
+ * graphics
20
+ */
1
21
  export function createShader(gl, source, type) {
2
22
  const shader = gl.createShader(type)
3
23
  gl.shaderSource(shader, source)
@@ -88,61 +108,4 @@ export function createTexture(gl, img) {
88
108
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
89
109
  gl.bindTexture(gl.TEXTURE_2D, null)
90
110
  return texture
91
- }
92
-
93
- export function lookAt(eye = [0, 0, 3], center = [0, 0, 0], up = [0, 1, 0]) {
94
- const ret = new Float32Array(16)
95
- const [ex, ey, ez] = eye
96
- const [cx, cy, cz] = center
97
- const [ux, uy, uz] = up
98
- let [z0, z1, z2] = [ex - cx, ey - cy, ez - cz]
99
- let lz = 1 / Math.sqrt(z0 ** 2 + z1 ** 2 + z2 ** 2)
100
- ;[z0, z1, z2] = !lz ? [0, 0, 0] : [z0 * lz, z1 * lz, z2 * lz]
101
- let [x0, x1, x2] = [uy * z2 - uz * z1, uz * z0 - ux * z2, ux * z1 - uy * z0]
102
- let lx = 1 / Math.sqrt(x0 ** 2 + x1 ** 2 + x2 ** 2)
103
- ;[x0, x1, x2] = !lx ? [0, 0, 0] : [x0 * lx, x1 * lx, x2 * lx]
104
- let [y0, y1, y2] = [z1 * x2 - z2 * x1, z2 * x0 - z0 * x2, z0 * x1 - z1 * x0]
105
- let ly = 1 / Math.sqrt(y0 ** 2 + y1 ** 2 + y2 ** 2)
106
- ;[y0, y1, y2] = !ly ? [0, 0, 0] : [y0 * ly, y1 * ly, y2 * ly]
107
- ret[0] = x0
108
- ret[1] = y0
109
- ret[2] = z0
110
- ret[3] = 0
111
- ret[4] = x1
112
- ret[5] = y1
113
- ret[6] = z1
114
- ret[7] = 0
115
- ret[8] = x2
116
- ret[9] = y2
117
- ret[10] = z2
118
- ret[11] = 0
119
- ret[12] = -(x0 * ex + x1 * ey + x2 * ez)
120
- ret[13] = -(y0 * ex + y1 * ey + y2 * ez)
121
- ret[14] = -(z0 * ex + z1 * ey + z2 * ez)
122
- ret[15] = 1
123
- return ret
124
- }
125
-
126
- export function perspective(fovy = 60, aspect = 1, near = 0.1, far = 10) {
127
- const ret = new Float32Array(16)
128
- const r = 1 / Math.tan((fovy * Math.PI) / 360)
129
- const d = far - near
130
- ret[0] = r / aspect
131
- ret[1] = ret[2] = ret[3] = ret[4] = 0
132
- ret[6] = ret[7] = ret[8] = ret[9] = 0
133
- ret[12] = ret[13] = ret[15] = 0
134
- ret[5] = r
135
- ret[10] = -(far + near) / d
136
- ret[11] = -1
137
- ret[14] = -(far * near * 2) / d
138
- return ret
139
- }
140
-
141
- export function identity() {
142
- const ret = new Float32Array(16)
143
- ret[1] = ret[2] = ret[3] = ret[4] = 0
144
- ret[6] = ret[7] = ret[8] = ret[9] = 0
145
- ret[11] = ret[12] = ret[13] = ret[14] = 0
146
- ret[0] = ret[5] = ret[10] = ret[15] = 1
147
- return ret
148
- }
111
+ }