From 659578bf28a0b6eaee0bf5b39e4f4e8de30882f8 Mon Sep 17 00:00:00 2001 From: John Chipps-Harding Date: Sun, 20 Oct 2019 22:25:18 +0100 Subject: [PATCH 1/5] Cancelable Animation --- src/GlslCanvas.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/GlslCanvas.js b/src/GlslCanvas.js index 382041d..b43d619 100644 --- a/src/GlslCanvas.js +++ b/src/GlslCanvas.js @@ -55,6 +55,7 @@ export default class GlslCanvas { this.uniforms = {}; this.vbo = {}; this.isValid = false; + this.animationFrameRequest = undefined; this.BUFFER_COUNT = 0; // this.TEXTURE_COUNT = 0; @@ -173,7 +174,7 @@ void main(){ } sandbox.render(); - window.requestAnimationFrame(RenderLoop); + sandbox.animationFrameRequest = window.requestAnimationFrame(RenderLoop); } // Start @@ -183,6 +184,8 @@ void main(){ } destroy() { + cancelAnimationFrame(this.animationFrameRequest); + this.animated = false; this.isValid = false; for (let tex in this.textures) { From 39151a443a81612f0ff38ecaecfc7311ccc8f6e7 Mon Sep 17 00:00:00 2001 From: John Chipps-Harding Date: Sun, 20 Oct 2019 22:26:44 +0100 Subject: [PATCH 2/5] Build --- dist/GlslCanvas.es.js | 5 ++++- dist/GlslCanvas.js | 5 ++++- dist/GlslCanvas.min.js | 2 +- dist/GlslCanvas.min.js.map | 2 +- lib/GlslCanvas.js | 5 ++++- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/dist/GlslCanvas.es.js b/dist/GlslCanvas.es.js index 4043d02..229d05a 100644 --- a/dist/GlslCanvas.es.js +++ b/dist/GlslCanvas.es.js @@ -1000,6 +1000,7 @@ var GlslCanvas = function () { this.uniforms = {}; this.vbo = {}; this.isValid = false; + this.animationFrameRequest = undefined; this.BUFFER_COUNT = 0; // this.TEXTURE_COUNT = 0; @@ -1092,7 +1093,7 @@ var GlslCanvas = function () { } sandbox.render(); - window.requestAnimationFrame(RenderLoop); + sandbox.animationFrameRequest = window.requestAnimationFrame(RenderLoop); } // Start @@ -1104,6 +1105,8 @@ var GlslCanvas = function () { createClass(GlslCanvas, [{ key: 'destroy', value: function destroy() { + cancelAnimationFrame(this.animationFrameRequest); + this.animated = false; this.isValid = false; for (var tex in this.textures) { diff --git a/dist/GlslCanvas.js b/dist/GlslCanvas.js index 9cd18ee..6342a5e 100644 --- a/dist/GlslCanvas.js +++ b/dist/GlslCanvas.js @@ -1405,6 +1405,7 @@ var GlslCanvas = function () { this.uniforms = {}; this.vbo = {}; this.isValid = false; + this.animationFrameRequest = undefined; this.BUFFER_COUNT = 0; // this.TEXTURE_COUNT = 0; @@ -1497,7 +1498,7 @@ var GlslCanvas = function () { } sandbox.render(); - window.requestAnimationFrame(RenderLoop); + sandbox.animationFrameRequest = window.requestAnimationFrame(RenderLoop); } // Start @@ -1509,6 +1510,8 @@ var GlslCanvas = function () { createClass(GlslCanvas, [{ key: 'destroy', value: function destroy() { + cancelAnimationFrame(this.animationFrameRequest); + this.animated = false; this.isValid = false; for (var tex in this.textures) { diff --git a/dist/GlslCanvas.min.js b/dist/GlslCanvas.min.js index 6ca51fb..5151500 100644 --- a/dist/GlslCanvas.min.js +++ b/dist/GlslCanvas.min.js @@ -1,2 +1,2 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.GlslCanvas=t()}(this,function(){"use strict";var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};var t="undefined"!=typeof window?window:void 0!==e?e:"undefined"!=typeof self?self:{},r=function(e){var t=i.call(e);return"[object Function]"===t||"function"==typeof e&&"[object RegExp]"!==t||"undefined"!=typeof window&&(e===window.setTimeout||e===window.alert||e===window.confirm||e===window.prompt)},i=Object.prototype.toString;var n,a=(function(e,t){(t=e.exports=function(e){return e.replace(/^\s*|\s*$/g,"")}).left=function(e){return e.replace(/^\s*/,"")},t.right=function(e){return e.replace(/\s*$/,"")}}(n={exports:{}},n.exports),n.exports),s=function(e,t,i){if(!r(t))throw new TypeError("iterator must be a function");arguments.length<3&&(i=this);"[object Array]"===o.call(e)?function(e,t,r){for(var i=0,n=e.length;i0&&(u=setTimeout(function(){if(!s){s=!0,o.abort("timeout");var e=new Error("XMLHttpRequest timeout");e.code="ETIMEDOUT",i(e)}},e.timeout)),o.setRequestHeader)for(a in c)c.hasOwnProperty(a)&&o.setRequestHeader(a,c[a]);else if(e.headers&&!function(e){for(var t in e)if(e.hasOwnProperty(t))return!1;return!0}(e.headers))throw new Error("Headers cannot be set on an XDomainRequest object");return"responseType"in e&&(o.responseType=e.responseType),"beforeSend"in e&&"function"==typeof e.beforeSend&&e.beforeSend(o),o.send(d||null),o}v.XMLHttpRequest=t.XMLHttpRequest||function(){},v.XDomainRequest="withCredentials"in new v.XMLHttpRequest?v.XMLHttpRequest:t.XDomainRequest,function(e,t){for(var r=0;r\n\n
\n
'+n+"
\n
\n\n"))}if(!window.WebGLRenderingContext)return i(x,_),null;var n=function(e,t){for(var r=["webgl","experimental-webgl"],i=null,n=0;n1?r-1:0),n=1;n2&&void 0!==arguments[2]?arguments[2]:{};T(this,e),L(this),this.gl=t,this.texture=t.createTexture(),this.texture&&(this.valid=!0),this.bind(),this.name=r,this.source=null,this.sourceType=null,this.loading=null,this.setData(1,1,new Uint8Array([0,0,0,255]),{filtering:"linear"}),this.setFiltering(i.filtering),this.load(i)}return p(e,[{key:"destroy",value:function(){this.valid&&(this.gl.deleteTexture(this.texture),this.texture=null,delete this.data,this.data=null,this.valid=!1)}},{key:"bind",value:function(t){this.valid&&("number"==typeof t&&e.activeUnit!==t&&(this.gl.activeTexture(this.gl.TEXTURE0+t),e.activeUnit=t),e.activeTexture!==this.texture&&(this.gl.bindTexture(this.gl.TEXTURE_2D,this.texture),e.activeTexture=this.texture))}},{key:"load",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.loading=null,"string"==typeof e.url?void 0!==this.url&&e.url===this.url||this.setUrl(e.url,e):e.element?this.setElement(e.element,e):e.data&&e.width&&e.height&&this.setData(e.width,e.height,e.data,e)}},{key:"setUrl",value:function(e){var t=this,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(this.valid)return this.url=e,this.source=this.url,this.sourceType="url",this.loading=new Promise(function(i,n){var a=e.split(".").pop().toLowerCase(),s="ogv"===a||"webm"===a||"mp4"===a,o=void 0;s?((o=document.createElement("video")).autoplay=!0,r.filtering="nearest"):o=new Image,o.onload=function(){try{t.setElement(o,r)}catch(e){console.log("Texture '"+t.name+"': failed to load url: '"+t.source+"'",e,r)}i(t)},o.onerror=function(e){console.log("Texture '"+t.name+"': failed to load url: '"+t.source+"'",e,r),i(t)},/^((?!chrome|android).)*safari/i.test(navigator.userAgent)&&"data:"===t.source.slice(0,5)||(o.crossOrigin="anonymous"),o.src=t.source,s&&t.setElement(o,r)}),this.loading}},{key:"setData",value:function(e,t,r){var i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{};return this.width=e,this.height=t,this.source=r,this.sourceType="data",this.update(i),this.setFiltering(i),this.loading=Promise.resolve(this),this.loading}},{key:"setElement",value:function(e,t){var r=this,i=e;if("string"==typeof e&&(e=document.querySelector(e)),e instanceof HTMLCanvasElement||e instanceof HTMLImageElement||e instanceof HTMLVideoElement)this.source=e,this.sourceType="element",e instanceof HTMLVideoElement?(e.addEventListener("canplaythrough",function(){r.intervalID=setInterval(function(){r.update(t)},15)},!0),e.addEventListener("ended",function(){e.currentTime=0,e.play()},!0)):this.update(t),this.setFiltering(t);else{var n="the 'element' parameter (`element: "+JSON.stringify(i)+"`) must be a CSS ";n+="selector string, or a , or