canvas新年快乐烟花绽放动画效果代码
代码语言:html
所属分类:动画
代码描述:canvas新年快乐烟花绽放动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> body { background: #141414; color: white; margin: 0; overflow: hidden; } </style> </head> <body translate="no"> <script> window.canvasOptions = { // autoClear: false, //- centered: true, }; </script> <script > var _window$canvasOptions, _window$canvasOptions2, _window$canvasOptions3, _window$canvasOptions4;const { PI, abs, floor, ceil, round, sign, pow, min, max, atan2, sqrt, asin } = Math; const HALF_PI = PI * 0.5; const TAU = PI * 2; const cos = a => Math.cos(a % TAU); const sin = a => Math.sin(a % TAU); const tan = Math.tan; class _VecShared { constructor(x = 0, y = 0) {this.x = x;this.y = y;} get xy() {return [this.x, this.y];} get yx() {return [this.y, this.x];} div() {throw new Error('Not implemented');} magSq() {throw new Error('Not implemented');} mag() {return Math.sqrt(this.magSq());} normalize(knownMag = this.mag()) {return knownMag ? this.div(knownMag) : this;} setMag(newMag, knownMag) {return this.normalize(knownMag).mult(newMag);} limit(magLimit, knownMag = this.mag()) {if (knownMag > magLimit) this.setMag(magLimit);return this;}} window._VecShared = _VecShared; class Vec2 extends _VecShared { static is(v) {return v instanceof Vec2;} static isAlike(v) {return typeof v !== 'number' && (v instanceof Vec2 || v instanceof Vec3);} static fa(a, m = 1) {return new Vec2(Math.cos(a), Math.sin(a)).mult(m);} static fromIndex(i, width) {return new Vec2(i % width, floor(i / width));} static random(xMin, xMax, yMin, yMax) { if (Vec2.isAlike(xMin) && xMax === undefined) { [xMin, xMax, yMin, yMax] = [0, xMin.x, 0, xMin.y]; } else if (Vec2.isAlike(xMin) && Vec2.isAlike(xMax)) { [xMin, xMax, yMin, yMax] = [xMin.x, xMax.x, xMin.y, xMax.y]; } else { if (Vec2.isAlike(xMin)) [xMin, xMax, yMin, yMax] = [...xMin.xy, xMax, yMin]; if (Vec2.isAlike(yMin)) [yMin, yMax] = [...yMin.xy]; } return new Vec2(random(xMin, xMax), random(yMin, yMax)); } static min(...args) {return args.flat().reduce((p, n) => p.min(n), new Vec2());} static max(...args) {return args.flat().reduce((p, n) => p.max(n), new Vec2());} constructor(x, y) {super(x, y);} copy() {return new Vec2(this.x, this.y);} get _() {return new Vec2(this.x, this.y);} pickXY() {return new Vec2(this.x, this.y);} set(x, y) {if (Vec2.isAlike(x)) ({ x, y } = x);[this.x, this.y] = [x, y];return this;} setX(x) {if (Vec2.isAlike(x)) ({ x } = x);this.x = x;return this;} setY(y) {if (Vec2.isAlike(y)) ({ y } = y);this.y = y;return this;} add(x, y = x) {if (Vec2.isAlike(x)) ({ x, y } = x);this.x += x;this.y += y;return this;} addX(x) {if (Vec2.isAlike(x)) ({ x } = x);this.x += x;return this;} addY(y) {if (Vec2.isAlike(y)) ({ y } = y);this.y += y;return this;} sub(x, y = x) {if (Vec2.isAlike(x)) ({ x, y } = x);this.x -= x;this.y -= y;return this;} subX(x) {if (Vec2.isAlike(x)) ({ x } = x);this.x -= x;return this;} subY(y) {if (Vec2.isAlike(y)) ({ y } = y);this.y -= y;return this;} mult(x, y = x) {if (Vec2.isAlike(x)) ({ x, y } = x);this.x *= x;this.y *= y;return this;} multX(x) {if (Vec2.isAlike(x)) ({ x } = x);this.x *= x;return this;} multY(y) {if (Vec2.isAlike(y)) ({ y } = y);this.y *= y;return this;} div(x, y = x) {if (Vec2.isAlike(x)) ({ x, y } = x);this.x /= x;this.y /= y;return this;} divX(x) {if (Vec2.isAlike(x)) ({ x } = x);this.x /= x;return this;} divY(y) {if (Vec2.isAlike(y)) ({ y } = y);this.y /= y;return this;} magSq() {return this.x * this.x + this.y * this.y;} heading() {return Math.atan2(this.y, this.x);} dist(v) {return v._.sub(this).mag();} rotate(a) {const [c, s] = [Math.cos(a), Math.sin(a)];[this.x, this.y] = [this.x * c - this.y * s, this.x * s + this.y * c];return this;} lerp(x, y, t) {if (Vec2.isAlike(x)) [x, y, t] = [x.x, x.y, y];[this.x, this.y] = [lerp(this.x, x, t), lerp(this.y, y, t)];return this;} dot(x, y) {if (Vec2.isAlike(x)) ({ x, y } = x);return this.x * x + this.y * y;} abs() {this.x = Math.abs(this.x);this.y = Math.abs(this.y);} min(x, y) {if (Vec2.isAlike(x)) ({ x, y } = x);[this.x, this.y] = [Math.min(this.x, x), Math.min(this.y, y)];return this;} max(x, y) {if (Vec2.isAlike(x)) ({ x, y } = x);[this.x, this.y] = [Math.max(this.x, x), Math.max(this.y, y)];return this;} halve() {return this.mult(0.5);} toIndex(width) {return this.y * width + this.x;} floor() {[this.x, this.y] = [floor(this.x), floor(this.y)];return this;} ceil() {[this.x, this.y] = [ceil(this.x), ceil(this.y)];return this;} round() {[this.x, this.y] = [round(this.x), round(this.y)];return this;} reflect(normal) {return this.sub(normal._.mult(this.dot(normal) * 2));}} window.Vec2 = Vec2; class Vec3 extends _VecShared { static is(v) {return v instanceof Vec3;} static fa(a, m) {return new Vec3(cos(a), sin(a), 0).mult(m);} static fromVec2(v2) {return new Vec3(v2.x, v2.y, 0);} constructor(x, y, z = 0) {super(x, y);this.z = z;} copy() {return new Vec3(this.x, this.y, this.z);} get _() {return new Vec3(this.x, this.y, this.z);} pickXY() {return new Vec3(this.x, this.y, 0);} pickYZ() {return new Vec3(0, this.y, this.z);} pickXZ() {return new Vec3(this.x, 0, this.z);} pickXYZ() {return new Vec3(this.x, this.y, this.z);} get yz() {return [this.y, this.z];} get zy() {return [this.z, this.y];} get xz() {return [this.x, this.z];} get zx() {return [this.z, this.x];} get xyz() {return [this.x, this.y, this.z];} get xzy() {return [this.x, this.z, this.y];} get yxz() {return [this.y, this.x, this.z];} get zxy() {return [this.z, this.x, this.y];} get yzx() {return [this.y, this.z, this.x];} get zyx() {return [this.z, this.y, this.x];} set(x = this.x, y = this.y, z = this.z) {if (Vec3.is(x)) ({ x, y, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');[this.x, this.y, this.z] = [x, y, z];return this;} setX(x = this.x) {if (Vec2.isAlike(x)) ({ x } = x);[this.x] = [x];return this;} setY(y = this.y) {if (Vec2.isAlike(y)) ({ y } = y);[this.y] = [y];return this;} setZ(z = this.z) {if (Vec3.is(z)) ({ z } = z);else if (Vec2.is(z)) throw new Error('Vec2 has no `z` property');[this.z] = [z];return this;} setXY(x = this.x, y = this.y) {if (Vec2.isAlike(x)) ({ x, y } = x);[this.x, this.y] = [x, y];return this;} setYZ(y = this.y, z = this.z) {if (Vec3.is(y)) ({ y, z } = y);else if (Vec2.is(y)) throw new Error('Vec2 has no `z` property');[this.y, this.z] = [y, z];return this;} setXZ(x = this.x, z = this.z) {if (Vec3.is(x)) ({ x, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');[this.x, this.z] = [x, z];return this;} add(x = 0, y = x, z = x) {if (Vec3.is(x)) ({ x, y, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');this.x += x;this.y += y;this.z += z;return this;} addX(x = 0) {if (Vec2.isAlike(x)) ({ x } = x);this.x += x;return this;} addY(y = 0) {if (Vec2.isAlike(y)) ({ y } = y);this.y += y;return this;} addZ(z = 0) {if (Vec3.is(z)) ({ z } = z);else if (Vec2.is(z)) throw new Error('Vec2 has no `z` property');this.z += z;return this;} addXY(x = 0, y = x) {if (Vec2.isAlike(x)) ({ x, y } = x);this.x += x;this.y += y;return this;} addYZ(y = 0, z = y) {if (Vec3.is(y)) ({ y, z } = y);else if (Vec2.is(y)) throw new Error('Vec2 has no `z` property');this.y += y;this.z += z;return this;} addXZ(x = 0, z = x) {if (Vec3.is(x)) ({ x, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');this.x += x;this.z += z;return this;} sub(x = 0, y = x, z = x) {if (Vec3.is(x)) ({ x, y, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');this.x -= x;this.y -= y;this.z -= z;return this;} subX(x = 0) {if (Vec2.isAlike(x)) ({ x } = x);this.x -= x;return this;} subY(y = 0) {if (Vec2.isAlike(y)) ({ y } = y);this.y -= y;return this;} subZ(z = 0) {if (Vec3.is(z)) ({ z } = z);else if (Vec2.is(z)) throw new Error('Vec2 has no `z` property');this.z -= z;return this;} subXY(x = 0, y = x) {if (Vec2.isAlike(x)) ({ x, y } = x);this.x -= x;this.y -= y;return this;} subYZ(y = 0, z = y) {if (Vec3.is(y)) ({ y, z } = y);else if (Vec2.is(y)) throw new Error('Vec2 has no `z` property');this.y -= y;this.z -= z;return this;} subXZ(x = 0, z = x) {if (Vec3.is(x)) ({ x, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');this.x -= x;this.z -= z;return this;} mult(x = 1, y = x, z = x) {if (Vec3.is(x)) ({ x, y, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');this.x *= x;this.y *= y;this.z *= z;return this;} multX(x = 1) {if (Vec2.isAlike(x)) ({ x } = x);this.x *= x;return this;} multY(y = 1) {if (Vec2.isAlike(y)) ({ y } = y);this.y *= y;return this;} multZ(z = 1) {if (Vec3.is(z)) ({ z } = z);else if (Vec2.is(z)) throw new Error('Vec2 has no `z` property');this.z *= z;return this;} multXY(x = 1, y = x) {if (Vec2.isAlike(x)) ({ x, y } = x);this.x *= x;this.y *= y;return this;} multYZ(y = 1, z = y) {if (Vec3.is(y)) ({ y, z } = y);else if (Vec2.is(y)) throw new Error('Vec2 has no `z` property');this.y *= y;this.z *= z;return this;} multXZ(x = 1, z = x) {if (Vec3.is(x)) ({ x, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');this.x *= x;this.z *= z;return this;} div(x = 1, y = x, z = x) {if (Vec3.is(x)) ({ x, y, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');this.x /= x;this.y /= y;this.z /= z;return this;} divX(x = 1) {if (Vec2.isAlike(x)) ({ x } = x);this.x /= x;return this;} divY(y = 1) {if (Vec2.isAlike(y)) ({ y } = y);this.y /= y;return this;} divZ(z = 1) {if (Vec3.is(z)) ({ z } = z);else if (Vec2.is(z)) throw new Error('Vec2 has no `z` property');this.z /= z;return this;} divXY(x = 1, y = x) {if (Vec2.isAlike(x)) ({ x, y } = x);this.x /= x;this.y /= y;return this;} divYZ(y = 1, z = y) {if (Vec3.is(y)) ({ y, z } = y);else if (Vec2.is(y)) throw new Error('Vec2 has no `z` property');this.y /= y;this.z /= z;return this;} divXZ(x = 1, z = x) {if (Vec3.is(x)) ({ x, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');this.x /= x;this.z /= z;return this;} magSq() {return this.x * this.x + this.y * this.y + this.z * this.z;} rotateX(a) {const { x, y } = new Vec2(this.y, this.z).rotate(a);return this.set(this.x, x, y);} rotateY(a) {const { x, y } = new Vec2(this.z, this.x).rotate(a);return this.set(y, this.y, x);} rotateZ(a) {const { x, y } = new Vec2(this.x, this.y).rotate(a);return this.set(x, y, this.z);} rotate(a) {const { x, y } = new Vec2(this.x, this.y).rotate(a);return this.set(x, y, this.z);} lerp(x, y, z, t) {if (Vec3.is(x)) [x, y, z, t] = [x.x, x.y, x.z, y];else if (Vec2.isAlike(x)) throw new Error('Vec2 has no `z` property');this.x = lerp(this.x, x, t);this.y = lerp(this.y, y, t);this.z = lerp(this.z, z, t);return this;} dot(x, y, z) {if (Vec3.is(x)) ({ x, y, z } = x);else if (Vec2.is(x)) throw new Error('Vec2 has no `z` property');return this.x * x + this.y * y + this.z * z;} cross(v) {return new Vec3(this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * v.y - this.y * v.x);} abs() {this.x = abs(this.........完整代码请登录后点击上方下载按钮下载查看
网友评论0