{"id":19564278,"url":"https://github.com/ronenness/vector2js","last_synced_at":"2025-04-27T00:32:51.604Z","repository":{"id":57391322,"uuid":"63515041","full_name":"RonenNess/Vector2js","owner":"RonenNess","description":"Simple 2D Vectors for JS.","archived":false,"fork":false,"pushed_at":"2018-11-12T20:36:46.000Z","size":979,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-04T19:07:23.434Z","etag":null,"topics":["2d-vector","javascript-library","nodejs","vector-math","vector2d"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RonenNess.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-17T04:41:01.000Z","updated_at":"2025-04-04T10:09:50.000Z","dependencies_parsed_at":"2022-08-25T18:41:59.762Z","dependency_job_id":null,"html_url":"https://github.com/RonenNess/Vector2js","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2FVector2js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2FVector2js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2FVector2js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RonenNess%2FVector2js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RonenNess","download_url":"https://codeload.github.com/RonenNess/Vector2js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251073058,"owners_count":21532005,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2d-vector","javascript-library","nodejs","vector-math","vector2d"],"created_at":"2024-11-11T05:21:10.104Z","updated_at":"2025-04-27T00:32:50.769Z","avatar_url":"https://github.com/RonenNess.png","language":"JavaScript","readme":"![Vector2js](./misc/logo.png)\n=============================\n\n# Vector2js\n[A simple 2D Vectors for JS](https://ronenness.github.io/Vector2js/).\n\n## Read the docs\n\nFull docs can be found [here](https://ronenness.github.io/Vector2js/).\n\n## Install\n\nYou can install via npm:\n\n```\nnpm install vector2js\n```\n\nOr bower:\n\n```\nbower install vector2js\n```\n\nOr simply download the js files from /dist/ in this repo.\n\n## Usage\n\nIf you are using [nodejs](https://nodejs.org/en/) or [browserify](http://browserify.org/), you can use Vector2js like this:\n\n```javascript\n// include vector2js\nvar Vector = require('vector2js');\n\n// create a new vector\nvar vec = new Vector(10, 10);\n```\n\nIf you include the JavaScript from /dist/ directly into your web pages, you will have 'Vector' in global scope and you can create new vectors like this:\n\n```javascript\n// create a new vector\nvar vec = new Vector(10, 10);\n```\n\n### Basic usage example:\n\nHere's a basic usage example - adding two vectors and print the result length:\n\n```javascript\nvar v1 = new Vector(5, 10);\nvar v2 = new Vector(1, 1);\nconsole.log(v1.add(v2).length())\n\u003e 12.529964086141668\n```\n\n## Tests \u0026 Browser support\n\nVector2js is fully testes on Node.js, IE6 \u0026 above, Edge, Chrome, FireFox and Opera, and have coverage of 100%. \n\n![BrowsersSupport](./misc/supported.png)\n\n### Test in browsers\n\nTo test in browsers just open the file `/tests/test.html` with your browser of choice.\n\n### Test in Node.js\n\nTo test in nodejs first install [qunit-cli](https://www.npmjs.com/package/qunit-cli):\n\n```\nnpm install qunit-cli\n```\n\nThen you can run the following command from the project root:\n\n```\nqunit-cli tests/test.js\n```\n\n## API\n\nThe following are all the API functions the vector class has:\n\n```js\n.clone ()\n\t[chainable][clone]\n\tClone the vector and return the cloned instance.\n\tReturn: Cloned Vector.\n\n.equals (vector)\n\tGet if equal to another vector\n\tParam vector - other vector to compare with.\n\tReturn: If Vectors Are Equal.\n\n.copy (vector)\n\t[chainable][changeSelf]\n\tSet values from another vector. this changes value of self.\n\tParam vector - other vector to get values from.\n\tReturn: Self.\n\n.copyX (vector)\n\t[chainable][changeSelf]\n\tSet only the x component from another vector.\n\tReturn: Self.\n\n.copyY (vector)\n\t[chainable][changeSelf]\n\tSet only the y component from another vector.\n\tReturn: Self.\n\n.toDict ()\n\tConvert to a dictionary with {x, y}.\n\tReturn: A Dictionary Representation Of The Vector.\n\n.toArray ()\n\tConvert to array with x, y.\n\tReturn: An Array Representation Of The Vector.\n\n.set (x, y)\n\t[chainable][changeSelf]\n\tSet values from x, y.\n\tParam x - optional x component to set.\n\tParam y - optional y component to set.\n\tReturn: Self.\n\n.flipXY ()\n\t[chainable][clone]\n\tClone and flip between x and y values.\n\tReturn: Cloned Vector With Flipped X And Y Components.\n\n.flipXYSelf ()\n\t[chainable][changeSelf]\n\tFlip between x and y values.\n\tReturn: Self.\n\n.invert ()\n\t[chainable][clone]\n\tClone and invert x and y values (like multiply with -1, eg x, y =\u003e -x, -y)\n\tReturn: Cloned Vector With Inverted Values.\n\n.invertSelf ()\n\t[chainable][changeSelf]\n\tInvert x and y values (like multiply with -1, eg x, y =\u003e -x, -y)\n\tReturn: Self.\n\n.distanceFrom (other)\n\tGet the distance from another vector.\n\tParam other - vector to get distance from.\n\tReturn: Distance Between Vectors.\n\n.radiansTo (other)\n\tGet angle from another vector in radians.\n\tReturn: Angle In Radians From This To Other.\n\n.degreesTo (other)\n\tGet degrees from another vector in degrees.\n\tReturn: Angle In Degrees From This To Other.\n\n.toRadians (other)\n\tConvert this vector to a radian angle.\n\tThis is equivalent to doing vector.zero.radiansto(this).\n\tReturn: Angle In Radians.\n\n.toDegrees (other)\n\tConvert this vector to degree.\n\tThis is equivalent to doing vector.zero.degreeto(this).\n\tReturn: Angle In Degrees (0-360).\n\n.rotateDegreesSelf (degrees)\n\t[chainable][changeSelf]\n\tRotate this vector by a given degree.\n\tParam degrees - degrees to rotate this vector by (can be positive or negative).\n\tReturn: Self.\n\n.rotateDegrees (degrees)\n\t[chainable]\n\tClone and rotate the vector by a given degree.\n\tParam degrees - degree to rotate this vector by (can be positive or negative).\n\tReturn: Cloned Rotated Vector.\n\n.rotateRadiansSelf (radians)\n\t[chainable][changeSelf]\n\tRotate this vector by a given radian.\n\tParam radians - radians to rotate this vector by (can be positive or negative).\n\tReturn: Self.\n\n.rotateRadians (radians)\n\t[chainable]\n\tClone and rotate the vector by a given degree.\n\tParam radians - radians to rotate this vector by (can be positive or negative).\n\tReturn: Cloned Rotated Vector.\n\n.length ()\n\tCalculate the length of this vector (aka magnitude).\n\tReturn: Vector Length.\n\n.normalizeSelf ()\n\t[chainable][changeSelf]\n\tNormalize this vector, eg make length equal 1.\n\tReturn: Self.\n\n.normalize ()\n\t[chainable][clone]\n\tClone and normalize the vector.\n\tReturn: Normalized Vector.\n\n.addSelf (other)\n\t[chainable][changeSelf]\n\tAdd other vector to self.\n\tFor example, v(10, 11) + v(5, 6) = v(15, 17).\n\tParam other - vector to add components to self.\n\tReturn: Self.\n\n.subSelf (other)\n\t[chainable][changeSelf]\n\tSubtract other vector from self.\n\tFor example, v(10, 10) - v(2, 3) = v(8, 7).\n\tParam other - vector to subtract components from self.\n\tReturn: Self.\n\n.divSelf (other)\n\t[chainable][changeSelf]\n\tDivide self by other vector.\n\tFor example, v(10, 20) / v(2, 5) = v(5, 4).\n\tParam other - vector to divide components from self.\n\tReturn: Self.\n\n.mulSelf (other)\n\t[chainable][changeSelf]\n\tMultiply self vector by other vector.\n\tFor example, v(2, 3) * v(3, 4) = v(6, 12).\n\tParam other - vector to multiply components with self.\n\tReturn: Self.\n\n.addScalarSelf (val)\n\t[chainable][changeSelf]\n\tAdd scalar value to self.\n\tFor example, v(2, 3) + 5 = v(7, 8).\n\tParam val - value to add to components.\n\tReturn: Self.\n\n.subScalarSelf (val)\n\t[chainable][changeSelf]\n\tSubtract scalar from self.\n\tFor example, v(7, 9) - 5 = v(3, 4).\n\tParam val - value to subtract from components.\n\tReturn: Self.\n\n.divScalarSelf (val)\n\t[chainable][changeSelf]\n\tDivide self by scalar.\n\tFor example, v(6, 8) / 5 = v(3, 4).\n\tParam val - value to divide components by.\n\tReturn: Self.\n\n.mulScalarSelf (val)\n\t[chainable][changeSelf]\n\tMultiply self by scalar.\n\tFor example, v(2, 3) * 2 = v(4, 6).\n\tParam val - value to multiply components with.\n\tReturn: Self.\n\n.add (other)\n\t[chainable][clone]\n\tClone self and add other vector to it.\n\tParam other - vector to add with.\n\tReturn: Cloned Vector.\n\n.sub (other)\n\t[chainable][clone]\n\tClone self and subtract other vector from it.\n\tParam other - vector to subtract with.\n\tReturn: Cloned Vector.\n\n.mul (other)\n\t[chainable][clone]\n\tClone self and multiply by other vector.\n\tParam other - vector to multiply with.\n\tReturn: Cloned Vector.\n\n.div (other)\n\t[chainable][clone]\n\tClone self and divide by other vector.\n\tParam other - vector to divide with.\n\tParam scalar - value to divide by.\n\tReturn: Cloned Vector.\n\n.addScalar (scalar)\n\t[chainable][clone]\n\tClone self and add scalar to it.\n\tParam scalar - value to add.\n\tReturn: Cloned Vector.\n\n.subScalar (scalar)\n\t[chainable][clone]\n\tClone self and substract scalar from it.\n\tParam scalar - value to subtract.\n\tReturn: Cloned Vector.\n\n.mulScalar (scalar)\n\t[chainable][clone]\n\tClone self and multiply by scalar.\n\tParam scalar - value to multiply with.\n\tReturn: Cloned Vector.\n\n.divScalar (scalar)\n\t[chainable][clone]\n\tClone self and divide by scalar.\n\tParam scalar - value to divide by.\n\tReturn: Cloned Vector.\n\n.clampSelf (min, max)\n\t[chainable][changeSelf]\n\tClamp vector values into range.\n\tNote: this function does not validate that min \u003c max.\n\tParam min - min value for x, y components.\n\tParam max - max value for x, y components.\n\tReturn: Self.\n\n.clamp (min, max)\n\t[chainable][clone]\n\tClone vector and clamp its values.\n\tNote: this function does not validate that min \u003c max.\n\tParam min - min value for x, y components.\n\tParam max - max value for x, y components.\n\tReturn: Cloned Vector In Range.\n\n.applySelf (func)\n\t[chainable][changeSelf]\n\tApply a function on x and y components of the vector.\n\tFor example, you can use math.round to round the vector x, y values.\n\tParam func - function to apply on components.\n\tReturn: Self.\n\n.apply (func)\n\t[chainable][clone]\n\tClone self and apply a function on x and y components of the clone vector.\n\tFor example, you can use math.round to round the vector x, y values.\n\tParam func - function to apply on components.\n\tReturn: Cloned Vector.\n\n.absSelf ()\n\t[chainable][changeSelf]\n\tTurn self to absolute values (eg turn x, y positive).\n\tReturn: Self.\n\n.abs ()\n\t[chainable][clone]\n\tClone and turn to absolute values (eg turn x, y positive).\n\tReturn: Cloned Vector.\n\n.roundSelf ()\n\t[chainable][changeSelf]\n\tTurn self to round values (eg turn x, y positive).\n\tReturn: Self.\n\n.round ()\n\t[chainable][clone]\n\tClone and turn to round values (eg turn x, y positive).\n\tReturn: Cloned Vector.\n\n.dot (other)\n\tCalculate dot-product of this vector with another vector.\n\tParam other - other vector to calculate dot-product with.\n\tReturn: Dot Product.\n\n.cross (other)\n\tCalculate cross-product of this vector with another vector.\n\tParam other - other vector to calculate cross-product with.\n\tReturn: Dot Product.\n\n.repairSelf (x, y)\n\t[chainable][changeSelf]\n\tIf any of the components of this vector are nan, null, undefined, etc. set them to defaults.\n\tNote: 0's are considered to be a valid number and will not be replaced with a default value.\n\tParam x - default value for x if undefined (0 if not defined).\n\tParam y - default value for y if undefined (0 if not defined).\n\tReturn: Self.\n\n.repair (x, y)\n\t[chainable][clone]\n\tCreate a clone and if any of the components of the vector are nan, null, undefined, etc. set them to default.\n\tNote: 0's are considered to be a valid number and will not be replaced with a default value.\n\tParam x - default value for x if undefined (0 if not defined).\n\tParam y - default value for y if undefined (0 if not defined).\n\tReturn: Repaired Clone.\n\n.toString ()\n\tConvert to string in the form of \"x,y\".\n\tReturn: String Representation Of The Vector.\n\n.format (format)\n\tConvert to a string with a given format.\n\tParam format - a string in which %x and %y will be replaced with the vector values.\n\tReturn: Formatted String Representing The Vector.\n```\n\n### Consts\n\nVector2js comes with few useful constant vectors you can use:\n\n```js\nVector.zero = new Vector(0, 0); \nVector.one = new Vector(1, 1); \nVector.up = new Vector(0, -1); \nVector.down = new Vector(0, 1); \nVector.left = new Vector(-1, 0); \nVector.right = new Vector(1, 0); \nVector.upLeft = new Vector(-1, -1); \nVector.downLeft = new Vector(-1, 1); \nVector.upRight = new Vector(1, -1); \nVector.downRight = new Vector(1, 1); \n```\n\n## License\n\nVector2js is distributed under the MIT license and is free for any personal or commercial purpose.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fronenness%2Fvector2js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fronenness%2Fvector2js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fronenness%2Fvector2js/lists"}