Flat, round, designer-friendly pseudo-3D engine for canvas & SVG
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

๐Ÿ›  remove Vector.sanitize

+33 -58
+2 -10
js/anchor.js
··· 20 20 }( this, function factory( utils, Vector ) { 21 21 22 22 var TAU = utils.TAU; 23 + var onePoint = { x: 1, y: 1, z: 1 }; 23 24 24 25 function Anchor( options ) { 25 26 this.create( options || {} ); ··· 33 34 // transform 34 35 this.translate = new Vector( options.translate ); 35 36 this.rotate = new Vector( options.rotate ); 36 - // scale 37 - if ( typeof options.scale == 'number' ) { 38 - // number, set all properties to number { x: 3, y: 3, z: 3 } 39 - this.scale = Vector.sanitize( {}, options.scale ); 40 - } else { 41 - // object, set undefined properties to 1 { x: 3, y: 1, z: 1 } 42 - this.scale = Vector.sanitize( options.scale, 1 ); 43 - } 44 - this.scale = new Vector( this.scale ); 45 - 37 + this.scale = new Vector( onePoint ).multiply( this.scale ); 46 38 // origin 47 39 this.origin = new Vector(); 48 40 this.renderOrigin = new Vector();
+31 -48
js/vector.js
··· 25 25 26 26 var TAU = utils.TAU; 27 27 28 + // 'pos' = 'position' 28 29 Vector.prototype.set = function( pos ) { 29 - pos = Vector.sanitize( pos ); 30 - this.x = pos.x; 31 - this.y = pos.y; 32 - this.z = pos.z; 30 + this.x = pos && pos.x || 0; 31 + this.y = pos && pos.y || 0; 32 + this.z = pos && pos.z || 0; 33 33 return this; 34 34 }; 35 35 ··· 37 37 // vec.write({ y: 2 }) only sets y coord 38 38 Vector.prototype.write = function( pos ) { 39 39 if ( !pos ) { 40 - return; 40 + return this; 41 41 } 42 42 this.x = pos.x != undefined ? pos.x : this.x; 43 43 this.y = pos.y != undefined ? pos.y : this.y; ··· 49 49 if ( !rotation ) { 50 50 return; 51 51 } 52 - rotation = Vector.sanitize( rotation ); 53 52 this.rotateZ( rotation.z ); 54 53 this.rotateY( rotation.y ); 55 54 this.rotateX( rotation.x ); ··· 69 68 }; 70 69 71 70 function rotateProperty( vec, angle, propA, propB ) { 72 - if ( angle % TAU === 0 ) { 71 + if ( !angle || angle % TAU === 0 ) { 73 72 return; 74 73 } 75 74 var cos = Math.cos( angle ); ··· 80 79 vec[ propB ] = b*cos + a*sin; 81 80 } 82 81 83 - Vector.prototype.add = function( vec ) { 84 - if ( !vec ) { 82 + Vector.prototype.add = function( pos ) { 83 + if ( !pos ) { 85 84 return this; 86 85 } 87 - vec = Vector.sanitize( vec ); 88 - this.x += vec.x; 89 - this.y += vec.y; 90 - this.z += vec.z; 86 + this.x += pos.x || 0; 87 + this.y += pos.y || 0; 88 + this.z += pos.z || 0; 91 89 return this; 92 90 }; 93 91 94 - Vector.prototype.subtract = function( vec ) { 95 - if ( !vec ) { 92 + Vector.prototype.subtract = function( pos ) { 93 + if ( !pos ) { 96 94 return this; 97 95 } 98 - vec = Vector.sanitize( vec ); 99 - this.x -= vec.x; 100 - this.y -= vec.y; 101 - this.z -= vec.z; 96 + this.x -= pos.x || 0; 97 + this.y -= pos.y || 0; 98 + this.z -= pos.z || 0; 102 99 return this; 103 100 }; 104 101 105 - Vector.prototype.multiply = function( value ) { 106 - if ( value === undefined ) { 102 + Vector.prototype.multiply = function( pos ) { 103 + if ( pos == undefined ) { 107 104 return this; 108 105 } 109 106 // multiple all values by same number 110 - if ( typeof value == 'number' ) { 111 - this.x *= value; 112 - this.y *= value; 113 - this.z *= value; 114 - return this; 107 + if ( typeof pos == 'number' ) { 108 + this.x *= pos; 109 + this.y *= pos; 110 + this.z *= pos; 111 + } else { 112 + // multiply object 113 + this.x *= pos.x != undefined ? pos.x : 1; 114 + this.y *= pos.y != undefined ? pos.y : 1; 115 + this.z *= pos.z != undefined ? pos.z : 1; 115 116 } 116 - // multiply object 117 - var vec = Vector.sanitize( value, 1 ); 118 - this.x *= vec.x; 119 - this.y *= vec.y; 120 - this.z *= vec.z; 121 117 return this; 122 118 }; 123 119 ··· 128 124 return this; 129 125 }; 130 126 131 - Vector.prototype.lerp = function( vec, t ) { 132 - vec = Vector.sanitize( vec ); 133 - this.x = utils.lerp( this.x, vec.x, t ); 134 - this.y = utils.lerp( this.y, vec.y, t ); 135 - this.z = utils.lerp( this.z, vec.z, t ); 127 + Vector.prototype.lerp = function( pos, t ) { 128 + this.x = utils.lerp( this.x, pos.x || 0, t ); 129 + this.y = utils.lerp( this.y, pos.y || 0, t ); 130 + this.z = utils.lerp( this.z, pos.z || 0, t ); 136 131 return this; 137 132 }; 138 133 ··· 156 151 157 152 Vector.prototype.copy = function() { 158 153 return new Vector( this ); 159 - }; 160 - 161 - // ----- utils ----- // 162 - 163 - // add missing properties 164 - Vector.sanitize = function( vec, value ) { 165 - vec = vec || {}; 166 - value = value || 0; 167 - vec.x = vec.x == undefined ? value : vec.x; 168 - vec.y = vec.y == undefined ? value : vec.y; 169 - vec.z = vec.z == undefined ? value : vec.z; 170 - return vec; 171 154 }; 172 155 173 156 return Vector;