Point
The Point object represents a point in the two dimensional space of the Paper.js project. It is also used to represent two dimensional vector objects.
Example:Create a point at x: 10, y: 5
var point = new Point(10, 5);
console.log(point.x); // 10
console.log(point.y); // 5
Constructors
Creates a Point object with the given x and y coordinates.
- x: Number — the x coordinate
- y: Number — the y coordinate
Parameters:
- Point
Returns:
Example:Create a point at x: 10, y: 5
var point = new Point(10, 5);
console.log(point.x); // 10
console.log(point.y); // 5
Creates a Point object using the numbers in the given array as coordinates.
- array: Array
Parameters:
- Point
Returns:
Example:Creating a point at x: 10, y: 5 using an array of numbers:
var array = [10, 5];
var point = new Point(array);
console.log(point.x); // 10
console.log(point.y); // 5
Example:Passing an array to a functionality that expects a point:
// Create a circle shaped path at x: 50, y: 50
// with a radius of 30:
var path = new Path.Circle([50, 50], 30);
path.fillColor = 'red';
// Which is the same as doing:
var path = new Path.Circle(new Point(50, 50), 30);
path.fillColor = 'red';
Creates a Point object using the properties in the given object.
- object: Object — the object describing the point’s properties
Parameters:
- Point
Returns:
Example:Creating a point using an object literal with length and angle properties:
var point = new Point({
length: 10,
angle: 90
});
console.log(point.length); // 10
console.log(point.angle); // 90
Example:Creating a point at x: 10, y: 20 using an object literal:
var point = new Point({
x: 10,
y: 20
});
console.log(point.x); // 10
console.log(point.y); // 20
Example:Passing an object to a functionality that expects a point:
var center = {
x: 50,
y: 50
};
// Creates a circle shaped path at x: 50, y: 50
// with a radius of 30:
var path = new Path.Circle(center, 30);
path.fillColor = 'red';
Creates a Point object using the width and height values of the given Size object.
- size: Size
Parameters:
- Point
Returns:
Example:Creating a point using a size object.
// Create a Size with a width of 100pt and a height of 50pt
var size = new Size(100, 50);
console.log(size); // { width: 100, height: 50 }
var point = new Point(size);
console.log(point); // { x: 100, y: 50 }
Operators
Returns the addition of the supplied value to both coordinates of the point as a new point. The object itself is not modified!
- number: Number — the number to add
Parameters:
- Point — the addition of the point and the value as a new point
Returns:
Example:
var point = new Point(5, 10);
var result = point + 20;
console.log(result); // {x: 25, y: 30}
Returns the addition of the supplied point to the point as a new point. The object itself is not modified!
- point: Point — the point to add
Parameters:
- Point — the addition of the two points as a new point
Returns:
Example:
var point1 = new Point(5, 10);
var point2 = new Point(10, 20);
var result = point1 + point2;
console.log(result); // {x: 15, y: 30}
Returns the subtraction of the supplied value to both coordinates of the point as a new point. The object itself is not modified!
- number: Number — the number to subtract
Parameters:
- Point — the subtraction of the point and the value as a new point
Returns:
Example:
var point = new Point(10, 20);
var result = point - 5;
console.log(result); // {x: 5, y: 15}
Returns the subtraction of the supplied point to the point as a new point. The object itself is not modified!
- point: Point — the point to subtract
Parameters:
- Point — the subtraction of the two points as a new point
Returns:
Example:
var firstPoint = new Point(10, 20);
var secondPoint = new Point(5, 5);
var result = firstPoint - secondPoint;
console.log(result); // {x: 5, y: 15}
Returns the multiplication of the supplied value to both coordinates of the point as a new point. The object itself is not modified!
- number: Number — the number to multiply by
Parameters:
- Point — the multiplication of the point and the value as a new point
Returns:
Example:
var point = new Point(10, 20);
var result = point * 2;
console.log(result); // {x: 20, y: 40}
Returns the multiplication of the supplied point to the point as a new point. The object itself is not modified!
- point: Point — the point to multiply by
Parameters:
- Point — the multiplication of the two points as a new point
Returns:
Example:
var firstPoint = new Point(5, 10);
var secondPoint = new Point(4, 2);
var result = firstPoint * secondPoint;
console.log(result); // {x: 20, y: 20}
Returns the division of the supplied value to both coordinates of the point as a new point. The object itself is not modified!
- number: Number — the number to divide by
Parameters:
- Point — the division of the point and the value as a new point
Returns:
Example:
var point = new Point(10, 20);
var result = point / 2;
console.log(result); // {x: 5, y: 10}
Returns the division of the supplied point to the point as a new point. The object itself is not modified!
- point: Point — the point to divide by
Parameters:
- Point — the division of the two points as a new point
Returns:
Example:
var firstPoint = new Point(8, 10);
var secondPoint = new Point(2, 5);
var result = firstPoint / secondPoint;
console.log(result); // {x: 4, y: 2}
The modulo operator returns the integer remainders of dividing the point by the supplied value as a new point.
- value: Number
Parameters:
- Point — the integer remainders of dividing the point by the value as a new point
Returns:
Example:
var point = new Point(12, 6);
console.log(point % 5); // {x: 2, y: 1}
The modulo operator returns the integer remainders of dividing the point by the supplied value as a new point.
- point: Point
Parameters:
- Point — the integer remainders of dividing the points by each other as a new point
Returns:
Example:
var point = new Point(12, 6);
console.log(point % new Point(5, 2)); // {x: 2, y: 0}
Properties
The x coordinate of the point
- Number
Type:
The y coordinate of the point
- Number
Type:
The length of the vector that is represented by this point’s coordinates. Each point can be interpreted as a vector that points from the origin (x
= 0
, y = 0
) to the point’s location. Setting the length changes the location but keeps the vector’s angle.
- Number
Type:
The vector’s angle in degrees, measured from the x-axis to the vector.
- Number
Type:
The vector’s angle in radians, measured from the x-axis to the vector.
- Number
Type:
The quadrant of the angle of the point.
Angles between 0 and 90 degrees are in quadrant 1
. Angles between 90 and 180 degrees are in quadrant 2
, angles between 180 and 270 degrees are in quadrant 3
and angles between 270 and 360 degrees are in quadrant 4
.
Read only.
- Number
Type:
Example:
var point = new Point({
angle: 10,
length: 20
});
console.log(point.quadrant); // 1
point.angle = 100;
console.log(point.quadrant); // 2
point.angle = 190;
console.log(point.quadrant); // 3
point.angle = 280;
console.log(point.quadrant); // 4
This property is only valid if the point is an anchor or handle point of a Segment or a Curve, or the position of an Item, as returned by item.position, segment.point, segment.handleIn, segment.handleOut, curve.point1, curve.point2, curve.handle1, curve.handle2.
In those cases, it returns true if it the point is selected, false otherwise.
Paper.js renders selected points on top of your project. This is very useful when debugging.
- false
Default:
- Boolean
Type:
Example:
Methods
Checks whether the coordinates of the point are equal to that of the supplied point.
- point: Point
Parameters:
- Boolean — true if the points are equal, false otherwise
Returns:
Example:
var point = new Point(5, 10);
console.log(point == new Point(5, 10)); // true
console.log(point == new Point(1, 1)); // false
console.log(point != new Point(1, 1)); // true
Returns a copy of the point.
- Point — the cloned point
Returns:
Example:
var point1 = new Point();
var point2 = point1;
point2.x = 1; // also changes point1.x
var point2 = point1.clone();
point2.x = 1; // doesn't change point1.x
- String — a string representation of the point
Returns:
Returns the smaller angle between two vectors. The angle is unsigned, no information about rotational direction is given.
- point: Point
Parameters:
- Number — the angle in degrees
Returns:
Returns the smaller angle between two vectors in radians. The angle is unsigned, no information about rotational direction is given.
- point: Point
Parameters:
- Number — the angle in radians
Returns:
Returns the distance between the point and another point.
- point: Point
- squared: Boolean — Controls whether the distance should remain squared, or its square root should be calculated — optional, default: false
Parameters:
- Number
Returns:
Normalize modifies the length of the vector to 1
without changing its angle and returns it as a new point. The optional length
parameter defines the length to normalize to. The object itself is not modified!
- length: Number — The length of the normalized vector — optional, default: 1
Parameters:
- Point — the normalized vector of the vector that is represented by this point’s coordinates
Returns:
Rotates the point by the given angle around an optional center point. The object itself is not modified.
Read more about angle units and orientation in the description of the angle property.
- angle: Number — the rotation angle
- center: Point — the center point of the rotation
Parameters:
- Point — the rotated point
Returns:
Tests
Checks whether the point is inside the boundaries of the rectangle.
- rect: Rectangle — the rectangle to check against
Parameters:
- Boolean — true if the point is inside the rectangle, false otherwise
Returns:
Checks if the point is within a given distance of another point.
- point: Point — the point to check against
- tolerance: Number — the maximum distance allowed
Parameters:
- Boolean — true if it is within the given distance, false otherwise
Returns:
Checks if the vector represented by this point is collinear (parallel) to another vector.
- point: Point — the vector to check against
Parameters:
- Boolean — true it is collinear, false otherwise
Returns:
Checks if the vector represented by this point is orthogonal (perpendicular) to another vector.
- point: Point — the vector to check against
Parameters:
- Boolean — true it is orthogonal, false otherwise
Returns:
Checks if this point has both the x and y coordinate set to 0.
- Boolean — true if both x and y are 0, false otherwise
Returns:
Checks if this point has an undefined value for at least one of its coordinates.
- Boolean — true if either x or y are not a number, false otherwise
Returns:
Checks if the vector is within the specified quadrant. Note that if the vector lies on the boundary between two quadrants, true
will be returned for both quadrants.
- quadrant: Number — the quadrant to check against
Parameters:
- Boolean — true if either x or y are not a number, false otherwise
Returns:
- quadrant
See also:
Vector Math Functions
Returns the dot product of the point and another point.
- point: Point
Parameters:
- Number — the dot product of the two points
Returns:
Returns the cross product of the point and another point.
- point: Point
Parameters:
- Number — the cross product of the two points
Returns:
Math Functions
Math Operator Functions
Returns the addition of the supplied value to both coordinates of the point as a new point. The object itself is not modified!
- number: Number — the number to add
Parameters:
- Point — the addition of the point and the value as a new point
Returns:
Example:
var point = new Point(5, 10);
var result = point + 20;
console.log(result); // {x: 25, y: 30}
Returns the addition of the supplied point to the point as a new point. The object itself is not modified!
- point: Point — the point to add
Parameters:
- Point — the addition of the two points as a new point
Returns:
Example:
var point1 = new Point(5, 10);
var point2 = new Point(10, 20);
var result = point1 + point2;
console.log(result); // {x: 15, y: 30}
Returns the subtraction of the supplied value to both coordinates of the point as a new point. The object itself is not modified!
- number: Number — the number to subtract
Parameters:
- Point — the subtraction of the point and the value as a new point
Returns:
Example:
var point = new Point(10, 20);
var result = point - 5;
console.log(result); // {x: 5, y: 15}
Returns the subtraction of the supplied point to the point as a new point. The object itself is not modified!
- point: Point — the point to subtract
Parameters:
- Point — the subtraction of the two points as a new point
Returns:
Example:
var firstPoint = new Point(10, 20);
var secondPoint = new Point(5, 5);
var result = firstPoint - secondPoint;
console.log(result); // {x: 5, y: 15}
Returns the multiplication of the supplied value to both coordinates of the point as a new point. The object itself is not modified!
- number: Number — the number to multiply by
Parameters:
- Point — the multiplication of the point and the value as a new point
Returns:
Example:
var point = new Point(10, 20);
var result = point * 2;
console.log(result); // {x: 20, y: 40}
Returns the multiplication of the supplied point to the point as a new point. The object itself is not modified!
- point: Point — the point to multiply by
Parameters:
- Point — the multiplication of the two points as a new point
Returns:
Example:
var firstPoint = new Point(5, 10);
var secondPoint = new Point(4, 2);
var result = firstPoint * secondPoint;
console.log(result); // {x: 20, y: 20}
Returns the division of the supplied value to both coordinates of the point as a new point. The object itself is not modified!
- number: Number — the number to divide by
Parameters:
- Point — the division of the point and the value as a new point
Returns:
Example:
var point = new Point(10, 20);
var result = point / 2;
console.log(result); // {x: 5, y: 10}
Returns the division of the supplied point to the point as a new point. The object itself is not modified!
- point: Point — the point to divide by
Parameters:
- Point — the division of the two points as a new point
Returns:
Example:
var firstPoint = new Point(8, 10);
var secondPoint = new Point(2, 5);
var result = firstPoint / secondPoint;
console.log(result); // {x: 4, y: 2}
The modulo operator returns the integer remainders of dividing the point by the supplied value as a new point.
- value: Number
Parameters:
- Point — the integer remainders of dividing the point by the value as a new point
Returns:
Example:
var point = new Point(12, 6);
console.log(point % 5); // {x: 2, y: 1}
The modulo operator returns the integer remainders of dividing the point by the supplied value as a new point.
- point: Point
Parameters:
- Point — the integer remainders of dividing the points by each other as a new point
Returns:
Example:
var point = new Point(12, 6);
console.log(point % new Point(5, 2)); // {x: 2, y: 0}
Static Methods
Returns a new point object with the smallest x and y of the supplied points.
- Point — the newly created point object
Returns:
Example:
var point1 = new Point(10, 100);
var point2 = new Point(200, 5);
var minPoint = Point.min(point1, point2);
console.log(minPoint); // {x: 10, y: 5}
Example:Find the minimum of multiple points:
var point1 = new Point(60, 100);
var point2 = new Point(200, 5);
var point3 = new Point(250, 35);
[point1, point2, point3].reduce(Point.min) // {x: 60, y: 5}
Returns a new point object with the largest x and y of the supplied points.
- Point — the newly created point object
Returns:
Example:
var point1 = new Point(10, 100);
var point2 = new Point(200, 5);
var maxPoint = Point.max(point1, point2);
console.log(maxPoint); // {x: 200, y: 100}
Example:Find the maximum of multiple points:
var point1 = new Point(60, 100);
var point2 = new Point(200, 5);
var point3 = new Point(250, 35);
[point1, point2, point3].reduce(Point.max) // {x: 250, y: 100}