var canvas = document.getElementById("canvas");
var text = document.getElementById("text");
var ctx = canvas.getContext("2d");
var width = canvas.width = 500;
var height = canvas.height = 200;
message: "Затолкайте красный шар за верхнюю грань",
isPerformed: (x,y)=>(y<0)
message: "Затолкайте красный шар за нижнюю грань",
isPerformed: (x,y)=>(y>height)
message: "Затолкайте красный шар за левую грань",
isPerformed: (x,y)=>(x<0)
message: "Затолкайте красный шар за правую грань",
isPerformed: (x,y)=>(x>width)
var task = tasks[Math.floor(Math.random()*tasks.length)];
text.innerText = task.message;
this.warpTime = Math.floor(10+Math.random()*10);
if(this.count>=this.warpTime){
this.x = 20+Math.floor(Math.random()*(width-40));
this.y = 20+Math.floor(Math.random()*(height-40));
if(!this.display) return;
this.ctx.fillStyle = "red";
this.ctx.arc( this.x, this.y, 2, 0, Math.PI * 2);
constructor(ctx, canvas){
this.x = e.pageX - e.target.offsetLeft;
this.y = e.pageY - e.target.offsetTop;
canvas.addEventListener('mousemove', listener);
canvas.addEventListener('touchmove', listener);
if(!this.display) return;
this.ctx.fillStyle = "blue";
this.ctx.arc( this.x, this.y, 6, 0, Math.PI * 2);
constructor(ctx, type="passive",isPerformed=(x,y)=>false){
this.isPerformed = isPerformed;
this.x = 15+Math.floor(Math.random()*(width-30));
this.y = 15+Math.floor(Math.random()*(height-30));
this.speed = this.speed||(0.05+Math.random()*0.7);
if(this.type === "source")
if(Array.isArray(target)){
this.targets.push(...target);
this.targets.push(target);
this.targets.forEach(target=>{
let dx = target.x-this.x;
let dy = target.y-this.y;
if(this.type === "passive" && target.type === "mouse"){
}else if(this.type === "active" && target.type === "mouse"){
}else if(this.type === "source" && target.type === "mouse"){
let dist = Math.sqrt(dx*dx+dy*dy);
let q = this.speed/this.tdist;
this.dx = this.dx*this.rubbing + this.tdx*q;
this.dy = this.dy*this.rubbing + this.tdy*q;
if(this.isPerformed(this.x,this.y)){
text.innerText="Вы отлично справились с заданием";
this.ctx.fillStyle = "red";
if(this.type === "active"){
this.ctx.fillStyle = "green";
}else if(this.type === "passive"){
this.ctx.fillStyle = "black";
this.ctx.arc( this.x, this.y, 2+0.5/this.speed, 0, Math.PI * 2);
foodList.push(new Food(ctx));
foodList.push(new Mouse(ctx, canvas));
for(let i=0; i<500; i++){
let entity1 = new Entity(ctx,"passive");
let entity2 = new Entity(ctx,"active");
entity1.addTarget(foodList);
entity2.addTarget(foodList);
entityList.push(entity1);
entityList.push(entity2);
var entity3 = new Entity(ctx,"source",task.isPerformed);
entity3.addTarget(foodList);
entityList.push(entity3);
ctx.fillStyle = "rgba(255,255,255,0.2";
ctx.fillRect( 0, 0, width, height);
entityList.forEach(entity=>{
if(!stop) window.requestAnimationFrame(loop);