Skip to main content

Lines

Interactive lines allow users to manipulate line segments by dragging their endpoints. This is useful for demonstrating concepts like distance, slope, and linear relationships.

Line Segment

A basic line segment with two draggable endpoints. Drag the start and end points to see the line update in real-time.

import { InteractiveLine } from '@wangyaoshen/locus-interaction';

const line = new InteractiveLine({
start: [100, 200],
end: [400, 100],
color: '#4CAF50',
lineWidth: 4,
pointRadius: 14,
onChange: (start, end) => {
const length = Math.sqrt(
Math.pow(end[0] - start[0], 2) +
Math.pow(end[1] - start[1], 2)
);
console.log('Length:', length);
},
});

Constrained Endpoints

You can apply constraints to individual endpoints, allowing for more controlled interactions.

import {
InteractiveLine,
horizontal,
vertical
} from '@wangyaoshen/locus-interaction';

// Start point moves horizontally, end point moves vertically
const line = new InteractiveLine({
start: [100, 150],
end: [300, 150],
constrainStart: horizontal(150),
constrainEnd: vertical(300),
onChange: (start, end) => {
console.log('Line changed:', start, end);
},
});

Line Properties

The line component provides several computed properties:

const line = new InteractiveLine({
start: [0, 0],
end: [100, 100],
});

// Get line length
const length = line.getLength(); // 141.42...

// Get midpoint
const midpoint = line.getMidpoint(); // [50, 50]

// Get slope
const slope = line.getSlope(); // 1

// Get angle in radians
const angle = line.getAngle(); // 0.785... (45 degrees)

Props

NameTypeDefaultDescription
startVector2[0, 0]Start point position
endVector2[100, 100]End point position
colorstring'#4CAF50'Line and point color
lineWidthnumber4Line stroke width
pointRadiusnumber14Endpoint radius
constrainStartConstraintFunction-Constraint for start point
constrainEndConstraintFunction-Constraint for end point
onChange(start, end) => void-Callback when line changes

Using with Motion Canvas 2D

In Motion Canvas 2D, you can use the InteractiveLine2D component:

import { InteractiveLine2D } from '@wangyaoshen/locus-2d';

// In your scene
<InteractiveLine2D
start={[100, 200]}
end={[400, 100]}
lineColor="#4CAF50"
lineWidth={4}
pointRadius={14}
interactive={true}
onChange={(start, end) => {
console.log('Line changed:', start, end);
}}
/>

InteractiveLine2D Props

NameTypeDefaultDescription
startVector2[0, 0]Start point position
endVector2[100, 0]End point position
lineColorstring'#4CAF50'Line color
lineWidthnumber4Line stroke width
pointColorstring'#4CAF50'Endpoint color
pointRadiusnumber14Endpoint radius
interactivebooleanfalseEnable interaction
constrainStartConstraintFunction-Start point constraint
constrainEndConstraintFunction-End point constraint
onChange(start, end) => void-Change callback

Use Cases

Distance Calculator

const line = new InteractiveLine({
start: [100, 200],
end: [300, 100],
onChange: (start, end) => {
const distance = line.getLength();
document.getElementById('distance').textContent =
`Distance: ${distance.toFixed(2)}px`;
},
});

Angle Demonstration

const line = new InteractiveLine({
start: [250, 150],
end: [350, 150],
constrainStart: ([x, y]) => [250, 150], // Fixed start
onChange: (start, end) => {
const angle = line.getAngle();
const degrees = (angle * 180) / Math.PI;
document.getElementById('angle').textContent =
`Angle: ${degrees.toFixed(1)}°`;
},
});