Repository navigation
Expand file tree
/
Copy pathpoint.max.js
More file actions
76 lines (59 loc) · 1.85 KB
/
Copy pathpoint.max.js
File metadata and controls
76 lines (59 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//// 0. Describe this demo
window.demoDescription = "A rectangular boundary is pinned by the minimum and maximum positions of a set of points. Move and click to change the points' positions.";
//// 1. Define Space and Form
var colors = {
a1: "#ff2d5d", a2: "#42dc8e", a3: "#2e43eb", a4: "#ffe359",
b1: "#96bfed", b2: "#f5ead6", b3: "#f1f3f7", b4: "#e2e6ef"
}
var space = new CanvasSpace("demo", "#222" ).display();
var form = new Form( space );
//// 2. Create Elements
var pts = [
new Point( space.size.$divide(2) ),
new Point( space.size.$divide(3) ),
new Point( space.size.$divide(3).multiply(2) )
];
var rect = new Rectangle( pts[1]).to( pts[2] );
var nextRect = new Rectangle( pts[1]).to( pts[2] );
// Calculate min and max points, and set the rectangular bound.
function getMinMax() {
var minPt = pts[0];
var maxPt = pts[0];
for (var i=1; i<pts.length; i++) {
minPt = minPt.$min( pts[i] );
}
for (i=1; i<pts.length; i++) {
maxPt = maxPt.$max( pts[i] );
}
nextRect.set( minPt).to( maxPt );
}
//// 3. Visualize, Animate, Interact
space.add({
animate: function(time, fps, context) {
form.fill( false ).stroke( colors.a4, 5 );
form.rect( rect );
form.stroke( false ).fill( "#fff" );
form.points( pts, 5, true );
// transition rectangle to new position
if (!rect.near(nextRect, 0.1)) {
rect.add( nextRect.$subtract(rect).multiply(0.2) );
}
if (!rect.p1.near(nextRect.p1, 0.1)) {
rect.p1.add( nextRect.p1.$subtract(rect.p1).multiply(0.2) );
}
},
onMouseAction: function(type, x, y, evt) {
if (type=="move") { // mouse mouve
pts[0].set(x, y);
getMinMax();
} else if (type=="up") { // mouse click, pin the point
pts.push( new Point(x, y) );
if (pts.length > 5) {
pts.splice(1, 1);
}
}
}
});
// 4. Start playing
space.bindMouse();
space.play();