source: main/trunk/model-sites-dev/von-sparql/js/paper/examples/Paperjs.org/HitTesting.html@ 28914

Last change on this file since 28914 was 28914, checked in by ak19, 10 years ago

Supporting javascript libraries and bespoke code written by Steffan to support the von-sparql user interface

File size: 2.5 KB
Line 
1<!DOCTYPE html>
2<html>
3<head>
4 <meta charset="UTF-8">
5 <title>Hit Testing</title>
6 <link rel="stylesheet" href="../css/style.css">
7 <script type="text/javascript" src="../../dist/paper.js"></script>
8 <script type="text/paperscript" canvas="canvas">
9 var values = {
10 paths: 50,
11 minPoints: 5,
12 maxPoints: 15,
13 minRadius: 30,
14 maxRadius: 90
15 };
16
17 var hitOptions = {
18 segments: true,
19 stroke: true,
20 fill: true,
21 tolerance: 5
22 };
23
24 createPaths();
25
26 function createPaths() {
27 var radiusDelta = values.maxRadius - values.minRadius;
28 var pointsDelta = values.maxPoints - values.minPoints;
29 for (var i = 0; i < values.paths; i++) {
30 var radius = values.minRadius + Math.random() * radiusDelta;
31 var points = values.minPoints + Math.floor(Math.random() * pointsDelta);
32 var path = createBlob(view.size * Point.random(), radius, points);
33 var lightness = (Math.random() - 0.5) * 0.4 + 0.4;
34 var hue = Math.random() * 360;
35 path.fillColor = { hue: hue, saturation: 1, lightness: lightness };
36 path.strokeColor = 'black';
37 };
38 }
39
40 function createBlob(center, maxRadius, points) {
41 var path = new Path();
42 path.closed = true;
43 for (var i = 0; i < points; i++) {
44 var delta = new Point({
45 length: (maxRadius * 0.5) + (Math.random() * maxRadius * 0.5),
46 angle: (360 / points) * i
47 });
48 path.add(center + delta);
49 }
50 path.smooth();
51 return path;
52 }
53
54 var segment, path;
55 var movePath = false;
56 function onMouseDown(event) {
57 segment = path = null;
58 var hitResult = project.hitTest(event.point, hitOptions);
59 if (!hitResult)
60 return;
61
62 if (event.modifiers.shift) {
63 if (hitResult.type == 'segment') {
64 hitResult.segment.remove();
65 };
66 return;
67 }
68
69 if (hitResult) {
70 path = hitResult.item;
71 if (hitResult.type == 'segment') {
72 segment = hitResult.segment;
73 } else if (hitResult.type == 'stroke') {
74 var location = hitResult.location;
75 segment = path.insert(location.index + 1, event.point);
76 path.smooth();
77 }
78 }
79 movePath = hitResult.type == 'fill';
80 if (movePath)
81 project.activeLayer.addChild(hitResult.item);
82 }
83
84 function onMouseMove(event) {
85 project.activeLayer.selected = false;
86 if (event.item)
87 event.item.selected = true;
88 }
89
90 function onMouseDrag(event) {
91 if (segment) {
92 segment.point = event.point;
93 path.smooth();
94 }
95
96 if (movePath)
97 path.position += event.delta;
98 }
99 </script>
100</head>
101<body>
102 <canvas id="canvas" resize style="background:black"></canvas>
103</body>
104</html>
Note: See TracBrowser for help on using the repository browser.