js打造线状足球3d模型效果可以下载为svg
代码语言:html
所属分类:三维
代码描述:js打造线状足球3d模型效果可以下载为svg
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
:root {
--sq: 500px;
}
html {
height: 100vh;
}
body {
align-items: center;
display: flex;
height: 100%;
justify-content: center;
margin: 0;
padding: 0;
}
canvas {
height: auto;
width: var(--sq);
}
.container {
height: var(--sq);
text-align: center;
width: var(--sq);
}
</style>
</head>
<body translate="no">
<div class="container"></div>
<script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/paper.js"></script>
<script >const conversionFactor = 180 / Math.PI;
var radianToDegrees = function(radian) {
return radian * conversionFactor;
}
var degreesToRadian = function(degrees) {
return degrees / conversionFactor;
}
let Vec2, Vec3, Vec4, Mat2, Mat3, Mat4, Quat;
/**
* A basic 2D Vector class that provides simple algebraic functionality in the form
* of 2D Vectors.
*
* We use Getters/setters for both principle properties (x & y) as well as virtual
* properties (rotation, length etc.).
*
* @class Vec2
* @author Liam Egan <liam@wethecollective.com>
* @version 1.0.0
* @created Jan 07, 2020
*/
Vec2 = class {
/**
* The Vector Class constructor
*
* @constructor
* @param {number} x The x coord
* @param {number} y The y coord
*/
constructor(x, y){
if(isNaN(x)) x = 0;
if(isNaN(y)) y = 0;
this.x = x;
this.y = y;
}
/**
* Resets the vector coordinates
*
* @public
* @param {number|Array} x The x coord, OR the array to reset to
* @param {number} y The y coord
*/
reset(x, y) {
if(x instanceof Array && x.length >= 2) {
this.x = x[0];
this.y = x[1];
} else {
this.x = x;
this.y = y;
}
}
/**
* Resets the vector coordinates to another vector object
*
* @public
* @param {Vector} v The vector object to use to reset the coordinates
*/
resetToVector(v) {
if(v instanceof Vec2) {
this.x = v.x;
this.y = v.y;
}
}
/**
* Clones the vector
*
* @public
* @return {Vector} The cloned vector
*/
clone() {
return new Vec2(this.x, this.y);
}
/**
* Adds one vector to another.
*
* @public
* @chainable
* @param {Vec2} vector The vector to add to this one
* @return {Vec2} Returns itself, modified
*/
add(vector) {
this.x += vector.x;
this.y += vector.y;
return this;
}
/**
* Clones the vector and adds the vector to it instead
*
* @public
* @chainable
* @param {Vec2} vector The vector to add to this one
* @return {Vec2} Returns the clone of itself, modified
*/
addNew(vector) {
return this.clone().add(vector);
}
/**
* Adds a scalar to the vector, modifying both the x and y
*
* @public
* @chainable
* @param {number} scalar The scalar to add to the vector
* @return {Vec2} Returns itself, modified
*/
addScalar(scalar) {
return this.add(new Vec2(scalar, scalar));
}
/**
* Clones the vector and adds the scalar to it instead
*
* @public
* @chainable
* @param {number} scalar The scalar to add to the vector
* @return {Vec2} Returns the clone of itself, modified
*/
addScalarNew(scalar) {
return this.clone().addScalar(scalar);
}
/**
* Subtracts one vector from another.
*
* @public
* @chainable
* @param {Vec2} vector The vector to subtract from this one
* @return {Vec2} Returns itself, modified
*/
subtract(vector) {
this.x -= vector.x;
this.y -= vector.y;
return this;
}
/**
* Clones the vector and subtracts the vector from it instead
*
* @public
* @chainable
* @param {Vec2} vector The vector to subtract from this one
* @return {Vec2} Returns the clone of itself, modified
*/
subtractNew(vector) {
return this.clone().subtract(vector);
}
/**
* Subtracts a scalar from the vector, modifying both the x and y
*
* @public
* @chainable
* @param {number} scalar The scalar to subtract from the vector
* @return {Vec2} Returns itself, modified
*/
subtractScalar(scalar) {
return this.subtract(new Vec2(scalar, scalar));
}
/**
* Clones the vector and subtracts the scalar from it instead
*
* @public
* @chainable
* @param {number} scalar The scalar to add to the vector
* @return {Vec2} Returns the clone of itself, modified
*/
subtractScalarNew(scalar) {
return this.clone().subtractScalar(scalar);
}
/**
* Divides one vector by another.
*
* @public
* @chainable
* @param {Vec2} vector The vector to divide this by
* @return {Vec2} Returns itself, modified
*/
divide(vector) {
if(vector.x !== 0) {
this.x /= vector.x
} else {
this.x = 0;
}
if(vector.y !== 0) {
this.y /= vector.y
} else {
this.y = 0;
}
return this;
}
/**
* Clones the vector and divides it by the vector instead
*
* @public
* @chainable
* @param {Vec2} vector The vector to divide the clone by
* @return {Vec2} Returns the clone of itself, modified
*/
divideNew(vector) {
return this.clone().divide(vector);
}
/**
* Divides the vector by a scalar.
*
* @public
* @chainable
* @param {number} scalar The scalar to divide both x and y by
* @return {Vec2} Returns itself, modified
*/
divideScalar(scalar) {
var v = new Vec2(scalar, scalar);
return this.divide(v);
}
/**
* Clones the vector and divides it by the provided scalar.
*
* @public
* @chainable
* @param {number} scalar The scalar to divide both x and y by
* @return {Vec2} Returns the clone of itself, modified
*/
divideScalarNew(scalar) {
return this.clone().divideScalar(scalar);
}
/**
* Multiplies one vector by another.
*
* @public
* @chainable
* @param {Vec2} vector The vector to multiply this by
* @return {Vec2} Returns itself, modified
*/
multiply(vector) {
this.x *= vector.x;
this.y *= vector.y;
return this;
}
/**
* Clones the vector and multiplies it by the vector instead
*
* @public
* @chainable
* @param {Vec2} vector The vector to multiply the clone by
* @return {Vec2} Returns the clone of itself, modified
*/
multiplyNew(vector) {
return this.clone().multiply(vector);
}
/**
* Multiplies the vector by a scalar.
*
* @public
* @chainable
* @param {number} scalar The scalar to multiply both x and y by
* @return {Vec2} Returns itself, modified
*/
multiplyScalar(scalar) {
var v = new Vec2(scalar, scalar);
return this.multiply(v);
}
/**
* Clones the vector and multiplies it by the provided scalar.
*
* @public
* @chainable
* @param {number} scalar The scalar to multiply both x and y by
* @return {Vec2} Returns the clone of itself, modified
*/
multiplyScalarNew(scalar) {
return this.clone().multiplyScalar(scalar);
}
/**
* Alias of {@link Vector#multiplyScalar__anchor multiplyScalar}
*/
scale(scalar) {
return this.multiplyScalar(scalar);
}
/**
* Alias of {@link Vector#multiplyScalarNew__anchor multiplyScalarNew}
*/
scaleNew(scalar) {
return this.multiplyScalarNew(scalar);
}
/**
* Rotates a vecor by a given amount, provided in radians.
*
* @public
* @chainable
* @param {number} radian The angle, in radians, to rotate the vector by
* @return {Vec2} Returns itself, modified
*/
rotate(radian) {
var x = (this.x * Math.cos(radian)) - (this.y * Math.sin(radian));
var y = (this.x * Math.sin(radian)) + (this.y * Math.cos(radian));
this.x = x;
this.y = y;
return this;
}
/**
* Clones the vector and rotates it by the supplied radian value
*
* @public
* @chainable
* @param {number} radian The angle, in radians, to rotate the vector by
* @return {Vec2} Returns the clone of itself, modified
*/
rotateNew(radian) {
return this.clone().rotate(radian);
}
/**
* Rotates a vecor by a given amount, provided in degrees. Converts the degree
* value to radians and runs the rotaet method.
*
* @public
* @chainable
* @param {number} degrees The angle, in degrees, to rotate the vector by
* @return {Vec2} Returns itself, modified
*/
rotateDeg(degrees) {
return this.rotate(degreesToRadian(degrees));
}
/**
* Clones the vector and rotates it by the supplied degree value
*
* @public
* @chainable
* @param {number} degrees The angle, in degrees, to rotate the vector by
* @return {Vec2} Returns the clone of itself, modified
*/
rotateDegNew(degrees) {
return this.rotateNew(degreesToRadian(degrees));
}
/**
* Alias of {@link Vector#rotate__anchor rotate}
*/
rotateBy(radian) {
return this.rotate(radian);
}
/**
* Alias of {@link Vector#rotateNew__anchor rotateNew}
*/
rotateByNew(radian) {
return this.rotateNew(radian);
}
/**
* Alias of {@link Vector#rotateDeg__anchor rotateDeg}
*/
rotateDegBy(degrees) {
return this.rotateDeg(degrees);
}
/**
* Alias of {@link Vector#rotateDegNew__anchor rotateDegNew}
*/
rotateDegByNew(radian) {
return this.rotateDegNew(radian);
}
/**
* Rotates a vector to a specific angle
*
* @public
* @chainable
* @param {number} radian The angle, in radians, to rotate the vector to
* @return {Vec2} Returns itself, modified
*/
rotateTo(radian) {
return this.rotate(radian-this.angle);
};
/**
* Clones the vector and rotates it to the supplied radian value
*
* @public
* @chainable
* @param {number} radian The angle, in radians, to rotate the vector to
* @return {Vec2} Returns the clone of itself, modified
*/
rotateToNew(radian) {
return this.clone().rotateTo(radian);
};
/**
* Rotates a vecor to a given amount, provided in degrees. Converts the degree
* value to radians and runs the rotateTo method.
*
* @public
* @chainable
* @param {number} degrees The angle, in degrees, to rotate the vector to
* @return {Vec2} Returns itself, modified
*/
rotateToDeg(degrees) {
return this.rotateTo(degreesToRadian(degrees));
}
/**
* Clones the vector and rotates it to the supplied degree value
*
* @public
* @chainable
* @param {number} degrees The angle, in degrees, to rotate the vector to
* @return {Vec2} Returns the clone of itself, modified
*/
rotateToDegNew(degrees) {
return this.rotateToNew(degreesToRadian(degrees));
}
/**
* Negates the vector.
*
* @public
* @chainable
* @return {Vec2} Returns itself, modified
*/
negate() {
return this.multiplyScalar(-1.);
}
/**
* Clones the vector and negates it.
*
* @public
* @chainable
* @return {Vec2} Returns itself, modified
*/
negateNew() {
return this.multiplyScalarNew(-1.);
}
/**
* Inverses the vector.
*
* @public
* @chainable
* @return {Vec2} Returns itself, modified
*/
inverse() {
this.x = 1./this.x;
this.y = 1./this.y;
return this;
}
/**
* Clones the vector and then inverses it.
*
* @public
* @chainable
* @return {Vec2} Returns itself, modified
*/
inverseNew() {
const c = new Vector();
c.x = 1./this.x;
c.y = 1./this.y;
return c;
}
/**
* Normalises the vector down to a length of 1 unit
*
* @public
* @chainable
* @return {Vec2} Returns itself, modified
*/
normalise() {
return this.divideScalar(this.length);
}
/**
* Clones the vector and normalises it
*
* @public
* @chainable
* @return {Vec2} Returns a clone of itself, modified
*/
normaliseNew() {
return this.divideScalarNew(this.length);
}
/**
* Calculates the distance between this and the supplied vector
*
* @param {Vec2} vector The vector to calculate the distance from
* @return {number} The distance between this and the supplied vector
*/
distance(vector) {
return this.subtractNew(vector).length;
}
/**
* Calculates the distance on the X axis between this and the supplied vector
*
* @param {Vec2} vector The vector to calculate the distance from
* @return {number} The distance, along the x axis, between this and the supplied vector
*/
distanceX(vector) {
return this.x - vector.x;
}
/**
* Calculated the distance on the Y axis between this and the supplied vector
*
* @param {Vec2} vector The vector to calculate the distance from
* @return {number} The distance, along the y axis, between this and the supplied vector
*/
distanceY(vector) {
return this.y - vector.y;
}
/**
* Calculates the dot product between this and a supplied vector
*
* @example
* // returns -14
* new.........完整代码请登录后点击上方下载按钮下载查看
















网友评论0