isomer
Version:
A simple isometric graphics library for HTML5 canvas
2 lines • 9.48 kB
JavaScript
/*! Isomer v0.2.6 | (c) 2014 Jordan Scales | jdan.github.io/isomer/license.txt */
!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):"object"==typeof exports?exports.Isomer=n():t.Isomer=n()}(this,function(){return function(t){function n(r){if(e[r])return e[r].exports;var o=e[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,n),o.loaded=!0,o.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n,e){t.exports=e(5)},function(t,n,e){function r(t,n,e){return this instanceof r?(this.x="number"==typeof t?t:0,this.y="number"==typeof n?n:0,this.z="number"==typeof e?e:0,void 0):new r(t,n,e)}r.ORIGIN=new r(0,0,0),r.prototype.translate=function(t,n,e){return t="number"==typeof t?t:0,n="number"==typeof n?n:0,e="number"==typeof e?e:0,new r(this.x+t,this.y+n,this.z+e)},r.prototype.scale=function(t,n,e,r){var o=this.translate(-t.x,-t.y,-t.z);return void 0===e&&void 0===r?e=r=n:r="number"==typeof r?r:1,o.x*=n,o.y*=e,o.z*=r,o.translate(t.x,t.y,t.z)},r.prototype.rotateX=function(t,n){var e=this.translate(-t.x,-t.y,-t.z),r=e.z*Math.cos(n)-e.y*Math.sin(n),o=e.z*Math.sin(n)+e.y*Math.cos(n);return e.z=r,e.y=o,e.translate(t.x,t.y,t.z)},r.prototype.rotateY=function(t,n){var e=this.translate(-t.x,-t.y,-t.z),r=e.x*Math.cos(n)-e.z*Math.sin(n),o=e.x*Math.sin(n)+e.z*Math.cos(n);return e.x=r,e.z=o,e.translate(t.x,t.y,t.z)},r.prototype.rotateZ=function(t,n){var e=this.translate(-t.x,-t.y,-t.z),r=e.x*Math.cos(n)-e.y*Math.sin(n),o=e.x*Math.sin(n)+e.y*Math.cos(n);return e.x=r,e.y=o,e.translate(t.x,t.y,t.z)},r.prototype.depth=function(){return this.x+this.y-2*this.z},r.distance=function(t,n){var e=n.x-t.x,r=n.y-t.y,o=n.z-t.z;return Math.sqrt(e*e+r*r+o*o)},t.exports=r},function(t,n,e){function r(t){this.points="[object Array]"===Object.prototype.toString.call(t)?t:Array.prototype.slice.call(arguments)}var o=e(1);r.prototype.push=function(t){this.points.push(t)},r.prototype.reverse=function(){var t=Array.prototype.slice.call(this.points);return new r(t.reverse())},r.prototype.translate=function(){var t=arguments;return new r(this.points.map(function(n){return n.translate.apply(n,t)}))},r.prototype.rotateX=function(){var t=arguments;return new r(this.points.map(function(n){return n.rotateX.apply(n,t)}))},r.prototype.rotateY=function(){var t=arguments;return new r(this.points.map(function(n){return n.rotateY.apply(n,t)}))},r.prototype.rotateZ=function(){var t=arguments;return new r(this.points.map(function(n){return n.rotateZ.apply(n,t)}))},r.prototype.scale=function(){var t=arguments;return new r(this.points.map(function(n){return n.scale.apply(n,t)}))},r.prototype.depth=function(){var t,n=0;for(t=0;t<this.points.length;t++)n+=this.points[t].depth();return n/(this.points.length||1)},r.Rectangle=function(t,n,e){void 0===n&&(n=1),void 0===e&&(e=1);var i=new r([t,new o(t.x+n,t.y,t.z),new o(t.x+n,t.y+e,t.z),new o(t.x,t.y+e,t.z)]);return i},r.Circle=function(t,n,e){e=e||20;var i,s=new r;for(i=0;e>i;i++)s.push(new o(n*Math.cos(2*i*Math.PI/e),n*Math.sin(2*i*Math.PI/e),0));return s.translate(t.x,t.y,t.z)},r.Star=function(t,n,e,i){var s,a,h=new r;for(s=0;2*i>s;s++)a=s%2===0?n:e,h.push(new o(a*Math.cos(s*Math.PI/i),a*Math.sin(s*Math.PI/i),0));return h.translate(t.x,t.y,t.z)},t.exports=r},function(t,n,e){function r(t){this.elem=t,this.ctx=this.elem.getContext("2d"),this.width=t.width,this.height=t.height}r.prototype.clear=function(){this.ctx.clearRect(0,0,this.width,this.height)},r.prototype.path=function(t,n){this.ctx.beginPath(),this.ctx.moveTo(t[0].x,t[0].y);for(var e=1;e<t.length;e++)this.ctx.lineTo(t[e].x,t[e].y);this.ctx.closePath(),this.ctx.save(),this.ctx.globalAlpha=n.a,this.ctx.fillStyle=this.ctx.strokeStyle=n.toHex(),this.ctx.stroke(),this.ctx.fill(),this.ctx.restore()},t.exports=r},function(t,n,e){function r(t,n,e,r){this.r=parseInt(t||0),this.g=parseInt(n||0),this.b=parseInt(e||0),this.a=parseFloat(Math.round(100*r)/100||1),this.loadHSL()}r.prototype.toHex=function(){var t=(256*this.r*256+256*this.g+this.b).toString(16);return t.length<6&&(t=new Array(6-t.length+1).join("0")+t),"#"+t},r.prototype.lighten=function(t,n){n=n||new r(255,255,255);var e=new r(n.r/255*this.r,n.g/255*this.g,n.b/255*this.b,this.a);return e.l=Math.min(e.l+t,1),e.loadRGB(),e},r.prototype.loadHSL=function(){var t,n,e=this.r/255,r=this.g/255,o=this.b/255,i=Math.max(e,r,o),s=Math.min(e,r,o),a=(i+s)/2;if(i===s)t=n=0;else{var h=i-s;switch(n=a>.5?h/(2-i-s):h/(i+s),i){case e:t=(r-o)/h+(o>r?6:0);break;case r:t=(o-e)/h+2;break;case o:t=(e-r)/h+4}t/=6}this.h=t,this.s=n,this.l=a},r.prototype.loadRGB=function(){var t,n,e,r=this.h,o=this.s,i=this.l;if(0===o)t=n=e=i;else{var s=.5>i?i*(1+o):i+o-i*o,a=2*i-s;t=this._hue2rgb(a,s,r+1/3),n=this._hue2rgb(a,s,r),e=this._hue2rgb(a,s,r-1/3)}this.r=parseInt(255*t),this.g=parseInt(255*n),this.b=parseInt(255*e)},r.prototype._hue2rgb=function(t,n,e){return 0>e&&(e+=1),e>1&&(e-=1),1/6>e?t+6*(n-t)*e:.5>e?n:2/3>e?t+(n-t)*(2/3-e)*6:t},t.exports=r},function(t,n,e){function r(t,n){n=n||{},this.canvas=new o(t),this.angle=Math.PI/6,this.scale=n.scale||70,this._calculateTransformation(),this.originX=n.originX||this.canvas.width/2,this.originY=n.originY||.9*this.canvas.height,this.lightPosition=n.lightPosition||new p(2,-1,3),this.lightAngle=this.lightPosition.normalize(),this.colorDifference=.2,this.lightColor=n.lightColor||new i(255,255,255)}var o=e(3),i=e(4),s=e(2),a=e(1),h=e(6),p=e(7);r.prototype.setLightPosition=function(t,n,e){this.lightPosition=new p(t,n,e),this.lightAngle=this.lightPosition.normalize()},r.prototype._translatePoint=function(t){var n=new a(t.x*this.transformation[0][0],t.x*this.transformation[0][1]),e=new a(t.y*this.transformation[1][0],t.y*this.transformation[1][1]),r=this.originX+n.x+e.x,o=this.originY-n.y-e.y-t.z*this.scale;return new a(r,o)},r.prototype.add=function(t,n){if("[object Array]"==Object.prototype.toString.call(t))for(var e=0;e<t.length;e++)this.add(t[e],n);else if(t instanceof s)this._addPath(t,n);else if(t instanceof h)for(var r=t.orderedPaths(),o=0;o<r.length;o++)this._addPath(r[o],n)},r.prototype._addPath=function(t,n){n=n||new i(120,120,120);var e=p.fromTwoPoints(t.points[1],t.points[0]),r=p.fromTwoPoints(t.points[2],t.points[1]),o=p.crossProduct(e,r).normalize(),s=p.dotProduct(o,this.lightAngle),a=n.lighten(s*this.colorDifference,this.lightColor);this.canvas.path(t.points.map(this._translatePoint.bind(this)),a)},r.prototype._calculateTransformation=function(){this.transformation=[[this.scale*Math.cos(this.angle),this.scale*Math.sin(this.angle)],[this.scale*Math.cos(Math.PI-this.angle),this.scale*Math.sin(Math.PI-this.angle)]]},r.Canvas=o,r.Color=i,r.Path=s,r.Point=a,r.Shape=h,r.Vector=p,t.exports=r},function(t,n,e){function r(t){this.paths="[object Array]"===Object.prototype.toString.call(t)?t:Array.prototype.slice.call(arguments)}var o=e(2),i=e(1);r.prototype.push=function(t){this.paths.push(t)},r.prototype.translate=function(){var t=arguments;return new r(this.paths.map(function(n){return n.translate.apply(n,t)}))},r.prototype.rotateX=function(){var t=arguments;return new r(this.paths.map(function(n){return n.rotateX.apply(n,t)}))},r.prototype.rotateY=function(){var t=arguments;return new r(this.paths.map(function(n){return n.rotateY.apply(n,t)}))},r.prototype.rotateZ=function(){var t=arguments;return new r(this.paths.map(function(n){return n.rotateZ.apply(n,t)}))},r.prototype.scale=function(){var t=arguments;return new r(this.paths.map(function(n){return n.scale.apply(n,t)}))},r.prototype.orderedPaths=function(){var t=this.paths.slice();return t.sort(function(t,n){return n.depth()-t.depth()})},r.extrude=function(t,n){n="number"==typeof n?n:1;var e,i=t.translate(0,0,n),s=new r;for(s.push(t.reverse()),s.push(i),e=0;e<t.points.length;e++)s.push(new o([i.points[e],t.points[e],t.points[(e+1)%t.points.length],i.points[(e+1)%i.points.length]]));return s},r.Prism=function(t,n,e,s){n="number"==typeof n?n:1,e="number"==typeof e?e:1,s="number"==typeof s?s:1;var a=new r,h=new o([t,new i(t.x+n,t.y,t.z),new i(t.x+n,t.y,t.z+s),new i(t.x,t.y,t.z+s)]);a.push(h),a.push(h.reverse().translate(0,e,0));var p=new o([t,new i(t.x,t.y,t.z+s),new i(t.x,t.y+e,t.z+s),new i(t.x,t.y+e,t.z)]);a.push(p),a.push(p.reverse().translate(n,0,0));var u=new o([t,new i(t.x+n,t.y,t.z),new i(t.x+n,t.y+e,t.z),new i(t.x,t.y+e,t.z)]);return a.push(u.reverse()),a.push(u.translate(0,0,s)),a},r.Pyramid=function(t,n,e,s){n="number"==typeof n?n:1,e="number"==typeof e?e:1,s="number"==typeof s?s:1;var a=new r,h=new o([t,new i(t.x+n,t.y,t.z),new i(t.x+n/2,t.y+e/2,t.z+s)]);a.push(h),a.push(h.rotateZ(t.translate(n/2,e/2),Math.PI));var p=new o([t,new i(t.x+n/2,t.y+e/2,t.z+s),new i(t.x,t.y+e,t.z)]);return a.push(p),a.push(p.rotateZ(t.translate(n/2,e/2),Math.PI)),a},r.Cylinder=function(t,n,e,i){n="number"==typeof n?n:1;var s=o.Circle(t,n,e),a=r.extrude(s,i);return a},t.exports=r},function(t,n,e){function r(t,n,e){this.i="number"==typeof t?t:0,this.j="number"==typeof n?n:0,this.k="number"==typeof e?e:0}r.fromTwoPoints=function(t,n){return new r(n.x-t.x,n.y-t.y,n.z-t.z)},r.crossProduct=function(t,n){var e=t.j*n.k-n.j*t.k,o=-1*(t.i*n.k-n.i*t.k),i=t.i*n.j-n.i*t.j;return new r(e,o,i)},r.dotProduct=function(t,n){return t.i*n.i+t.j*n.j+t.k*n.k},r.prototype.magnitude=function(){return Math.sqrt(this.i*this.i+this.j*this.j+this.k*this.k)},r.prototype.normalize=function(){var t=this.magnitude();return 0===t?new r(0,0,0):new r(this.i/t,this.j/t,this.k/t)},t.exports=r}])});