paper.js/examples/Tools/SquareRounded.html

77 lines
2.5 KiB
HTML
Raw Normal View History

2011-03-09 10:15:01 -05:00
<!DOCTYPE html>
<html>
<head>
2014-08-16 13:24:54 -04:00
<meta charset="UTF-8">
<title>Square Rounded</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../dist/paper-full.js"></script>
<script type="text/paperscript" canvas="canvas">
/////////////////////////////////////////////////////////////////////
// Values
2011-03-09 10:15:01 -05:00
2014-08-16 13:24:54 -04:00
var values = {
radius: 10,
tolerance: 5
};
2011-03-09 10:15:01 -05:00
2014-08-16 13:24:54 -04:00
checkValues();
2011-03-09 10:15:01 -05:00
2014-08-16 13:24:54 -04:00
/////////////////////////////////////////////////////////////////////
// Mouse handling
2011-03-09 10:15:01 -05:00
2014-08-16 13:24:54 -04:00
var handle;
function checkValues() {
var min = values.radius * 2;
if (values.tolerance < min) values.tolerance = min;
handle = values.radius * Numerical.KAPPA;
}
2011-03-09 10:15:01 -05:00
2014-08-16 13:24:54 -04:00
var path;
function onMouseDown(event) {
path = new Path({
segments: [event.point, event.point],
strokeColor: 'black',
strokeWidth: 5,
strokeCap: 'round'
});
prevPoint = path.firstSegment.point;
curPoint = path.lastSegment.point;
curHandleSeg = null;
}
2011-03-09 10:15:01 -05:00
2014-08-16 13:24:54 -04:00
var curPoint, prevPoint, curHandleSeg;
function onMouseDrag(event) {
var point = event.point;
var diff = (point - prevPoint).abs();
if (diff.x < diff.y) {
curPoint.x = prevPoint.x;
curPoint.y = point.y;
} else {
curPoint.x = point.x;
curPoint.y = prevPoint.y;
}
var normal = curPoint - prevPoint;
normal.length = 1;
if (curHandleSeg) {
curHandleSeg.point = prevPoint + (normal * values.radius);
curHandleSeg.handleIn = normal * -handle;
}
var minDiff = Math.min(diff.x, diff.y);
if (minDiff > values.tolerance) {
var point = curPoint - (normal * values.radius);
var segment = new Segment(point, null, normal * handle);
path.insert(path.segments.length - 1, segment);
curHandleSeg = path.lastSegment;
// clone as we want the unmodified one:
prevPoint = curHandleSeg.point.clone();
path.add(curHandleSeg);
curPoint = path.lastSegment.point;
}
}
</script>
2011-03-09 10:15:01 -05:00
</head>
<body>
2014-08-16 13:24:54 -04:00
<canvas id="canvas" resize></canvas>
2011-05-30 18:27:39 -04:00
</body>
</html>