21.11.2017, 20:45
|
|
Профессор
|
|
Регистрация: 11.08.2017
Сообщений: 208
|
|
рони,
я в шоке!! Как???
А за счет чего в прошлой версии падала производительность?
Я пытался оптимизировать, но до такого вообще и близко не дотянул)
А, все - я понял) Это "бомба"!
Последний раз редактировалось void(), 21.11.2017 в 20:53.
|
|
21.11.2017, 21:10
|
|
Профессор
|
|
Регистрация: 27.05.2010
Сообщений: 33,068
|
|
Сообщение от void()
|
А за счет чего в прошлой версии падала производительность?
|
там было сколько точек столько ножек, не все видно но они есть, сейчас ножки ограничены - меньше прорисовки.
|
|
23.11.2017, 01:05
|
Профессор
|
|
Регистрация: 07.11.2013
Сообщений: 4,662
|
|
void(),
<canvas width="400" height="200" style="outline:1px solid black;cursor:none;"></canvas>
<script>
class Point {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
render(game) {
game.ctx2d.beginPath();
game.ctx2d.arc(this.x, this.y, this.radius, 0, 7);
game.ctx2d.fill();
}
}
class Cursor extends Point {
constructor(x, y, radius, length, filter) {
super(x, y, radius);
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.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, 50, (A, B) => A.length > Math.hypot(A.x - B.x, A.y - B.y)),
new Array(100).fill(0).map(() => new Point(400 * Math.random(), 200 * Math.random(), 2))
);
</script>
|
|
23.11.2017, 01:29
|
|
Профессор
|
|
Регистрация: 27.05.2010
Сообщений: 33,068
|
|
Rise,
здорово!
|
|
23.11.2017, 19:43
|
|
Профессор
|
|
Регистрация: 11.08.2017
Сообщений: 208
|
|
Rise,
клааас. Мега-круто! Даже визуально код выглядит, как стихи - я поражаюсь... Надеюсь, недели 2 мне хватит, чтобы выкупить, что в этом скрипте происходит А потом, научившись на этих примерах, я уже и сам тоже начну думать мозгом)
|
|
27.11.2017, 06:03
|
Профессор
|
|
Регистрация: 07.11.2013
Сообщений: 4,662
|
|
рони,
void(),
// Square
(A, B) => B.x > A.x - A.length && B.x < A.x + A.length && B.y > A.y - A.length && B.y < A.y + A.length
// Grid
new Array(10 * 20).fill(0).map((v, i) => new Point(20 * (i % 20), 20 * (i / 20 | 0), 2))
|
|
27.11.2017, 09:44
|
|
Профессор
|
|
Регистрация: 27.05.2010
Сообщений: 33,068
|
|
Rise,
<canvas width="400" height="200" style="outline:1px solid black;cursor:none;"></canvas>
<script>
"use strict"
class Point {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = "#0000FF";
}
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, length, filter) {
super(x, y, radius);
this.length = length;
this.filter = filter;
this.color = "#" + ("000000" + (Math.random() * 0xffffff | 0).toString(16)).slice(-6);
this.points = new Set();
}
search(points) {
this.points.clear();
for (let point of points) {
point.color = "#0000FF";
if (this.filter(this, point)) {
point.color = "#7FFF00";
this.points.add(point);
}
}
}
render(game) {
this.search(game.points);
game.ctx2d.beginPath();
game.ctx2d.strokeStyle = this.color;
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.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, 50, (A, B) => B.x > A.x - A.length && B.x < A.x + A.length && B.y > A.y - A.length && B.y < A.y + A.length),
new Array(10 * 20).fill(0).map((v, i) => new Point(20 * (i % 20) + 10, 20 * (i / 20 | 0) + 10, 2))
);
</script>
|
|
29.11.2017, 15:59
|
Профессор
|
|
Регистрация: 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>
|
|
29.11.2017, 16:02
|
|
Профессор
|
|
Регистрация: 27.05.2010
Сообщений: 33,068
|
|
Rise,
|
|
|
|