目录
效果
代码
index.html
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Spinner Thingy</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<canvas id="canvas"></canvas>
<script src='js/jjpbdkp.js'></script>
<script src="js/script.js"></script>
</body>
</html>
style.css
body, html {
margin: 0;
}
canvas {
display: block;
}
jjpbdkp.js
/*
Johan Karlsson
https://github.com/DonKarlssonSan/vectory
*/
"use strict";
class Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(v) {
return new Vector(
this.x + v.x,
this.y + v.y);
}
addTo(v) {
this.x += v.x;
this.y += v.y;
}
sub(v) {
return new Vector(
this.x - v.x,
this.y - v.y);
}
subFrom(v) {
this.x -= v.x;
this.y -= v.y;
}
mult(n) {
return new Vector(this.x * n, this.y * n);
}
multTo(n) {
this.x *= n;
this.y *= n;
return this;
}
div(n) {
return new Vector(this.x / n, this.y / n);
}
setAngle(angle) {
var length = this.getLength();
this.x = Math.cos(angle) * length;
this.y = Math.sin(angle) * length;
}
setLength(length) {
var angle = this.getAngle();
this.x = Math.cos(angle) * length;
this.y = Math.sin(angle) * length;
}
getAngle() {
return Math.atan2(this.y, this.x);
}
getLength() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
getLengthSq() {
return this.x * this.x + this.y * this.y;
}
distanceTo(v) {
return this.sub(v).getLength();
}
copy() {
return new Vector(this.x, this.y);
}
equals(v) {
return this.x == v.x && this.y == v.y;
}
}
script.js
/*
Johan Karlsson, 2019
https://twitter.com/DonKarlssonSan
MIT License, see Details View
*/
let canvas;
let ctx;
let w, h;
let size;
let circles;
class Circle {
constructor(r) {
this.r = r;
let nrOfPoints = 24;
this.points = [];
for (let circlePoint = 0; circlePoint < nrOfPoints; circlePoint++) {
let angle = Math.PI * 2 / nrOfPoints * circlePoint;
let x = Math.cos(angle) * r;
let y = Math.sin(angle) * r;
this.points.push(new Vector(x, y));
}
}
move() {
let deltaAngle = 0.05 * this.r / size;
this.points.forEach(p => {
// https://en.wikipedia.org/wiki/Rotation_matrix
//