var w;
onmessage = e => {
if (e.date.msg == 'create') {
w =e.data.w;
w.offscreen = new OffscreenCanvas(1000, 1000);
w.ctx =w.offscreen.getContext('2d');
}
if (e.data.msg == 'run') {
postMessage();
var mouseX = e.data.mouseX;
var mouseY = e.data.mouseY;
var ak = w.ak;
var vMax = w.vMax;
var dMin = w.dMin;
console.log(666);
postMessage();
w.ctx.clearRect(0,0,1000,1000);
w.dots.forEach(dot => {
var rr = (mouseX - dot.pX) * (mouseX - dot.pX) +
(mouseY - dot.pY) * (mouseY - dot.pY);
rr = Math.max(dMin * dMin, rr);
var aX = ak / (rr * (mouseX - dot.pX) / Math.abs(mouseX - dot.pX));
var aY = ak / (rr * (mouseY - dot.pY) / Math.abs(mouseY - dot.pY));
dot.vX += aX;
dot.vY += aY;
dot.vX = Math.max(-vMax, Math.min(vMax, dot.vX));
dot.vY = Math.max(-vMax, Math.min(vMax, dot.vY));
dot.pX += dot.vX;
dot.pY += dot.vY;
w.ctx.fillStyle = colorList[Random(0, colorList.length + 1)];
w.ctx.beginPath();
w.ctx.arc(dot.pX, dot.pY, 2, 0, 2 * Math.PI);
w.ctx.fill();
});
var img =w.offscreen.transferToImageBitMap();
postMessage({img:img});
}
}
console