Показать сообщение отдельно
  #28 (permalink)  
Старый 29.11.2017, 15:59
Профессор
Отправить личное сообщение для Rise Посмотреть профиль Найти все сообщения от Rise
 
Регистрация: 07.11.2013
Сообщений: 4,662

рони,
... хамеленожка
<canvas width="400" height="200" style="outline:1px solid black;cursor:none;"></canvas>
<script>
class Point {
    constructor(x, y, radius, color) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.color = color;
    }
    render(game) {
        game.ctx2d.beginPath();
        game.ctx2d.fillStyle = this.color;
        game.ctx2d.arc(this.x, this.y, this.radius, 0, 7);
        game.ctx2d.fill();
    }
}
class Cursor extends Point {
    constructor(x, y, radius, color, length, filter) {
        super(x, y, radius, color);
        this.length = length;
        this.filter = filter;
        this.points = new Set();
    }
    search(points) {
        this.points.clear();
        for (let point of points)
            if (this.filter(this, point))
                this.points.add(point);
    }
    render(game) {
        this.search(game.points);
        game.ctx2d.beginPath();
        for (let point of this.points) {
            game.ctx2d.moveTo(this.x, this.y);
            game.ctx2d.lineTo(point.x, point.y);
        }
        game.ctx2d.closePath();
        game.ctx2d.strokeStyle = `hsl(${Math.random() * 360 | 0}, 100%, 50%)`;
        game.ctx2d.stroke();
        super.render(game);
    }
}
class Game {
    constructor(canvas, cursor, points) {
        this.ctx2d = canvas.getContext('2d');
        this.cursor = cursor;
        this.points = new Set(points);
        this.render();
        canvas.onmousemove = (e) => {
            this.cursor.x = e.offsetX;
            this.cursor.y = e.offsetY;
            this.render();
        };
    }
    render() {
        this.ctx2d.clearRect(0, 0, 400, 200);
        for (let point of this.points)
            point.render(this);
        this.cursor.render(this);
    }
}
const theGame = new Game(
    document.querySelector('canvas'),
    new Cursor(200, 100, 2, 'gray', 50, (A, B) => A.length > Math.hypot(A.x - B.x, A.y - B.y)),
    new Array(10 * 20).fill(0).map((v, i) => new Point(20 * (i % 20), 20 * (i / 20 | 0) + 10 * (i % 2), 2, 'gray'))
);
</script>
Ответить с цитированием