mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
103 lines
No EOL
3.3 KiB
HTML
103 lines
No EOL
3.3 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
|
|
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<title>Example</title>
|
|
<script type="text/javascript" src="../../lib/Bootstrap.js"></script>
|
|
<script type="text/javascript" src="../../src/Paper.js"></script>
|
|
<script type="text/javascript" src="../../src/item/Item.js"></script>
|
|
<script type="text/javascript" src="../../src/basic/Point.js"></script>
|
|
<script type="text/javascript" src="../../src/basic/Rectangle.js"></script>
|
|
<script type="text/javascript" src="../../src/basic/Size.js"></script>
|
|
<script type="text/javascript" src="../../src/path/Segment.js"></script>
|
|
<script type="text/javascript" src="../../src/path/PathItem.js"></script>
|
|
<script type="text/javascript" src="../../src/path/Path.js"></script>
|
|
<script type="text/javascript" src="../../src/document/Doc.js"></script>
|
|
<script type="text/javascript" src="../../src/item/Layer.js"></script>
|
|
<script type="text/javascript" src="../../src/item/Group.js"></script>
|
|
<script type="text/javascript" src="../../src/ToolEvent.js"></script>
|
|
<script type="text/javascript" src="../../src/ToolHandler.js"></script>
|
|
<script type="text/javascript" src="../../src/Tool.js"></script>
|
|
<script>
|
|
window.onload = function() {
|
|
var canvas = document.getElementById('canvas');
|
|
var doc = new Doc(canvas);
|
|
|
|
var path;
|
|
var strokeEnds = 6;
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// Mouse handling
|
|
|
|
var lastPoint;
|
|
|
|
var tool = new Tool({
|
|
onMouseDown: function(event) {
|
|
path = new Path();
|
|
path.fillColor = event.count % 2 ? 'red' : 'black';
|
|
},
|
|
|
|
onMouseDrag: function(event) {
|
|
// If this is the first drag event,
|
|
// add the strokes at the start:
|
|
if (event.count == 1) {
|
|
addStrokes(event.middlePoint, event.delta.multiply(-1));
|
|
} else {
|
|
var step = event.delta.divide(2);
|
|
step.angle = step.angle + 90;
|
|
|
|
// The top point: the middle point + the step rotated by 90 degrees:
|
|
// -----*
|
|
// |
|
|
// ------
|
|
var top = event.middlePoint.add(step);
|
|
|
|
// The bottom point: the middle point - the step rotated by 90 degrees:
|
|
// ------
|
|
// |
|
|
// -----*
|
|
var bottom = event.middlePoint.subtract(step);
|
|
|
|
path.add(top);
|
|
path.insert(0, bottom);
|
|
}
|
|
path.smooth();
|
|
|
|
lastPoint = event.middlePoint;
|
|
},
|
|
|
|
onMouseUp: function(event) {
|
|
var delta = event.point.subtract(lastPoint);
|
|
delta.length = tool.maxDistance;
|
|
addStrokes(event.point, delta);
|
|
path.closed = true;
|
|
path.smooth();
|
|
}
|
|
});
|
|
|
|
function addStrokes(point, delta) {
|
|
var step = delta.rotate(90);
|
|
var strokePoints = strokeEnds * 2 + 1;
|
|
point = point.subtract(step.divide(2));
|
|
step = step.divide(strokePoints - 1);
|
|
for(var i = 0; i < strokePoints; i++) {
|
|
var strokePoint = point.add(step.multiply(i));
|
|
var offset = delta.multiply(Math.random() * 0.3 + 0.1);
|
|
if (i % 2) {
|
|
offset = offset.multiply(-1);
|
|
}
|
|
strokePoint = strokePoint.add(offset);
|
|
path.insert(0, strokePoint);
|
|
}
|
|
}
|
|
|
|
tool.document = doc;
|
|
tool.fixedDistance = 80;
|
|
// tool.minDistance = values.minDistance;
|
|
}
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<canvas id='canvas' width=1024 height=768></canvas>
|
|
</body> |