paper.js/examples/Tools/WormFarm.html

89 lines
2.7 KiB
HTML
Raw Normal View History

2011-02-21 08:31:26 -05:00
<!DOCTYPE html>
2011-02-08 19:10:55 -05:00
<html>
<head>
2014-08-16 13:24:54 -04:00
<meta charset="UTF-8">
<title>Worm Farm</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-02-08 19:10:55 -05:00
2014-08-16 13:24:54 -04:00
var values = {
minDistance: 10,
maxDistance: 30,
varyThickness: true
};
2011-02-08 19:10:55 -05:00
2014-08-16 13:24:54 -04:00
// All newly created items will inherit the following styles:
project.currentStyle = {
fillColor: 'white',
strokeColor: 'black'
};
2011-05-06 12:11:46 -04:00
2014-08-16 13:24:54 -04:00
/////////////////////////////////////////////////////////////////////
// Mouse handling
2011-02-08 19:10:55 -05:00
2014-08-16 13:24:54 -04:00
tool.minDistance = values.minDistance;
tool.maxDistance = values.maxDistance;
2011-02-08 19:10:55 -05:00
2014-08-16 13:24:54 -04:00
var worm;
2011-02-08 19:10:55 -05:00
2014-08-16 13:24:54 -04:00
// Every time the user clicks the mouse to drag we create a path
// and when a user drags the mouse we add points to it
function onMouseDown(event) {
worm = new Path();
worm.add(event.point, event.point);
worm.closed = true;
}
2011-03-04 06:38:38 -05:00
2014-08-16 13:24:54 -04:00
function onMouseDrag(event) {
// the vector in the direction that the mouse moved
var step = event.delta;
2011-03-04 06:38:38 -05:00
2014-08-16 13:24:54 -04:00
// if the vary thickness checkbox is marked
// divide the length of the step vector by two:
if (values.varyThickness) {
step.length = step.length / 2;
} else {
// otherwise set the length of the step vector to half of
// minDistance
step.length = values.minDistance / 2;
}
2011-02-08 19:10:55 -05:00
2014-08-16 13:24:54 -04:00
// the top point: the middle point + the step rotated by -90
// degrees
// -----*
// |
// ------
var top = event.middlePoint + step.rotate(-90);
2011-02-08 19:10:55 -05:00
2014-08-16 13:24:54 -04:00
// the bottom point: the middle point + the step rotated by 90
// degrees
// ------
// |
// -----*
var bottom = event.middlePoint + step.rotate(90);
2011-02-08 19:10:55 -05:00
2014-08-16 13:24:54 -04:00
// add the top point to the end of the path
worm.add(top);
2011-02-08 19:10:55 -05:00
2014-08-16 13:24:54 -04:00
// insert the bottom point after the first segment of the path
worm.insert(1, bottom);
2011-03-04 06:38:38 -05:00
2014-08-16 13:24:54 -04:00
// make a new line path from top to bottom
new Path(top, bottom);
2011-07-07 10:09:02 -04:00
2014-08-16 13:24:54 -04:00
// This is the point at the front of the worm:
worm.firstSegment.point = event.point;
2011-03-04 06:38:38 -05:00
2014-08-16 13:24:54 -04:00
// smooth the segments of the path
worm.smooth();
}
</script>
2011-02-08 19:10:55 -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>