代码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
github/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 =Length();
this.x = s(angle)* length;
this.y = Math.sin(angle)* length;
}
setLength(length){
var angle =Angle();
this.x = s(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
twitter/DonKarlssonSan
MIT License, see Details View
*/
let canvas;
let ctx;
实现特效的代码jslet w, h;
let size;
let circles;
class Circle {
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 = s(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 =>{
// /wiki/Rotation_matrix
// 2=cos 1−sin 1
// 2=sin 1+cos 1
let x2 = s(deltaAngle)* p.x - Math.sin(deltaAngle)* p.y;
let y2 = Math.sin(deltaAngle)* p.x + s(deltaAngle)* p.y;
p.x = x2;
p.y = y2;
});
}}
class Circles {
constructor(nrOfCircles){
this.init(nrOfCircles);
}
init(nrOfCircles){
this.circles =new Array(nrOfCircles);
let s = size / nrOfCircles *0.45;
for(let i =0; i < nrOfCircles; i++){
let r = i * s + s;
this.circles[i]=new Circle(r);
}
}
move(){
this.circles.forEach(c => c.move());
}
draw(){
for(let circle =0; circle <this.circles.length -1; circle++){
let nrOfPoints = Math.min(this.circles[circle].points.length,this.circles[circle +1].points.length);
for(let i =0; i < nrOfPoints; i++){
ctx.beginPath();
ctx.lineTo(this.circles[circle +1].points[i].x,this.circles[circle +1].points[i].y);
ctx.stroke();
ctx.beginPath();
ctx.arc(this.circles[circle].points[i].x,this.circles[circle].points[i].y,3,0, Math.PI*2);
ctx.fill();
ctx.beginPath();
ctx.arc(this.circles[circle +1].points[i].x,this.circles[circle +1].points[i].y,3,0, Math.PI*2); ctx.fill();
}
}
}}
function setup(){
canvas = document.querySelector("#canvas");
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论