-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspiral.ps.js
executable file
·168 lines (138 loc) · 3.99 KB
/
spiral.ps.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @file Holds all RoboPaint spiral mode Paper.JS code
*/
canvas.paperInit(paper);
// Make this on the actionLayer
paper.canvas.actionLayer.activate();
paper.motionPath = new Path({
data: {height: []} // A 1:1 match array for the motion path to set the height.
});
// Reset Everything on non-mainLayer and vars
paper.resetAll = function() {
if (spiral) spiral.remove();
paper.motionPath.removeSegments(0);
}
// Please note: dragging and dropping images only works for
// certain browsers when serving this script online:
var spiral, position, max;
var count = 0;
var grow = false;
var raster = null;
function onFrame(event) {
canvas.onFrame(event);
if (grow) {
if (raster && (view.center - position).length < max) {
for (var i = 0, l = count / 36 + 1; i < l; i++) {
growSpiral();
}
spiral.smooth();
} else {
grow = false;
if (paper.spiralComplete) paper.spiralComplete();
// Enable the start/pause button after the spiral has been made.
$('#pause').prop('disabled', false);
}
}
}
function growSpiral() {
count++;
var vector = new Point({
angle: count * 5,
length: count / 100
});
var rot = vector.rotate(90);
var color = raster.getAverageColor(position + vector / 2);
var value = color ? (1 - color.gray) * 3.7 : 0;
paper.motionPath.add(position + vector / 2);
var h = robopaint.utils.map((color ? color.gray : 0), 0, 1, 1, 0.4);
paper.motionPath.data.height.push(h);
rot.length = Math.max(value, 0.2);
spiral.add(position + vector - rot);
spiral.insert(0, position + vector + rot);
position += vector;
}
paper.resetSpiral = function(callback) {
paper.motionPath.removeSegments(0); // Remove all motionPath segments
grow = true;
paper.spiralComplete = callback;
// Transform the raster, so it fills the view:
raster.fitBounds(view.bounds);
paper.canvas.tempLayer.activate(); // Draw the spiral on the tempLayer
if (spiral) {
spiral.remove();
}
position = view.center;
count = 0;
spiral = new Path({
fillColor: 'black',
closed: true
});
position = view.center;
max = Math.min(raster.bounds.width, raster.bounds.height) * 0.5;
}
// Automatically paint the single spiral path.
paper.autoPaintSpiral = function(){
mode.run([
'wash',
['media', 'color0'],
['status', mode.t('status.printing')],
'up'
]);
var path = paper.motionPath;
// Initial move without height set to get out onto the canvas.
mode.run('move', {x: path.firstSegment.point.x, y: path.firstSegment.point.y});
_.each(path.segments, function(seg, segIndex){
mode.run([
['height', path.data.height[segIndex]],
['move', {x: seg.point.x, y: seg.point.y}]
]);
});
mode.run([
'wash',
'park',
['status', i18n.t('libs.autocomplete')],
['callbackname', 'spiralComplete']
]);
// This tells pause Till Empty that we're ready to start checking for
// local buffer depletion. We can't check sooner as we haven't finished
// sending all the data yet!
robopaint.pauseTillEmpty(false);
}
function onKeyDown(event) {
if (event.key == 'space') {
spiral.selected = !spiral.selected;
}
}
paper.pickSpiralImage = function() {
mainWindow.dialog({
t: 'OpenDialog',
title: mode.t('filepick.title'),
filters: [
{ name: mode.t('filepick.files'), extensions: ['jpg', 'jpeg', 'gif', 'png'] }
]
}, function(filePath){
if (!filePath) { // Open cancelled
return;
}
paper.loadSpiralImage(filePath[0]);
});
};
paper.loadSpiralImage = function (path) {
paper.canvas.mainLayer.activate(); // Draw the raster to the main layer
if (raster) raster.remove();
try {
raster = new Raster({
source: dataURI(path),
position: view.center
});
raster.onLoad = function() {
raster.fitBounds(view.bounds);
paper.canvas.mainLayer.opacity = 0.1;
paper.resetSpiral();
$('#pause').prop('disabled', true);
}
} catch(e) {
console.error('Problem loading image:', path, e);
}
};
paperLoadedInit();