@Konstantin_P

Как на js реализовать математические операции с объектами?

Есть библиотека paper.js, которая позволяет такое. Как черт возьми это работает? Сам не смог разобраться.
paperjs.org/tutorials/geometry/mathematical-operations
// Define a point to start with
var point1 = new Point(10, 20);

// Create a second point that is 4 times the first one.
// This is the same as creating a new point with x and y
// of point1 multiplied by 4:
var point2 = point1 * 4;
console.log(point2); // { x: 40, y: 80 }

// Now we calculate the difference between the two.
var point3 = point2 - point1;
console.log(point3); // { x: 30, y: 60 }

// Create yet another point, with a numeric value added to point3:
var point4 = point3 + 30;
console.log(point4); // { x: 60, y: 90 }

// How about a third of that?
var point5 = point4 / 3;
console.log(point5); // { x: 20, y: 30 }

// Multiplying two points with each other multiplies each 
// coordinate seperately
var point6 = point5 * new Point(3, 2);
console.log(point6); // { x: 60, y: 60 }
  • Вопрос задан
  • 72 просмотра
Решения вопроса 1
@timokins
JS не поддерживает переопределение арифметических операций.
А paperjs делает это с помощью своего компилятора из PaperScript в JavaScript:
https://github.com/paperjs/paper.js/blob/89c60b1a0...
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы