mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-07-03 08:20:31 -04:00
Update PathStructure example to code from website.
This commit is contained in:
parent
413e4efce4
commit
2973fef6bd
1 changed files with 56 additions and 73 deletions
|
@ -6,91 +6,74 @@
|
||||||
<link rel="stylesheet" href="../css/style.css">
|
<link rel="stylesheet" href="../css/style.css">
|
||||||
<script type="text/javascript" src="../../dist/paper.js"></script>
|
<script type="text/javascript" src="../../dist/paper.js"></script>
|
||||||
<script type="text/paperscript" canvas="canvas">
|
<script type="text/paperscript" canvas="canvas">
|
||||||
var path = new Path();
|
var y = view.size.height / 2;
|
||||||
|
var width = view.size.width;
|
||||||
var segmentTexts = new Layer();
|
var vector = new Point({
|
||||||
for (var i = 0; i < 3; i++) {
|
angle: 45,
|
||||||
new PointText({
|
length: width / 5
|
||||||
content: i,
|
});
|
||||||
justification: 'center',
|
var offset = width / 30;
|
||||||
fontSize: 12
|
var handleTexts = [];
|
||||||
});
|
var path = new Path({
|
||||||
}
|
segments: [
|
||||||
|
[[offset, y], null, vector.rotate(-90)],
|
||||||
var handleTexts = new Layer();
|
[[width / 2, y], vector.rotate(-180), vector],
|
||||||
for (var i = 0; i < 2; i++) {
|
[[width - offset, y], vector.rotate(90), null]
|
||||||
new PointText({
|
],
|
||||||
content: 'handleIn',
|
fullySelected: true
|
||||||
justification: 'center',
|
});
|
||||||
fontSize: 12
|
|
||||||
});
|
|
||||||
|
|
||||||
new PointText({
|
|
||||||
content: 'handleOut',
|
|
||||||
justification: 'center',
|
|
||||||
fontSize: 12
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function onResize() {
|
|
||||||
var width = view.size.width;
|
|
||||||
var offset = width / 30;
|
|
||||||
var vector = new Point({
|
|
||||||
angle: 45,
|
|
||||||
length: width / 5
|
|
||||||
});
|
|
||||||
var yMiddle = view.size.height / 2;
|
|
||||||
path.segments = [
|
|
||||||
{
|
|
||||||
point: [offset, yMiddle],
|
|
||||||
handleIn: null,
|
|
||||||
handleOut: vector.rotate(-90)
|
|
||||||
},
|
|
||||||
{
|
|
||||||
point: [width / 2, yMiddle],
|
|
||||||
handleIn: vector.rotate(-180),
|
|
||||||
handleOut: vector
|
|
||||||
},
|
|
||||||
{
|
|
||||||
point: [width - offset, yMiddle],
|
|
||||||
handleIn: vector.rotate(90),
|
|
||||||
handleOut: null
|
|
||||||
}
|
|
||||||
];
|
|
||||||
path.fullySelected = true;
|
|
||||||
|
|
||||||
for (var i = 0; i < path.segments.length; i++) {
|
|
||||||
var point = path.segments[i].point;
|
|
||||||
segmentTexts.children[i].point = point - [0, 10];
|
|
||||||
}
|
|
||||||
|
|
||||||
onMouseMove({
|
|
||||||
point: view.bounds.bottomRight
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function onMouseMove(event) {
|
function onMouseMove(event) {
|
||||||
var point = event.point.clone();
|
var point = event.point.clone();
|
||||||
// Constrain the event point, to not cut off the text:
|
// Constrain the event point, to not cut off the text:
|
||||||
if (point.y < 22) {
|
if (point.y < 22)
|
||||||
point.y = 22;
|
point.y = 22;
|
||||||
}
|
if (point.y > view.size.height - 24)
|
||||||
if (point.y > view.size.height - 24) {
|
|
||||||
point.y = view.size.height - 24;
|
point.y = view.size.height - 24;
|
||||||
}
|
var delta = point - view.center;
|
||||||
var delta = point - view.center;
|
for (var i = 0; i < 2; i++) {
|
||||||
|
|
||||||
for (var i = 0; i < path.curves.length; i++) {
|
|
||||||
var curve = path.curves[i];
|
var curve = path.curves[i];
|
||||||
curve.handle1.y = curve.handle2.y = delta.y * (i % 2 ? 1 : -1);
|
curve.handle1.y = curve.handle2.y = delta.y * (i % 2 ? 1 : -1);
|
||||||
var firstPoint = curve.point1 + curve.handle1;
|
var firstPoint = curve.point1 + curve.handle1;
|
||||||
var secondPoint = curve.point2 + curve.handle2;
|
var secondPoint = curve.point2 + curve.handle2;
|
||||||
var yMiddle = view.size.height / 2;
|
handleTexts[i * 2].point = secondPoint -
|
||||||
var offset = (firstPoint.y < yMiddle ? [0, 10] : [0, -18]);
|
(firstPoint.y < y ? [0, 10] : [0, -18]);
|
||||||
handleTexts.children[i * 2].point = secondPoint - offset;
|
handleTexts[i * 2 + 1].point = firstPoint -
|
||||||
handleTexts.children[i * 2 + 1].point = firstPoint - offset;
|
(firstPoint.y < y ? [0, 10] : [0, -18]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
project.currentStyle.fillColor = 'black';
|
||||||
|
for (var i = 0; i < 3; i++) {
|
||||||
|
var segment = path.segments[i];
|
||||||
|
var text = new PointText({
|
||||||
|
point: segment.point - [0, 10],
|
||||||
|
content: i,
|
||||||
|
fontSize: 12,
|
||||||
|
justification: 'center'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < 2; i++) {
|
||||||
|
var handleInText = new PointText({
|
||||||
|
content: 'handleIn',
|
||||||
|
fontSize: 12,
|
||||||
|
justification: 'center'
|
||||||
|
});
|
||||||
|
handleTexts.push(handleInText);
|
||||||
|
|
||||||
|
var handleOutText = new PointText({
|
||||||
|
content: 'handleOut',
|
||||||
|
fontSize: 12,
|
||||||
|
justification: 'center'
|
||||||
|
});
|
||||||
|
handleTexts.push(handleOutText);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call onMouseMove once to correctly position the text items:
|
||||||
|
onMouseMove({
|
||||||
|
point: view.center + vector.rotate(-90)
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue