JSFiddle - React, Tailwind, and code Playground
HTML
<canvas width="500" height="500"></canvas>
<input type="button" value="prev"/>
<input type="button" value="play"/>
CSS
canvas{
position:relative;
}
canvas.onHover{
cursor:pointer;
}
JavaScript
function genPerm(n){
var rsort=new Array(n);
for(var i=0;i<n;++i){
rsort[i]=i+1;
}
// then proceed to shuffle the rsort array
for(var idx = 0; idx < rsort.length; idx++)
{
var swpIdx = idx + Math.floor(Math.random() * (rsort.length - idx));
// now swap elements at idx and swpIdx
var tmp = rsort[idx];
rsort[idx] = rsort[swpIdx];
rsort[swpIdx] = tmp;
}
return rsort;
}
function Coin(pos, radius, value){
this.x=pos.x,
this.y=pos.y,
this.radius=radius,
this.value=value;
this.coinStyle='orange';
}
Coin.prototype.setCoinStyle=function(style){
this.coinStyle=style;
}
Coin.prototype.plot=function(ctx){
ctx.fillStyle=this.coinStyle;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
ctx.fill();
ctx.stroke();
ctx.closePath();
ctx.fillStyle='black';
ctx.fillText(this.value, this.x, this.y);
}
Coin.prototype.contains=function(pos){
return d(this, pos) < this.radius;
}
function Game(opts){
this.canvas = opts.canvas;
this.ctx = this.canvas.getContext('2d');
this.ctx.textAlign='center';
this.ctx.textBaseline='middle';
this.radius = opts.radius;
this.cbkSuccess=opts.cbkSuccess?opts.cbkSuccess:function(){}
var deltaAngle = Math.PI*2/opts.numbers.length;
var currentAngle = 0;
this.coins = opts.numbers.map(function(n){
var center={
x:this.canvas.height/2,
y:this.canvas.width/2
};
var r=0.9*Math.min(center.x, center.y);
var point = {
x:r*Math.cos(currentAngle),
y:r*Math.sin(currentAngle)
};
var p = {
x:point.x + center.x,
y:point.y+center.y
}
var coin = new Coin(p, this.radius, n);
currentAngle += deltaAngle;
return coin;
}, this);
this.moves=[];
}
Game.prototype.refresh=function(){
var ctx = this.ctx;
ctx.clearRect(0,0,this.w,this.h);
this.coins.forEach(function(x){
x.plot(ctx);
});
}
Game.prototype.correspondingCoin=function(o){
var minDis=10000;
var...