diff --git a/dist/dwelibs.min.js b/dist/dwelibs.min.js index 9e165ea..3fa67b5 100644 --- a/dist/dwelibs.min.js +++ b/dist/dwelibs.min.js @@ -1 +1 @@ -!function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";window.DWE={};n(1),n(2),n(3)},function(t,e,n){"use strict";DWE.context=function(t){return t.star=function(e,n,r,i,o){var a=Math.PI/2*3,u=e,c=n,h=Math.PI/r;t.beginPath(),t.moveTo(e,n-i);for(var s=0;sthis.width/2+t.r)&&(!(e.y>this.height/2+t.r)&&(e.x<=this.width/2||(e.y<=this.height/2||Math.pow(e.x-this.width/2,2)+Math.pow(e.y-this.height/2,2)<=Math.pow(t.r,2))))}}]),t}(),DWE.Math.Circle=function(){function t(e,n,r){i(this,t),this.x=e,this.y=n,this.r=r}return r(t,[{key:"radius",get:function(){return this.r}}]),t}()},function(t,e,n){"use strict";DWE.getMousePosition=function(t,e){var n=void 0,r=void 0;if(t.changedTouches){var i=t.changedTouches[0];i&&(t.pageX=i.pageX,t.pageY=i.pageY)}return t.pageX||t.pageY?(n=t.pageX,r=t.pageY):(n=t.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,r=t.clientY+document.body.scrollTop+document.documentElement.scrollTop),{x:n-=e.offsetLeft,y:r-=e.offsetTop}}}]); \ No newline at end of file +!function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(r,i,function(e){return t[e]}.bind(null,i));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";window.DWE={};n(1),n(2),n(3),n(4)},function(t,e,n){"use strict";DWE.context=function(t){return t.star=function(e,n,r,i,o){var a=Math.PI/2*3,h=e,c=n,u=Math.PI/r;t.beginPath(),t.moveTo(e,n-i);for(var s=0;sthis.width/2+t.r)&&(!(e.y>this.height/2+t.r)&&(e.x<=this.width/2||(e.y<=this.height/2||Math.pow(e.x-this.width/2,2)+Math.pow(e.y-this.height/2,2)<=Math.pow(t.r,2))))}}]),t}(),DWE.Math.Circle=function(){function t(e,n,r){i(this,t),this.x=e,this.y=n,this.r=r}return r(t,[{key:"radius",get:function(){return this.r}}]),t}()},function(t,e,n){"use strict";DWE.getMousePosition=function(t,e){var n=void 0,r=void 0;if(t.changedTouches){var i=t.changedTouches[0];i&&(t.pageX=i.pageX,t.pageY=i.pageY)}return t.pageX||t.pageY?(n=t.pageX,r=t.pageY):(n=t.clientX+document.body.scrollLeft+document.documentElement.scrollLeft,r=t.clientY+document.body.scrollTop+document.documentElement.scrollTop),n-=e.offsetLeft,r-=e.offsetTop,new DWE.Math.Vector2(n,r)}},function(t,e,n){"use strict";DWE.Image={},DWE.Image.Image=function(t){var e=this;this.src=t,this.img=new Image,this.img.src=this.src,this.width=0,this.height=0,this.loaded=!1,this.errored=!1,this.img.onload=function(){e.loaded=!0,e.width=e.img.width,e.height=e.img.height,e.onload&&e.onload.apply(e,[])},this.img.onerror=function(t){e.loaded=!1,e.errored=!0,e.onerror&&e.onerror.apply(e,[t])}},DWE.Image.loadImageArray=function(t){return new Promise(function(e,n){var r=0,i=t.length,o=[];!function a(h){var c=t[h],u=new DWE.Image.Image(c);u.onload=function(){o.push(u),++r==i?e(o):a(r)},u.onerror=function(t){n(t)}}(r)})}}]); \ No newline at end of file diff --git a/src/image.js b/src/image.js new file mode 100644 index 0000000..5bb9143 --- /dev/null +++ b/src/image.js @@ -0,0 +1,63 @@ + +DWE.Image = {} + +DWE.Image.Image = function (src) { + this.src = src + this.img = new Image() + this.img.src = this.src + + this.width = 0 + this.height = 0 + + this.loaded = false + this.errored = false + + this.img.onload = () => { + this.loaded = true + this.width = this.img.width + this.height = this.img.height + + if (this['onload']) { + this.onload.apply(this, []) + } + } + + this.img.onerror = (e) => { + this.loaded = false + this.errored = true + + if (this['onerror']) { + this.onerror.apply(this, [e]) + } + } +} + +// Load an array of image URLs +// Returns a Promise +DWE.Image.loadImageArray = function (uris) { + return new Promise(function (resolve, reject) { + let loaded = 0 + let toLoad = uris.length + let images = [] + + function loadNext(index) { + let uri = uris[index] + let img = new DWE.Image.Image(uri) + img.onload = function () { + images.push(img) + loaded++ + + if (loaded == toLoad) + resolve(images) + else + loadNext(loaded) + } + + img.onerror = function (e) { + reject(e) + } + } + + loadNext(loaded) + }) +} diff --git a/src/index.js b/src/index.js index b4a34a8..7c7aa89 100644 --- a/src/index.js +++ b/src/index.js @@ -8,3 +8,6 @@ require('./math.js') // Mouse helpers require('./mouse.js') + +// Image helpers +require('./image.js') diff --git a/src/mouse.js b/src/mouse.js index 0d878f1..ddf04e0 100644 --- a/src/mouse.js +++ b/src/mouse.js @@ -21,5 +21,5 @@ DWE.getMousePosition = function (e, canvas) { x -= canvas.offsetLeft y -= canvas.offsetTop - return {x: x, y: y} + return new DWE.Math.Vector2(x, y) }